Count Promotional Periods
Learn this problemProblem statement
Data analysts at Amazon are studying product order patterns. They classify a period of at least three consecutive days as a promotional period when the order counts on the first and last days are both greater than every order count on the days between them.
More formally, for an array orders, a subarray from index i to index j is a promotional period if j - i + 1 >= 3 and min(orders[i], orders[j]) > max(orders[i + 1], orders[i + 2], ..., orders[j - 1]).
Given the order statistics for n consecutive days, return the number of promotional periods.
Function
countPromotionalPeriods(orders: int[]) → longComplete the function countPromotionalPeriods.
countPromotionalPeriods has the following parameter:
int orders[n]: the order statistics for each day
Returns
long: the number of promotional periods.
Examples
Example 1
orders = [3, 2, 8, 6]return = 1Using 1-based indexing, the candidate periods of length at least 3 are [1, 3], [1, 4], and [2, 4]. Period [1, 3] is valid because min(3, 8) = 3 and the only middle value is 2. The other two periods are invalid because their middle maximum is 8. Therefore, the answer is 1.
Example 2
orders = [5, 1, 4, 2, 6]return = 3The promotional periods are [5, 1, 4], [4, 2, 6], and [5, 1, 4, 2, 6].
Constraints
3 <= n <= 2 x 10^51 <= orders[i] <= 10^9- All integers in
ordersare distinct.
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026