Partition Equal Subset Sum | Practice | GeeksforGeeks

Given an array arr[] of size N, check if it can be partitioned into two parts such that the sum of elements in both parts is the same.
Example 1:
Input: N = 4
arr = {1, 5, 11, 5}
Output: YES
Explaination: 
The two parts are {1, 5, 5} and {11}.

Example 2:
Input: N = 3
arr = {1, 3, 5}
Output: NO
Explaination: This array can never be 
partitioned into two such parts.

Your Task:
You do not need to read input or print anything. Your task is to complete the function equalPartition() which takes the value N and the array as input parameters and returns 1 if the partition is possible. Otherwise, returns 0.

Expected Time Complexity: O(N*sum of elements)
Expected Auxiliary Space: O(N*sum of elements)

Constraints:
1 ≤ N ≤ 100
1 ≤ arr[i] ≤ 1000
0
adarshgupta4014 days ago
Easy Java DP knapSack technique-
class Solution{
    static int equalPartition(int n, int nums[])
    {
        int sum = 0;
        for(int a :  nums) sum += a;
         
        if(sum % 2 != 0) return 0;      // return false if odd sum
        
        return subsetSum( nums,n, sum/2);
    }
    private static int subsetSum(int[] nums, int n, int sum){
        int dp[][] = new int[n+1][sum+1];
        
        for(int i =0 ; i< sum+1 ; ++i) dp[0][i] = 0;
        for(int i = 0; i< n+1 ; ++i)   dp[i][0] = 1;
        
        for(int i = 1; i< n+1 ; i++){
            for( int j = 1;  j < sum+1 ; j++){
                if(nums[i-1] <= j)
                        dp[i][j] = Math.max( dp[i-1][j-nums[i-1]], dp[i-1][j] );
                else dp[i][j] = dp[i-1][j];
            }
        }
        return dp[n][sum];
    }
}
0
milindprajapatmst196 days ago
const int N = 1e2, M = 1e5;
bool dp[N][M + 1];

class Solution {
public:
    int equalPartition(int n, int arr[]) {
        int _sum = 0;
        for (int i = 0; i < n; i++)
            _sum += arr[i];
        if (_sum & 1) return false;
        
        memset(dp, false, sizeof(dp));
        dp[0][0] = dp[0][arr[0]] = true;
        for (int i = 1; i < n; i++) {
            dp[i][0] = true;
            for (int j = 0; j <= _sum; j++) {
                dp[i][j] |= dp[i - 1][j];
                dp[i][j + arr[i]] |= dp[i - 1][j];
            }
        }
        return dp[n - 1][_sum >> 1];
    }
};
+1
shishir17pandey1 week ago
      int sum=0;       int n=N;       for(int i=0;i<n;i++){           sum=sum+arr[i];       }       int a=0;       if(sum%2!=0){return false;       }       else{           a=sum/2;       }       bool dp[n+1][a+1];       dp[0][0]=true;            for(int i=1;i<=n;i++){           for(int j=0;j<=a;j++){               if(j>=arr[i-1]){                   dp[i][j]= dp[i-1][j] || dp[i-1][j-arr[i-1]];               }               else{                   dp[i][j]=dp[i-1][j];               }           }       }       return dp[n][a];   }
