Construct array with sum of product of same indexed elements in the given array equal to zero | Java program to implement the above approach ; 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 [ ] ; Print new array elements ; Driver Code
import java . util . * ; class GFG { static void constructNewArraySumZero ( int arr [ ] , int N ) { int newArr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( newArr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , - 5 , - 6 , 8 } ; int N = arr . length ; constructNewArraySumZero ( arr , N ) ; } }