Partition a set into two subsets such that the difference of subset sums is minimum.
You are given an array containing N non-negative integers. Your task is to partition this array into two subsets such that the absolute difference between subset sums is minimum.
You just need to find the minimum absolute difference considering any valid division of the array elements.
Note:
1. Each element of the array should belong to exactly one of the subset.
2. Subsets need not be contiguous always. For example, for the array : {1,2,3}, some of the possible divisions are a) {1,2} and {3} b) {1,3} and {2}.
3. Subset-sum is the sum of all the elements in that subset.
Input Format:
The first line of input contains the integer T, denoting the number of test cases.
The first line of each test case contains an integer N, denoting the size of the array.
The second and the last line of each test case contains N space-separated integers denoting the array elements.
Output Format:
For each test case, return the minimum possible absolute difference in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
0 <= ARR[i] <= 10^3
0 <= SUM <= 10^4,
where SUM denotes the sum of all elements in the array for a given test case.
Time Limit: 1sec
CodingNinjas
author
2y
Recursion
- This problem can be solved using recursion. And the idea behind this is to generate all the possible sums from the given set.
- We will try different combinations for one of the subsets and acco...read more
CodingNinjas
author
2y
Dynamic Programming
We can think of this problem as the standard problem of dynamic programming, which is to check whether there exists a subset with the given SUM.
Since here we need to find if there ...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Engineer Intern
>
Sterlite Technologies Software Engineer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app