Given a sorted array A of n elements. We are given n queries of the type [l, r] where l and r are the indices. 0 <= l , r < n In each query, we have to find out the mode in the range [l,r] both inclusive. We can use no more than n * log(n) memory and the solution should run in n * log(n) time.
Can we do it in given constraints on memory and time?









No.
Assume if there are many modes we can give any one of them.
Let's assume A=[0,0,0,1,1...] (easier to index them in segment tree), for each number store their beginning and end(**), count the frequency and build a segment tree with maximum frequency(and store the frequency). (takes n log n space and time)
For every query [l,r], query the segment tree the number with maximum count within value $$$v_i$$$ and $$$v_r$$$. If the answer obtained is not $$$v_i$$$ or $$$v_r$$$ then you are finish. Else make another query within value $$$v_i+1$$$ and $$$v_r-1$$$, and compare with the the frequency of $$$v_i$$$ and $$$v_r$$$ inside [l,r] (O(1) as you have **) and output your answer.
We can also use a sparse table to find the answer. Each element of the sparse table $$$sparse[i][j]$$$ is the pair $$$(a,b)$$$ where $$$a$$$ is the mode of the range $$$j \ldots j+2^i-1$$$ and $$$b$$$ is the number of occurrences of $$$a$$$ in the range.
When we merge two elements of the sparse table $$$sparse[i-1][j]$$$ and $$$sparse[i-1][j+2^{i-1}]$$$ to get $$$sparse[i][j]$$$ we can first take the pair with the maximum $$$b$$$ as the answer. Then we can find the number of occurrences of the value $$$A[j+2^{i-1}]$$$ in the range $$$[j,j+2^i-1]$$$ and update the answer if this number is the mode.
To count the number of occurrences of a number in a range you can create two arrays $$$pre$$$ and $$$suf$$$ where $$$pre[i]$$$ denotes the number of elements that have a value of $$$A[i]$$$ with index at most $$$i$$$ and $$$suf[i]$$$ denotes the the number of elements with a value of $$$A[i]$$$ that have an index of at least $$$i$$$. If you have a range $$$l..r$$$, where $$$l \leq i \leq r$$$, the number of occurrences of $$$A[i]$$$ is $$$min(i-l+1, pre[i]) + min(r-i+1, suf[i])-1$$$.
Combining two elements of a sparse table to find the answer for a query is very similar to how they were combined to create the sparse table. This gives $$$O(nlogn)$$$ complexity for memory and time and an $$$O(1)$$$ complexity per query.
...how is that correct?
The logic is very similar to the second comment. Since the array is sorted, the mode of an array will create a segment. So if the mode of the range $$$[l,r]$$$ is $$$x$$$, $$$[l,r]$$$ looks like $$$\ldots, x, x, \ldots, x, \ldots$$$. My logic basically states that if we combine $$$[l,m]$$$ and $$$[m+1,r]$$$ to get $$$[l,r]$$$, the segment of $$$x$$$s is either fully contained in the $$$[l,m]$$$ segment, the $$$[m+1,r]$$$ segment or goes between both segments. If it goes between both segments, it forms some segment $$$[l_2, r_2]$$$ with $$$l_2 \leq m \lt r_2$$$. So if we just check the length of this segment, we can combine it with the answers for the two sub-intervals to find the answer.
That's exactly problem from XXI POJ: Link. If you allow randomization then there exists even O(n+q) solution.
In this task array is not sorted.
Can you elaborate it more, please?
I can solve it only in O(logn) per query and O(n) preprocessing (indices[x] is all i such that p[i] = x) using randomization (pick, say, 6 random elements, count frequency of each using binary search).
UPD: I realised that 6 is too few. If we pick k elements, then the probability that we won't find dominant company is $$$1 - (1/2)^k$$$, so we have to choose $$$k \gt log_2(q)$$$. And this solution turns into too slow.
I also came up with O(nlogn + qlogn) idea without randomization, but it uses segment tree with fractional cascading. It seems to be too complicated.
Persistent segtree? Insert values one-by-one, from left to right. The segtree keeps track of the counts of each value (which you compress beforehand). It's a max segtree. Not sure if this is much easier than fractional cascading though...
You can find count of x in [l; r] using the persistent segtree quite fast. However, how do you find the dominant value? I can't think of how to find it using the "max" property of your segtree.
My idea was in splitting queries into O(logn) segments. The query dominant value is dominant in one of them. So I have mistaken in deterministic complexity, it should be $$$O(nlogn + qlog^2n)$$$ due to checking O(logn) elements in O(logn) time.
OK, in fact it can be reduced to $$$O(nlogn + qlogn)$$$. The idea is that we do not need to examine all the segments a query is splitted to. The four biggest segments cover strictly more than $$$3/4$$$ of the query. Thus it's enough to check only first and second maximums in the four biggest segments. But $$$O(nlogn + 8qlogn)$$$ doesn't sound fast.
Regarding my approach, I see my mistake; max isn't invertible so you can't use persistent segtree to query a time range the way you can with operations like sum.
If I understand your idea correctly, it's to look at the individual segments in the segtree that are used to compute the range result, get the mode in each, and see how frequently that mode appears in the other segments? I don't think that works, because the overall mode may not be the mode in any segment. Like the segments could have [1, 2, 2], [1, 3, 3], [1, 4, 4], [1, 5, 5] (these segments aren't the right lengths but you get the point) where the overall mode is 1 but it's not the mode in any segment.
Yeah, but the key point is the problem asks to find dominant value. It appears at least $$$L/2$$$ times on the query segment (what is of length $$$L$$$).
Got it, yeah the $$$8q\log n$$$ makes sense then. I didn't read the linked problem carefully and just thought it was the unsorted form of the question on this page.
This is nothing like that problem. That problem asks for majoritary element in range, this one asks for the most frequent element in range. These are two very different statements.
Oh, you’re right. I’ve misunderstood the question.
If I understood the problem corrently, given that the array is sorted, then equal elements appear in a contiguous range. How about doing Divide & Conquer / Segment tree to solve this problem, and when you combine two answers you also take into account the frequency of the element in the middle if
a[m] == a[m + 1]?