13. Tywin's War Strategy
β GFG solution to Tywin's War Strategy problem: find minimum soldiers to add to make at least βn/2β troops lucky using greedy approach with priority queue. π
π§© Problem Description
π Examples
Example 1
Input: arr = [5, 6, 3, 2, 1], k = 2
Output: 1
Explanation: By adding 1 soldier for the troop with 1 soldier, we get [5, 6, 3, 2, 2].
Now 3 out of 5 troops (6, 2, and 2) are multiples of 2 that satisfy the requirement.Example 2
Input: arr = [3, 5, 6, 7, 9, 10], k = 4
Output: 4
Explanation: We need at least 3 lucky troops since β6 / 2β = 3. Currently, no troop is divisible by 4.
Add 1 soldier for troop 3 β 4,
Add 2 for troop 6 β 8,
Add 1 for troop 7 β 8.
New array: [4, 5, 8, 8, 9, 10] with 3 lucky troops (4, 8, 8).
Total soldiers added = 4.π Constraints
β
My Approach
Greedy + Priority Queue Algorithm
Key Insight:
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated