Find the non decreasing order array from given array
Last Updated :
23 Jun, 2022
Given an array A[] of size N / 2, the task is to construct the array B[] of size N such that:
- B[] is sorted in non-decreasing order.
- A[i] = B[i] + B[n - i + 1].
Note: Array A[] is given in such a way that the answer is always possible.
Examples:
Input: A[] = {3, 4}
Output: 0 1 3 3
Input: A[] = {4, 1}
Output: 0 0 1 4
Approach: Let's present the following greedy approach. The numbers will be restored in pairs (B[0], B[n - 1]), (B[1], B[n - 2]) and so on. Thus, we can have some limits on the values of the current pair (satisfying the criteria about sorted result).
Initially, l = 0 and r = 109, they are updated with l = a[i] and r = a[n - i + 1]. Let l be minimal possible in the answer. Take a[i] = max(l, b[i] - r) and r = b[i] - l, that way l was chosen in such a way that both l and r are within the restrictions and l is also minimal possible.
If l was any greater than we would move both l limit up and r limit down leaving less freedom for later choices.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to print
// the contents of the array
void printArr(int b[], int n)
{
for (int i = 0; i < n; i++)
cout << b[i] << " ";
}
// Function to build array B[]
void ModifiedArray(int a[], int n)
{
// Lower and upper limits
int l = 0, r = INT_MAX;
// To store the required array
int b[n] = { 0 };
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
int main()
{
int a[] = { 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
ModifiedArray(a, 2 * n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class solution
{
// Utility function to print
// the contents of the array
void printArr(int b[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.print(" " + b[i] + " ");
}
}
// Function to build array B[]
void ModifiedArray(int a[], int n)
{
// Lower and upper limits
int l = 0, r = Integer.MAX_VALUE;
// To store the required array
int[] b = new int[n];
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = Math.max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
public static void main(String args[])
{
int a[] = { 5, 6 };
int n = a.length ;
solution s=new solution();
s.ModifiedArray(a, 2 * n);
}
}
//This code is contributed by Shivi_Aggarwal
Python3
# Python 3 implementation of the approach
import sys
# Utility function to print the
# contents of the array
def printArr(b, n):
for i in range(0, n, 1):
print(b[i], end = " ")
# Function to build array B[]
def ModifiedArray(a, n):
# Lower and upper limits
l = 0
r = sys.maxsize
# To store the required array
b = [0 for i in range(n)]
# Apply greedy approach
for i in range(0, int(n / 2), 1):
b[i] = max(l, a[i] - r)
b[n - i - 1] = a[i] - b[i]
l = b[i]
r = b[n - i - 1]
# Print the built array b[]
printArr(b, n)
# Driver code
if __name__ == '__main__':
a = [5, 6]
n = len(a)
ModifiedArray(a, 2 * n)
# This code is contributed by
# Shashank_Sharma
C#
// C# implementation of the approach
using System;
public class GFG{
// Utility function to print
// the contents of the array
static void printArr(int []b, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(" " + b[i] + " ");
}
}
// Function to build array B[]
static void ModifiedArray(int []a, int n)
{
// Lower and upper limits
int l = 0, r = int.MaxValue;
// To store the required array
int[] b = new int[n];
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = Math.Max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
static public void Main (){
int []a = { 5, 6 };
int n = a.Length;
ModifiedArray(a, 2 * n);
}
}
// This code is contributed
// by Sach_Code
PHP
<?php
// PHP implementation of the approach
// Utility function to print the
// contents of the array
function printArr($b, $n)
{
for ($i = 0; $i < $n; $i++)
echo $b[$i] . " ";
}
// Function to build array B[]
function ModifiedArray($a, $n)
{
// Lower and upper limits
$l = 0; $r = PHP_INT_MAX;
// To store the required array
$b = array(0);
// Apply greedy approach
for ($i = 0; $i < $n / 2; $i++)
{
$b[$i] = max($l, $a[$i] - $r);
$b[$n - $i - 1] = $a[$i] - $b[$i];
$l = $b[$i];
$r = $b[$n - $i - 1];
}
// Print the built array b[]
printArr($b, $n);
}
// Driver code
$a = array( 5, 6 );
$n = sizeof($a);
ModifiedArray($a, 2 * $n);
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Javascript program of the above approach
// Utility function to print
// the contents of the array
function printArr(b, n)
{
for (let i = 0; i < n; i++)
{
document.write(" " + b[i] + " ");
}
}
// Function to build array B[]
function ModifiedArray(a, n)
{
// Lower and upper limits
let l = 0, r = Number.MAX_VALUE;
// To store the required array
let b = Array(n).fill(0);
// Apply greedy approach
for (let i = 0; i < n / 2; i++) {
b[i] = Math.max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
let a = [ 5, 6 ];
let n = a.length ;
ModifiedArray(a, 2 * n);
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Find the Prefix-MEX Array for given Array Given an array A[] of N elements, the task is to create a Prefix-MEX array for this given array. Prefix-MEX array B[] of an array A[] is created such that MEX of A[0] till A[i] is B[i]. MEX of an array refers to the smallest missing non-negative integer of the array. Examples: Input: A[] = {1, 0, 2,
13 min read
Sort a K-Increasing-Decreasing Array Given a K-increasing-decreasing array arr[], the task is to sort the given array. An array is said to be K-increasing-decreasing if elements repeatedly increase upto a certain index after which they decrease, then again increase, a total of K times. The diagram below shows a 4-increasing-decreasing
12 min read
Make the array non-decreasing with the given operation Given an array arr[] of size N, the task is to check if it is possible to make the array non-decreasing by applying the given operation at most once on each array element. In a single operation, one can decrease the element by one i.e. arr[i] = arr[i] - 1.Examples: Input: arr[] = {1, 2, 1, 2, 3} Out
6 min read
Determine whether the given Array forms a valley or not Given an array arr[] of length N, the task is to check whether the given array forms a valley or not. An array is said to be a valley if there is a point till which the array is non-increasing and after that increases in nature. Formally arr[0] ? arr[1] ? . . . ? arr[i] ? arr[i+1] ? . . . ? arr[N-1]
6 min read
Sorting element of an array by frequency in decreasing order Given an array arr[] of N integers. The task is to sort the array arr[] according to the frequency of elements in decreasing order. Note: If the frequencies of the two elements are the same, then the smaller element should come first. Examples: Input: arr[] = { 4, 4, 5, 6, 4, 2, 2, 8, 5 } Output: 4
15+ min read