给定一个包含N个元素的数组,每个元素为1或2。任务是找出该数组是否可以分为2部分,以使这两部分中的元素总和相等。
例子:
Input : N = 3, arr[] = {1, 1, 2}
Output : YES
Input : N = 4, arr[] = {1, 2, 2, }
Output : NO
这样做的目的是要观察到,只有当数组的总和为偶数(即可被2整除)时,才可以将数组分为相等总和的两个部分。
假设数组的总和由sum表示。
现在,出现两种情况:
- 如果sum / 2是偶数:当sum / 2的值也是偶数时,这意味着两个部分中的每一个的和也是偶数,因此我们不需要考虑任何特殊的问题。因此,在这种情况下返回true。
- 如果sum / 2为奇数:当sum / 2的值为ODD时,表示各部分的和也为奇数。仅当数组的两个部分中的每一个都包含至少一个1时,才有可能。请考虑sum = 2或6或10的情况。因此,当sum / 2为奇数时,请检查数组中是否存在至少一个1。
下面是上述方法的实现:
C++
// C++ implementation of the above
// approach:
#include
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 = 3
a = [ 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
时间复杂度: O(N)