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.
Complete the function countPromotionalPeriods.
countPromotionalPeriods has the following parameter:
int orders[n]: the order statistics for each day
Returns
long: the number of promotional periods.
orders = [3, 2, 8, 6] return = 1
Using 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.
orders = [5, 1, 4, 2, 6] return = 3
The promotional periods are [5, 1, 4], [4, 2, 6], and [5, 1, 4, 2, 6].
3 <= n <= 2 * 10^51 <= orders[i] <= 10^9
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
- Drone Delivery RouteOA · Seen May 2026
public long countPromotionalPeriods(int[] orders) {
// write your code here
}