Data Analysts at Amazon are analyzing sales patterns. Based on their analysis, the team…
- User
Data Analysts at Amazon are analyzing sales patterns. Based on their analysis, the team concluded that whenever a limited-period offer is rolled out, there is a spike in sales on the first and last days of the offer. They classify a period of 3 or more days as an offer period if the minimum value of the sales on the first and last days of the period outweigh the maximum value of sales on all other days in that period. In mathematical terms, a period of daysli, jf(1 ≤ is n- 2 and i +1 < j≤ n) is classified as an offer period if:
- The period [i, jf is an offer period if: 0 1≤i≤n-2 o H1 <j≤n c++ code
- min(sales[i], sales[j) > max(sales[i+ 1], sales[i+2], ..., sales[j-1]) Given an array of distinct integers, sales, with sales statistics over a period of n consecutive days, report the number of offer periods identified. Example Suppose n= 4, sales = [3, 2, 8, 6] Assume 1-based indexing. The number of offer periods is 1. Period Min(sales[i], sales[j]) Max(sales[i+1].....sales[j-1]) Offer Period [1, 3] min(3, 8) = 3 max(2) = 2 Since 3 > 2, so YES [1, 4] min(3, 6) = 3 max(2,8) = 8 Since 3 < 8, so NO [2, 4] min(2, 6) = 2 max (8) = 8 Since 2 < 8, so NO
Assume 1-based indexing. Period Min(sales[i], sales (l) Max(sales[i+1],....sales[-1]) Offer Period [1, 3] min(3, 8) = 3 max(2) = 2 Since 3 > 2, so YES [1,4] min(3, 6) = 3 max(2,8) = 8 Since 3 < 8, so NO [2, 4] min(2, 6) = 2 max(8) = 8 Since 2 < 8, so NO 1 2 The number of offer periods is 1. Function Description Complete the function getTotalOfferPeriods in the editor below. getTotalOfferPeriods has the following parameter: int sales[n]: the sales statistics for Amazon on n consecutive days. Returns long int: the number of offer periods in the n consecutive days Constraints
- 3≤n≤ 2x105 A 1 ≤ sales[il ≤ 109
- Input Format For Custom Testing The first line contains an integer, n, the number of elements in sales. Each of the next n lines contains an integer, sales[i]
Sample Input For Custom Testing STDIN FUNCTION sales [] size n = 5 sales = [5, 2, 1, 3, 6] 213 Sample Output Explanation Assume 1-based indexing.
- Period [1, 4] → min(sales/1], sales[4) = 3 > max(sales/2], sales(3)) = 2.
- Period [1, 5] → min(sales(1], sales(5)) = 5 > max(sales(2], sales(3], sales(4)) = 3.
- Period [2, 4] → min(sales[2], sales(41) = 2 > max(sales(3]) = 1.
Sample Input For Custom Testing STDIN FUNCTION → 10 → 6 8 5 sale[] size n = 6 sales = [10, 6, 8, 5, 11, 9] 11 Sample Output 3 Explanation
- Period (1, 3] → min(sales[11, Sales[3) = 8 = max(sales[2) = 6.
- Period [1, 5] → min(sales[1], sales(5) = 10 > max(sales(2], sales(3], sales(4) = 8.
- Period [3, 5] → min(sales(3], sales(51) = 8 > max(sales[4)) = 5.
- Assistant