geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166
Minimum sum partition
Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'N' denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
In each seperate line print minimum absolute difference.
Constraints:
1<=T<=30
1<=N<=50
1<=A[I]<=50
Example:
Input:
2
4
1 6 5 11
4
36 7 46 40
Output :
1
23
Explaination :
Subset1 = {1, 5, 6}, sum of Subset1 = 12
Subset2 = {11}, sum of Subset2 = 11
import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static int func(int[] arr) { int n = arr.length, tot = 0;
for(int i=0; i<n; ++i) {
tot += arr[i];
}
int half = tot/2, e = half; for(; e>=0; --e) {
boolean[][] dp = new boolean[n + 1][e + 1]; for(int i=0; i<=n; ++i) {
dp[i][0] = true;
} for(int i=1; i<=n; ++i) {
for(int j=1; j<=e; ++j) {
if(j >= arr[i - 1]) {
dp[i][j] |= dp[i-1][j] | dp[i-1][j - arr[i-1]];
}
dp[i][j] |= dp[i-1][j];
}
}
if(dp[n][e]) break;
} //System.out.println(e);
return Math.abs(e - (tot - e));
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; ++i) {
arr[i] = in.nextInt();
} System.out.println(func(arr));
}
}
}
geeksforgeeks@ Minimum sum partition (Dynamic Programming)的更多相关文章
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- About Dynamic Programming
Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 动态规划Dynamic Programming
动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...
- [Optimization] Advanced Dynamic programming
这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...
- 详解动态规划(Dynamic Programming)& 背包问题
详解动态规划(Dynamic Programming)& 背包问题 引入 有序号为1~n这n项工作,每项工作在Si时间开始,在Ti时间结束.对于每项工作都可以选择参加与否.如果选择了参与,那么 ...
- Algo: Dynamic programming
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 6专题总结-动态规划dynamic programming
专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...
随机推荐
- hibernate annotation配置经验
1.将annotation写在entity类文件的get方法上面
- Qt之QHeaderView自定义排序(获取正确的QModelIndex)
简述 前几节中分享过关于自定义排序的功能,貌似我们之前的内容已经可以很好地解决排序问题了,但是,会由此引发一些很难发现的问题...比如:获取QModelIndex索引错误. 下面,我们先来实现一个整行 ...
- [Swift系列]003- 函数
[基础] Swift函数格式: 1.定义格式: func 函数名(参数名1:数据类型,... ,参数名n:数据类型) -> (返回值类型1,...,返回值类型n){ ///函数体内语句 } ...
- iOS8 LaunchScreen.storyboard
我目前的需求是需要将启动图片通过LaunchScreen.storyboard 来实现. 我首先想到的是创建一个Sb,使用自动布局来布局imageview,并设置如下图: 布局好之后,在Image里 ...
- dom4j创建格式化的xml文件
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java. ...
- rmmod 无法卸载模块问题
用insmod加载驱动模块成功后,用rmmod无法卸载 解决方法:在板子上建立一个空目录:lib/modules/2.6.30.4/
- HDU pog loves szh II (数的处理)
题意: 给一个序列,找出两个数字a和b(可以相等但不可相同),要求(a+b)%p的结果最大. 思路: 先将所有元素模p,再排序.要找出a和b,分两种情况,a+b>p和a+b<p.第一种,肯 ...
- (转)python 优先队列
#!/usr/bin/python from Queue import Queue from Queue import PriorityQueue a1='a1' a2='a2' a3='a3' a4 ...
- MySQL多表连接
主要分3种:内连接,外连接,交叉连接 其 他:联合连接,自然连接 1.内联接 典型的联接运算,使用像 = 或 <> 之类的比较运算).包括相等联接和自然联接. 内联接使用比 ...
- mac os 系统密码正确的 但是进不了系统
今天开始重新学习C 但是一早起来开机进不了系统 密码明明正确的 无语了,后来打苹果售后电话解决了. 今天记录下如何解决的,以后万一又出现这种问题,至于原因嘛 我也不知道 有可能跟我装的双系统有关系.只 ...