Partition Set Into Two Subsets With Minimum Difference

Given an array of N non-negative integers, split the array into two subsets such that the absolute difference between their sums is minimized.

The task is to find the minimum absolute difference between the sums of any valid partition of the array elements.

Input:

The first line contains the integer T, representing the number of test cases.
For each test case:
- The first line contains an integer N, the number of elements in the array.
- The second line contains N space-separated integers, the elements of the array.

Output:

For each test case, return the minimum possible absolute difference between the sums of the two subsets, on a new line.

Example:

Given the array [1, 2, 3], the partition can be [1, 2] and [3] or [1, 3] and [2]. The minimum absolute difference is 0 (possible by dividing as [2] and [1, 3]).

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^3
  • 0 <= ARR[i] <= 10^3
  • 0 <= SUM(ARR) <= 10^4
  • Time Limit: 1 second

Note:

Each element of the array should be included in exactly one subset. Subsets need not be contiguous. Implement the function to compute the desired output, no need to handle input/output operations.
AnswerBot
3d

Split array into two subsets with minimum absolute difference between their sums.

  • Use dynamic programming to find the minimum absolute difference between the sums of two subsets.

  • Iterate through all pos...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter