Tywin's War Strategy
Basic Accuracy: 31.88% Submissions: 204 Points: 1

Tywin Lannister is facing a war. He has N troops of soldiers. Each troop has a certain number of soldiers denoted by an array A.
Tywin thinks that a troop is lucky if it has a number of soldiers as a multiple of K. He doesn't want to lose any soldiers, so he decides to train some more.
He will win the war if he has at least half of his troops are lucky troops.
Determine the minimum number of soldiers he must train to win the war.

Example 1:

​Input : arr[ ] = {5, 6, 3, 2, 1} and K = 2
Output : 1
Troop with 1 soldier is increased to 2.

​Example 2:

Input : arr[ ] = {2, 3, 4, 9, 8, 7} 
Output :  1
Troop with 3 soldier is increased to 4. 


Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function min_soldiers() that takes an array (arr), sizeOfArray (n), an integer K, and return the minimum number of soldiers he must train to win the war. The driver code takes care of the printing.

Expected Time Complexity: O(N*LOG(N)).
Expected Auxiliary Space: O(1).


Constraints :
1 ≤ N, K ≤ 105
1 ≤ Ai ≤ 105

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Tywin's War Strategy

Output Window