Dsa
Sliding Window
Easy
The Growth Constraint
In a lab, a bacterial culture's growth factor is measured hourly as a sequence of positive integers. To prevent an explosion, the product of growth factors in any observation period must not exceed a limit P. Find the length of the longest contiguous observation period that satisfies this constraint.
Input Format:
- The first line contains two integers: P (the product limit) and N (the number of hours).
- The second line contains N space-separated positive integers.
Output Format:
- Print a single integer representing the length of the longest valid period.
Example:
Input:
20 5
2 3 2 5 2
Output:
3
Explanation: The period [2, 3, 2] has a product of 12 (<= 20). [2, 5, 2] has a product of 20 (<= 20). Both are length 3.
Key concepts
sliding_windowproduct_windowtwo_pointers
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now