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)?
Given an array of integers, the task is to find the maximum value in the array and the number of times this maximum value occurs.
An array of integers, nums
.
An array containing two elements: the maximum value and the number of occurrences of this maximum value.
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 how many times it appears. This problem is significant in various applications such as statistical analysis, data processing, and more.
Potential pitfalls include not handling edge cases like arrays with all identical elements or arrays with negative numbers.
To solve this problem, we can use a single pass through the array to find both the maximum value and its count. This ensures an O(n) time complexity and O(1) space complexity.
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 this by using a single pass. During the iteration, we keep track of the current maximum value and its count. If we find a new maximum, we update the maximum value and reset the count. If we find another occurrence of the current maximum, we increment the count.
Here is a step-by-step breakdown of the optimized 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
and count
.// Function to find the maximum value and its number of occurrences
function findMaxAndCount(nums) {
// Initialize maxVal to the first element and count to 0
let maxVal = nums[0];
let count = 0;
// Iterate through each element in the array
for (let val of nums) {
// If current value is greater than maxVal, update maxVal and reset count
if (val > maxVal) {
maxVal = val;
count = 1;
}
// If current value is equal to maxVal, increment count
else if (val === maxVal) {
count += 1;
}
}
// Return the result as an array
return [maxVal, count];
}
// Example usage
const nums = [2, 7, 11, 8, 11, 8, 3, 11];
console.log(findMaxAndCount(nums)); // Output: [11, 3]
The time complexity of this approach is O(n) because we only iterate through the array once. The space complexity is O(1) as we are using a constant amount of extra space.
Consider the following edge cases:
[5, 5, 5, 5]
should return [5, 4]
.[-1, -2, -3, -1]
should return [-1, 2]
.To test the solution comprehensively, consider a variety of test cases:
When approaching such problems, consider breaking down the problem into smaller parts. Think about how you can optimize your solution by reducing the number of passes through the data. Practice similar problems to improve your problem-solving skills.
In this blog post, we discussed how to find the maximum value in an array and its number of occurrences in O(n) time using JavaScript. We explored the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving your algorithmic thinking and coding skills.
For further reading and practice, consider the following resources: