Hi, Can anyone please tell me why I could get all test cases to pass except one. I had tried it for 4 continuous days to identify my mistake but in vain. I got really frustrated at one moment of time. It will be really helpful if someone can point me the mistake or the test case for which it fails.The link to my submission is: CodeChef: Practical coding for everyone
Just for explanation of the program, I am using a boolean array to store the set to which the ith number belongs. “false” means set 0 and “true” means set 1.
@rahuldugar I don’t think there is a need to calculate even the sum…
link to my solution https://www.codechef.com/viewsolution/16878569
Though the solution is based on similar lines ie. printing set of strings to make separation into equal sum.
I thought of the following solution. Plz can someone tell me if it is valid and can be implemented without getting a tle:
Find the sum and check if it is possible to divide the numbers into two sets of equal sums.
If possible, then we start with an array of numbers from 1 to n.
We put the first and last numbers in one set and the second and second last numbers in second set and we skip the number x.
We maintain two sum counts for each set. We check after the above division if thw two sums are equal or not.
If equal we have the division. If not then, there are 2 cases. We calculate the difference between the required sum and the set with the smaller sum. If the number equal to the difference is present in the set then we can add it in the set. If not then we find the number from the smaller set to be swapped with a number in the larger set.
You can step almost directly to the right place to split the set by calculating the triangular number closest above the partition totals. Then with only a small adjustment of one to three elements, you can get a qualifying partition without intermediate calculation.
Kudos to the test case writer for finding “4 4” to break my first submission - the partition total is 3, which is less than 4 (my test) but not less than the highest remaining element. D’oh.
For the exceptional cases when N - x is even,
The greedy solution of going from N to 1 and addint the current number to set with lesser sum works for most of the cases except when x=1 and N>=6 and N=2 and N>=7. So for that you can manually assign number from 1 to 6 and apply greedy solution to remaining.