algoadvance

Leetcode 2923. Find Champion I

Problem Statement

You are given an array of integers where each integer represents the points a player scored in a game. You need to identify the player who scored the maximum points and return that value. If there are multiple players with the same maximum points, return any one of them.

Clarifying Questions

  1. Input Range: Are there any constraints on the size of the array or the range of the integers?
    • Assume the array can have a size ranging from 1 to (10^6) and integer values can range from (-10^6) to (10^6).
  2. Return Value: If there are multiple players with the same maximum points, can I return any one of them?
    • Yes, you can return any one of them.
  3. Edge Cases: What should we return if the array is empty?
    • The array is guaranteed to have at least one element.

Strategy

  1. Initialize a Variable: Create a variable to keep track of the maximum points found so far.
  2. Iterate Through Array: Loop through the array to compare each player’s score with the current maximum points.
  3. Update Maximum Points: If a higher score is found, update the maximum points.
  4. Return Result: Once we have traversed the entire array, return the maximum points.

Code

public class FindChampion {
    public static int findChampion(int[] scores) {
        // Initial assumption that the maximum score is the first element
        int maxPoints = scores[0];
        
        // Loop through the array starting from the second element
        for (int i = 1; i < scores.length; i++) {
            if (scores[i] > maxPoints) {
                maxPoints = scores[i];
            }
        }
        
        // Return the maximum points
        return maxPoints;
    }

    public static void main(String[] args) {
        // Example test case
        int[] scores = {10, 20, 30, 20, 30, 10};
        System.out.println("Champion's Score: " + findChampion(scores)); // Output: 30
    }
}

Time Complexity

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI