A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Given an array arr[] having N integers. The task is to determine if the array can be partitioned into 3 subsequences of equal sum or not. If yes then print “Yes”. Otherwise, print “No”.
Examples:
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.
Recursive Approach: This problem can be solved using recursion. Below are the steps:
// C++ program for the above approach
#include <bits/stdc++.h>
**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;
}
Output:
Yes
Time Complexity:_ O(3N)_
Auxiliary Space:_ O(1)_
Dynamic Programming** Approach:** This problem can be solved using dynamic programming, the idea is to store all the overlapping subproblems value in a map and use the value of overlapping substructure to reduce the number of the recursive calls. Below are the steps:
(to_string(sum1) + “_” + _**_to_string_(sum2) + “_” + _to_string**(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)
string s = to_string(sum1) + ‘_’ + to_string(sum1) + to_string(sum1)
return dp[s] = max(a, max(b, c)
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
map<string,
**int**``> 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;
}
Output:
Yes
Time Complexity:_ O(N*K2) _
Auxiliary Space:_ O(N*K2) where K is the sum of the array._
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
In this article, I will introduce the concept of dynamic programming, developed by Richard Bellman in the 1950s, a powerful algorithm design technique to solve problems by breaking them down into smaller
Recursion is the one idea I constantly use while I solve coding problems. Most of the time I don’t start by thinking “RECURSION WILL SOLVE THIS!”. However recursion just ends up being the logical way to reach an answer.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.