It gives wrong answer ignore the last test case.
Question link -> https://www.codechef.com/problems/DIVIDE_GROUP?tab=statement
Solution link → https://www.codechef.com/viewsolution/97381828
| # | User | Rating |
|---|---|---|
| 1 | jiangly | 3810 |
| 2 | Benq | 3676 |
| 3 | Kevin114514 | 3655 |
| 4 | maroonrk | 3463 |
| 5 | strapple | 3390 |
| 6 | Um_nik | 3387 |
| 7 | tourist | 3384 |
| 8 | heuristica | 3322 |
| 9 | turmax | 3319 |
| 10 | jiangbowen | 3291 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 156 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | Um_nik | 144 |
| 5 | Errichto | 139 |
| 6 | AmShZ | 138 |
| 7 | adamant | 137 |
| 8 | maroonrk | 132 |
| 8 | BledDest | 132 |
| 10 | qwexd | 129 |
It gives wrong answer ignore the last test case.
Question link -> https://www.codechef.com/problems/DIVIDE_GROUP?tab=statement
Solution link → https://www.codechef.com/viewsolution/97381828
| Name |
|---|



Looking at it your method seems to be taking the lowest remaining value in the array each time (call this x), then subtracting x from the k-1 highest remaining values in the array to create x groups, repeating this process until no more groups can be created. In this case the greedy method fails in the following case:
In this case 3 groups can be made from boxes [1,2,3] , [2,3,4] , and [1,4,5]. In your current solution what is happening currently is that you take the lowest value 2, and end up creating two boxes of [1,2,5], leaving only two types of candies left. Technically this can be fixed by only making one box at a time and then resorting the array after each box, but the problem constraints (1 <= A[I] <= 10^9) would cause this to TLE. In practice the actual solution doesn't require you to explicitly determine how each group is created in the solution.