Java: I love %username%

Thought Process

The problem requires counting amazing performances where a contestant's score breaks their previous records. We initialize minScore and maxScore with the first contest's score. For each subsequent score, we check if it either exceeds maxScore or falls below minScore. When either condition is met, we update the corresponding extreme value and increment our amazingCount.

import java.util.Scanner;

public class CF {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int noOfContests = sc.nextInt();

        int refScore = sc.nextInt();
        int maxScore = refScore;
        int minScore = refScore;
        int noOfAmazingPerformance = 0;

        for(int contest=1;contest<noOfContests;contest++){

            int score = sc.nextInt();
            if(score > maxScore){
                maxScore = score;
                noOfAmazingPerformance++;
            }
            if(score < minScore){
                minScore = score;
                noOfAmazingPerformance++;
            }
        }
        System.out.println(noOfAmazingPerformance);
    }
}

Code Complexity

Time Complexity: O(n)

The algorithm iterates through the array of scores once, making it linear in time complexity.

Space Complexity: O(1)

The algorithm uses a constant amount of extra space, regardless of the input size.

Code copied!