304 North Cardinal St.
Dorchester Center, MA 02124

# Most Frequent Element CodeChef Solution

## Most Frequent Element CodeChef Solution in C++14

``````#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll n, m; cin>>n>>m; vector<ll>vec(n);
for(ll i=0; i<n; i++) cin>>vec[i];
vector<ll>dp(n); dp[n-1] = 1;
for(ll i=n-2; i>=0; i--) {
dp[i] = 1;
dp[i] += (vec[i] == vec[i+1])*dp[i+1];
}

// for(auto id:dp) cout<<id<<" ";
// cout<<endl;

for(ll i=0; i<m; i++) {
ll l, r, k,ans=0; cin>>l>>r>>k;
for(ll j=l-1; j<=(r-k); j++)
if(dp[j] >= k) {
cout<<vec[j]<<endl;
ans = 1; break;
} if(!ans) cout<<-1<<endl;
}
}``````

## Most Frequent Element CodeChef Solution in PYTH 3

``````# cook your dish here
n, m = map(int, input().split())
a = list(map(int, input().split()))
pre, cur = [], 0
for i in range(len(a)):
if i == 0 or a[i] == a[i-1]:
cur += 1
else:
cur = 1
pre.append(cur)
for _ in range(m):
l, r, k = map(int, input().split())
l -= 1
r -= 1
i, j = l+k-1, r-k+1
if a[i] == a[l] and pre[i] >= k:
print(a[i])
continue
if a[j] == a[r] and pre[r] >= k:
print(a[j])
continue
mid = (l+r)//2
if pre[mid] > mid-l+1:
d = pre[mid]-pre[l]+1
else:
d = pre[mid]
_j = mid+k-d
if _j <= r and pre[_j] >= k:
print(a[mid])
continue
print(-1)

``````

## Most Frequent Element CodeChef Solution in C

``````#include <stdio.h>
#define max(x, y)	((x < y) ? (y) : (x))
#define min(x, y)	((x > y) ? (y) : (x))

int main(){
int N, M;
scanf("%d %d", &N, &M);
int arr[N];
for(int i=0; i<N; i++)	scanf("%d", arr+i);

// Forming Right and Left arrays
int left[N], right[N];
left[0] = 0;
right[N-1] = N-1;
for(int i=1; i<N; i++){
if(arr[i-1] == arr[i])	left[i] = left[i-1];
else left[i] = i;
if(arr[N-1-i] == arr[N-i])	right[N-1-i] = right[N-i];
else right[N-1-i] = N-1-i;
}

int L, R, k, mid;
for(int i=0; i<M; i++){
scanf("%d %d %d", &L, &R, &k);	// L and R are for 1-indexed array
L--;
R--;
mid = (L+R)/2;
if(min(right[mid], R)-max(left[mid], L)+1 >= k)	printf("%d\n", arr[mid]);
else	printf("-1\n");
}

return 0;
}``````

## Most Frequent Element CodeChef Solution in JAVA

``````import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;

public class Main {
static PrintWriter out;
static int mod = 998244353;
public static void main(String[] args) {
out = new PrintWriter(System.out);
int tt = 1;

/*
*     [1222222567] [1522222267]
* */

while(tt -- > 0) {
int start[] = new int[n];
int end[] = new int[n];
for(int i = 0 ; i < n ; i++){
if(i-1>=0 && a[i-1] == a[i]){
start[i] = start[i-1];
}else{
start[i] = i;
}
}
for(int i = n-1; i >= 0; i--){
if(i+1 < n && a[i] == a[i+1] ){
end[i] = end[i+1];
}else{
end[i] = i;
}
}
for(int i= 0 ; i < m; i++){
int mid = (l+r)/2;
if(min(r,end[mid])-max(l,start[mid])+1 >= k ){
out.println(a[mid]);
}else{
out.println(-1);
}
}

}
out.close();

}

public static class Pair{
int val;
int index;

public Pair(int val, int index) {
this.val = val;
this.index = index;
}

@Override
public String toString() {
return "Pair{" +
"val=" + val +
", index=" + index +
'}';
}
}

// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
static Random __r = new Random();
static int[][] direction = new int[][]{{-1,-1}, {-1,0}, {-1,1},  {0,1}, {1,1},  {1,0},  {1,-1},  {0, -1}};

// math util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}

static int minof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
static char maxc(char a, char b){
if(a > b){
return a;
}
return b;
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}

static long minof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}

static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}

static int maxof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}

static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}

static long maxof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}

static int powi(int a, int b) {
if (a == 0)
return 0;
int ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}

static long powl(long a, int b) {
if (a == 0)
return 0;
long ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}

static int fli(double d) {
return (int) d;
}

static int cei(double d) {
return (int) ceil(d);
}

static long fll(double d) {
return (long) d;
}

static long cel(double d) {
return (long) ceil(d);
}

static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}

static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}

static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}

