Given an array of integers, return the maximum value and its number of occurrences.
Example:
Input: nums = [2, 7, 11, 8, 11, 8, 3, 11]
Output: [11, 3]
Explanation: The maximum value is 11 and it appears 3 times
Your algorithm should run in O(n) time and use O(1) space.
Could you do this in one pass (e.g. looping over the array only once)?
The problem requires us to find the maximum value in an array of integers and count how many times this maximum value appears. The solution should be efficient, running in O(n) time complexity and using O(1) space complexity.
nums = [2, 7, 11, 8, 11, 8, 3, 11]
[11, 3]
Input: nums = [2, 7, 11, 8, 11, 8, 3, 11]
Output: [11, 3]
Explanation: The maximum value is 11 and it appears 3 times
The core challenge is to find the maximum value in the array and count its occurrences efficiently. This problem is significant in various applications such as data analysis, where finding the most frequent maximum value is essential.
To solve this problem, we can use a single pass through the array to find both the maximum value and its count. This ensures we meet the O(n) time complexity requirement.
A naive solution would involve two passes: one to find the maximum value and another to count its occurrences. However, this is not optimal as it requires two passes through the array.
We can optimize the solution by using a single pass through the array. During this pass, we keep track of the maximum value and its count simultaneously.
maxVal
to the first element of the array and count
to 0.maxVal
and count
as needed.maxVal
and reset count
to 1.maxVal
, increment count
.Here is a step-by-step breakdown of the algorithm:
maxVal
to the first element of the array and count
to 0.maxVal
, update maxVal
and reset count
to 1.maxVal
, increment count
.[maxVal, count]
.def max_val_and_count(nums):
# Initialize maxVal to the first element and count to 0
maxVal, count = nums[0], 0
# Iterate through each element in the array
for val in nums:
if val > maxVal:
# Found a new maximum value
maxVal = val
count = 1
elif val == maxVal:
# Found another occurrence of the current maximum value
count += 1
return [maxVal, count]
# Example usage
nums = [2, 7, 11, 8, 11, 8, 3, 11]
print(max_val_and_count(nums)) # Output: [11, 3]
The time complexity of this solution is O(n) because we only iterate through the array once. The space complexity is O(1) because we only use a fixed amount of extra space regardless of the input size.
Potential edge cases include:
[1, 1, 1, 1]
. The output should be [1, 4]
.[5]
. The output should be [5, 1]
.# Edge case: all identical elements
print(max_val_and_count([1, 1, 1, 1])) # Output: [1, 4]
# Edge case: single element
print(max_val_and_count([5])) # Output: [5, 1]
To test the solution comprehensively, include a variety of test cases:
# Simple case
print(max_val_and_count([2, 7, 11, 8, 11, 8, 3, 11])) # Output: [11, 3]
# Edge case: all identical elements
print(max_val_and_count([1, 1, 1, 1])) # Output: [1, 4]
# Edge case: single element
print(max_val_and_count([5])) # Output: [5, 1]
# Large array
large_array = [i for i in range(1000000)] + [999999]
print(max_val_and_count(large_array)) # Output: [999999, 2]
When approaching such problems:
In this blog post, we discussed how to find the maximum value in an array and count its occurrences efficiently. We explored a naive solution and an optimized solution, analyzed their complexities, and tested various edge cases. Understanding and solving such problems is crucial for improving algorithmic thinking and coding skills.
For further reading and practice: