Find the non decreasing order array from given array
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++
#include <bits/stdc++.h>
using namespace std;
void printArr( int b[], int n)
{
for ( int i = 0; i < n; i++)
cout << b[i] << " " ;
}
void ModifiedArray( int a[], int n)
{
int l = 0, r = INT_MAX;
int b[n] = { 0 };
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];
}
printArr(b, n);
}
int main()
{
int a[] = { 5, 6 };
int n = sizeof (a) / sizeof (a[0]);
ModifiedArray(a, 2 * n);
return 0;
}
|
Java
import java.util.*;
class solution
{
void printArr( int b[], int n)
{
for ( int i = 0 ; i < n; i++)
{
System.out.print( " " + b[i] + " " );
}
}
void ModifiedArray( int a[], int n)
{
int l = 0 , r = Integer.MAX_VALUE;
int [] b = new int [n];
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 ];
}
printArr(b, n);
}
public static void main(String args[])
{
int a[] = { 5 , 6 };
int n = a.length ;
solution s= new solution();
s.ModifiedArray(a, 2 * n);
}
}
|
Python3
import sys
def printArr(b, n):
for i in range ( 0 , n, 1 ):
print (b[i], end = " " )
def ModifiedArray(a, n):
l = 0
r = sys.maxsize
b = [ 0 for i in range (n)]
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 ]
printArr(b, n)
if __name__ = = '__main__' :
a = [ 5 , 6 ]
n = len (a)
ModifiedArray(a, 2 * n)
|
C#
using System;
public class GFG{
static void printArr( int []b, int n)
{
for ( int i = 0; i < n; i++)
{
Console.Write( " " + b[i] + " " );
}
}
static void ModifiedArray( int []a, int n)
{
int l = 0, r = int .MaxValue;
int [] b = new int [n];
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];
}
printArr(b, n);
}
static public void Main (){
int []a = { 5, 6 };
int n = a.Length;
ModifiedArray(a, 2 * n);
}
}
|
PHP
<?php
function printArr( $b , $n )
{
for ( $i = 0; $i < $n ; $i ++)
echo $b [ $i ] . " " ;
}
function ModifiedArray( $a , $n )
{
$l = 0; $r = PHP_INT_MAX;
$b = array (0);
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];
}
printArr( $b , $n );
}
$a = array ( 5, 6 );
$n = sizeof( $a );
ModifiedArray( $a , 2 * $n );
?>
|
Javascript
<script>
function printArr(b, n)
{
for (let i = 0; i < n; i++)
{
document.write( " " + b[i] + " " );
}
}
function ModifiedArray(a, n)
{
let l = 0, r = Number.MAX_VALUE;
let b = Array(n).fill(0);
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];
}
printArr(b, n);
}
let a = [ 5, 6 ];
let n = a.length ;
ModifiedArray(a, 2 * n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...