0
deepakkumarshaw662 weeks ago
int helperDP(int n, int *arr, int sum) {              int **dp = new int*[n + 1];       for(int i = 0; i <= n; i++)           dp[i] = new int[sum + 1];                  //INITIALIZING FIRST ROW       for(int i = 0; i <= sum; i++)           dp[0][i] = 0;       //INITIALISING FIRST COLUMN       for(int i = 0; i <= n; i++)           dp[i][0] = 1;                  //FILLING REAINING ROWS AND COLUMNS       for(int i = 1; i <= n; i++) {           for(int j = 1; j <= sum; j++) {               if(arr[i - 1] <= j)                   dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j];               else                   dp[i][j] = dp[i - 1][j];           }       }       return dp[n][sum];   }      int equalPartition(int N, int arr[])   {       long sum = 0;       for(int i = 0; i < N; i++)           sum += arr[i];       //IF SUM IS ODD           if(sum % 2 != 0)           return 0;              return helperDP(N, arr, sum/2);          }
+1
aniketsaraswat1122 weeks ago
int equalPartition(int n, int arr[]){
        int sum = 0; 
        for(int i = 0; i<n; i++) sum += arr[i];
        if(sum&1) return 0;
        sum /= 2;
        int dp[n+1][sum+1] = {};
        for(int i = 0; i<n+1; i++) dp[i][0] = 1;
        for(int i = 1; i<n+1; i++){
            for(int j = 1; j<= sum; j++)
                if(dp[i-1][j] || (arr[i-1] <= j && dp[i-1][j-arr[i-1]])) 
                    dp[i][j] = 1;
        }
        return dp[n][sum];
    }
0
uratabhi3 weeks ago
class Solution{public:   int dp[102][1002];   Solution(){       memset(dp, -1, sizeof(dp));   }   bool ispossible(int arr[], int N, int sum){                     if(dp[N][sum]!=-1){           return dp[N][sum];       }              if(sum==0) return 1;       if(N<=0 || sum<0) return 0;                     if(arr[N-1]>sum){           dp[N][sum] = ispossible(arr, N-1, sum);       }       else{                      dp[N][sum] = ispossible(arr, N-1, sum-arr[N-1]) || ispossible(arr, N-1, sum);       }       return dp[N][sum];   }   int equalPartition(int N, int arr[])   {       int sum =0;       for(int i=0; i<N; i++) sum+=arr[i];       if(sum%2==1){           return 0;       }              int k = ispossible(arr, N, sum/2);       return k;                 }};
0
pravin_swv3 weeks ago
// { Driver Code Starts// Initial Template for C++
#include <bits/stdc++.h>using namespace std;
// } Driver Code Ends// User function Template for C++
class Solution{public:
   // bool rec(int ind,int arr[],int sum,vector<vector<int>>dp)   // {   //     //base case   //     if(sum==0) return true;   //     if(ind==0)   //     {   //         if(arr[0]==sum ) return true;   //     }   //     if(ind==0 && sum!=0) return false;   //     if(dp[ind][sum]!=-1) return dp[ind][sum];   //     bool not_pick=rec(ind-1,arr,sum,dp);   //     bool pick=false;   //     if(arr[ind]<=sum) pick=rec(ind-1,arr,sum-arr[ind],dp);          //     return dp[ind][sum]=not_pick || pick;   // }   //Tabulation code   int equalPartition(int N, int arr[])   {       // code here       int tot_sum=0;              for(int i=0;i<N;i++)       {           tot_sum+=arr[i];       }       if(tot_sum%2==1) return 0;       int k=tot_sum/2;       vector<vector<int>> dp( N , vector<int> (k+1, 0));       for(int i=0;i<N;i++)       {           dp[i][0]=1;       }       if(arr[0]<=k) dp[0][arr[0]]=1;       for(int ind=1;ind<N;ind++){           for(int tar=1;tar<=k;tar++)           {               bool not_pick=dp[ind-1][tar];               bool pick=0;               if(arr[ind]<=tar) pick=dp[ind-1][tar-arr[ind]];                      dp[ind][tar]=not_pick || pick;                          }       }                     return dp[N-1][k];   }};
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--){       int N;       cin>>N;       int arr[N];       for(int i = 0;i < N;i++)           cin>>arr[i];              Solution ob;       if(ob.equalPartition(N, arr))           cout<<"YES\n";       else           cout<<"NO\n";   }   return 0;}  // } Driver Code Ends
0
rounakdiptaghosh19993 weeks ago
bool solve(int arr[],int n ,int sum)  {      int dp[n+1][sum+1];      for(int i=0;i<=n;i++){          for(int j=0;j<=sum;j++){              if(i==0) dp[i][j]=false;              if(j==0) dp[i][j]=true;          }      }      for(int i=1;i<n+1;i++){          for(int j=1;j<sum+1;j++){            if(arr[i-1]<=j)              dp[i][j]=dp[i-1][j-arr[i-1]]||dp[i-1][j];            else            dp[i][j]=dp[i-1][j];                        }      }      return dp[n][sum];  }
   int equalPartition(int n, int arr[])   {       // code here       int sum=0;      for(int i=0;i<n;i++)  sum+=arr[i];      if(sum%2!=0) return false;      else if(sum%2==0) return solve(arr,n,sum/2);   }
0
rockskhare2613 weeks ago
#DP_LOGIC
class Solution:   def equalPartition(self, N, arr):       def subs(arr,sum_,n):           dp=[[False]*(sum_+1) for i in range(n+1)]           for i in range(n+1):               dp[i][0]=True           for i in range(n+1):               for j in range(sum_+1):                   if arr[i-1]>j:                       dp[i][j]=dp[i-1][j]                   else:                       dp[i][j] = dp[i - 1][j] or dp[i - 1][j-arr[i-1]]           return dp[n][sum_]       sum_=sum(arr)       if sum_%2!=0:           return False       else:           return subs(arr,sum_//2,len(arr))       
0
jainmuskan5653 weeks ago
int equalPartition(int N, int arr[])   {       //knapsack algo will be used       int sum=0;       for(int i=0;i<N;i++){           sum+= arr[i];       }       if(sum%2 !=0){           return 0;       }       else{           int k=sum/2;           bool dp[N+1][k+1];           for(int i=0;i<=N;i++){               for(int j=0;j<=k;j++){                   if(i==0){                       dp[i][j]=false;                   }                   if(j==0){                       dp[i][j]= true;                   }               }           }           //dp[0][0]=false;           for(int i=1;i<=N;i++){               for(int j=1;j<=k;j++){                   if(arr[i-1]<=j){                       dp[i][j]= dp[i-1][j] || dp[i-1][j-arr[i-1]];                   }                   else{                       dp[i][j]= dp[i-1][j];                   }               }           }           if(dp[N][k]==true){               return 1;           }           return 0;       }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.