Making zero array by decrementing pairs of adjacent | CPP program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleToZero ( int a [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossibleToZero ( arr , n ) ) cout << " YES " ; else cout << " NO " ; }