Given an array containing **N** elements, each element is either 1 or 2. The task is to find out whether the array can be divided into 2 parts such that sum of elements in both parts is equal.

**Examples:**

Input :N = 3, arr[] = {1, 1, 2}Output :YESInput :N = 4, arr[] = {1, 2, 2, }Output :NO

The idea is to observe that the array can be divided into two parts with equal sum only if the overall sum of the array is even, i.e. divisible by 2.

Let’s say the overall sum of the array is denoted by **sum**.

Now, there arises two cases:

**If sum/2 is even**: When the value of sum/2 is also even, it means that sum of each of the two parts is also even and we need not to consider anything special. So, return true for this case.**If sum/2 is odd**: When the value of sum/2 is ODD, it means that sum of each part is also odd. This is only possible when each of the two parts of the array contains atleast one 1. Consider the cases when sum = 2 or 6 or 10. So, when sum/2 is odd, check if there is atleast one 1 in the array.

Below is the implementation of the above approach:

`// C++ implementation of the above ` `// approach: ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if it is possible to ` `// split the array in two parts with ` `// equal sum ` `bool` `isSpiltPossible(` `int` `n, ` `int` `a[]) ` `{ ` ` ` `int` `sum = 0, c1 = 0; ` ` ` ` ` `// Calculate sum of elements ` ` ` `// and count of 1's ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` ` ` `if` `(a[i] == 1) { ` ` ` `c1++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If total sum is odd, return False ` ` ` `if` `(sum % 2) ` ` ` `return` `false` `; ` ` ` ` ` `// If sum of each part is even, ` ` ` `// return True ` ` ` `if` `((sum / 2) % 2 == 0) ` ` ` `return` `true` `; ` ` ` ` ` `// If sum of each part is even but ` ` ` `// there is atleast one 1 ` ` ` `if` `(c1 > 0) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `int` `a[] = { 1, 1, 2 }; ` ` ` ` ` `if` `(isSpiltPossible(n, a)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:** O(N)

## Recommended Posts:

- Find if array can be divided into two subarrays of equal sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Divide binary array into three equal parts with same value
- Split the array into equal sum parts according to given conditions
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Check if a sorted array can be divided in pairs whose sum is k
- Check if an array can be divided into pairs whose sum is divisible by k
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Minimum cuts required to divide the Circle into equal parts
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if it is possible to make array equal by doubling or tripling
- Check if the array has an element which is equal to product of remaining elements
- Check if elements of array can be made equal by multiplying given prime numbers