Reduce all array elements to zero by performing given operations thrice | 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 ; Input ; Function call to make all array elements equal to 0
function ConvertArray ( arr , N ) { if ( N == 1 ) { document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + - 1 * arr [ 0 ] + " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + 1 * arr [ 0 ] + " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + 1 + " " ) ; document . write ( " " + - 1 * arr [ 0 ] + " " ) ; } else { document . write ( " " + 1 + " " + N + " " ) ; document . write ( " " ) ; for ( i = 0 ; i < N ; i ++ ) { document . write ( - 1 * arr [ i ] * N + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + 1 + " " + ( N - 1 ) + " " ) ; document . write ( " " ) ; for ( i = 0 ; i < N - 1 ; i ++ ) { document . write ( arr [ i ] * ( N - 1 ) + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + N + " " + N + " " ) ; document . write ( " " ) ; document . write ( arr [ N - 1 ] * ( N - 1 ) + " " ) ; } } var arr = [ 1 , 3 , 2 , 4 ] ; var N = arr . length ; ConvertArray ( arr , N ) ;