encyclopegia.org

Range mode query

In data structures, the range mode query problem asks to build a data structure on some input data to efficiently answer queries asking for the mode of any consecutive subset of the input.[src]

In data structures, the range mode query problem asks to build a data structure on some input data to efficiently answer queries asking for the mode of any consecutive subset of the input.[src] ==Problem statement== Given an array A[1:n] = [a_1,a_2,...,a_n] , we wish to answer queries of the form mode(A, i:j) , where 1 \leq i \leq j \leq n .[src] The mode mode(S) of any array S = [s_1,s_2,...,s_k] is an element s_i such that the frequency of s_i is greater than or equal to the frequency of s_j \; \forall j \in \{1,...,k\} .[src] For example, if S = [1,2,4,2,3,4,2] , then mode(S) = 2 because it occurs three times, while all other values occur fewer times.[src] In this problem, the queries ask for the mode of subarrays of the form A[i:j] = [a_i,a_{i+1},...,a_j] .[src] ===Theorem 1=== Let A and B be any multisets.[src] If c is a mode of A \cup B and c \notin A , then c is a mode of B .[src] ===Proof=== Let c \notin A be a mode of C = A \cup B and f_c be its frequency in C .[src] Suppose that c is not a mode of B .[src] Thus, there exists an element b with frequency f_b that is the mode of B .[src] Since b is the mode of B and that c \notin A , then f_b > f_c .[src] Thus, b should be the mode of C which is a contradiction.[src] ==Results== ==Lower bound== Any data structure using S cells of w bits each needs \Omega\left(\frac{\log n}{\log (S w/n)}\right) time to answer a range mode query.[src] This contrasts with other range query problems, such as the range minimum query which have solutions offering constant time query time and linear space.[1] This is due to the hardness of the mode problem, since even if we know the mode of A[i:j] and the mode of A[j+1:k] , there is no simple way of computing the mode of A[i:k] .[src] Any element of A[i:j] or A[j+1:k] could be the mode.[src] For example, if mode(A[i:j]) = a and its frequency is f_a , and mode(A[j+1:k]) = b and its frequency is also f_a , there could be an element c with frequency f_a-1 in A[i:j] and frequency f_a-1 in A[j+1:k] .[src] a \not= c \not= b , but its frequency in A[i:k] is greater than the frequency of a and b , which makes c a better candidate for mode(A[i:k]) than a or b .[src] ==Linear space data structure with square root query time== This method by Chan et al.[1] uses O(n + s^2) space and O(n/s) query time.[src] By setting s=\sqrt{n} , we get O(n) and O(\sqrt{n}) bounds for space and query time.[src] ===Preprocessing=== Let A[1:n] be an array, and D[1:\Delta] be an array that contains the distinct values of A, where \Delta is the number of distinct elements.[src] We define B[1:n] to be an array such that, for each i , B[i] contains the rank (position) of A[i] in D .[src] Arrays B,D can be created by a linear scan of A .[src] Arrays Q_1, Q_2, ..., Q_\Delta are also created, such that, for each a \in \{1,...,\Delta\} , Q_a = \{b\; |\; B[b] = a\} .[src] We then create an array B'[1:n] , such that, for all b \in \{1,...,n\} , B'[b] contains the rank of b in Q_{B[b]} .[src] Again, a linear scan of B suffices to create arrays Q_1,Q_2,...,Q_\Delta and B' .[src] It is now possible to answer queries of the form "is the frequency of B[i] in B[i:j] at least q " in constant time, by checking whether Q_{B[i]}[B'[i]+q-1] \leq j .[src] The array is split B into s blocks b_1,b_2,...,b_s , each of size t=\lceil n/s\rceil .[src] Thus, a block b_i spans over B[i\cdot t+1 : (i+1)t] .[src] The mode and the frequency of each block or set of consecutive blocks will be pre-computed in two tables S and S' .[src] S[b_i,b_j] is the mode of b_i \cup b_{i+1} \cup ...[src] \cup b_j , or equivalently, the mode of B[b_it+1 : (b_j+1) t] , and S' stores the corresponding frequency.[src] These two tables can be stored in O(s^2) space, and can be populated in O(s\cdot n) by scanning B s times, computing a row of S,S' each time with the following algorithm: algorithm computeS_Sprime is input: Array B = [0:n - 1], Array D = [0:Delta - 1], Integer s output: Tables S and Sprime let S ← Table(0:n - 1, 0:n - 1) let Sprime ← Table(0:n - 1, 0:n - 1) let firstOccurence ← Array(0:Delta - 1) for all i in {0, ..., Delta - 1} do firstOccurence[i] ← -1 end for for i ← 0:s - 1 do let j ← i × t let c ← 0 let fc ← 0 let noBlock ← i let block_start ← j let block_end ← min{(i + 1) × t - 1, n - 1} while j B .[src] This can be translated to an answer over A , since for any a,i,j , B[a] is a mode for B[i:j] if and only if A[a] is a mode for A[i:j] .[src] We can convert an answer for B to an answer for A in constant time by looking in A or B at the corresponding index.[src] Given a query mode(B,i,j) , the query is split in three parts: the prefix, the span and the suffix.[src] Let b_i=\lceil (i-1)/t\rceil and b_j = \lfloor j/t \rfloor -1 .[src] These denote the indices of the first and last block that are completely contained in B .[src] The range of these blocks is called the span.[src] The prefix is then B[i:min\{b_i t,j\}] (the set of indices before the span), and the suffix is B[max\{(b_j+1)t+1,i\}:j] (the set of indices after the span).[src] The prefix, suffix or span can be empty, the latter is if b_j .[src] For the span, the mode c is already stored in S[b_i,b_j] .[src] Let f_c be the frequency of the mode, which is stored in S'[b_i,b_j] .[src] If the span is empty, let f_c=0 .[src] Recall that, by Theorem 1, the mode of B[i:j] is either an element of the prefix, span or suffix.[src] A linear scan is performed over each element in the prefix and in the suffix to check if its frequency is greater than the current candidate c , in which case c and f_c are updated to the new value.[src] At the end of the scan, c contains the mode of B[i:j] and f_c its frequency.[src] ====Scanning procedure==== The procedure is similar for both prefix and suffix, so it suffice to run this procedure for both: Let x be the index of the current element.[src] There are three cases: If Q_{B[x]}[B'[x]-1] \geq i , then it was present in B[i:x-1] and its frequency has already been counted.[src] Pass to the next element.[src] Otherwise, check if the frequency of B[x] in B[i:j] is at least f_c (this can be done in constant time since it is the equivalent of checking it for B[x:j] ).[src] If it is not, then pass to the next element.[src] If it is, then compute the actual frequency f_x of B[x] in B[i:j] by a linear scan (starting at index B'[x]+f_c-1 ) or a binary search in Q_{B[x]} .[src] Set c:= B[x] and f_c := f_x .[src] This linear scan (excluding the frequency computations) is bounded by the block size t , since neither the prefix or the suffix can be greater than t .[src] A further analysis of the linear scans done for frequency computations shows that it is also bounded by the block size.[src] Thus, the query time is O(t) = O(n/s) .[src] ==Subquadratic space data structure with constant query time== This method by uses O\left(\frac{n^2 \log{\log{n}}}{\log{n}}\right) space for a constant time query.[1] We can observe that, if a constant query time is desired, this is a better solution than the one proposed by Chan et al., as the latter gives a space of O(n^2) for constant query time if s=n .[1] ===Preprocessing=== Let A[1:n] be an array.[src] The preprocessing is done in three steps: Split the array A in s blocks b_1,b_2,...,b_s , where the size of each block is t=\lceil n/s \rceil .[src] Build a table S of size s \times s where S[i,j] is the mode of b_i \cup b_{i+1} \cup ...[src] \cup b_j .[src] The total space for this step is O(s^2) For any query mode(A,i,j) , let b_{i'} be the block that contains i and b_{j'} be the block that contains j .[src] Let the span be the set of blocks completely contained in A[i:j] .[src] The mode c of the block can be retrieved from S .[src] By Theorem 1, the mode can be either an element of the prefix (indices of A[i:j] before the start of the span), an element of the suffix (indices of A[i:j] after the end of the span), or c .[src] The size of the prefix plus the size of the suffix is bounded by 2t , thus the position of the mode isstored as an integer ranging from 0 to 2t , where [0:2t-1] indicates a position in the prefix/suffix and 2t indicates that the mode is the mode of the span.[src] There are \binom{t}{2} possible queries involving blocks b_{i'} and b_{j'} , so these values are stored in a table of size t^2 .[src] Furthermore, there are (2t+1)^{t^2} such tables, so the total space required for this step is O(t^2 (2t+1)^{t^2}) .[src] To access those tables, a pointer is added in addition to the mode in the table S for each pair of blocks.[src] To handle queries mode(A,i,j) where i and j are in the same block, all such solutions are precomputed.[src] There are O(st^2) of them, they are stored in a three dimensional table T of this size.[src] The total space used by this data structure is O(s^2 + t^2(2t+1)^{t^2} + st^2) , which reduces to O\left(\frac{n^2 \log{\log{n}}}{\log{n}}\right) if we take t=\sqrt{\log{n}/\log{\log{n}}} .[1] ===Query=== Given a query mode(A,i,j) , check if it is completely contained inside a block, in which case the answer is stored in table T .[src] If the query spans exactly one or more blocks, then the answer is found in table S .[src] Otherwise, use the pointer stored in table S at position S[b_{i'},b_{j'}] , where b_{i'},b_{j'} are the indices of the blocks that contain respectively i and j , to find the table U_{b_{i'},b_{j'}} that contains the positions of the mode for these blocks and use the position to find the mode in A .[src] This can be done in constant time.[src] ==References==[src]

