Reduce all array elements to zero by performing given operations thrice | C # 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
using System ; class GFG { static void ConvertArray ( int [ ] arr , int N ) { if ( N == 1 ) { Console . WriteLine ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + 1 * arr [ 0 ] ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 3 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; } else { Console . WriteLine ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + N ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( - 1 * arr [ i ] * N + " ▁ " ) ; } Console . WriteLine ( ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + ( N - 1 ) ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { Console . Write ( arr [ i ] * ( N - 1 ) + " ▁ " ) ; } Console . WriteLine ( ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 3 ▁ : ▁ " + N + " ▁ " + N ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; Console . WriteLine ( arr [ N - 1 ] * ( N - 1 ) ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; int N = arr . Length ; ConvertArray ( arr , N ) ; } }