Minimum Full Glasses for an Exact Water Volume
Problem statement
There is exactly one glass of each integer capacity from 1 through N litres.
You want to hold exactly K litres of water. Each glass must be either completely full or empty; partial filling is not allowed.
Return the minimum number of full glasses needed, or -1 if the target volume is impossible. If K = 0, return 0.
Function
solution(N: int, K: long) → intExamples
Example 1
N = 5K = 8return = 2Fill the glasses of capacities 5 and 3. One glass cannot hold 8 litres because the largest capacity is 5.
Example 2
N = 3K = 7return = -1All three glasses together hold only 1 + 2 + 3 = 6 litres.
Example 3
N = 3K = 6return = 3Every glass must be full to reach the total capacity of 6.
Example 4
N = 7K = 0return = 0Leave every glass empty.
Constraints
1 <= N <= 10^9.0 <= K <= 10^18.- Each capacity is available exactly once.