给定具有N个整数的数组arr [] 。任务是确定是否可以将数组划分为相等总和的3个子序列。如果是,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {1, 1, 1}
Output: Yes
Explanation:
Here array can be partition into 3 equal sum. {1}
Input: arr[] = {40}
Output: No
Explanation:
Here array cannot be partition into 3 equal sum.
递归方法:可以使用递归来解决此问题。步骤如下:
- 将三个变量sum1 , sum2和sum3初始化为值0。
- 然后,将数组arr []的每个元素添加到这3个变量中的任何一个,它们给出了所有可能的组合。
- 如果任何子序列具有3个相等的和,则可以对数组进行分区。
- 如果阵列可以分区,则打印“是”,否则打印“否” 。
C++
// C++ program for
// the above approach
#include
using namespace std;
// Utility function to check array can
// be partition to 3 subsequences of
// equal sum or not
int checkEqualSumUtil(int arr[], int N,
int sm1, int sm2,
int sm3, int j)
{
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N,
sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Return maximum value among
// all above 3 recursive call
return max(max(l, m), r);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0)== 1)
{
cout << "Yes";
}
else
{
cout << "No";
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = {17, 34, 59, 23, 17, 67,
57, 2, 18, 59, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
checkEqualSum(arr, N);
return 0;
}
Java
// Java program for
// the above approach
class GFG{
// Utility function to check array can
// be partition to 3 subsequences of
// equal sum or not
static int checkEqualSumUtil(int arr[], int N,
int sm1, int sm2,
int sm3, int j)
{
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N,
sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Return maximum value among
// all above 3 recursive call
return Math.max(Math.max(l, m), r);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1};
int N = arr.length;
// Function Call
checkEqualSum(arr, N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Utility function to check array can
# be partition to 3 subsequences of
# equal sum or not
def checkEqualSumUtil(arr, N, sm1, sm2, sm3, j):
# Base case
if j == N:
if sm1 == sm2 and sm2 == sm3:
return 1
else:
return 0
else:
# When element at index
# j is added to sm1
l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1)
# When element at index
# j is added to sm2
m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1)
# When element at index
# j is added to sm3
r = checkEqualSumUtil(arr, N, sm1,
sm2, sm3 + arr[j],
j + 1)
# Return maximum value among
# all above 3 recursive call
return max(l, m, r)
# Function to check array can be
# partition to 3 subsequences of
# equal sum or not
def checkEqualSum(arr, N):
# Initialise 3 sums to 0
sum1 = sum2 = sum3 = 0
# Function call
if checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1:
print("Yes")
else:
print("No")
# Driver code
# Given array arr[]
arr = [ 17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1 ]
N = len(arr)
# Function call
checkEqualSum(arr, N)
# This code is contributed by Stuti Pathak
C#
// C# program for
// the above approach
using System;
class GFG{
// Utility function to check array can
// be partition to 3 subsequences of
// equal sum or not
static int checkEqualSumUtil(int[] arr, int N,
int sm1, int sm2,
int sm3, int j)
{
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N,
sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N,
sm1, sm2,
sm3 + arr[j],
j + 1);
// Return maximum value among
// all above 3 recursive call
return Math.Max(Math.Max(l, m), r);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int[] arr, int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
// Given array arr[]
int[] arr = {17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1};
int N = arr.Length;
// Function Call
checkEqualSum(arr, N);
}
}
// This code is contributed by Chitranayal
C++
// C++ program for the above approach
#include
using namespace std;
map dp;
// Function to check array can be
// partition into sum of 3 equal
int checkEqualSumUtil(int arr[], int N,
int sm1,
int sm2,
int sm3, int j)
{
string s = to_string(sm1)
+ "_" + to_string(sm2)
+ to_string(j);
// Base Case
if (j == N) {
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.find(s) != dp.end())
return dp[s];
else {
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(
arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(
arr, N, sm1, sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(
arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
return dp[s] = max(max(l, m), r);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(
arr, N, sum1,
sum2, sum3, 0)
== 1) {
cout << "Yes";
}
else {
cout << "No";
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[]
= { 17, 34, 59, 23, 17, 67,
57, 2, 18, 59, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
checkEqualSum(arr, N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static HashMap dp = new HashMap();
// Function to check array can be
// partition into sum of 3 equal
static int checkEqualSumUtil(int arr[], int N,
int sm1, int sm2,
int sm3, int j)
{
String s = String.valueOf(sm1) + "_" +
String.valueOf(sm2) + String.valueOf(j);
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.containsKey(s))
return dp.get(s);
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
dp.put(s, Math.max(Math.max(l, m), r));
return dp.get(s);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1};
int N = arr.length;
// Function Call
checkEqualSum(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
dp = {}
# Function to check array can be
# partition into sum of 3 equal
def checkEqualSumUtil(arr, N, sm1, sm2, sm3, j):
s = str(sm1) + "_" + str(sm2) + str(j)
# Base Case
if j == N:
if sm1 == sm2 and sm2 == sm3:
return 1
else:
return 0
# If value at particular index is not
# -1 then return value at that index
# which ensure no more further calls
if s in dp:
return dp[s]
# When element at index
# j is added to sm1
l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1)
# When element at index
# j is added to sm2
m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j], sm3,
j + 1)
# When element at index
# j is added to sm3
r = checkEqualSumUtil(arr, N, sm1,
sm2, sm3 + arr[j],
j + 1)
# Update the current state and
# return that value
dp[s] = max(l, m, r)
return dp[s]
# Function to check array can be
# partition to 3 subsequences of
# equal sum or not
def checkEqualSum(arr, N):
# Initialise 3 sums to 0
sum1 = sum2 = sum3 = 0
# Function Call
if checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1:
print("Yes")
else:
print("No")
# Driver code
# Given array arr[]
arr = [ 17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1 ]
N = len(arr)
# Function call
checkEqualSum(arr, N)
# This code is contributed by Stuti Pathak
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static Dictionary dp = new Dictionary();
// Function to check array can be
// partition into sum of 3 equal
static int checkEqualSumUtil(int []arr, int N,
int sm1, int sm2,
int sm3, int j)
{
string s = sm1.ToString() + "_" +
sm2.ToString() + j.ToString();
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.ContainsKey(s))
return dp[s];
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
dp[s] = Math.Max(Math.Max(l, m), r);
return dp[s];
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int []arr, int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1 };
int N = arr.Length;
// Function call
checkEqualSum(arr, N);
}
}
// This code is contributed by rutvik_56
Yes
时间复杂度: O(3 N )
辅助空间: O(1)
动态编程方法:此问题可以使用动态编程解决,其思想是将所有重叠的子问题值存储在映射中,并使用重叠的子结构的值来减少递归调用的次数。步骤如下:
- 令sum1 , sum2和sum3为要划分的三个相等的总和。
- 创建具有密钥的映射dp 。
- 遍历给定数组并执行以下操作:
- 基本情况:遍历数组时,如果我们到达数组的末尾,然后检查sum1 , sum2和sum3的值是否相等,然后返回1,这将确保我们可以将给定的数组分解为相等的sum值的子序列。否则,返回0。
- 递归调用:对于数组中的每个元素,将sum1 , sum2和sum3中的每个元素一一包含,并返回这些递归调用中的最大值。
a = recursive_function(arr, N, sum1 + arr[j], sum2, sum3, j + 1)
b = recursive_function(arr, N, sum1, sum2 + arr[j], sum3, j + 1)
c = recursive_function(arr, N, sum1, sum2, sum3 + arr[j], j + 1)
- 返回语句:在上面的递归调用中,三个值中的最大值将给出当前递归调用的结果。将dp表中的当前状态更新为:
string s = to_string(sum1) + ‘_’ + to_string(sum2) + to_string(j)
return dp[s] = max(a, max(b, c) )
- 如果可以进行分区,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
map dp;
// Function to check array can be
// partition into sum of 3 equal
int checkEqualSumUtil(int arr[], int N,
int sm1,
int sm2,
int sm3, int j)
{
string s = to_string(sm1)
+ "_" + to_string(sm2)
+ to_string(j);
// Base Case
if (j == N) {
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.find(s) != dp.end())
return dp[s];
else {
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(
arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(
arr, N, sm1, sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(
arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
return dp[s] = max(max(l, m), r);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(
arr, N, sum1,
sum2, sum3, 0)
== 1) {
cout << "Yes";
}
else {
cout << "No";
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[]
= { 17, 34, 59, 23, 17, 67,
57, 2, 18, 59, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
checkEqualSum(arr, N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static HashMap dp = new HashMap();
// Function to check array can be
// partition into sum of 3 equal
static int checkEqualSumUtil(int arr[], int N,
int sm1, int sm2,
int sm3, int j)
{
String s = String.valueOf(sm1) + "_" +
String.valueOf(sm2) + String.valueOf(j);
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.containsKey(s))
return dp.get(s);
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
dp.put(s, Math.max(Math.max(l, m), r));
return dp.get(s);
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int arr[], int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function Call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = {17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1};
int N = arr.length;
// Function Call
checkEqualSum(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
dp = {}
# Function to check array can be
# partition into sum of 3 equal
def checkEqualSumUtil(arr, N, sm1, sm2, sm3, j):
s = str(sm1) + "_" + str(sm2) + str(j)
# Base Case
if j == N:
if sm1 == sm2 and sm2 == sm3:
return 1
else:
return 0
# If value at particular index is not
# -1 then return value at that index
# which ensure no more further calls
if s in dp:
return dp[s]
# When element at index
# j is added to sm1
l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1)
# When element at index
# j is added to sm2
m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j], sm3,
j + 1)
# When element at index
# j is added to sm3
r = checkEqualSumUtil(arr, N, sm1,
sm2, sm3 + arr[j],
j + 1)
# Update the current state and
# return that value
dp[s] = max(l, m, r)
return dp[s]
# Function to check array can be
# partition to 3 subsequences of
# equal sum or not
def checkEqualSum(arr, N):
# Initialise 3 sums to 0
sum1 = sum2 = sum3 = 0
# Function Call
if checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1:
print("Yes")
else:
print("No")
# Driver code
# Given array arr[]
arr = [ 17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1 ]
N = len(arr)
# Function call
checkEqualSum(arr, N)
# This code is contributed by Stuti Pathak
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static Dictionary dp = new Dictionary();
// Function to check array can be
// partition into sum of 3 equal
static int checkEqualSumUtil(int []arr, int N,
int sm1, int sm2,
int sm3, int j)
{
string s = sm1.ToString() + "_" +
sm2.ToString() + j.ToString();
// Base Case
if (j == N)
{
if (sm1 == sm2 && sm2 == sm3)
return 1;
else
return 0;
}
// If value at particular index is not
// -1 then return value at that index
// which ensure no more further calls
if (dp.ContainsKey(s))
return dp[s];
else
{
// When element at index
// j is added to sm1
int l = checkEqualSumUtil(arr, N, sm1 + arr[j],
sm2, sm3, j + 1);
// When element at index
// j is added to sm2
int m = checkEqualSumUtil(arr, N, sm1,
sm2 + arr[j],
sm3, j + 1);
// When element at index
// j is added to sm3
int r = checkEqualSumUtil(arr, N, sm1, sm2,
sm3 + arr[j], j + 1);
// Update the current state and
// return that value
dp[s] = Math.Max(Math.Max(l, m), r);
return dp[s];
}
}
// Function to check array can be
// partition to 3 subsequences of
// equal sum or not
static void checkEqualSum(int []arr, int N)
{
// Initialise 3 sums to 0
int sum1, sum2, sum3;
sum1 = sum2 = sum3 = 0;
// Function call
if (checkEqualSumUtil(arr, N, sum1,
sum2, sum3, 0) == 1)
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 17, 34, 59, 23, 17,
67, 57, 2, 18, 59, 1 };
int N = arr.Length;
// Function call
checkEqualSum(arr, N);
}
}
// This code is contributed by rutvik_56
Yes
时间复杂度: O(N * K 2 )
辅助空间: O(N * K 2 )其中K是数组的总和。
(to_string(sum1) + “_” + to_string(sum2) + “_” + to_string(sum3))
- 如果找到3个相等的子集,则value为1 ,否则value为0 。