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
4mo

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!
Select
Add answer anonymously...

Goldman Sachs Software Analyst interview questions & answers

A Software Analyst was asked Q. Describe how to implement two stacks using a single array.
A Software Analyst was asked Q. Design a stack that supports push, pop, top, and retrieving the minimum element ...read more
A Software Analyst was asked Q. What is the difference between SQL and NoSQL databases?

Popular interview questions of Software Analyst

A Software Analyst was asked Q1. Describe how to implement two stacks using a single array.
A Software Analyst was asked Q2. Design a stack that supports push, pop, top, and retrieving the minimum element ...read more
A Software Analyst was asked Q3. What is the difference between SQL and NoSQL databases?
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits