Find Minimum Days
Updated
Problem statement
A student is preparing for a scholarship test which is organized on the Amazon Academy platform and scheduled for next month.
There are n chapters to be studied, where the i-th chapter has pages[i] pages. On each day, the student chooses some window of exactly k consecutive chapters. From each chapter in that chosen window, the student reads up to p of its remaining pages: the remaining page count of every chapter in the window is reduced by p. If a chapter has fewer than p pages remaining, the student reads all of its remaining pages and its remaining page count becomes 0 (it never goes negative). A chapter already at 0 remaining pages may still lie inside a chosen window; it simply stays at 0.
Find the minimum number of days the student needs so that the remaining page count of every chapter is 0.
Note: The k chapters chosen each day must be contiguous, and each chosen window must lie fully within the chapters (its starting index ranges over 0 to n - k).
Function
findMinimumDays(pages: int[], k: int, p: int) → long Complete the function findMinimumDays in the editor below.
findMinimumDays has the following parameters:
int pages[n]: the number of pages in each chapterint k: the number of consecutive chapters chosen each dayint p: the maximum number of pages read from each chosen chapter in a day
Returns long int: the minimum number of days needed so that all chapters have 0 remaining pages.
Examples
Example 1
pages = [3, 1, 4]k = 2p = 2return = 4There are n=3 chapters with pages=[3,1,4], k=2 consecutive chapters per day, and up to p=2 pages read from each chosen chapter per day. One optimal sequence (4 days): Day 1: Choose chapters 1 and 2. Remaining pages: [1, 0, 4]. Day 2: Choose chapters 1 and 2. Remaining pages: [0, 0, 4]. Day 3: Choose chapters 2 and 3. Remaining pages: [0, 0, 2]. Day 4: Choose chapters 2 and 3. Remaining pages: [0, 0, 0]. Chapter 1 needs ceil(3/2)=2 coverings, chapter 2 needs ceil(1/2)=1, and chapter 3 needs ceil(4/2)=2. Since every window has length k=2 and must lie within the chapters, chapters 1 and 3 can never be covered by the same window, so their required coverings (2 + 2 = 4) cannot be combined into the same days. Hence the chapters cannot be completed in fewer than 4 days, and the answer is 4.
Example 2
pages = [3, 4]k = 1p = 2return = 4Let's trace this sample case: Day 1: Choose chapter 1 (pages = 3). Read min(3,2)=2 pages. Remaining pages: [3−2,4]=[1,4]. Day 2: Choose chapter 1 (pages = 1). Read min(1,2)=1 page. Remaining pages: [1−1,4]=[0,4]. Day 3: Choose chapter 2 (pages = 4). Read min(4,2)=2 pages. Remaining pages: [0,4−2]=[0,2]. Day 4: Choose chapter 2 (pages = 2). Read min(2,2)=2 pages. Remaining pages: [0,2−2]=[0,0]. So, for this sample case, the minimum number of days would be 4.
Constraints
1 ≤ n ≤ 10^51 ≤ pages[i] ≤ 10^91 ≤ k ≤ n1 ≤ p ≤ 10^9
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Sliding Window MedianAuxoAI · OA
- Find Array Uniqueness Median 🐈Microsoft · OAMicrosoft interview questions
- Find Minimum IdlenessSalesforce · OASalesforce interview questions
- Log Analyzer: Busiest Service in a Rolling WindowByteDance · PHONE SCREENByteDance interview questions
- Longest Run of Ones After Flipping k ZerosC3.ai · PHONE SCREENC3.ai interview questions
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Tender Part 4 - RefundsStripe · OA · PHONE SCREENStripe interview questions
- Credit Accounts with Scheduled TransfersAirbnb · OAAirbnb interview questions
- Longest Repeated Letter Block with Adjacent SwapsIntuit · OAIntuit interview questions
- Maximum Barbell WeightVirtu Financial · OAVirtu Financial interview questions