Check if an array of 1s and 2s can be divided into 2 parts with equal sum
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 : YES
Input : 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++
// 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; } |
Java
// Java implementation of the above // approach: class GFG { // Function to check if it is possible // to split the array in two parts with // equal sum static boolean 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 != 0) 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 public static void main(String[] args) { int n = 3; int a[] = { 1, 1, 2 }; if (isSpiltPossible(n, a)) System.out.println("YES"); else System.out.println("NO"); } } // This code is contributed by // Code Mech |
Python3
# Python3 implementation of the above # approach: # Function to check if it is possible # to split the array in two halfs with # equal Sum def isSpiltPossible(n, a): Sum = 0 c1 = 0 # Calculate Sum of elements # and count of 1's for i in range(n): Sum += a[i] if (a[i] == 1): c1 += 1 # If total Sum is odd, return False if (Sum % 2): return False # If Sum of each half is even, # return True if ((Sum // 2) % 2 == 0): return True # If Sum of each half is even # but there is atleast one 1 if (c1 > 0): return True else: return False # Driver Code n = 3a = [ 1, 1, 2 ] if (isSpiltPossible(n, a)): print("YES") else: print("NO") # This code is contributed # by Mohit Kumar |
C#
// C# implementation of the above // approach: using System; class GFG { // Function to check if it is possible // to split the array in two parts with // equal sum static 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 != 0) 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 public static void Main() { int n = 3; int[] a = { 1, 1, 2 }; if (isSpiltPossible(n, a)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed by // Code Mech |
PHP
0)
return true;
else
return false;
}
// Driver Code
$n = 3;
$a = array( 1, 1, 2 );
if (isSpiltPossible($n, $a))
echo(“YES”);
else
echo(“NO”);
// This code is contributed by
// Code Mech
?>
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 array into two parts with equal sum according to the given constraints
- 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 an array can be divided into pairs whose sum is divisible by k
- Check if a sorted array can be divided in pairs whose sum is 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 it is possible to make array equal by doubling or tripling
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
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.



