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.
Problem statement Given an array A [ 1 : n ] = [ a 1 , a 2 , . . . , a n ] {\displaystyle A[1:n]=[a_{1},a_{2},...,a_{n}]} , we wish to answer queries of the form m o d e ( A , i : j ) {\displaystyle mode(A,i:j)} , where 1 ≤ i ≤ j ≤ n {\displaystyle 1\leq i\leq j\leq n} . The mode m o d e ( S ) {\displaystyle mode(S)} of any array S = [ s 1 , s 2 , . . . , s k ] {\displaystyle S=[s_{1},s_{2},...,s_{k}]} is an element s i {\displaystyle s_{i}} such that the frequency of s i {\displaystyle s_{i}} is greater than or equal to the frequency of s j ∀ j ∈ { 1 , . . . , k } {\displaystyle s_{j}\;\forall j\in \{1,...,k\}} . For example, if S = [ 1 , 2 , 4 , 2 , 3 , 4 , 2 ] {\displaystyle S=[1,2,4,2,3,4,2]} , then m o d e ( S ) = 2 {\displaystyle mode(S)=2} because it occurs three times, while all other values occur fewer times. In this problem, the queries ask for the mode of subarrays of the form A [ i : j ] = [ a i , a i + 1 , . . . , a j ] {\displaystyle A[i:j]=[a_{i},a_{i+1},...,a_{j}]} .
Theorem 1 Let A {\displaystyle A} and B {\displaystyle B} be any multisets. If c {\displaystyle c} is a mode of A ∪ B {\displaystyle A\cup B} and c ∉ A {\displaystyle c\notin A} , then c {\displaystyle c} is a mode of B {\displaystyle B} .
Proof Let c ∉ A {\displaystyle c\notin A} be a mode of C = A ∪ B {\displaystyle C=A\cup B} and f c {\displaystyle f_{c}} be its frequency in C {\displaystyle C} . Suppose that c {\displaystyle c} is not a mode of B {\displaystyle B} . Thus, there exists an element b {\displaystyle b} with frequency f b {\displaystyle f_{b}} that is the mode of B {\displaystyle B} . Since b {\displaystyle b} is the mode of B {\displaystyle B} and that c ∉ A {\displaystyle c\notin A} , then f b > f c {\displaystyle f_{b}>f_{c}} . Thus, b {\displaystyle b} should be the mode of C {\displaystyle C} which is a contradiction.
Results
… excerpt ends here. Continue reading the full article.
