Generate original array from difference between every two consecutive elements | 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
function findPerm ( n , differences ) { let ans = [ ] ; ans . push ( 0 ) ; let x = 0 ; for ( let i = 0 ; i <= n - 2 ; ++ i ) { let diff = differences [ i ] ; x = x + diff ; ans . push ( x ) ; } let anss = [ ] ; for ( let obj = 0 ; obj < ans . length ; obj ++ ) anss . push ( ans [ obj ] ) ; ans . sort ( function ( a , b ) { return a - b ; } ) ; let flag = - 1 ; for ( let i = 1 ; i <= n - 1 ; ++ i ) { let res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { document . write ( - 1 ) ; return ; } else { let mpp = new Map ( ) ; let j = 1 ; let value_at_index = [ ] ; for ( let x1 = 0 ; x1 < ans . length ; x1 ++ ) { mpp . set ( ans [ x1 ] , j ) ; ++ j ; } for ( let x2 = 0 ; x2 < anss . length ; x2 ++ ) { value_at_index . push ( mpp . get ( anss [ x2 ] ) ) ; } for ( let x3 = 0 ; x3 < value_at_index . length ; x3 ++ ) { document . write ( value_at_index [ x3 ] + " " ) ; } document . write ( " " ) ; } } let differences = [ ] ; differences . push ( 2 ) ; differences . push ( - 3 ) ; differences . push ( 2 ) ; let n = differences . length + 1 ; findPerm ( n , differences ) ;