Back to problems Sign in to unlock
Codeforces
Medium
Arrays
Amazon
Books
Find the longest contiguous segment of books whose total reading time does not exceed a given limit.
Acceptance 0%
Problem Statement
You are given an array of positive integers where each value represents the time needed to read one book, in order on a shelf. You have a fixed amount of free time .
Choose a contiguous sequence of books and read them all. Your task is to determine the maximum number of consecutive books you can read without the total reading time exceeding .
Input Format
Input
- The first line contains two integers and .
- The second line contains integers where is the reading time of the -th book.
Notes
- The chosen books must form a contiguous subarray.
Output Format
Output
- Print one integer: the maximum length of a contiguous subarray whose sum is at most .
Constraints
Examples
Sample cases returned by the problem API.
Example 1
Input
7 8 3 1 2 1 1 1 5
Output
5
Explanation
The longest valid segment is [1, 2, 1, 1, 1], which has sum 6 and length 5.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.