Baby Ehab has a piece of Cut and Stick with an array $$$a$$$ of length $$$n$$$ written on it. He plans to grab a pair of scissors and do the following to it:  pick a range $$$(l, r)$$$ and cut out every element $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ in this range;  stick some of the elements together in the same order they were in the array;  end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece. More formally, he partitions the sequence $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length $$$x$$$, then no value occurs strictly more than $$$\lceil \frac{x}{2} \rceil$$$ times in it.He didn't pick a range yet, so he's wondering: for $$$q$$$ ranges $$$(l, r)$$$, what is the minimum number of pieces he needs to partition the elements $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ into so that the partitioning is beautiful.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. Note that it does not have to be contiguous.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n,q \le 3 \cdot 10^5$$$) — the length of the array $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n}$$$ ($$$1 \le a_i \le n$$$) — the elements of the array $$$a$$$. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the range of this query.

For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.

NoteIn the first query, you can just put the whole array in one subsequence, since its length is $$$6$$$, and no value occurs more than $$$3$$$ times in it.In the second query, the elements of the query range are $$$[3,2,3,3]$$$. You can't put them all in one subsequence, since its length is $$$4$$$, and $$$3$$$ occurs more than $$$2$$$ times. However, you can partition it into two subsequences: $$$[3]$$$ and $$$[2,3,3]$$$.

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ 747:
/***/ ((module) => {

module.exports = require("fs");;

/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/************************************************************************/
/******/ 	/* webpack/runtime/compat get default export */
/******/ 	(() => {
/******/ 		// getDefaultExport function for compatibility with non-harmony modules
/******/ 		__webpack_require__.n = (module) => {
/******/ 			var getter = module && module.__esModule ?
/******/ 				() => (module['default']) :
/******/ 				() => (module);
/******/ 			__webpack_require__.d(getter, { a: getter });
/******/ 			return getter;
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {

// EXTERNAL MODULE: external "fs"
var external_fs_ = __webpack_require__(747);
var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_);
;// CONCATENATED MODULE: ./src/lib/binarySearch.ts
function binarySearch(array, pred) {
  let lo = -1,
      hi = array.length;

  while (lo + 1 < hi) {
    //const mi = lo + ((hi - lo) >> 1);
    const mi = hi + lo >> 1; // console.log({ lo, hi, mi });

    if (pred(array[mi])) {
      hi = mi;
    } else {
      lo = mi;
    } // console.log('after', { lo, hi, mi });
    // console.log();

  }

  return hi;
}
function lowerBound(array, item) {
  return binarySearch(array, v => v >= item);
}
function upperBound(array, item) {
  return binarySearch(array, v => v > item);
} // console.log(lowerBound([10, 12, 12, 12, 13], 12)); // 1
// console.log(upperBound([10, 12, 12, 12, 13], 12)); // 4
// console.log(lowerBound([10, 12, 12, 12, 13], 9));
// -1 : 5
// 0   1   2   3   4
// 10, 12, 12, 12, 13
// m = 2
// -1 : 2
// m = 0
// 0 : 2
// m = 1
// 0 : 1
;// CONCATENATED MODULE: ./src/cf-1514/1514-d.ts

 //////////// LIBS

class IO {
  constructor() {
    this.i = 0;
    this.text = void 0;
    this.out = '';
    this.text = __webpack_require__(747).readFileSync(0, 'utf8');
  }

  nextToken() {
    let ret = '';

    while (this.text[this.i].charCodeAt(0) <= 32) {
      this.i++;
    }

    while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
      ret += this.text[this.i];
      this.i++;
    }

    return ret;
  }

  nextNum() {
    return Number(this.nextToken());
  }

  nextNumArray(n) {
    let ret = [];

    while (n--) {
      ret.push(this.nextNum());
    }

    return ret;
  }

  _write(x) {
    this.out += x;
  }

  write(x) {
    this._write(x + ' ');
  }

  writeLine(x) {
    this._write(x + '\n');
  }

  flush() {
    external_fs_default().writeFileSync(1, this.out);
    this.out = '';
  }

} // function binarySearch<T>(array: T[], pred: (a: T) => boolean) {
//   let lo = -1,
//     hi = array.length;
//   while (1 + lo < hi) {
//     const mi = lo + ((hi - lo) >> 1);
//     if (pred(array[mi])) {
//       hi = mi;
//     } else {
//       lo = mi;
//     }
//   }
//   return hi;
// }
// function lowerBound(array: number[], item: number) {
//   return binarySearch(array, (j) => item <= j);
// }
// function upperBound(array: number[], item: number) {
//   return binarySearch(array, (j) => item < j);
// }

const io = new IO();

function solve(n, queryCount, arr, queries) {
  let indexes = new Map();

  for (let i = 0; i < n; i++) {
    let a = arr[i];

    if (!indexes.has(a)) {
      indexes.set(a, []);
    }

    indexes.get(a).push(i);
  }

  const randoms = Array.from({
    length: 30
  }).map(_ => Math.round(Math.random() * 1e9));

  for (let q of queries) {
    let l = q[0] - 1;
    let r = q[1] - 1;
    let max = 0;

    for (let rand of randoms) {
      let checkIdx = rand % (r - l + 1) + l;
      let check = arr[checkIdx];
      let checkIndexes = indexes.get(check);
      let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);

      if (current > max) {
        max = current;
      }
    }

    let c = r - l + 1;
    const ret = Math.max(2 * max - c, 1);
    io.writeLine(ret);
  }
}

function main() {
  try {
    let n = io.nextNum();
    let queryCount = io.nextNum();
    let arr = io.nextNumArray(n);
    let queries = [];

    for (let i = 0; i < queryCount; i++) {
      queries.push([io.nextNum(), io.nextNum()]);
    }

    solve(n, queryCount, arr, queries);
    io.flush();
  } catch (e) {
    console.log(e.stack);
  }
}

main();

})();

/******/ })()
;

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ 747:
/***/ ((module) => {

module.exports = require("fs");;

/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/************************************************************************/
/******/ 	/* webpack/runtime/compat get default export */
/******/ 	(() => {
/******/ 		// getDefaultExport function for compatibility with non-harmony modules
/******/ 		__webpack_require__.n = (module) => {
/******/ 			var getter = module && module.__esModule ?
/******/ 				() => (module['default']) :
/******/ 				() => (module);
/******/ 			__webpack_require__.d(getter, { a: getter });
/******/ 			return getter;
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {

// EXTERNAL MODULE: external "fs"
var external_fs_ = __webpack_require__(747);
var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_);
;// CONCATENATED MODULE: ./src/lib/binarySearch.ts
function binarySearch(array, pred) {
  let lo = -1,
      hi = array.length;

  while (lo + 1 < hi) {
    const mi = lo + (hi - lo >> 1); // const mi = (hi + lo) >> 1;
    // console.log({ lo, hi, mi });

    if (pred(array[mi])) {
      hi = mi;
    } else {
      lo = mi;
    } // console.log('after', { lo, hi, mi });
    // console.log();

  }

  return hi;
}
function lowerBound(array, item) {
  return binarySearch(array, v => v >= item);
}
function upperBound(array, item) {
  return binarySearch(array, v => v > item);
} // console.log(lowerBound([10, 12, 12, 12, 13], 12)); // 1
// console.log(upperBound([10, 12, 12, 12, 13], 12)); // 4
// console.log(lowerBound([10, 12, 12, 12, 13], 9));
// -1 : 5
// 0   1   2   3   4
// 10, 12, 12, 12, 13
// m = 2
// -1 : 2
// m = 0
// 0 : 2
// m = 1
// 0 : 1
;// CONCATENATED MODULE: ./src/cf-1514/1514-d.ts

 //////////// LIBS

class IO {
  constructor() {
    this.i = 0;
    this.text = void 0;
    this.out = '';
    this.text = __webpack_require__(747).readFileSync(0, 'utf8');
  }

  nextToken() {
    let ret = '';

    while (this.text[this.i].charCodeAt(0) <= 32) {
      this.i++;
    }

    while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
      ret += this.text[this.i];
      this.i++;
    }

    return ret;
  }

  nextNum() {
    return Number(this.nextToken());
  }

  nextNumArray(n) {
    let ret = [];

    while (n--) {
      ret.push(this.nextNum());
    }

    return ret;
  }

  _write(x) {
    this.out += x;
  }

  write(x) {
    this._write(x + ' ');
  }

  writeLine(x) {
    this._write(x + '\n');
  }

  flush() {
    external_fs_default().writeFileSync(1, this.out);
    this.out = '';
  }

} // function binarySearch<T>(array: T[], pred: (a: T) => boolean) {
//   let lo = -1,
//     hi = array.length;
//   while (1 + lo < hi) {
//     const mi = lo + ((hi - lo) >> 1);
//     if (pred(array[mi])) {
//       hi = mi;
//     } else {
//       lo = mi;
//     }
//   }
//   return hi;
// }
// function lowerBound(array: number[], item: number) {
//   return binarySearch(array, (j) => item <= j);
// }
// function upperBound(array: number[], item: number) {
//   return binarySearch(array, (j) => item < j);
// }

const io = new IO();

function solve(n, queryCount, arr, queries) {
  let indexes = new Map();

  for (let i = 0; i < n; i++) {
    let a = arr[i];

    if (!indexes.has(a)) {
      indexes.set(a, []);
    }

    indexes.get(a).push(i);
  }

  const randoms = Array.from({
    length: 30
  }).map(_ => Math.round(Math.random() * 1e9));

  for (let q of queries) {
    let l = q[0] - 1;
    let r = q[1] - 1;
    let max = 0;

    for (let rand of randoms) {
      let checkIdx = rand % (r - l + 1) + l;
      let check = arr[checkIdx];
      let checkIndexes = indexes.get(check);
      let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);

      if (current > max) {
        max = current;
      }
    }

    let c = r - l + 1;
    const ret = Math.max(2 * max - c, 1);
    io.writeLine(ret);
  }
}

function main() {
  try {
    let n = io.nextNum();
    let queryCount = io.nextNum();
    let arr = io.nextNumArray(n);
    let queries = [];

    for (let i = 0; i < queryCount; i++) {
      queries.push([io.nextNum(), io.nextNum()]);
    }

    solve(n, queryCount, arr, queries);
    io.flush();
  } catch (e) {
    console.log(e.stack);
  }
}

main();

})();

/******/ })()
;

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ 747:
/***/ ((module) => {

module.exports = require("fs");;

/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/************************************************************************/
/******/ 	/* webpack/runtime/compat get default export */
/******/ 	(() => {
/******/ 		// getDefaultExport function for compatibility with non-harmony modules
/******/ 		__webpack_require__.n = (module) => {
/******/ 			var getter = module && module.__esModule ?
/******/ 				() => (module['default']) :
/******/ 				() => (module);
/******/ 			__webpack_require__.d(getter, { a: getter });
/******/ 			return getter;
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(747);
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_0__);
 // import { lowerBound, upperBound } from '../lib/binarySearch';
//////////// LIBS

class IO {
  constructor() {
    this.i = 0;
    this.text = void 0;
    this.out = '';
    this.text = __webpack_require__(747).readFileSync(0, 'utf8');
  }

  nextToken() {
    let ret = '';

    while (this.text[this.i].charCodeAt(0) <= 32) {
      this.i++;
    }

    while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
      ret += this.text[this.i];
      this.i++;
    }

    return ret;
  }

  nextNum() {
    return Number(this.nextToken());
  }

  nextNumArray(n) {
    let ret = [];

    while (n--) {
      ret.push(this.nextNum());
    }

    return ret;
  }

  _write(x) {
    this.out += x;
  }

  write(x) {
    this._write(x + ' ');
  }

  writeLine(x) {
    this._write(x + '\n');
  }

  flush() {
    fs__WEBPACK_IMPORTED_MODULE_0___default().writeFileSync(1, this.out);
    this.out = '';
  }

}

function binarySearch(array, pred) {
  let lo = -1,
      hi = array.length;

  while (1 + lo < hi) {
    const mi = lo + (hi - lo >> 1);

    if (pred(array[mi])) {
      hi = mi;
    } else {
      lo = mi;
    }
  }

  return hi;
}

function lowerBound(array, item) {
  return binarySearch(array, j => item <= j);
}

function upperBound(array, item) {
  return binarySearch(array, j => item < j);
}

const io = new IO();

function solve(n, queryCount, arr, queries) {
  let indexes = new Map();

  for (let i = 0; i < n; i++) {
    let a = arr[i];

    if (!indexes.has(a)) {
      indexes.set(a, []);
    }

    indexes.get(a).push(i);
  }

  const randoms = Array.from({
    length: 30
  }).map(_ => Math.round(Math.random() * 1e9));

  for (let q of queries) {
    let l = q[0] - 1;
    let r = q[1] - 1;
    let max = 0;

    for (let rand of randoms) {
      let checkIdx = rand % (r - l + 1) + l;
      let check = arr[checkIdx];
      let checkIndexes = indexes.get(check);
      let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);

      if (current > max) {
        max = current;
      }
    }

    let c = r - l + 1;
    const ret = Math.max(2 * max - c, 1);
    io.writeLine(ret);
  }
}

function main() {
  try {
    let n = io.nextNum();
    let queryCount = io.nextNum();
    let arr = io.nextNumArray(n);
    let queries = [];

    for (let i = 0; i < queryCount; i++) {
      queries.push([io.nextNum(), io.nextNum()]);
    }

    solve(n, queryCount, arr, queries);
    io.flush();
  } catch (e) {
    console.log(e.stack);
  }
}

main();

})();

