Find the most frequent element K positions apart from X in given Array
Last Updated :
28 Mar, 2022
Given an array nums[], and integer K and X, the task is to find the most frequent element K positions away from X in the given array.
Examples:
Input: nums = [1, 100, 200, 1, 100], K = 1, X = 1
Output: 100
Explanation: Elements 1 position apart from 1 is only 100.
So the answer is 100.
Input: nums = [2, 2, 2, 2, 3], K = 2, X = 2
Output: X = 2 occurs in indices {0, 1, 2, 3}.
Explanation: Elements 2 position apart are at {2}, {3}, {0, 4}, {1} i.e. 2, 2, {2, 3} and 2.
So 2 occurs 4 times and 3 one time, Therefore 2 is the most frequent element.
Approach: The problem can be solved using the idea of array traversal and storing the elements K distance away from X.
Follow the steps mentioned below to solve the problem:
- Search all occurrences of X in the given array
- For each occurrence of X, store the element at distance K with its frequency in a map
- At the end, just find the element in the map with most frequency and return it.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int mostFrequent(vector< int >& nums,
int K, int X)
{
map< int , int > m;
for ( int i = 0; i < nums.size() - K;
i++)
if (nums[i] == X) {
if (m.find(nums[i + K])
!= m.end())
m[nums[i + K]]++;
else
m.insert({ nums[i + K], 1 });
if (i - K >= 0)
m[nums[i - K]]++;
}
int ans = 0, count = 0;
for ( auto i : m) {
if (i.second > count) {
ans = i.first;
count = i.second;
}
}
return ans;
}
int main()
{
vector< int > nums = { 1, 100, 200, 1, 100 };
int K = 1, X = 1;
cout << mostFrequent(nums, K, X);
return 0;
}
|
Java
import java.io.*;
import java.util.HashMap;
import java.util.Map;
class GFG {
static int mostFrequent( int [] nums,
int K, int X)
{
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < nums.length - K; i++)
if (nums[i] == X) {
if (m.containsKey(nums[i + K])){
m.put(nums[i + K], m.get(nums[i + K]) + 1 );
}
else
m.put( nums[i + K], 1 );
if (i - K >= 0 ){
if (m.containsKey(nums[i - K]))
m.put(nums[i - K], m.get(nums[i - K]) + 1 );
}
}
int ans = 0 , count = 0 ;
for (Map.Entry<Integer, Integer> e : m.entrySet()){
if (e.getValue() > count){
ans = e.getKey();
count = e.getValue();
}
}
return ans;
}
public static void main (String[] args) {
int [] nums = { 1 , 100 , 200 , 1 , 100 };
int K = 1 , X = 1 ;
System.out.print(mostFrequent(nums, K, X));
}
}
|
Python3
def mostFrequent(nums, K, X):
m = dict ()
for i in range ( len (nums) - K):
if nums[i + K] in m:
m[nums[i + K]] + = 1
else :
m[nums[i + K]] = 1
if (i - K > = 0 ):
if (nums[i - K] in m):
m[nums[i - K]] + = 1
else :
m[nums[i - K]] = 1
ans = 0
count = 0
for i in m:
if m[i] > count:
ans = i
count = m[i]
return ans
nums = [ 1 , 100 , 200 , 1 , 100 ]
K = 1
X = 1
print (mostFrequent(nums, K, X))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int mostFrequent( int [] nums, int K, int X)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < nums.Length - K; i++)
if (nums[i] == X) {
if (m.ContainsKey(nums[i + K]))
m[nums[i + K]] = m[nums[i + K]] + 1;
else
m.Add(nums[i + K], 1);
if (i - K >= 0
&& m.ContainsKey(nums[i - K])) {
m[nums[i - K]] = m[nums[i - K]] + 1;
}
}
int ans = 0, count = 0;
foreach (KeyValuePair< int , int > x in m)
{
if (x.Value > count) {
ans = x.Key;
count = x.Value;
}
}
return ans;
}
public static void Main()
{
int [] nums = { 1, 100, 200, 1, 100 };
int K = 1, X = 1;
Console.Write(mostFrequent(nums, K, X));
}
}
|
Javascript
<script>
const mostFrequent = (nums, K, X) => {
let m = {};
for (let i = 0; i < nums.length - K; i++)
if (nums[i] == X) {
if (nums[i + K] in m)
m[nums[i + K]]++;
else
m[nums[i + K]] = 1;
if (i - K >= 0)
if (nums[i - K] in m) m[nums[i - K]]++;
else m[nums[i - K]] = 1;
}
let ans = 0, count = 0;
for (let i in m) {
if (m[i] > count) {
ans = i;
count = m[i];
}
}
return ans;
}
let nums = [1, 100, 200, 1, 100];
let K = 1, X = 1;
document.write(mostFrequent(nums, K, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...