Generate original array from difference between every two consecutive elements | C ++ implementation of the approach ; Function to print the required permutation ; Take x = 0 for simplicity ; Calculate aint the differences and store it in a vector ; Preserve the original array ; Check if aint the consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPerm ( int n , vector < int > & differences ) { vector < int > ans ; ans . clear ( ) ; ans . push_back ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences [ i ] ; x = x + diff ; ans . push_back ( x ) ; } vector < int > anss = ans ; sort ( ans . begin ( ) , ans . end ( ) ) ; int flag = -1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { cout << -1 ; return ; } else { unordered_map < int , int > mpp ; mpp . clear ( ) ; int j = 1 ; vector < int > value_at_index ; for ( auto & x : ans ) { mpp [ x ] = j ; ++ j ; } for ( auto & x : anss ) { value_at_index . push_back ( mpp [ x ] ) ; } for ( auto & x : value_at_index ) { cout << x << " ▁ " ; } cout << endl ; } } int main ( ) { vector < int > differences ; differences . push_back ( 2 ) ; differences . push_back ( -3 ) ; differences . push_back ( 2 ) ; int n = differences . size ( ) + 1 ; findPerm ( n , differences ) ; return 0 ; }