Problem · Array

Count Possible Winners (All About Rewards :)

Learn this problem
MediumAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

SDE II

Amazon Shopping is running a reward collection event for its customers.

There are n customers and the i-th customer has collected initialRewards[i] points so far.

One final tournament is to take place where:

  • The champion earns n additional points
  • The second place earns n - 1 points
  • The third place earns n - 2 points
  • … and the last place earns 1 point
  • Given an integer array initialRewards of length n, representing the initial reward points of the customers before the final tournament:

    Find the number of customers i (1 ≤ i ≤ n) such that, if the i-th customer wins the final tournament, they would have the highest total points.

    Note -

    The total points = initialRewards[i] + n (if they win). Other customers also get points in the tournament depending on their ranks (from n - 1 to 1). You must check if the i-th customer, upon winning, ends up with the highest total score, regardless of how others place.

    Function

    countPossibleWinners(initialRewards: int[], n: int) → int

    Examples

    Example 1

    initialRewards = [1, 3, 4]n = 3return = 2

    Let's analyze for each customer if they win the final tournament, would they have the highest total points or not:

    • If the 1st customer wins the final tournament, their total points would be: 1 + 3 = 4, but this is not the highest possible points in this case.

      For example, if the 3rd customer with an initial reward of 4 comes 2nd, then they would achieve a total of: 4 + 2 = 6 points which is the higher than 1st customer points.

    • If the 2nd customer wins the final tournament, their total points would be: 3 + 3 = 6, and this is the highest total points in this case.

      Even if the 3rd customer with an initial reward of 4 comes 2nd, then they would achieve a total of: 4 + 2 = 6 points which is not greater than 2nd customer points.

    • If the 3rd customer wins the final tournament, their total points would be: 4 + 3 = 7, and this is the highest total points, as there are no other customers that can achieve the total point of 7 in this case.

    Thus, the customers 2 and 3 are the ones such that, if they win the final tournament, they would have the highest total points.

    Hence the answer is 2.

    Example 2

    initialRewards = [5, 7, 9, 11]n = 4return = 1

    Only the 4th customer is the one such that, if they win the final tournament, they would have the highest total points.

    Example 3

    initialRewards = [8, 10, 9]n = 3return = 2

    Only the 2nd and the 3rd customers are the ones such that, if they win the final tournament, they would have the highest total points.

    • If the 2nd customer wins the final tournament, their total points would be: 10 + 3 = 13, and this is the highest total points, as there are no other customers that can achieve the total point of 13 in this case.

    • If the 3rd customer wins the final tournament, their total points would be: 9 + 3 = 12, and this is the highest total points in this case.

      Even if the 2nd customer with an initial reward of 10 comes 2nd, then they would achieve a total of: 10 + 2 = 12 points which is not greater than 2nd customer points.

    Constraints

    • 1 ≤ n ≤ 10^5
    • 0 ≤ initialRewards[i] ≤ 10^5
    • Complete constraints added on 06-18-2025

    More Amazon problems

    drafts saved locally
    public int countPossibleWinners(int[] initialRewards, int n) {
      // write your code here
    }
    
    initialRewards[1, 3, 4]
    n3
    expected2
    checking account