Maximum Element and Frequency

Easy
0/40
0 upvote

Problem statement

You are given an array A of N integers. Your task is to find two pieces of information: the largest value in the array, and the number of times that largest value appears.


You must implement a function that processes the array and determines these two values.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer N, the number of elements in the array.
The second line contains N space-separated integers, representing the elements of the array A.


Output Format:
Your function should return the maximum element and its frequency. The runner code will handle printing these two values on a single line, separated by a space.


Note:
This problem can be solved efficiently in a single pass (O(N) time complexity) through the array. You should keep track of the current maximum element seen so far and its count. If you find a new, larger maximum, you reset the count.
Sample Input 1:
5
2 5 1 5 4


Sample Output 1:
5 2


Explanation for Sample 1:
The largest element in the array is 5. It appears 2 times.


Expected Time Complexity:
The expected time complexity is O(N).


Constraints:
1 <= N <= 10^5
-10^9 <= A[i] <= 10^9
Time Limit: 1 sec
Approaches (1)
Brute Force
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Maximum Element and Frequency
Full screen
Console