#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) { 
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const set<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll n,q;
const ll N = 3e5+7;
vll pos[N];
ll a[N];
const ll X = 40;

ll getcnt(ll l, ll r, ll x) {
    auto lit = lower_bound(all(pos[x]), l);
    auto rit = upper_bound(all(pos[x]), r);
    return rit-lit;
}

vll solve(vpll qs) {
    F(n) pos[a[i]].pb(i);
    vll ret;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    for(auto [l,r]: qs) {
        ll mxc = 0;
        ll len = r-l+1;
        REP(__,X) {
            ll pos = uniform_int_distribution<int>(l,r)(rng);
            ll cnt = getcnt(l,r,a[pos]);
            mxc = max(mxc,cnt);
        }
        ll cur = 1;
        if(mxc>(len+1)/2) {
            ll rest = len-mxc;
            cur += max(mxc-rest-1,0ll);
        }
        ret.pb(cur);
    }
    return ret;
}

void gen() {
    F(N) pos[i].clear();
    F(N) a[i]=0;
    n = rand()%30+1;
    F(n) a[i] = rand()%n;
    ll l=rand()%n;
    ll r=l+(rand()%(n-l));
    ll mxc = 0;
    F(n) {
        ll c = 0;
        FOR(j,l,r+1) c+=a[j]==i;
        mxc=max(mxc,c);
    }
    ll len = r-l+1;
    vll ref = {1ll+max(0ll, mxc-(len-mxc)-1)};
    vll ret = solve({{l,r}});
    if(ret!=ref) {
        cerr << n << ' ' << 1 << endl;
        F(n) cerr << a[i]+1 << ' ';
        cerr << endl;
        cerr<<l+1 << ' ' << r+1 << endl;
        dbg("ret",ret);
        dbg("ref",ref);
        exit(1);
    }
}

int main(void) {
    ios_base::sync_with_stdio(false);
    srand(time(0));
//     REP(_,10000) gen();
    cin >> n >> q;
    F(n) { cin >> a[i]; a[i]--; }
    vpll qs;
    F(q) {
        ll l,r; cin >> l >> r;
        l--; r--;
        qs.eb(l,r);
    }
    auto ret = solve(qs);
    for(auto i: ret) cout << i << endl;
    return 0;
}
