Problem · Array

Shopkeeper Final Price

Learn this problem
MediumUberOA
See Uber hiring insights

Problem statement

A shopkeeper arranges items in a list for a sale. Starting from the left, each item is sold at its full price minus the price of the first item to its right that is of equal or lower price.

If no such item exists, the current item is sold at its full price.

Print the sum of the final cost for all items, then on the next line print a space-separated, 0-based list of the indices of items that are sold at full price, in ascending order.

Function

finalPrice(prices: int[]) → String[]

Complete the function finalPrice in the editor with the following parameter:

  • int prices[n]: an array of item prices

Returns

Return a two-element String[] representing the required output lines: the total cost as the first string, and the space-separated full-price item indices as the second string.

Constraints

  • 1 <= n <= 10^5
  • 1 <= prices[j] <= 10^9, where 0 <= j < n

Examples

Example 1

prices = [9, 2, 1, 2, 4, 2]return = ["13","2 5"]

For item prices [9, 2, 1, 2, 4, 2]:

  • Item 0, priced at 9, is discounted by the next item priced 2, so its final cost is 7.
  • Item 1, priced at 2, is discounted by the next equal-or-lower item priced 1, so its final cost is 1.
  • Item 2, priced at 1, sells at full price because there is no equal-or-lower item to its right.
  • Item 3, priced at 2, is discounted by the later item priced 2, so its final cost is 0.
  • Item 4, priced at 4, is discounted by the later item priced 2, so its final cost is 2.
  • Item 5, priced at 2, sells at full price.

The total cost is 7 + 1 + 1 + 0 + 2 + 2 = 13, and the full-price item indices are 2 5.

The output lines are 13 and 2 5.

Source note (June 29, 2026): The source image is blurry and this example is hard to read. FastPrep worked out the output from the stated discount rule. If you notice a mistake, please let us know and we'll correct it. If a clearer source appears later, we'll recheck this example.

Example 2

prices = [5, 1, 3, 4, 6, 2]return = ["14","1 5"]

For item prices [5, 1, 3, 4, 6, 2]:

  • Item 0, priced at 5, is discounted by the next item priced 1, so its final cost is 4.
  • Item 1, priced at 1, sells at full price because there is no equal-or-lower item to its right.
  • Item 2, priced at 3, is discounted by the later item priced 2, so its final cost is 1.
  • Item 3, priced at 4, is discounted by the later item priced 2, so its final cost is 2.
  • Item 4, priced at 6, is discounted by the later item priced 2, so its final cost is 4.
  • Item 5, priced at 2, sells at full price.

The total cost is 4 + 1 + 1 + 2 + 4 + 2 = 14, and the full-price item indices are 1 5.

The output lines are 14 and 1 5.

Source note (June 29, 2026): The source image is blurry and this example is hard to read. FastPrep worked out the output from the stated discount rule. If you notice a mistake, please let us know and we'll correct it. If a clearer source appears later, we'll recheck this example.

More Uber problems

drafts saved locally
public String[] finalPrice(int[] prices) {
  // write your code here
}
prices[9, 2, 1, 2, 4, 2]
expected["13", "2 5"]
checking account