Generate Array whose difference of each element with its left yields the given Array | 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
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 ( " " ) ; } let N = 3 ; let arr = [ - 2 , 1 ] ; let M = arr . length ; find_seq ( arr , M , N ) ;