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)的更多相关文章

  1. [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 ...

  2. About Dynamic Programming

    Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...

  3. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  4. [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 ...

  5. 动态规划Dynamic Programming

    动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...

  6. [Optimization] Advanced Dynamic programming

    这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...

  7. 详解动态规划(Dynamic Programming)& 背包问题

    详解动态规划(Dynamic Programming)& 背包问题 引入 有序号为1~n这n项工作,每项工作在Si时间开始,在Ti时间结束.对于每项工作都可以选择参加与否.如果选择了参与,那么 ...

  8. Algo: Dynamic programming

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  9. 6专题总结-动态规划dynamic programming

    专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...

随机推荐

  1. Java 数据类型转换

    int iValue = new Integer(strValue).intValue();String str = intObj.toString();int number = Integer.pa ...

  2. leetcode Database2 (四)

    一.Duplicate Emails Write a SQL query to find all duplicate emails in a table named Person. +----+--- ...

  3. Machine Learning for hackers读书笔记(四)排序:智能收件箱

    #数据集来源http://spamassassin.apache.org/publiccorpus/ #加载数据 library(tm)library(ggplot2)data.path<-'F ...

  4. Qt环境搭建(Visual Studio)

    简述 经常有人问我编写Qt程序时使用什么IDE,其实这个真的很难回答(各有所长),只能说看个人爱好了,因为我两个都用,而且两个都很喜欢(比较多情吧O(∩_∩)O~)! 下面将进行Qt Creator与 ...

  5. 51nod1122 机器人走方格 V4

    矩阵快速幂求出每个点走n步后到某个点的方案数.然后暴力枚举即可 #include<cstdio> #include<cstring> #include<cctype> ...

  6. [转]JavaScript 的性能优化:加载和执行

    原文链接:http://www.ibm.com/developerworks/cn/web/1308_caiys_jsload/index.html?ca=drs- JavaScript 的性能优化: ...

  7. (转) error: linker command failed with exit code 1 (use -v to see invocation)

    转自:http://blog.csdn.net/tiantian1980/article/details/9175777   像这样的一大堆,总体说编译链接时错误 /Users/zhangtianji ...

  8. SQL 数据库表标识列初始化 DBCC

    把ArimaIndexForecastModel这张表的标识列重置为0,前提是这张表执行过删除操作 示例:  dbcc checkident('ArimaIndexForecastModel',res ...

  9. ffmpeg命令学习

    1.组成 程序:ffmpeg.ffplay.ffprobe.ffserverffmpeg:转码程序ffplay:播放程序ffserver:服务器程序 库:libavcodec.libavdevice. ...

  10. 学习java之泛型类和泛型方法

    上一篇博文中我自己试着用了下泛型类,昨天看java编程思想一书,发现里面有这么一段话: 使用参数化方法而不是用参数化类的方便之处在于:你不必为需要应用的每种不同类型都使用一个参数去实例化这个类,并且你 ...