Generate Array whose difference of each element with its left yields the given Array | C ++ program for the above approach ; Function to find the sequence ; initializing 1 st element ; Creating sequence in terms of x ; Finding min element ; Finding value of x ; Creating original sequence ; Output original sequence ; Driver function
#include <bits/stdc++.h> NEW_LINE 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 ; }