Given a sequence of non-negative integers, say a1, a2, …, an. Only following actions can be performed on the given sequence:
- Subtract 1 from a[i] and a[i+1] both.
Find if the series can be modified into all zeros using any required number of above operations.
Examples :
Input : 1 2 Output : NO Explanation: Only two elements, if we subtract 1 then it will convert into [0, 1].
so answer is NO. Input : 0 1 1 0 Output : YES Explanation: Here we can choose both 1 and subtract 1 from them then array becomes [0, 0, 0, 0].
So answer is YES. Input : 1 2 3 4 Output : NO Explanation: if we try to subtract 1 any number of times then array will be [0, 0, 0, 1]. [1, 2, 3, 4]->[0, 1, 3, 4]->[0, 0, 2, 3]-> [0, 0, 1, 2]->[0, 0, 0, 1].
Approach 1 : If all adjacent elements(i, i+1) in array are equal and total number of element in array is even then it’s all element can be converted to zero. For example, if array elements are like {1, 1, 2, 2, 3, 3} then its all element is convertible into zero.
Then in this case sum of every odd positioned value are always equal to the sum of even positioned value.
C++
// CPP program to find if it is possible // to make all array elements 0 by decrement // operations. #include <bits/stdc++.h> using namespace std; bool isPossibleToZero(int a[], int n) { // used for storing the sum of even // and odd position element in array. int even = 0, odd = 0; for (int i = 0; i < n; i++) { // if position is odd, store sum // value of odd position in odd if (i & 1) odd += a[i]; // if position is even, store sum // value of even position in even else even += a[i]; } return (odd == even); } // Driver program int main() { int arr[] = { 0, 1, 1, 0 }; int n = sizeof(arr) / sizeof(arr[0]); if (isPossibleToZero(arr, n)) cout << "YES"; else cout << "NO"; } |
Java
// Java program to find if // it is possible to make // all array elements 0 by // decrement operations. import java.io.*; class GFG { static boolean isPossibleToZero(int a[], int n) { // used for storing the // sum of even and odd // position element in array. int even = 0, odd = 0; for (int i = 0; i < n; i++) { // if position is odd, // store sum value of // odd position in odd if ((i & 1) == 0) odd += a[i]; // if position is even, // store sum value of // even position in even else even += a[i]; } return (odd == even); } // Driver Code public static void main(String[] args) { int arr[] = { 0, 1, 1, 0 }; int n = arr.length; if (isPossibleToZero(arr, n)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by m_kit |
Python3
# Python3 program to find if it is # possible to make all array elements # 0 by decrement operations. def isPossibleToZero(a, n): # used for storing the # sum of even and odd # position element in array. even = 0; odd = 0; for i in range(n): # if position is odd, store sum # value of odd position in odd if (i & 1): odd += a[i]; # if position is even, store sum # value of even position in even else: even += a[i]; return (odd == even); # Driver Code arr = [0, 1, 1, 0]; n = len(arr); if (isPossibleToZero(arr, n)): print("YES"); else: print("NO"); # This code is contributed by mits |
C#
// C# program to find if // it is possible to make // all array elements 0 by // decrement operations. using System; class GFG { static bool isPossibleToZero(int []a, int n) { // used for storing the // sum of even and odd // position element in array. int even = 0, odd = 0; for (int i = 0; i < n; i++) { // if position is odd, // store sum value of // odd position in odd if ((i & 1) == 0) odd += a[i]; // if position is even, // store sum value of // even position in even else even += a[i]; } return (odd == even); } // Driver Code static public void Main () { int []arr = {0, 1, 1, 0}; int n = arr.Length; if (isPossibleToZero(arr, n)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed // by m_kit |
PHP
<?php // PHP program to find if it // is possible to make all // array elements 0 by // decrement operations. function isPossibleToZero($a, $n) { // used for storing the // sum of even and odd // position element in array. $even = 0; $odd = 0; for ($i = 0; $i < $n; $i++) { // if position is odd, // store sum value of // odd position in odd if ($i & 1) $odd += $a[$i]; // if position is even, // store sum value of // even position in even else $even += $a[$i]; } return ($odd == $even); } // Driver Code $arr = array (0, 1, 1, 0); $n = sizeof($arr); if (isPossibleToZero($arr, $n)) echo "YES"; else echo "NO"; // This code is contributed by ajit ?> |
YES
Approach 2: If Number formed by given array element is divisible by 11 then all elements of array also can be convertible to zero.
For ex: given array {0, 1, 1, 0}, number formed by this array is 110 then it is divisible by 11. So all elements can be converted into zero.
C++
// CPP implementation of the // above approach #include <bits/stdc++.h> using namespace std; bool isPossibleToZero(int a[], int n) { // converting array element into number int num = 0; for (int i = 0; i < n; i++) num = num * 10 + a[i]; // Check if divisible by 11 return (num % 11 == 0); } // Driver program int main() { int arr[] = { 0, 1, 1, 0 }; int n = sizeof(arr) / sizeof(arr[0]); if (isPossibleToZero(arr, n)) cout << "YES"; else cout << "NO"; } |
Java
// Java implementation of the above approach import java.io.*; class GFG { static boolean isPossibleToZero(int a[], int n) { // converting array element into number int num = 0; for (int i = 0; i < n; i++) num = num * 10 + a[i]; // Check if divisible by 11 return (num % 11 == 0); } // Driver program public static void main (String[] args) { int arr[] = {0, 1, 1, 0}; int n = arr.length; if (isPossibleToZero(arr, n)) System.out.println( "YES"); else System.out.println ("NO"); } } // This code is contributed by @ajit |
Python3
# Python3 implementation of the # above approach def isPossibleToZero(a, n): # converting array element # into number num = 0; for i in range(n): num = num * 10 + a[i]; # Check if divisible by 11 return (num % 11 == 0); # Driver Code arr = [ 0, 1, 1, 0 ]; n = len(arr); if (isPossibleToZero(arr, n)): print("YES"); else: print("NO"); # This code is contributed mits |
C#
// C# implementation of the above approach using System; class GFG { static bool isPossibleToZero(int[] a, int n) { // converting array element into number int num = 0; for (int i = 0; i < n; i++) num = num * 10 + a[i]; // Check if divisible by 11 return (num % 11 == 0); } // Driver Code public static void Main() { int[] arr = {0, 1, 1, 0}; int n = arr.Length; if (isPossibleToZero(arr, n)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed // by Akanksha Rai |
PHP
<?php // PHP implementation of // the above approach function isPossibleToZero($a, $n) { // converting array // element into number $num = 0; for ($i = 0; $i < $n; $i++) $num = $num * 10 + $a[$i]; // Check if divisible by 11 return ($num % 11 == 0); } // Driver Code $arr = array( 0, 1, 1, 0 ); $n = sizeof($arr); if (isPossibleToZero($arr, $n)) echo "YES"; else echo "NO"; // This code is contributed ajit ?> |
YES
The above implementation causes overflow for slightly bigger arrays. We can use below method to avoid overflow.Check if a large number is divisible by 11 or not
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Reduce given three Numbers by decrementing in Pairs
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Smallest number possible by swapping adjacent even odd pairs
- Check if at least half array is reducible to zero by performing some operations
- Quadruplet pair with XOR zero in the given Array
- Right most non-zero digit in multiplication of array elements
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Minimum steps required to reduce all the elements of the array to zero
- Minimize the non-zero elements in the Array by given operation
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Ways to form n/2 pairs such that difference of pairs is minimum
- Reach the numbers by making jumps of two given lengths
- Check if it is possible to reach a number by making jumps of two given length
- Making three numbers equal with the given operations
- Weighted Sum Method - Multi Criteria Decision Making
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

