Generate original array from difference between every two consecutive elements | C # program to implement the above 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
using System ; using System . Collections . Generic ; class GFG { static void findPerm ( int n , List < int > differences ) { List < int > ans = new List < int > ( ) ; ans . Clear ( ) ; ans . Add ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences [ i ] ; x = x + diff ; ans . Add ( x ) ; } List < int > anss = new List < int > ( ) ; foreach ( int obj in ans ) anss . Add ( obj ) ; ans . Sort ( ) ; 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 ) { Console . Write ( - 1 ) ; return ; } else { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; mpp . Clear ( ) ; int j = 1 ; List < int > value_at_index = new List < int > ( ) ; foreach ( int x1 in ans ) { mpp . Add ( x1 , j ) ; ++ j ; } foreach ( int x2 in anss ) { value_at_index . Add ( mpp [ x2 ] ) ; } foreach ( int x3 in value_at_index ) { Console . Write ( x3 + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { List < int > differences = new List < int > ( ) ; differences . Add ( 2 ) ; differences . Add ( - 3 ) ; differences . Add ( 2 ) ; int n = differences . Count + 1 ; findPerm ( n , differences ) ; } }