Find non-decreasing array brr[] of size 2*N such that each arr[i] equals sum of brr[i] and brr[2*n – i +1]
Given an array arr[] of size N, the task is to find another array brr[] of size 2*N such that it is non-decreasing and for each ith from 1 to N arr[i] = brr[i] + brr[2*n – i +1].
Examples:
Input: n = 2, arr[] = { 5, 6 }
Output: 0 1 5 5
Explanation: For i =1, arr[1] = 5, brr[1]+brr[2*2-1+1] = 5, so both are equal, For i =2, arr[2] = 6, brr[2]+brr[2*2-2+1] = 6, so both are equal.
Input: n = 3, arr[] = { 2, 1, 2 }
Output: 0 0 1 1 1 2
Approach: Numbers of array brr[] will be restored in pairs (brr[1], brr[2*n]), (brr[2], brr[2*n-1]) and so on. Thus, a certain limit can be there on these values satisfying the above conditions brr[i]+brr[2*N-i-1]==arr[i]. Let l be minimal possible and r be the maximum possible in the answer. Initially, l=0, r=10^18 and they are updated with l=brr[i], r=brr[2*n-i-1]. Follow the steps below to solve the problem:
- Initialize the variables l as 0 and r as INF64.
- Multiply the value of N by 2 to keep the count of the size of the second array.
- Define a function brute(ind, l, r) where ind is the index of the array for which values are to be filled, l and r are the range of the values. Call this function recursively to compute the values for each pair in the second array brr[].
- In the function brute(ind, l, r)
- Define the base case i.e, when the value of ind becomes equal to the size of the first array arr[].
- If yes, then print the elements of the second array brr[] and exit the function.
- Else, iterate in the range [l, arr[ind]/2] and perform the following steps.
- If the value of arr[ind]-i is less than r, then set the value of brr[ind] to i and brr[n-ind-1] to arr[ind]-i.
- Set the value of l to i and r to arr[ind]-i as the updated values of l and r.
- Call the same function recursively brute(ind+1, l, r) for the next index.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const long long INF64 = 1000000000000000000ll;
const int N = 200 * 1000 + 13;
int n;
long long arr[N], brr[N];
void brute( int ind, long long l, long long r)
{
if (ind == n / 2) {
for ( int i = 0; i < int (n); i++)
printf ( "%lld " , brr[i]);
puts ( "" );
exit (0);
}
for ( long long i = l; i <= arr[ind] / 2; ++i)
if (arr[ind] - i <= r) {
brr[ind] = i;
brr[n - ind - 1] = arr[ind] - i;
brute(ind + 1, i, arr[ind] - i);
}
}
int main()
{
n = 2;
n *= 2;
arr[0] = 5;
arr[1] = 6;
brute(0, 0, INF64);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int INF64 = ( int )1e10;
static int N = 200 * 1000 + 13 ;
static int n;
static int arr[] = new int [N];
static int brr[] = new int [N];
static void brute( int ind, int l, int r)
{
if (ind == n / 2 )
{
for ( int i = 0 ; i < ( int )n; i++)
System.out.print(brr[i] + " " );
System.exit( 0 );
}
for ( int i = l; i <= arr[ind] / 2 ; ++i)
if (arr[ind] - i <= r)
{
brr[ind] = i;
brr[n - ind - 1 ] = arr[ind] - i;
brute(ind + 1 , i, arr[ind] - i);
}
}
public static void main(String[] args)
{
n = 2 ;
n *= 2 ;
arr[ 0 ] = 5 ;
arr[ 1 ] = 6 ;
brute( 0 , 0 , INF64);
}
}
|
Python3
N = 200 * 1000 + 13
n = 0
arr = [ 0 for i in range (N)]
brr = [ 0 for i in range (N)]
import sys
def brute(ind, l, r):
if (ind = = n / 2 ):
for i in range (n):
print (brr[i],end = " " )
sys.exit()
for i in range (l,arr[ind] / / 2 + 1 , 1 ):
if (arr[ind] - i < = r):
brr[ind] = i
brr[n - ind - 1 ] = arr[ind] - i
brute(ind + 1 , i, arr[ind] - i)
if __name__ = = '__main__' :
n = 2
n * = 2
arr[ 0 ] = 5
arr[ 1 ] = 6
INF64 = 1000000000000000000
brute( 0 , 0 , INF64)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int INF64 = ( int )1e8;
static int N = 200 * 1000 + 13;
static int n;
static int [] arr = new int [N];
static int [] brr = new int [N];
static void brute( int ind, int l, int r)
{
if (ind == n / 2)
{
for ( int i = 0; i < ( int )n; i++)
Console.Write(brr[i] + " " );
System.Environment.Exit(0);
}
for ( int i = l; i <= arr[ind] / 2; ++i)
if (arr[ind] - i <= r)
{
brr[ind] = i;
brr[n - ind - 1] = arr[ind] - i;
brute(ind + 1, i, arr[ind] - i);
}
}
public static void Main()
{
n = 2;
n *= 2;
arr[0] = 5;
arr[1] = 6;
brute(0, 0, INF64);
}
}
|
Javascript
<script>
const INF64 = 1000000000000000000;
const N = 200 * 1000 + 13;
let n;
let arr = Array(N);
let brr = Array(N);
function brute(ind, l, r)
{
if (ind == n / 2)
{
for (let i = 0; i < n; i++)
document.write(brr[i]+ " " );
exit(0);
}
for (let i = l; i <= arr[ind] / 2; ++i)
if (arr[ind] - i <= r)
{
brr[ind] = i;
brr[n - ind - 1] = arr[ind] - i;
brute(ind + 1, i, arr[ind] - i);
}
}
n = 2;
n *= 2;
arr[0] = 5;
arr[1] = 6;
brute(0, 0, INF64);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...