Fair Contest

https://www.hackerrank.com/contests/codenection-2021-closed-category/challenges/fair-contest

Question

There are N students in a class with known amount of programming skills (here the skill of a person is scored with a number). Now the lecturer wants to form two programming teams in the class in such a way that their overall difference in programming skill in minimal.

Your task is to help the lecturer find the minimum such achievable difference. (Note : The sizes of groups do not have to be equal)

Input Format

The first input line has an integer N the number of students.

The next line has N integers p1,p2,...,pnp_1,p_2,...,p_n the programming skill of each student.

Constraints

1N201 \le N \le 20
1pi1091 \le p_i \le 10^9

Output Format

Output one integer the minimum difference between the skills of the groups.

Sample Inputs:

Input

Output


First Attempt — Greedy Algorithm

My first thought is to by simply using greedy algorithm, which works by first sorting the numbers in descending order, thus adding the numbers on both arrays. Thus, inserting the next number into smaller array to try balancing out.

Meanwhile, this won't work at all as this method only gives approximation, not exact minimum difference. For sample input, this method would output 62 instead of 2, which is not viable solution.

But this could be the first start before opting to better strategy.

Second Attempt — MiTM Dynamic Programming

After realizing that greedy algorithm won't work anyways, I tried to attempt this question by using dynamic programming instead. First, we know that the most optimized difference is 0, which is when both array is same. By making both array is same, the most optimized value should be the half of the sum of the inputs.

After getting the optimized target, now we can use iterative method to find if the combination of subset is able to reach the target. After taking all pairs of targets possible, we can simply divide the input array into 2 subsets (with all possible ways), compute all possible sums on each half, and lastly getting the best way to combine the sum as close as the target.

Although sounds feasible, meanwhile it defeats itself (TLE) when the test case values are large. Therefore, we have to think another better way to attempt this question.

Final Solution — Optimize with binary search, combinations

If we somehow utilize the ready-made library, maybe it can make it faster in some reason. The previous attempt hits the TLE as it took too much time to traverse all possible pairs.

After thinking a bit, I think implementing a binary search during search phase, and introducing the combination library could help optimizing the problem.

Here's the final solution, which passed all test cases:

Last updated