/******/ })()
;

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
// let indexes: Map<number, number[]> = new Map();
let indexes = [];
function count(v, l, r) {
    let arr = indexes[v];
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r
// ol: number,
// or: number
) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return count(tree[node], ol, or);
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm), ol, or);
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r, ol, or);
    // return Math.max(val1, val2);
    let en = tr;
    let st = tl;
    if (en < l || st > r) {
        return 0;
    }
    if (l <= st && en <= r) {
        return count(tree[node], l, r);
    }
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes[a]) {
            indexes[a] = [];
        }
        indexes[a].push(i);
        // if (!indexes.has(a)) {
        //   indexes.set(a, []);
        // }
        // indexes.get(a)!.push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = query(1, 0, n - 1, l, r, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
// let indexes: Map<number, number[]> = new Map();
let indexes = [];
function count(v, l, r) {
    let arr = indexes[v];
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r, ol, or) {
    if (l > r) {
        // console.log({ node, tl, tr, l, r });
        return 0;
    }
    if (tl === l && tr === r) {
        // console.log({ node, tl, tr, l, r });
        return count(tree[node], ol, or);
    }
    let tm = (tl + tr) >> 1;
    // console.log({ node, tl, tr, l, r, tm });
    let val1 = query(2 * node, tl, tm, l, Math.min(r, tm), ol, or);
    let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r, ol, or);
    return Math.max(val1, val2);
    // let en = tr;
    // let st = tl;
    // if (en < l || st > r || r < l) return 0;
    // if (l <= st && en <= r) return count(tree[node], l, r);
    // let mid = (st + en) >> 1;
    // return Math.max(
    //   query(2 * node, st, mid, l, r),
    //   query(2 * node + 1, mid + 1, en, l, r)
    // );
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes[a]) {
            indexes[a] = [];
        }
        indexes[a].push(i);
        // if (!indexes.has(a)) {
        //   indexes.set(a, []);
        // }
        // indexes.get(a)!.push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = query(1, 0, n - 1, l, r, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
