Banana Bunches
InterviewTime limit20sMemory limit1024 MB
Given tree bunch counts, find the fewest trees to buy so the chosen trees form at most two contiguous blocks summing to exactly K, or -1 if impossible.
- Level
Medium5 of 10
- Topics
- Sliding window, Prefix sum, Array
- Solved
- No attempts yet
Problem
Barbara visits Alan's banana farm. The banana trees stand in one long line, represented by an array . The tree at position has banana bunches. Every tree costs the same. When Barbara buys a tree, she gets all the banana bunches on it. Alan has a special rule: he does not want too many gaps in his line, so Barbara may buy at most 2 contiguous sections of the tree line. Barbara wants the total number of banana bunches on the trees she buys to equal her basket capacity . She wants to spend as little money as possible. How many trees should she buy?
Input
The first line contains the number of test cases . test cases follow.
Each test case begins with a line containing two integers , the number of trees on Alan's farm, and , the capacity of Barbara's basket.
The next line contains non-negative integers , where the -th integer is the number of banana bunches on the -th tree.
Output
For each test case, output one line in the form Case #x: y, where is the test case number (starting from 1) and is the minimum number of trees Barbara must buy to get exactly banana bunches using at most 2 contiguous sections of the farm, or -1 if it is impossible.
Constraints
- .
- , for each from 1 to .