static int[] exgcd(int a, int b) {
if (b == 0)
return new int[] { 1, 0 };
int[] y = exgcd(b, a % b);
return new int[] { y[1], y[0] - y[1] * (a / b) };
}

static long[] exgcd(long a, long b) {
if (b == 0)
return new long[] { 1, 0 };
long[] y = exgcd(b, a % b);
return new long[] { y[1], y[0] - y[1] * (a / b) };
}

static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}

static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}

public static boolean[] findPrimes(int limit) {
assert limit >= 2;

final boolean[] nonPrimes = new boolean[limit];
nonPrimes[0] = true;
nonPrimes[1] = true;

int sqrt = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrt; i++) {
if (nonPrimes[i])
continue;
for (int j = i; j < limit; j += i) {
if (!nonPrimes[j] && i != j)
nonPrimes[j] = true;
}
}

return nonPrimes;
}

// array util
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}

static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}

static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}

static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}

static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}

static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}

static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}

static void rsort(int[] a) {
shuffle(a);
sort(a);
}

static void rsort(long[] a) {
shuffle(a);
sort(a);
}

static void rsort(double[] a) {
shuffle(a);
sort(a);
}

static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}

static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}

static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}

static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}

StringTokenizer st;

}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(next());
return a;
}

long nextLong() {
return Long.parseLong(next());
}

long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(next());
return a;
}

double nextDouble() {
return Double.parseDouble(next());
}

String nextLine() {
String str = "";
try {
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}``````

## Most Frequent Element CodeChef Solution in PYPY 3

``````import sys
from math import *
from collections import *
out=sys.stdout.write
# n=int(inp())
# arr=list(map(int,inp().split()))
n,q=map(int,inp().split())
arr=list(map(int,inp().split()))
pre_sum=[0]*n
cnt,last=1,-1
for i,val in enumerate(arr):
if i-1>=0: pre_sum[i-1]=cnt
if val==last:
cnt+=1
else:
cnt=1
last=val
pre_sum[-1]=cnt
for _ in range(q):
l,r,k=map(int,inp().split())
l-=1
r-=1
mid=(l+r)>>1
if pre_sum[mid]>=k and (mid-l+1)==k:
print(arr[mid])
continue
sub= mid-l+1 if pre_sum[mid]>=mid-l+1 else pre_sum[mid]
if mid+k-sub<=r and pre_sum[mid+k-sub]>=k:
print(arr[mid])
else:
print(-1)

``````

## Most Frequent Element CodeChef Solution in PYTH

``````n,m=map(int,raw_input().split())
a=map(int,raw_input().split())
b=[1]
for _ in xrange(1,n):
if a[_]==a[_-1]:    b.append(b[-1]+1)
else:       b.append(1)
for _ in xrange(m):
l,r,k=map(int,raw_input().split())
l,r=l-1,r-1
mid=(r-l+1)/2+1
st=b[l+mid-1]
if b[l+mid-1]>mid:
st=mid
dif=k-st
if (l+mid-1+dif)>r:
print -1
continue
if b[l+mid-1+dif]>=k:
print a[l+mid-1]
else:
print -1``````

## Most Frequent Element CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MFREQ
{
class Program
{
static int[] A = new int[100000];
static int[] last = new int[100000];
static int[] first = new int[100000];

static void Main(string[] args)
{
int N, M;
N = int.Parse(tokens[0]);
M = int.Parse(tokens[1]);

int index = -1;
for (int i = 0; i < N; i++)
{
A[i] = int.Parse(tokens[i]);
if (i > 0)
{
if (A[i] == A[index])
first[i] = index;
else
first[i] = index = i;
}
else
first[i] = index = i;
}

for (int i = N - 1; i >= 0; i--)
{
if (i < N - 1)
{
if (A[i] == A[index])
last[i] = index;
else
last[i] = index = i;
}
else
last[i] = index = i;
}

//for (int i = 0; i < N; i++)
//{
//    Console.WriteLine("first[" + i + "]=" + first[i] + ", last[" + i + "]=" + last[i]);
//}

StringBuilder sb = new StringBuilder();
int L, R, k, mid, a, b;
for (int i = 0; i < M; i++)
{
L = int.Parse(tokens[0])-1;
R = int.Parse(tokens[1])-1;
k = int.Parse(tokens[2]);
mid = (L + R) / 2;
//Console.WriteLine("mid=" + mid);
//Console.WriteLine("f[mid]="+first[mid]);
//Console.WriteLine("l[mid]=" + last[mid]);
a = Math.Max(first[mid], L);
b = Math.Min(last[mid], R);
//Console.WriteLine("a=" + a + ", b=" + b);
sb.AppendLine(((b - a + 1) >= k ? A[mid] : -1).ToString());
}
Console.Write(sb);
}
}
}``````
##### Most Frequent Element CodeChef Solution Review:

In our experience, we suggest you solve this Most Frequent Element CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Most Frequent Element CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Most Frequent Element CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!