Given an binary array **arr[]** cof size **N**, the task is to reduce the array to a single element by the following two operations:

- A triplet of consecutive
**0**‘s or**1**‘s remains unchanged. - A triplet of consecutive array elements consisting of
**two 0’s and a single 1 or vice versa**can be converted to more frequent element.

**Examples:**

Input:arr[] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}Output:NoExplanation:

Following are the operations performed on the array:

{0, 1, 1} -> 1 modifies the array to {1, 1, 1, 0, 0, 1, 1, 1, 1}

{1, 0, 0} -> 0 modifies the array to {1, 1, 0, 1, 1, 1, 1}

{1, 0, 1} -> 1 modifies the array to {1, 1, 1, 1, 1}

Since, all the remaining elements are 1, they remain unchanged.

Therefore, the array cannot be reduced to a single element.

Input:arr[] = {1, 0, 0, 0, 1, 1, 1}Output:YesExplanation:

Following are the operations performed on the array:

{1, 0, 0} -> 0 {0, 0, 1, 1, 1}

{0, 0, 1} -> 0 {0, 1, 1}

{0, 1, 1} -> 1 {1}

**Approach: **

Follow the steps below to solve the problem:

- Count the frequency of
**0**‘s and**1**‘s. - Calculate the absolute difference their respective counts.
- If the difference is 1, only then the array can be reduced to 1. Therefore, print Yes.
- Otherwise, print No.

Below is the implementation of the above approach:

## C++

`// C++ program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if it is possible to ` `// reduce the array to a single element ` `void` `solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Stores frequency of 0's ` ` ` `int` `countzeroes = 0; ` ` ` ` ` `// Stores frequency of 1's ` ` ` `int` `countones = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `if` `(arr[i] == 0) ` ` ` `countzeroes++; ` ` ` `else` ` ` `countones++; ` ` ` `} ` ` ` ` ` `// Condition for array to be reduced ` ` ` `if` `(` `abs` `(countzeroes - countones) == 1) ` ` ` `cout << ` `"Yes"` `; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `cout << ` `"No"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 1, 0, 0, 1, 1, 1 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `solve(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to check if it is possible to ` `// reduce the array to a single element ` `static` `void` `solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Stores frequency of 0's ` ` ` `int` `countzeroes = ` `0` `; ` ` ` ` ` `// Stores frequency of 1's ` ` ` `int` `countones = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] == ` `0` `) ` ` ` `countzeroes++; ` ` ` `else` ` ` `countones++; ` ` ` `} ` ` ` ` ` `// Condition for array to be reduced ` ` ` `if` `(Math.abs(countzeroes - countones) == ` `1` `) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` ` ` `solve(arr, n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to check if it is possible to ` `# reduce the array to a single element ` `def` `solve(arr, n): ` ` ` ` ` `# Stores frequency of 0's ` ` ` `countzeroes ` `=` `0` `; ` ` ` ` ` `# Stores frequency of 1's ` ` ` `countones ` `=` `0` `; ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `=` `=` `0` `): ` ` ` `countzeroes ` `+` `=` `1` `; ` ` ` `else` `: ` ` ` `countones ` `+` `=` `1` `; ` ` ` ` ` `# Condition for array to be reduced ` ` ` `if` `(` `abs` `(countzeroes ` `-` `countones) ` `=` `=` `1` `): ` ` ` `print` `(` `"Yes"` `); ` ` ` ` ` `# Otherwise ` ` ` `else` `: ` ` ` `print` `(` `"No"` `); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `]; ` ` ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `solve(arr, n); ` ` ` `# This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check if it is possible to ` `// reduce the array to a single element ` `static` `void` `solve(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Stores frequency of 0's ` ` ` `int` `countzeroes = 0; ` ` ` ` ` `// Stores frequency of 1's ` ` ` `int` `countones = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] == 0) ` ` ` `countzeroes++; ` ` ` `else` ` ` `countones++; ` ` ` `} ` ` ` ` ` `// Condition for array to be reduced ` ` ` `if` `(Math.Abs(countzeroes - countones) == 1) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 0, 1, 0, 0, 1, 1, 1 }; ` ` ` ` ` `int` `n = arr.Length; ` ` ` ` ` `solve(arr, n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(N) **Auxiliary Space:** O(1)

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:

- Reduce the array to a single element with the given operation
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Minimize sum of adjacent difference with removal of one element from array
- Print the Forests of a Binary Tree after removal of given Nodes
- Minimum number of given operations required to reduce the array to 0 element
- Reduce the array such that each element appears at most 2 times
- Maximize cost obtained by removal of substrings "pr" or "rp" from a given String
- Minimize the maximum minimum difference after one removal from array
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Count of triplets in an array that satisfy the given conditions
- Reduce a given number to form a key by the given operations
- Count of triplets of numbers 1 to N such that middle element is always largest
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Count of strings that become equal to one of the two strings after one removal
- Longest palindromic string possible after removal of a substring
- Largest number possible after removal of K digits
- Minimum addition/removal of characters to be done to make frequency of each character prime

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.