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 days[i, j](1 ≤ is n-2 and i +1 <j<n) is classified as an offer period if:
The period [i, j] is an offer period if:
-
1<=i<=n-2
-
i+1 < j<=n
-
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.
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
The number of offer periods is 1. Function Description
get Total Offer Periods 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 ≤ 2 x 105
1 ≤ sales[i] < 10^9
► Input Format For Custom Testing
▼ Sample Case 0
Sample Input For Custom Testing
STDIN FUNCTION
5 -> sales[] size n = 5
5 ->sales [5, 2, 1, 3, 6] 2
1
3
6
Sample Output
3 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[4]) =2>max(sales[3]) = 1. Complete the below c+ code- int getTotalOfferPeriods(vector sales) {
}
-
- Assistant