给定一个整数数组arr [] ,任务是检查输入数组是否可以分为两个子数组,使得:
- 两个子阵列的总和相等。
- 所有可被5整除的元素应在同一组中。
- 所有可被3整除(但不能被5整除)的元素应在另一组中。
- 不能被5或3整除的元素可以放在任何组中。
如果可能,请打印“是”,否则打印“否” 。
例子:
Input: arr[] = {1, 2}
Output: No
The elements cannot be divided in groups such that there sum is equal.
Input: arr[] = {1, 4, 3}
Output: Yes
{1, 3} and {4} are the groups satisfying the given condition.
方法:我们可以通过保留左和和右和来维护两个不同的组来使用递归方法。左和为5的倍数,右和为3的倍数(不是5的倍数),并且不能被5或3整除的元素可以位于满足等和规则的任何组中(将它们包括在左边)和和右加一)。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Recursive function that returns true if the array
// can be divided into two sub-arrays
// satisfying the given condition
bool helper(int* arr, int n, int start, int lsum, int rsum)
{
// If reached the end
if (start == n)
return lsum == rsum;
// If divisible by 5 then add to the left sum
if (arr[start] % 5 == 0)
lsum += arr[start];
// If divisible by 3 but not by 5
// then add to the right sum
else if (arr[start] % 3 == 0)
rsum += arr[start];
// Else it can be added to any of the sub-arrays
else
// Try adding in both the sub-arrays (one by one)
// and check whether the condition satisfies
return helper(arr, n, start + 1, lsum + arr[start], rsum)
|| helper(arr, n, start + 1, lsum, rsum + arr[start]);
// For cases when element is multiple of 3 or 5.
return helper(arr, n, start + 1, lsum, rsum);
}
// Function to start the recursive calls
bool splitArray(int* arr, int n)
{
// Initially start, lsum and rsum will all be 0
return helper(arr, n, 0, 0, 0);
}
// Driver code
int main()
{
int arr[] = { 1, 4, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
if (splitArray(arr, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
class Solution
{
// Recursive function that returns true if the array
// can be divided into two sub-arrays
// satisfying the given condition
static boolean helper(int arr[], int n,
int start, int lsum, int rsum)
{
// If reached the end
if (start == n)
return lsum == rsum;
// If divisible by 5 then add to the left sum
if (arr[start] % 5 == 0)
lsum += arr[start];
// If divisible by 3 but not by 5
// then add to the right sum
else if (arr[start] % 3 == 0)
rsum += arr[start];
// Else it can be added to any of the sub-arrays
else
// Try adding in both the sub-arrays (one by one)
// and check whether the condition satisfies
return helper(arr, n, start + 1, lsum + arr[start], rsum)
|| helper(arr, n, start + 1, lsum, rsum + arr[start]);
// For cases when element is multiple of 3 or 5.
return helper(arr, n, start + 1, lsum, rsum);
}
// Function to start the recursive calls
static boolean splitArray(int arr[], int n)
{
// Initially start, lsum and rsum will all be 0
return helper(arr, n, 0, 0, 0);
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 4, 3 };
int n = arr.length;
if (splitArray(arr, n))
System.out.println( "Yes");
else
System.out.println( "No");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python 3 implementation of the approach
# Recursive function that returns true if
# the array can be divided into two sub-arrays
# satisfying the given condition
def helper(arr, n, start, lsum, rsum):
# If reached the end
if (start == n):
return lsum == rsum
# If divisible by 5 then add
# to the left sum
if (arr[start] % 5 == 0):
lsum += arr[start]
# If divisible by 3 but not by 5
# then add to the right sum
elif (arr[start] % 3 == 0):
rsum += arr[start]
# Else it can be added to any of
# the sub-arrays
else:
# Try adding in both the sub-arrays
# (one by one) and check whether
# the condition satisfies
return (helper(arr, n, start + 1,
lsum + arr[start], rsum) or
helper(arr, n, start + 1,
lsum, rsum + arr[start]));
# For cases when element is multiple of 3 or 5.
return helper(arr, n, start + 1, lsum, rsum)
# Function to start the recursive calls
def splitArray(arr, n):
# Initially start, lsum and rsum
# will all be 0
return helper(arr, n, 0, 0, 0)
# Driver code
if __name__ == "__main__":
arr = [ 1, 4, 3 ]
n = len(arr)
if (splitArray(arr, n)):
print("Yes")
else:
print("No")
# This code is contributed by ita_c
C#
// C# implementation of the approach
using System;
class GFG
{
// Recursive function that returns true if the array
// can be divided into two sub-arrays
// satisfying the given condition
static bool helper(int []arr, int n,
int start, int lsum, int rsum)
{
// If reached the end
if (start == n)
return lsum == rsum;
// If divisible by 5 then add to the left sum
if (arr[start] % 5 == 0)
lsum += arr[start];
// If divisible by 3 but not by 5
// then add to the right sum
else if (arr[start] % 3 == 0)
rsum += arr[start];
// Else it can be added to any of the sub-arrays
else
// Try adding in both the sub-arrays (one by one)
// and check whether the condition satisfies
return helper(arr, n, start + 1, lsum + arr[start], rsum)
|| helper(arr, n, start + 1, lsum, rsum + arr[start]);
// For cases when element is multiple of 3 or 5.
return helper(arr, n, start + 1, lsum, rsum);
}
// Function to start the recursive calls
static bool splitArray(int []arr, int n)
{
// Initially start, lsum and rsum will all be 0
return helper(arr, n, 0, 0, 0);
}
// Driver code
public static void Main()
{
int []arr = { 1, 4, 3 };
int n = arr.Length;
if (splitArray(arr, n))
Console.WriteLine( "Yes");
else
Console.WriteLine( "No");
}
}
// This code is contributed by Ryuga
PHP
输出:
Yes