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 ...
随机推荐
- file的这几个取得path的方法各有不同,下边说说详细的区别
html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...
- 如何实现wpf的多国语言
http://www.cnblogs.com/horan/archive/2012/04/20/wpf-multilanguage.html 4.0版本的locbaml http://michaels ...
- 【转】Android之内存泄漏调试学习与总结
大家有或经常碰到OOM的问题,对吧?很多这样的问题只要一出现相信大家的想法跟小马的一样,就是自己的应用:优化.优化.再优化!而且如果出现类似于OOM这样级别的问题,根本就不好处理,LogCat日志中显 ...
- PHP项目中composer和Git的组合使用
highlight: 在国内由于众所周知的原因,composer的package可能无法访问,解决办法是使用中国的全镜像: composer config -g repositories.packag ...
- asp.net,CSS设置<TableListView>的title居左,居左,居上
居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...
- HDU 1024 (不重叠m段最大和) Max Sum Plus Plus
题解是看的这里的: http://www.acmerblog.com/hdu-1024-Max-Sum-Plus-Plus-1276.html 当前这个状态是dp[i][j],i 表示当前的段,j表示 ...
- [反汇编练习] 160个CrackMe之027
[反汇编练习] 160个CrackMe之027. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- Android如何获取系统高度、标题栏和状态栏高度
在android应用中,有时需要计算个View的位置,导致需要计算状态栏高度,标题栏高度等信息.为以后方便,在此做个简单记录. 晒代码前先了解一下android屏幕区域的划分,如下图(该图引用自此文h ...
- php 使用date()函数的报错
错误提示: Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* ...
- 【转】itunes connect 如何修改主要语言
原文网址:http://blog.csdn.net/yuedong56/article/details/50662181 刚开始提交app,没有做国际化(本地化),提交的app只有简体中文一种语言,第 ...