Problem · Binary Search
Find Maximum Length
Learn this problemProblem statement
You are given an array of n magical fruits. The power of the fruit is given in an array of length n. You are also given an integer k. Your task is to find the maximum length L such that every subarray of length L has the sum of powers strictly less than k.
Input Format
The first line contains two integers n and k — the number of fruits and the maximum allowed power sum. The second line contains n integers — the array a, where a[i] is the power of the i-th fruit.
Output Format
Print a single integer — the maximum value of L.
Constraints
1 ≤ n ≤ 2 * 10^51 ≤ a[i] ≤ 10^91 ≤ k ≤ 10^18
Function
findMaximumLength(a: int[], k: int) → intExamples
Example 1
a = [3, 1, 2, 4]k = 8return = 3🦐