Reduce all array elements to zero by performing given operations thrice | C ++ program of 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ConvertArray ( int arr [ ] , int N ) { if ( N == 1 ) { cout << " Operation ▁ 1 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << -1 * arr [ 0 ] << endl ; cout << endl ; cout << " Operation ▁ 2 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << 1 * arr [ 0 ] << endl ; cout << endl ; cout << " Operation ▁ 3 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << -1 * arr [ 0 ] << endl ; } else { cout << " Operation ▁ 1 ▁ : ▁ " << 1 << " ▁ " << N << endl ; cout << " Added ▁ elements : ▁ " ; for ( int i = 0 ; i < N ; i ++ ) { cout << -1 * arr [ i ] * N << " ▁ " ; } cout << endl ; cout << endl ; cout << " Operation ▁ 2 ▁ : ▁ " << 1 << " ▁ " << N - 1 << endl ; cout << " Added ▁ elements : ▁ " ; for ( int i = 0 ; i < N - 1 ; i ++ ) { cout << arr [ i ] * ( N - 1 ) << " ▁ " ; } cout << endl ; cout << endl ; cout << " Operation ▁ 3 ▁ : ▁ " << N << " ▁ " << N << endl ; cout << " Added ▁ elements : ▁ " ; cout << arr [ N - 1 ] * ( N - 1 ) << endl ; } } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ConvertArray ( arr , N ) ; return 0 ; }