Construct array with sum of product of same indexed elements in the given array equal to zero | Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Prlet new array elements ; Driver Code
function constructNewArraySumZero ( arr , N ) { let newArr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( newArr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , - 5 , - 6 , 8 ] ; let N = arr . length ; constructNewArraySumZero ( arr , N ) ;