Given **N** pairs of integers and an integer **K**, the task is to find the minimum number of reductions required such that the sum of the first elements of each pair is **≤ K**.Each reduction involves reducing the first value of a pair to its second value. If it is not possible to make the sum ≤ **K**, print -1.

Examples:

Input:N = 5, K = 32

10 6

6 4

8 5

9 8

5 2

Output:2

Explanation:

Total Sum = 10 + 6 + 8 + 9 + 5 = 38 > K

Reducing 10 – > 6 and 8 – > 5 reduces the sum to 31( 6 + 6 + 5 + 9 + 5) which is less than K.

Input:N = 4, K = 25

10 5

20 9

12 10

4 2

Output:-1

**Approach:**

Follow the steps below to solve the problem:

- Calculate the sum of the first element of every pair. If the sum is already ≤ K, print 0.
- Sort the given pairs based on their difference.
- Count the number of differences of pairs that need to be added in non-increasing order to get the sum to be less than
**K**. - If the sum exceeds K after traversal of all pairs, print -1. Otherwise, print the count.

Below is the implementation of the above approach:

## C++

`// C++ Program to find the count of ` `// minimum reductions required to ` `// get the required sum K ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of minimum reductions ` `int` `countReductions( ` ` ` `vector<pair<` `int` `, ` `int` `> >& v, ` ` ` `int` `K) ` `{ ` ` ` ` ` `int` `sum = 0; ` ` ` `for` `(` `auto` `i : v) { ` ` ` `sum += i.first; ` ` ` `} ` ` ` ` ` `// If the sum is already ` ` ` `// less than K ` ` ` `if` `(sum <= K) { ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Sort in non-increasing ` ` ` `// order of difference ` ` ` `sort(v.begin(), v.end(), ` ` ` `[&]( ` ` ` `pair<` `int` `, ` `int` `> a, ` ` ` `pair<` `int` `, ` `int` `> b) { ` ` ` `return` `(a.first - a.second) ` ` ` `> (b.first - b.second); ` ` ` `}); ` ` ` ` ` `int` `i = 0; ` ` ` `while` `(sum > K && i < v.size()) { ` ` ` `sum -= (v[i].first ` ` ` `- v[i].second); ` ` ` `i++; ` ` ` `} ` ` ` ` ` `if` `(sum <= K) ` ` ` `return` `i; ` ` ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4, K = 25; ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > v(N); ` ` ` `v[0] = { 10, 5 }; ` ` ` `v[1] = { 20, 9 }; ` ` ` `v[2] = { 12, 10 }; ` ` ` `v[3] = { 4, 2 }; ` ` ` ` ` `// Function Call ` ` ` `cout << countReductions(v, K) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

-1

**Time Complexity:**O(NlogN)

**Auxillary Space:**O(1)

## Recommended Posts:

- Minimum count of numbers required ending with 7 to sum as a given number
- Minimum count of numbers required from given array to represent S
- Count minimum factor jumps required to reach the end of an Array
- Minimum count of numbers required with unit digit X that sums up to N
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum Players required to win the game
- Minimum number of changes required to make the given array an AP
- Minimum number of primes required such that their sum is equal to N
- Minimum concatenation required to get strictly LIS for the given array
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number of operations required to reduce N to 0
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum changes required to make two arrays identical
- Minimum boxes required to carry all gifts
- Minimum number operations required to convert n to m | Set-2
- Minimum inversions required so that no two adjacent elements are same
- Minimum number of given operation required to convert n to m
- Minimum number of operations required to reduce N to 1
- Minimum number of integers required such that each Segment contains at least one of them

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.