There are n products being sold on the Amazon Online shopping app. The price of theth…
- User
There are n products being sold on the Amazon Online shopping app. The price of theth product is price[i]. The developers at Amazon decided to give special gift cards to innovative customers. A gift card will be given if any customer buys a contiguous subsegment of products and at least 2 of the products have a matching price. Find the minimum amount of money a customer needs to spend in order to get the gift card. If it is not possible for any customer to get a gift card, return -1.
Example
price = [1, 2, 3, 1, 2, 1]
In the above example, the number of products n = 6. The subsegments where not all prices are distinct are (in increasing order of starting positions):
Subsegment
Remarks
Total Cost
[1, 2, 3, 1]
1 has a matching price
7
[1, 2, 3, 1, 2]
1 and 2 have a matching price
9
[1, 2, 3, 1, 2, 1]
1 and 2 have a matching price
10
[2, 3, 1.2]
2 has a matching price
8
[3. 1, 2, 1]
1 has a matching price
7
[1.2.1]
1 has a matching price
4
For all the other subsegments, all prices are distinct. The subsegment with the minimum price is from index 3 to 5 which costs 1+2+1= 4. Return 4.
Function Description
Complete the function getMinPrice in the editor below.
getMinPrice has the following parameter:
int price[n]: the prices of the products▼ Sample Case 0
Sample Input For Custom Testing
STDIN
FUNCTION
4 number of products n = 4
1 → price = [1, 2, 1, 2]
2
1
2
Sample Output
4
Explanation
The subsegments where not all prices are distinct are [1, 2, 1], [2, 1, 2] and [1, 2, 1, 2]. Of these, the minimum total price is 4.
▼Sample Case 1
Sample Input For Custom Testing
STDIN
FUNCTION
5 → number of products n = 5
1 ラ price [1, 100, 1, 7, 7]
100
1
7
7
Sample Output
14
Explanation
The subsegments where not all prices are distinct are: [1, 100, 1]
- Assistant