-
Notifications
You must be signed in to change notification settings - Fork 20k
Minimum sum partition #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Minimum sum partition #1338
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Partition a set into two subsets such that the difference of subset sums is minimum | ||
|
||
/* | ||
Input: arr[] = {1, 6, 11, 5} | ||
Output: 1 | ||
Explanation: | ||
Subset1 = {1, 5, 6}, sum of Subset1 = 12 | ||
Subset2 = {11}, sum of Subset2 = 11 | ||
|
||
Input: arr[] = {36, 7, 46, 40} | ||
Output: 23 | ||
Explanation: | ||
Subset1 = {7, 46} ; sum of Subset1 = 53 | ||
Subset2 = {36, 40} ; sum of Subset2 = 76 | ||
*/ | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
class GFG | ||
{ | ||
public static void main (String[] args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
int t=sc.nextInt(); | ||
while(t-->0) | ||
{ | ||
int n=sc.nextInt(); | ||
int arr[]=new int[n]; | ||
int sum=0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
arr[i]=sc.nextInt(); | ||
sum+=arr[i]; | ||
} | ||
int ans[]=new int[sum]; | ||
ans=subset(arr,sum); | ||
int min=Integer.MAX_VALUE; | ||
for(int i=0;i<ans.length;i++) | ||
min=Math.min(min,(sum-2*ans[i])); | ||
|
||
System.out.println(min); | ||
|
||
|
||
} | ||
} | ||
static int[] subset(int arr[],int sum) | ||
{ | ||
int n=arr.length; | ||
boolean dp[][]=new boolean[n+1][sum+1]; | ||
for(int i=0;i<=n;i++) | ||
dp[i][0]=true; | ||
for(int i=1;i<=sum;i++) | ||
dp[0][i]=false; | ||
|
||
// subset sum concept | ||
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]; | ||
} | ||
} | ||
|
||
//storing last dp column whose value is true till sum/2 | ||
int index[]=new int[sum]; | ||
int p=0; | ||
for(int i=0;i<=sum/2;i++) | ||
{ | ||
if(dp[n][i]==true) | ||
index[p++]=i; | ||
} | ||
return index; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly provide some sample test cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.