Generate Array whose difference of each element with its left yields the given Array
Last Updated :
05 Mar, 2021
Given an integer N and an arr1[], of (N – 1) integers, the task is to find the sequence arr2[] of N integers in the range [1, N] such that arr1[i] = arr2[i+1] – arr2[i]. The integers in sequence arr1[] lies in range [-N, N].
Examples:
Input: N = 3, arr1[] = {-2, 1}
Output: arr2[] = {3, 1, 2}
Explanation:
arr2[1] – arr2[0] = (1 – 3) = -2 = arr1[0]
arr2[2] – arr2[1] = (2 – 1) = 1 = arr1[1]
Input: N = 5, arr1 = {1, 1, 1, 1, 1}
Output: arr2 = {1, 2, 3, 4, 5}
Explanation:
arr2[1] – arr2[0] = (2 – 1) = 1 = arr1[0]
arr2[2] – arr2[1] = (3 – 2) = 1 = arr1[1]
arr2[3] – arr2[2] = (4 – 3) = 1 = arr1[2]
arr2[4] – arr2[3] = (5 – 4) = 1 = arr1[3]
Approach:
Follow the steps to solve the problem:
- Assume the first element of arr2[] to be X.
- The next element will be X + arr1[0].
- The rest of the elements of arr2[] can be represented, w.r.t X.
- It is known that the sequence arr2[] can contain integers in the range [1, N]. So the minimum possible integer would be 1.
- The minimum number of the arr2[] can be found out in terms of X, and equate it with 1 to find the value of X.
- Finally using the values of X, all the other numbers in arr2[] can be found out.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_seq( int arr[],
int m, int n) {
int b[n];
int x = 0;
b[0] = x;
for ( int i = 0;
i < n - 1; i++) {
b[i + 1] = x +
arr[i] + b[i];
}
int mn = n;
for ( int i = 0; i < n; i++)
{
mn = min(mn, b[i]);
}
x = 1 - mn;
for ( int i = 0; i < n; i++) {
b[i] += x;
}
for ( int i = 0; i < n; i++) {
cout << b[i] << " " ;
}
cout << endl;
}
int main()
{
int N = 3;
int arr[] = { -2, 1 };
int M = sizeof (arr) / sizeof ( int );
find_seq(arr, M, N);
return 0;
}
|
Java
class GFG{
static void find_seq( int arr[], int m,
int n)
{
int b[] = new int [n];
int x = 0 ;
b[ 0 ] = x;
for ( int i = 0 ; i < n - 1 ; i++)
{
b[i + 1 ] = x + arr[i] + b[i];
}
int mn = n;
for ( int i = 0 ; i < n; i++)
{
mn = Math.min(mn, b[i]);
}
x = 1 - mn;
for ( int i = 0 ; i < n; i++)
{
b[i] += x;
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(b[i] + " " );
}
System.out.println();
}
public static void main (String[] args)
{
int N = 3 ;
int arr[] = new int []{ - 2 , 1 };
int M = arr.length;
find_seq(arr, M, N);
}
}
|
Python3
def find_seq(arr, m, n):
b = []
x = 0
b.append(x)
for i in range (n - 1 ):
b.append(x + arr[i] + b[i])
mn = n
for i in range (n):
mn = min (mn, b[i])
x = 1 - mn
for i in range (n):
b[i] + = x
for i in range (n):
print (b[i], end = ' ' )
print ()
if __name__ = = '__main__' :
N = 3
arr = [ - 2 , 1 ]
M = len (arr)
find_seq(arr, M, N)
|
C#
using System;
class GFG{
static void find_seq( int []arr, int m,
int n)
{
int []b = new int [n];
int x = 0;
b[0] = x;
for ( int i = 0; i < n - 1; i++)
{
b[i + 1] = x + arr[i] + b[i];
}
int mn = n;
for ( int i = 0; i < n; i++)
{
mn = Math.Min(mn, b[i]);
}
x = 1 - mn;
for ( int i = 0; i < n; i++)
{
b[i] += x;
}
for ( int i = 0; i < n; i++)
{
Console.Write(b[i] + " " );
}
Console.WriteLine();
}
public static void Main(String[] args)
{
int N = 3;
int []arr = new int []{ -2, 1 };
int M = arr.Length;
find_seq(arr, M, N);
}
}
|
Javascript
<script>
function find_seq(arr,m, n) {
let b = new Array(n);
let x = 0;
b[0] = x;
for (let i = 0;
i < n - 1; i++) {
b[i + 1] = x +
arr[i] + b[i];
}
let mn = n;
for (let i = 0; i < n; i++)
{
mn = Math.min(mn, b[i]);
}
x = 1 - mn;
for (let i = 0; i < n; i++) {
b[i] += x;
}
for (let i = 0; i < n; i++) {
document.write(b[i] + " " );
}
document.write( "<br>" );
}
let N = 3;
let arr = [ -2, 1 ];
let M = arr.length;
find_seq(arr, M, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...