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)?
We need to find the maximum value in an array of integers and count how many times this maximum value occurs. The solution should be efficient, running in O(n) time complexity and using O(1) space complexity.
An array of integers nums
.
A list containing two elements: the maximum value and its number of occurrences.
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 statistical analysis, data processing, and more. A common pitfall is to use a two-pass solution, which is not optimal for this problem.
To solve this problem efficiently, we can use a single-pass algorithm. The idea is to iterate through the array once, keeping track of the maximum value and its count simultaneously.
A naive solution would involve two passes over the array: one to find the maximum value and another to count its occurrences. This approach is not optimal as it requires O(n) time complexity but involves two passes.
We can optimize the solution by combining the two passes into one. During a single iteration, we can update the maximum value and its count as follows:
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]
.
#include <iostream>
#include <vector>
using namespace std;
pair findMaxAndCount(const vector& nums) {
// Initialize maxVal to the first element and count to 0
int maxVal = nums[0];
int count = 0;
// Iterate through the array
for (int val : nums) {
if (val > maxVal) {
// Found a new maximum value
maxVal = val;
count = 1; // Reset count to 1
} else if (val == maxVal) {
// Found another occurrence of the current maximum value
count++;
}
}
// Return the maximum value and its count
return {maxVal, count};
}
int main() {
vector nums = {2, 7, 11, 8, 11, 8, 3, 11};
pair result = findMaxAndCount(nums);
cout << "Max Value: " << result.first << ", Count: " << result.second << endl;
return 0;
}
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]
[-1, -2, 3, 4, -1, 4]
[10]
Each of these cases should be handled correctly by the algorithm.
To test the solution comprehensively, consider a variety of test cases:
Using a testing framework like Google Test can help automate and manage these tests effectively.
When approaching such problems, consider the following tips:
In this blog post, we discussed how to find the maximum value in an array and count its occurrences efficiently using a single-pass algorithm in C++. Understanding and solving such problems is crucial for improving your algorithmic thinking and coding skills. Keep practicing and exploring further to master these concepts.
For further reading and practice, consider the following resources: