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.
Input: N = 5, K = 32
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
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:
- Minimize Sum of an Array by at most K reductions
- Number of continuous reductions of A from B or B from A to make them (1, 1)
- Minimum increment in the sides required to get non-negative area of a triangle
- Minimum concatenation required to get strictly LIS for array with repetitive elements | Set-2
- Minimum concatenation required to get strictly LIS for the given array
- Minimum count of numbers required ending with 7 to sum as a given number
- Minimum count of digits required to obtain given Sum
- Remove minimum numbers from the array to get minimum OR value
- Count minimum steps to get the given desired array
- Minimum count of numbers required from given array to represent S
- 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
- Count minimum moves required to convert A to B
- Count minimum factor jumps required to reach the end of an Array
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of ways to get Odd Sum
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of operations required to sum to binary string S
- Minimum number of primes required such that their sum is equal to N
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : nidhi_biet