X and Y were in an election battle in their city. This city consists of N towns and each town has M1 number of people who will vote for Y and M2 number of people who will not take part in the election. X after seeing the lack of his supporters, decided to campaign.
If X does a campaign in town T, both M1 and M2 number of people in this town become his supporters.
Calculate the least number of towns in which X has to campaign.
Input: N = Number of towns.
N lines containing M1 and M2 = Number of Y voters and Number of people not voting in town
Example:
4
2 1
2 2
5 1
1 3
Answer = 1. X can campaign in town 3 and win 6 voters while Y will be left with only 5 voters.
Greedy Solution Idea:
Sort the array of <M1,M2> pair on decreasing order of M1. If two elements have same M1 value, sort them on decreasing order of M1+M2. While x votes < y votes keep removing elements from the head of the array and adding M1+M2 to number of x votes and subtracting M1 from number of y votes
However, the greedy solution failed. Can someone give another idea or a test case where the greedy approach fails.