Problem

Count Promotional Periods

INTERNOA
See Amazon online assessment and hiring insights

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 Description

Complete the function countPromotionalPeriods.

countPromotionalPeriods has the following parameter:

  1. int orders[n]: the order statistics for each day

Returns

long: the number of promotional periods.

Examples
01 · Example 1
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.

02 · Example 2
orders = [5, 1, 4, 2, 6]
return = 3

The promotional periods are [5, 1, 4], [4, 2, 6], and [5, 1, 4, 2, 6].

Constraints
  • 3 <= n <= 2 * 10^5
  • 1 <= orders[i] <= 10^9
More Amazon problems
drafts saved locally
public long countPromotionalPeriods(int[] orders) {
  // write your code here
}
orders[3, 2, 8, 6]
expected1
sign in to submit