Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | C ++ program for the above approach ; Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPossible ( int * arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { cout << " No STRNEWLINE " ; return ; } else { arr [ i ] -= arr [ i - 1 ] ; arr [ i - 1 ] = 0 ; } } if ( arr [ n - 1 ] == 0 ) { cout << " Yes STRNEWLINE " ; } else { cout << " No STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkPossible ( arr , N ) ; return 0 ; }