#include <bits/stdc++.h>
using namespace std;
#define limit (300000 + 5)
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a ; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
typedef long long ll;
typedef unsigned long long ull;
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}
void write(ll x){
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n, m;
struct node{
    int l, r, qid, blo;
    int len(){
        return r - l + 1;
    }
    bool operator<(const node &rhs)const{
        if(blo ^ rhs.blo)return l < rhs.l;
        return blo & 1 ? r < rhs.r : rhs.r < r;
    }
}query[limit];
int cnt[limit],a[limit],sum[limit];
int res;
void add(int x){
    --sum[cnt[a[x]]];
    ++cnt[a[x]];
    res = max(res, cnt[a[x]]);
    ++sum[cnt[a[x]]];
}
void del(int x){
    --sum[cnt[a[x]]];
    if(res == cnt[a[x]] && !sum[cnt[a[x]]])--res;
    --cnt[a[x]];
    ++sum[cnt[a[x]]];
}
int vis[limit];
int main(){
    n = read(), m = read();
    rep(i ,1, n){
      int x=read();
      a[i]=x;
    }
    int block = int(sqrt(n >= 3 ? n * (2.0 / 3) : n));
    rep(i, 1, m){
        query[i].l = read(), query[i].r = read(), query[i].qid = i;
        query[i].blo = query[i].l / block;
    }
    sort(query + 1, query + 1 + m);
    int l = 1 , r = 0;
    res = 0;
    rep(i ,1, m){
        while(l > query[i].l)add(--l);
        while(r < query[i].r)add(++r);
        while(r > query[i].r)del(r--);
        while(l < query[i].l)del(l++);
        vis[query[i].qid] = max(res - (query[i].len() - res), 1);
    }
    rep(i ,1, m){
      printf("%d\n", vis[i]);
    }
    return 0;
}