This question evaluates array-processing skills, competency with next-smaller-or-equal element patterns, and the ability to compute aggregated results under ordering constraints.
You are given an array prices of length . Items are sold from left to right, and each item’s final selling price is computed as follows:
For item :
prices[j] <= prices[i]
.
prices[i] - prices[j]
.
prices[i]
(sold at full price).
You must output two lines:
prices
of length
If prices = [5, 1, 3], then item 0 is discounted by 1 (final 4), item 1 has no qualifying item to its right (final 1), item 2 has none (final 3). Total = 8, full-price indices = 1 2.