#define OPTIMIZE 1
#define Interactive 0

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 3e5, MAXV = -1;
const int INF = 0x3f3f3f3f, MOD = 1e9+7; // MOD = 1e9+7, 1e8+7, 998244353
const long long LINF = 1e18;
#define INIT(arr, val) fill(arr, arr+(int)(sizeof(arr)/sizeof(arr[0])), val)
#define REP(i, lb, rb, inc) for(int i = (lb); i < (rb); i += inc)
#define RREP(i, rb, lb, dec) for(int i = (rb)-1; i >= (lb); i -= dec)
#define REPN(i, s, n) for(int i = s; i < n; ++i)
#define RREPN(i, n, s) for(int i = n-1; i >= s; --i)
typedef long long ll;
typedef queue<int> QI;
typedef vector<int> VI;
typedef vector<ll> VLL;
#define pb push_back
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
template <typename T> using Vector = complex<T>;
#define X real()
#define Y imag()
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#if !Interactive
#	define endl '\n'
#endif
#define Foxyy cin.tie(0); cout.sync_with_stdio(0); cout.tie(0);
#define I_Suck_At_Coding signed main()

#if OPTIMIZE
#	pragma GCC optimize("Ofast")
//#	pragma GCC target("avx,avx2,fma")
#endif

#if IDE && !Interactive
	ifstream fin("cin.in");
	ofstream fout("cout.out");
	ofstream ferr("cerr.out");
	#define Input fin
	#define Output(x) { fout << x; cout << x; }
	#define Error(x) { ferr << x; cerr << x; }
#else
	#define Input cin
	#define Output(x) cout << x;
	#define Error(x) ;
#endif

#define READ(a, t) static t a; Input >> a;
#define READARR(arr, n) REP(___i, 0, (n), 1) Input >> arr[___i];
#define PRINTARR(arr, n) { REP(___i, 0, (n), 1) Output(arr[___i] << " ") Output(endl) }

#define RI(a) READ(a, int)
#define RD(a) READ(a, double)
#define RC(a) READ(a, char)
#define RLL(a) READ(a, ll)
#define RSTR(a) READ(a, string)
#define RVI(__a, n) VI __a(n); READARR(__a, n)
#define RVLL(__a, n) VLL __a(n); READARR(__a, n)

#define randInt(l, r) uniform_int_distribution<int>(l, r-1)(rng)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int randomk = 30;
VI pos[MAXN+1];

I_Suck_At_Coding {
	Foxyy
	
	RI(n); RI(q);
	VI a(n);
	REPN(i, 0, n) {
		Input >> a[i];
		pos[a[i]].pb(i);
	}
	while(q--) {
		int ans = 1;
		RI(ql); RI(qr);
		REPN(_, 0, randomk) {
			int x = a[randInt(ql-1, qr)], f = upper_bound(ALL(pos[x]), qr-1)-lower_bound(ALL(pos[x]), ql-1);
			ans = max(ans, f*2-(qr-ql+1));
		}
		Output(ans << endl);
	}
}