Reduce all array elements to zero by performing given operations thrice | Java program for the above approach ; Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Driver code ; Input ; Function call to make all array elements equal to 0
import java . util . * ; class GFG { static void ConvertArray ( int arr [ ] , int N ) { if ( N == 1 ) { System . out . println ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + 1 * arr [ 0 ] ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 3 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; } else { System . out . println ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + N ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( - 1 * arr [ i ] * N + " ▁ " ) ; } System . out . println ( ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + ( N - 1 ) ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { System . out . print ( arr [ i ] * ( N - 1 ) + " ▁ " ) ; } System . out . println ( ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 3 ▁ : ▁ " + N + " ▁ " + N ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; System . out . println ( arr [ N - 1 ] * ( N - 1 ) ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = arr . length ; ConvertArray ( arr , N ) ; } }