81 sentences · 81 cited inline · 2 references stored

References

  1. Chan, Timothy M. Linear-Space Data Structur . · retrieved 2026-08-09 · rev 1368611043
    Cite this reference

    MLA "Chan, Timothy M. Linear-Space Data Structur ." Chan, Timothy M. 2026-08-09. https://en.wikipedia.org/w/index.php?oldid=1368611043. Accessed 25 November 2026.

    Chicago "Chan, Timothy M. Linear-Space Data Structur ." Chan, Timothy M. 2026-08-09. https://en.wikipedia.org/w/index.php?oldid=1368611043.

    APA Chan, Timothy M. (2026). Chan, Timothy M. Linear-Space Data Structur . Retrieved November 25, 2026, from https://en.wikipedia.org/w/index.php?oldid=1368611043

  2. - | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2 || |- | O(n^{2-2\epsilon}) || O(n^\epsilon \log n) || 0\leq \epsilon\leq 1/2 . · retrieved 2026-08-09 · rev 1368611043
    Cite this reference

    MLA "- | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2 || |- | O(n^{2-2\epsilon}) || O(n^\epsilon \log n) || 0\leq \epsilon\leq 1/2 ." - | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2. 2026-08-09. https://en.wikipedia.org/w/index.php?oldid=1368611043. Accessed 25 November 2026.

    Chicago "- | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2 || |- | O(n^{2-2\epsilon}) || O(n^\epsilon \log n) || 0\leq \epsilon\leq 1/2 ." - | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2. 2026-08-09. https://en.wikipedia.org/w/index.php?oldid=1368611043.

    APA - | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2. (2026). - | O(n^2\log\log n/ \log n) || O(1) || || |- |- | O(n^{2-2\epsilon}/ \log n) || O(n^\epsilon) || 0\leq \epsilon\leq 1/2 || |- | O(n^{2-2\epsilon}) || O(n^\epsilon \log n) || 0\leq \epsilon\leq 1/2 . Retrieved November 25, 2026, from https://en.wikipedia.org/w/index.php?oldid=1368611043

Cite this article

MLA

"Range mode query." encyclopegia.org, 2026, https://encyclopegia.org/wiki/range-mode-query. Accessed 25 November 2026.

Chicago

"Range mode query." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/range-mode-query.

APA

encyclopegia.org. (2026). Range mode query. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/range-mode-query

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Orange Range

stub · references available

Moka Range

stub · references available

God mode

stub · references available

The Range Rider

stub · references available

ARAG (Range)

stub · references available

Abakan Range

stub · references available

hearing range

stub · references available

tripes à la mode de Caen

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1368611043 · 0.5 · snapshot enwiki-001368611043

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.