2190. Most Frequent Number Following Key In an Array LeetCode Solution
In this guide, you will get 2190. Most Frequent Number Following Key In an Array LeetCode Solution with the best time and space complexity. The solution to Most Frequent Number Following Key In an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Most Frequent Number Following Key In an Array solution in C++
- Most Frequent Number Following Key In an Array solution in Java
- Most Frequent Number Following Key In an Array solution in Python
- Additional Resources
Problem Statement of Most Frequent Number Following Key In an Array
You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums.
For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that:
0 <= i <= nums.length – 2,
nums[i] == key and,
nums[i + 1] == target.
Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique.
Example 1:
Input: nums = [1,100,200,1,100], key = 1
Output: 100
Explanation: For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
No other integers follow an occurrence of key, so we return 100.
Example 2:
Input: nums = [2,2,2,2,3], key = 2
Output: 2
Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
The test cases will be generated such that the answer is unique.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1001) = O(1)
2190. Most Frequent Number Following Key In an Array LeetCode Solution in C++
class Solution {
public:
int mostFrequent(vector<int>& nums, int key) {
vector<int> count(1001);
for (int i = 0; i + 1 < nums.size(); ++i)
if (nums[i] == key)
++count[nums[i + 1]];
return ranges::max_element(count) - count.begin();
}
};
/* code provided by PROGIEZ */
2190. Most Frequent Number Following Key In an Array LeetCode Solution in Java
class Solution {
public int mostFrequent(int[] nums, int key) {
int[] count = new int[1001];
int ans = 0;
for (int i = 0; i + 1 < nums.length; ++i)
if (nums[i] == key)
++count[nums[i + 1]];
for (int i = 1; i < 1001; ++i)
if (count[i] > count[ans])
ans = i;
return ans;
}
}
// code provided by PROGIEZ
2190. Most Frequent Number Following Key In an Array LeetCode Solution in Python
class Solution:
def mostFrequent(self, nums: list[int], key: int) -> int:
count = collections.Counter()
for a, b in itertools.pairwise(nums):
if a == key:
count[b] += 1
return max(count, key=lambda num: count[num])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.