// let indexes: Map<number, number[]> = new Map();
let indexes = [];
function count(v, l, r) {
    let arr = indexes[v];
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes[a]) {
            indexes[a] = [];
        }
        indexes[a].push(i);
        // if (!indexes.has(a)) {
        //   indexes.set(a, []);
        // }
        // indexes.get(a)!.push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = query(1, 0, n - 1, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = query(1, 0, n - 1, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = query(1, 0, n - 1, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        tree = Array.from({ length: n * 4 + 1 });
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        str += ret + '\n';
    }
    fs_1.default.writeFileSync(1, str);
}
function main() {
    try {
        const io = new IO();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    fs_1.default.writeFileSync(1, str);
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            if (checkIndexes == null) {
                console.log({
                    l,
                    r,
                    check,
                    checkIndexes,
                    checkIdx,
                });
                throw new Error('a');
            }
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    fs_1.default.writeFileSync(1, str, { encoding: 'ascii' });
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            if (checkIndexes == null) {
                console.log({
                    l,
                    r,
                    check,
                    checkIndexes,
                    checkIdx,
                });
                throw new Error('a');
            }
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        out.writeNum(ret);
        // str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    out.done();
    // fs.writeFileSync(1, str);
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(20 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            if (checkIndexes == null) {
                console.log({
                    l,
                    r,
                    check,
                    checkIndexes,
                    checkIdx,
                });
                throw new Error('a');
            }
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        out.writeNum(ret);
        // str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    out.done();
    // fs.writeFileSync(1, str);
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            if (checkIndexes == null) {
                console.log({
                    l,
                    r,
                    check,
                    checkIndexes,
                    checkIdx,
                });
                throw new Error('a');
            }
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        str += ret + '\n';
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    fs_1.default.writeFileSync(1, str);
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let val = query(1, 0, n - 1, l, r);
        let max = count(val, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        tree = Array.from({ length: n * 4 + 1 });
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) >> 1;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let val = query(1, 0, n - 1, l, r);
        let max = count(val, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return;
    }
    let m = (tl + tr) >> 1;
    build(arr, 2 * node, tl, m);
    build(arr, 2 * node + 1, m + 1, tr);
    let val1 = tree[2 * node];
    let val2 = tree[2 * node + 1];
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) / 2;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let val = query(1, 0, n - 1, l, r);
        let max = count(val, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return tree[node];
    }
    let m = (tl + tr) >> 1;
    let val1 = build(arr, 2 * node, tl, m);
    let val2 = build(arr, 2 * node + 1, m + 1, tr);
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
    return tree[node];
}
function query(node, tl, tr, l, r) {
    // if (l > r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return 0;
    // }
    // if (tl === l && tr === r) {
    //   // console.log({ node, tl, tr, l, r });
    //   return tree[node];
    // }
    // let tm = (tl + tr) >> 1;
    // // console.log({ node, tl, tr, l, r, tm });
    // let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    // let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    // if (count(val1, l, r) > count(val2, l, r)) {
    //   return val1;
    // } else {
    //   return val2;
    // }
    let en = tr;
    let st = tl;
    if (en < l || st > r || r < l)
        return 0;
    if (l <= st && en <= r)
        return count(tree[node], l, r);
    let mid = (st + en) / 2;
    return Math.max(query(2 * node, st, mid, l, r), query(2 * node + 1, mid + 1, en, l, r));
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let val = query(1, 0, n - 1, l, r);
        let max = count(val, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO {
    constructor() {
        this.i = 0;
        this.out = '';
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
    _write(x) {
        this.out += x;
    }
    write(x) {
        this._write(x + ' ');
    }
    writeLine(x) {
        this._write(x + '\n');
    }
    flush() {
        fs_1.default.writeFileSync(1, this.out);
        this.out = '';
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
const io = new IO();
let tree = [];
let indexes = new Map();
function count(v, l, r) {
    let arr = indexes.get(v);
    if (!arr) {
        return 0;
    }
    return upperBound(arr, r) - lowerBound(arr, l);
}
function build(arr, node, tl, tr) {
    if (tl === tr) {
        tree[node] = arr[tl];
        return tree[node];
    }
    let m = (tl + tr) >> 1;
    let val1 = build(arr, 2 * node, tl, m);
    let val2 = build(arr, 2 * node + 1, m + 1, tr);
    if (count(val1, tl, tr) > count(val2, tl, tr)) {
        tree[node] = val1;
    }
    else {
        tree[node] = val2;
    }
    return tree[node];
}
function query(node, tl, tr, l, r) {
    if (l > r) {
        // console.log({ node, tl, tr, l, r });
        return 0;
    }
    if (tl === l && tr === r) {
        // console.log({ node, tl, tr, l, r });
        return tree[node];
    }
    let tm = (tl + tr) >> 1;
    // console.log({ node, tl, tr, l, r, tm });
    let val1 = query(2 * node, tl, tm, l, Math.min(r, tm));
    let val2 = query(2 * node + 1, tm + 1, tr, Math.max(l, tm + 1), r);
    if (count(val1, l, r) > count(val2, l, r)) {
        return val1;
    }
    else {
        return val2;
    }
}
function solve(n, queryCount, arr, queries) {
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    build(arr, 1, 0, n - 1);
    if (n === 300000) {
        console.log('X');
        return;
    }
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        let val = query(1, 0, n - 1, l, r);
        let max = count(val, l, r);
        let c = r - l + 1;
        const ret = Math.max(2 * max - c, 1);
        io.writeLine(ret);
    }
}
function main() {
    try {
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        solve(n, queryCount, arr, queries);
        io.flush();
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    if (n === 300000) {
        queries.pop();
    }
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    if (n === 300000) {
        let start = Date.now();
        fs_1.default.writeFileSync(1, str);
        console.log(Date.now() - start);
    }
    else {
        fs_1.default.writeFileSync(1, str);
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    if (n === 300000) {
        queries.unshift();
    }
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    if (n === 300000) {
        let start = Date.now();
        fs_1.default.writeFileSync(1, str);
        console.log(Date.now() - start);
    }
    else {
        fs_1.default.writeFileSync(1, str);
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    if (n === 300000) {
        let start = Date.now();
        fs_1.default.writeFileSync(1, str);
        console.log(Date.now() - start);
    }
    else {
        fs_1.default.writeFileSync(1, str);
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    if (n === 300000) {
        let start = Date.now();
        fs_1.default.writeFileSync(1, str);
        console.log('X', Date.now() - start);
    }
    else {
        fs_1.default.writeFileSync(1, str);
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
class Output {
    constructor() {
        this.buffer = Buffer.allocUnsafe(10 * 1024 * 1024);
        this.size = 0;
    }
    writeNum(num) {
        let str = num.toString() + '\n';
        this.buffer.write(str, this.size, 'ascii');
        this.size += str.length;
    }
    done() {
        fs_1.default.writeFileSync(1, this.buffer.slice(0, this.size), { encoding: 'ascii' });
    }
}
let start = Date.now();
function solve(n, queryCount, arr, queries) {
    let out = new Output();
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    let str = '';
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l + 1)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        const ret = Math.max(2 * max - c, 1);
        // out.writeNum(ret);
        str += ret + '\n';
        // let buf = Buffer.from([]);
        // buf.write()
        // fs.writeFileSync(1, ret.toString() + '\n');
        // console.log();
    }
    // out.done();
    if (n === 300000) {
        console.log('X', Date.now() - start);
    }
    else {
        fs_1.default.writeFileSync(1, str);
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            if (checkIndexes == null) {
                console.log({
                    l,
                    r,
                    check,
                    checkIndexes,
                    checkIdx,
                });
                throw new Error('a');
            }
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        console.log(Math.max(2 * max - c, 1));
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import crypto from 'crypto';
const assert_1 = __importDefault(require("assert"));
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            assert_1.default(checkIndexes, 'checkIndexes ' + check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        console.log(Math.max(2 * max - c, 1));
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
// import crypto from 'crypto';
Object.defineProperty(exports, "__esModule", { value: true });
//////////// LIBS
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        console.log(Math.max(2 * max - c, 1));
    }
}
function main() {
    try {
        let start = Date.now();
        const io = new IO2();
        let n = io.nextNum();
        let queryCount = io.nextNum();
        let arr = io.nextNumArray(n);
        let queries = [];
        for (let i = 0; i < queryCount; i++) {
            queries.push([io.nextNum(), io.nextNum()]);
        }
        // if (n === 300000) {
        //   console.log('X', Date.now() - start);
        //   return;
        // }
        solve(n, queryCount, arr, queries);
        // let t = io.nextNum();
        // while (t--) {
        //   let n = io.nextNum();
        //   let str = io.nextLine();
        //   solve();
        // }}
    }
    catch (e) {
        console.log(e.stack);
    }
}
main();

"use strict";
// import crypto from 'crypto';
Object.defineProperty(exports, "__esModule", { value: true });
//////////// LIBS
class IO {
    constructor() {
        this.lines = [];
        this.tokens = [];
        this.lines = require('fs').readFileSync(0, 'utf8').split('\n');
    }
    getTokens() {
        if (!this.tokens.length) {
            this.tokens = this.nextLine().split(' ');
        }
        return this.tokens;
    }
    nextLine() {
        return this.lines.shift().trim();
    }
    nextNum() {
        return Number(this.getTokens().shift());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
class IO2 {
    constructor() {
        this.i = 0;
        this.text = require('fs').readFileSync(0, 'utf8');
    }
    nextToken() {
        let ret = '';
        while (this.text[this.i].charCodeAt(0) <= 32) {
            this.i++;
        }
        while (this.i < this.text.length && this.text[this.i].charCodeAt(0) > 32) {
            ret += this.text[this.i];
            this.i++;
        }
        return ret;
    }
    // nextLine() {
    //   return this.lines.shift()!.trim();
    // }
    nextNum() {
        return Number(this.nextToken());
    }
    nextNumArray(n) {
        let ret = [];
        while (n--) {
            ret.push(this.nextNum());
        }
        return ret;
    }
}
function binarySearch(array, pred) {
    let lo = -1, hi = array.length;
    while (1 + lo < hi) {
        const mi = lo + ((hi - lo) >> 1);
        if (pred(array[mi])) {
            hi = mi;
        }
        else {
            lo = mi;
        }
    }
    return hi;
}
function lowerBound(array, item) {
    return binarySearch(array, (j) => item <= j);
}
function upperBound(array, item) {
    return binarySearch(array, (j) => item < j);
}
function solve(n, queryCount, arr, queries) {
    let indexes = new Map();
    for (let i = 0; i < n; i++) {
        let a = arr[i];
        if (!indexes.has(a)) {
            indexes.set(a, []);
        }
        indexes.get(a).push(i);
    }
    const randoms = Array.from({ length: 30 }).map((_) => Math.round(Math.random() * 1e9));
    // console.log(indexes);
    for (let q of queries) {
        let l = q[0] - 1;
        let r = q[1] - 1;
        // console.log({ l, r });
        let max = 0;
        for (let rand of randoms) {
            let checkIdx = (rand % (r - l)) + l;
            let check = arr[checkIdx];
            // console.log({ l, r, checkIdx, check });
            let checkIndexes = indexes.get(check);
            let current = upperBound(checkIndexes, r) - lowerBound(checkIndexes, l);
            // console.log({ check, current });
            if (current > max) {
                max = current;
            }
        }
        let c = r - l + 1;
        // console.log({ max, c });
        console.log(Math.max(2 * max - c, 1));
    }
}
function main() {
    let start = Date.now();
    const io = new IO2();
    let n = io.nextNum();
    let queryCount = io.nextNum();
    let arr = io.nextNumArray(n);
    let queries = [];
    for (let i = 0; i < queryCount; i++) {
        queries.push([io.nextNum(), io.nextNum()]);
    }
    if (n === 300000) {
        console.log('X', Date.now() - start);
        return;
    }
    solve(n, queryCount, arr, queries);
    // let t = io.nextNum();
    // while (t--) {
    //   let n = io.nextNum();
    //   let str = io.nextLine();
    //   solve();
    // }
}
main();