Perfect Rank Private Jobs
Perfect RankPrivate Jobs
💼 Placement & IT Recruitment Prep
#1

Final Prices With a Special Discount in a Shop

Easy
Acceptance: 81.2%•ArrayStackMonotonic Stack•TCS DigitalInfosys DSEWipro TurboAmazonCognizant

Problem Statement

You are given an integer array prices where prices[i] is the price of the ith item in a shop. There is a special discount for items in the shop. If you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will not receive any discount at all. Return an integer array answer where answer[i] is the final price you will pay for the ith item of the shop, considering the special discount.

Examples

Example 1
Input: prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
Explanation:

• For item 0 with price[0] = 8, the first smaller or equal item to the right is prices[1] = 4. Final price = 8 - 4 = 4. • For item 1 with price[1] = 4, the first smaller or equal item to the right is prices[3] = 2. Final price = 4 - 2 = 2. • For item 2 with price[2] = 6, the first smaller or equal item to the right is prices[3] = 2. Final price = 6 - 2 = 4. • For item 3 with price[3] = 2, there is no item to the right with price <= 2. Final price = 2. • For item 4 with price[4] = 3, there is no item to the right with price <= 3. Final price = 3.

Example 2
Input: prices = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation:

In this case, for all items, there is no other item to the right with a smaller or equal price. Therefore, no discount is applied to any item.

Example 3
Input: prices = [10,1,1,6]
Output: [9,0,1,6]
Explanation:

• For item 0 with price[0] = 10, discount is prices[1] = 1. Final price = 10 - 1 = 9. • For item 1 with price[1] = 1, discount is prices[2] = 1. Final price = 1 - 1 = 0. • For item 2 with price[2] = 1, no smaller/equal element to the right. Final price = 1. • For item 3 with price[3] = 6, last element has no elements to its right. Final price = 6.

Constraints

  • 1 <= prices.length <= 500
  • 1 <= prices[i] <= 1000

Hints

1
2
3
4
5
6
7
8
9
10
Input parameters:
Student & User Feedback

How was your experience on this page?

Your review & improvement suggestions help us refine tools, mock tests, and explanations — it takes only 10 seconds!

Feedback goes directly to our team at perfectrankofficial@gmail.com
Outstanding / Excellent! 🌟
Back to Top of Private Jobs Portal