Indexing and Encoding Arrays for Element Distinctness Queries
Abstract
We introduce the data structure variant of the well-known element distinctness problem. Given an array of elements, the goal is to preprocess the array into a data structure that supports queries asking whether all elements within a given query range are distinct. This has applications in text indexing and possibly also in other algorithmic domains.
In the indexing model (where access to the input array is allowed), we design a data structure using bits and answering queries in the time needed to solve an online element distinctness instance of size , for any . As a concrete instantiation of this, there exists an index that answers queries in time using bits of additional space.
Moving to the encoding model (where access to the input array is not allowed), we begin by proving an information-theoretic lower bound for the space usage of bits, and then design a matching encoding with time queries. We then consider the case in which the alphabet size is constant. In this setting, the lower bound can be refined to bits, where . This lower bound is matched by an encoding with time queries.
Keywords and phrases:
element distinctness, range queries, lower bounds, succinct data structuresFunding:
Filippo Lari: Supported by the project “Enhancing Compression and Search Capabilities in the Software Heritage Archive”, grant #G-2025-25193 (https://sloan.org), funded by the Alfred P. Sloan Foundation.Copyright and License:
2012 ACM Subject Classification:
Theory of computation Data structures design and analysisEditors:
Philip Bille and Nicola PrezzaSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Given an array of elements drawn from an alphabet , let denote the subarray of from position to . A range query , for some (and possibly additional parameters “”), is any function whose value depends only on the subarray (and on the additional parameters, if any). Prominent examples of range queries include: range minimum queries, defined as , which return the position of the minimum element in when is totally ordered [8]; rank queries, defined as for some , which count the occurrences of the symbol in [14]; and range mode queries, which return the most frequent element in [17]. Many other examples have been studied in the literature [24]. We are interested in the data structure perspective of range queries, i.e., given a static array and a function (or simply in the following), the goal is to build a compact data structure that allows us to answer queries efficiently.
We introduce a new type of query which, despite its fundamental nature, to the best of our knowledge appears not to have been previously studied from the data structure perspective: element distinctness queries. Formally, a query returns true if all elements in are distinct, and false otherwise.
In an algorithmic setting, where the task is to detect whether an array contains any duplicate elements, this is a well-studied problem; see [25] for recent progress in that context.
We argue that it is worth considering this problem in the array query setting. When is the document array [20] of a text collection, an index returns the suffix array interval corresponding to a given pattern . Asking whether occurs exactly once in each text then corresponds precisely to a query, which could, for example, provide insight into the biological function of a queried DNA fragment.
There are two models of range query problems [6], depending on whether the input array is available at query time (indexing model) or not (encoding model). In the indexing model, some space for the data structure can potentially be saved, as the query algorithm may compensate for missing information by consulting when answering the queries. However, since access to is required at query time, the total space usage, which is bits, can never be sublinear in the space needed to store itself.
This contrasts with the encoding model, where the data structure must be constructed so that queries can be answered without consulting . Any encoding data structure is automatically also an indexing data structure. Conversely, an indexing data structure can always be converted to an encoding data structure by storing together with a copy of . Consequently, distinguishing between the two models is meaningful only if there exist indexing data structures that use less space than the best encoding data structures, and if encoding data structures can achieve space usage sublinear in .
As for range minimum queries [8], we show that this is indeed the case for All-Distinct-queries. Specifically, in Sect. 2 we prove the following result, which, for the sake of generality, is stated in terms of parameters and denoting the time and space complexity of online algorithms for the element distinctness problem:
Theorem 1.
Let be an array of elements drawn from a totally ordered set of size and . Assuming that the element distinctness problem on an -sized instance can be solved in time using bits of additional space, there exists an indexing data structure using bits of space and answering All-Distinct queries in time and bits of working space. The data structure can be constructed in time and bits of additional working space.
Let us give two concrete instantiations of the theorem for the choice of , which yields an index using bits of space. First, assuming that , we can solve the element distinctness problem using counting sort in time and bits of working space, which does not exceed the space of the index. Second, without any restriction on , one can use comparison based sorting to solve element distinctness queries in time using only a negligible amount bits of working space. We refer to [25] and references therein for other space-time trade-offs for the element distinctness problem. Using comparison sort, we can actually set the block size to any to achieve sublinear space and query time. For instance, with for a positive constant integer , where denotes iterations of the logarithm, the space of the index is bits, and the query time is .
Moving to the encoding model, in Sect. 3, we begin by showing the following result:
Theorem 2.
Let be an array of elements drawn from a totally ordered set of size . There exists an encoding data structure answering All-Distinct queries in time while using bits of space, which is asymptotically optimal. The data structure can be constructed in time and bits of additional working space.
We then consider restricted alphabet sizes, where we refine Thm. 2 as follows:
Theorem 3.
Let , and be an array of elements drawn from a totally ordered set of constant size . There exists an encoding data structure using bits of space and answering All-Distinct queries in time, which is asymptotically optimal. The data structure can be constructed in time using bits of working space.
Interpreting the term as a function of , we observe that , therefore , and hence the leading term in the space usage of Thm. 3 is never larger than bits. Some special cases for small alphabet sizes ( can be computed explicitly as , , and , respectively. Interestingly, these are the same values for range minimum queries on restricted alphabets [16].
Finally, we note that all our results hold in the transdichotomous word-RAM model, where the word size satisfies , and arithmetic as well as bitwise operations on bits are performed in constant time. Additionally, our results also require the alphabet to be linearly sortable (e.g., characters drawn from a universe of size ). This assumption is necessary to compactify the alphabet to in linear time; otherwise, we would violate the natural lower bound for duplicate detection in in the comparison model [18].
2 Indexing
In the indexing version of the All-Distinct problem, the data structure is allowed to access the input array while answering queries. In this section, we design an index whose space usage and query time can be tuned based on an input parameter . To this end, we first introduce two sequences that form the basis of our solution:
Definition 4.
Let be an array of elements drawn from a totally ordered set of size . For every , let and .
Both and are monotone, with being non-decreasing and being non-increasing. Moreover, by the pigeonhole principle, any range of length greater than must contain at least one duplicate. It follows that for each , we have and similarly .
Constructing either of these two sequences can be easily done in time and bits of space. We describe the procedure for , but the same idea applies to the construction of . We compute for all in increasing order, starting with . We maintain an auxiliary array , initialized with negative values, where stores the index of the last occurrence of symbol in the current range . When computing , we can inductively assume that has already been computed. If , we set to , as the new symbol does not induce a duplicate in . Otherwise, we set to , which is the minimal index such that is not repeated in . In all cases, we update to .
Having access to either or is already sufficient to answer any query in time: because of Def. 4, the answer is false if , and true otherwise (equivalently, one may check whether ). However, even though and can certainly be stored in fewer than bits, it is impossible to use less than (roughly) a linear number of bits without having access to (see Sect. 3 for details). Therefore, we proceed to show how this can be achieved in the indexing setting.
Our index is obtained by first partitioning the input array into blocks of size . For each , we sample the array at position , i.e., the last element of the -th block, and store the resulting value in a separate array . Similarly, for the same values of , we sample the array at position , i.e., the first element of the -th block, storing these values in another array . After collecting these samples, the full arrays and are discarded, and only their sampled versions are used to answer queries.
and both require bits each if stored plainly, but we can save space using ideas similar to Elias-Fano codes [21, §3.4.3]: Let us now focus on , as the same considerations apply to . For each , we compute the integer division , and store the quotient in , and the remainder in . is again monotonically increasing, with each , hence by encoding the differences between consecutive elements in negated unary and augmenting the resulting binary sequence with support [15, 13] we can access each element of in time while using bits of space. is stored plainly using bits, and supports time access. Each can then be reconstructed in time as . Overall, this encoding for both and requires bits of space.
Answering queries with our index is straightforward. Given a query , we first split the range into three parts: , , and , where is the maximal subrange of that perfectly aligns with full blocks of size in . Using the same idea as for answering queries with either or , we check in constant time whether the aligned portion contains any duplicates by verifying if or . If this test fails, the answer to the query is immediately false. However, this is insufficient to guarantee uniqueness over the entire range . Indeed, an element in may also appear in , and such duplicates are not yet detected by simply verifying the previous condition. Therefore, must be checked for duplicates, which is an -sized instance of the element distinctness problem.
Construction.
The data structure can be easily constructed in time by running the previously sketched algorithm only on the sampled positions of , and directly storing the values in (via bit vector appends) and (via simple array writes). Augmenting with support for operations can be done in-place in time linear in the size of the bit vector. Symmetrically, the same procedure applies to all arrays based on , which completes the proof of Thm. 1.
3 Encoding
We now consider All-Distinct queries in the encoding model, in which the underlying array cannot be accessed at query time. We first consider the general case, developing upper and lower bounds that are independent of the alphabet size (Thm. 2). We then focus on restricted alphabets, where tighter bounds can be obtained (Thm. 3).
3.1 Unbounded Alphabet
The following easy lemma forms the basis of the lower bound in Thm. 2.
Lemma 5.
Let and be two arrays of length , each drawn from a totally ordered set of size . For all , we have if and only if .
Proof.
Let us assume that , otherwise the lemma immediately follows. We prove the two implications by contradiction. () Suppose that for all , but . Let be the smallest index such that , and assume without loss of generality that (the other case is symmetric). Then while , which is a contradiction. () Suppose that , but there exists a range such that . Without loss of generality, assume . This implies that there exists a such that . Since , it must be that , which in turn means , a contradiction.
Lemma 5 implies that the number of equivalence classes of the All-Distinct problem corresponds to the number of possible -arrays as in Def. 4. From a simple information-theoretic argument, taking the logarithm of such a quantity yields the lower bound of Thm. 2. To perform such a counting, we begin by introducing the following definition:
Definition 6.
The delta sequence of is defined by setting and, for each , .
Notice that, because of Def. 4, for each . The following result is already formulated more generally to make it also applicable to restricted alphabet sizes (in this section, it should be interpreted with ):
Lemma 7.
The set of arrays computed from any array of elements drawn from a totally ordered set of size is in bijection with the set of ordered trees with nodes and height at most .
Proof.
We prove this by giving a bijection with balanced parenthesis sequences of length having maximum excess , which in turn gives the desired bijection. Given , we construct the sequence as follows: Initially, is empty. Then, for all in increasing order, append closed parentheses and a single open parenthesis to . Finish by appending closed parentheses and enclose everything in another pair of open and closed parentheses.
It is easy to see that has length and is balanced. The latter follows because, for any position , if we denote by the step in which its parenthesis is generated, then (there are open parentheses before ), and . Using a similar argument, we also obtain that, for any position , , which is at most by Def. 4. If , then . Otherwise, if , then .
It remains to prove that our mapping is bijective. Surjectivity follows easily, since any sequence of balanced parentheses allows us to construct and hence . Injectivity can be proved by contradiction, in a way similar to the proof of Lemma 5.
From Lemma 7, the number of different -arrays for is given by the -th Catalan number , which implies the following:
Lemma 8.
Let be an array of elements drawn from a totally ordered set of size . Any encoding data structure for supporting All-Distinct queries must use at least bits of space.
We are now left with designing a data structure that uses space close to the lower bound of Lemma 8 while still providing efficient queries. As already mentioned in Sect. 2, if we have access to , answering any query reduces to checking whether . Since is a monotone sequence satisfying for all , we can store it succinctly by encoding the -values in negated unary encoding in a bit vector , i.e., . has ones and zeros, and since by Def. 4, , the overall size of is at most bits. We conclude by noticing that , and that the operation can be supported in time by only adding bits on top of [15, 13].
Construction.
3.2 Restricted Alphabet
In this section, we consider the All-Distinct problem under a restricted alphabet (i.e., ). We first prove a lower bound that explicitly depends on , providing a refinement of Lemma 8 for this setting. We then complement this result by designing an encoding whose space usage approaches the lower bound while still supporting query time.
For the special cases or , the lower bound of Lemma 8 can be surpassed. It suffices to store the input array directly (after compactifying the alphabet), using bits for or bits for . Queries of length at most (or , respectively) can then be answered by scanning the corresponding elements of , while longer queries must necessarily contain a duplicate. However, already for , this simple approach cannot use fewer than bits. Nevertheless, a tighter bound can still be achieved.
3.2.1 Lower Bound
Lemma 9.
Let be an array of elements drawn from a totally ordered set of constant size . For large enough , any encoding data structure for supporting All-Distinct queries must use at least bits of space, where .
Proof.
Exploiting Lemmas 7 and 5, the number of equivalence classes for the All-Distinct problem on a restricted alphabet is given by the number of ordered trees having nodes and maximum height at most . Let us denote with this number. De Bruijn et al. [3] already studied this combinatorial problem, giving the following asymptotic equivalence for any constant :
The asymptotic equivalence means . In our case, this implies that, for large enough , with an hidden constant arbitrarily close to . Observing that for all , and recalling that on , we have . Using this inequality, and letting , we obtain the following lower bound on , from which our claim follows:
3.2.2 Upper Bound
We show how to represent in space as close as possible to the lower bound of Lemma 9, while still allowing arbitrary entries to be retrieved in constant time. All-Distinct queries are then answered as in Sect. 2, hence checking whether . Our approach follows the idea of the -bit indexing data structure for range minimum queries [8]. Specifically, we partition the array into sufficiently small blocks, assign a type to each block that uniquely identifies it among all possible blocks, and use a universal lookup table that stores precomputed answers to queries within a block. A running example is provided in Fig. 1.
Notice that, due to the bijection with ordered trees of bounded height of Lemma 7, a natural approach to proving Thm. 3 would be based on tree covering [4] or, possibly, on hypersuccinct trees [19], in the spirit of [16]. In Appendix B, we sketch such an alternative. However, its space has a larger second-order term than Thm. 3, and, more importantly, we are not aware of a space-efficient construction algorithm analogous to that in Thm. 3.
Given an array , we partition into consecutive blocks of elements each. For every such block, we sample its first value and store it relative to its position in a separate array (i.e., for the -th block , which is always bounded by ). The key idea is that, once the first value of a block is known, all the values within that block can be reconstructed from the corresponding portion of the delta sequence , without using . Indeed, for each position , if we let be its corresponding block and its starting position in , then:
| (1) |
Notice that, because of the samples, the first value of each block is not used when reconstructing an entry of . In Fig. 1, to recover the value of in position , we access the sample array in position , then, using , we compute .
Therefore, to efficiently compute Eq. 1, we use the following precomputed tables:
-
, which stores, for each block of size , an integer identifying its type. For all , we have if and only if for all .
-
, which stores, for each possible block type and every position within such a block, in the sum of deltas up to that position starting from the second element of the block, with the first entries always set to zero.
Having access to those tables, Eq. 1 can be computed in time as follows:
| (2) |
with being the block number of . It remains to show how to assign types to blocks based on their sequence, and that the combined space usage of , , and approaches that of Lemma 9.
Computing the Block Types
We begin by describing an algorithm for assigning types to blocks. To this end, it is convenient to interpret the delta sequence as a two-dimensional lattice path in the Cartesian plane going from to and consisting only of East and North steps (i.e., and ). Such a path can be obtained using the same idea we exploited to prove the bijection with balanced parenthesis sequences (see Lemma 7). For in increasing order, perform North steps followed by a single East step. Terminate by appending North steps. Notice that, because of Def. 4, the number of North steps is never more than the number of East steps. Consequently, the path never goes above the main diagonal and therefore corresponds to a Dyck path [10, p.76]. Moreover, the vertical distance between the path and the main diagonal (i.e., the difference between the number of East and North steps) is bounded by . See Fig. 1 as an example.
Each block of size in naturally corresponds to a Dyck path fragment confined within a parallelogram of base and height . As an example, see the region delimited by the points , , , and in Fig. 1, corresponding to the second block of in the example. This holds for every block except the first one, but we can conceptually regard it as having a parallelogram extending below the -axis, with its path starting at its leftmost top vertex and always beginning with North steps. Notice, however, that due to Eq. 1, we are only concerned with paths of length , since the first value in each block is not used.
Exploiting this reduction to Dyck paths, we are now left with the problem of assigning the same identifier to blocks corresponding to the same path. To this end, we begin by defining the following (conceptual) graph . See Fig. 2 for an example with and ; ignore the dotted part in the lower left corner.
Definition 10.
Graph has nodes for all and all . Each node with has an outgoing edge to if , and an outgoing edge to if .
We then assign to each node a value representing the number of distinct paths starting at and reaching the terminal node (i.e., the only node without outgoing edges, shown as the top rightmost vertex in Fig. 2). These values are stored in a table of elements, such that stores the number of paths from node to , with ’s in cells not corresponding to any node. By exploiting the fact that there are at most two options for the next step from (either first going North or first going East), the following formula allows us to precompute the -array in time:
| (3) |
In Fig. 2, the non-zero -values are shown next to each node in . Note that the numbers from Sect. 3.2.1 appear on the upper diagonal of .
We proceed by describing an algorithm for computing the type of the -th block, that is, for the sequence of deltas (remember that the first delta of each block is ignored). As before, this sequence of deltas can be seen as a path in , starting at for some height , and ending at for some . We begin by normalizing the path by shifting it downward as much as possible, by simply computing the minimum vertical distance between the path and the lower boundary, subtracting this value from every point along the path. This prevents assigning different codes to paths having the same shape but different starting heights.
To ensure each path starts at , we modify it by prepending North steps. We then assign the codes as follows: initialize a variable code to , and each time the path moves from to via a North step, increment code by the number of paths that would be skipped if the path were to take an East step to ; that is, by if such a node exists, or otherwise. Lastly, we note that, following this numbering scheme, paths starting from different nodes may be assigned the same code. As an example, see the paths corresponding to the second and fourth blocks in Fig. 2, which would both get code 86, although the delta sequence of the blocks is different. To still ensure a unique code for different blocks, we prepend the binary encoding of the starting height to code using bits to obtain the final block type. See Alg. 1 for a complete, formal description of our block type assignment procedure, which runs in time, since all paths together contain at most North and East steps.
It remains to prove that the algorithm we just described is a valid numbering scheme, that is, it never assigns the same code to two blocks having the same starting height and representing different normalized Dyck path fragments. In the following, we sketch the main idea of the proof and defer the complete formal argument to Appendix A.
Lemma 11.
Let be the table produced by Alg. 1. For all indices , if then for all , .
Proof Sketch.
We proceed by contradiction. Suppose there exist such that , but for some , . This difference then translates to a difference in the corresponding Dyck path fragments of the two blocks.
Let and be the paths corresponding to blocks and , and let be the first node at which they differ. Moreover, without loss of generality, assume that at the path takes a North step while takes an East step.
The key idea of our proof is that, starting from , the minimum possible code that can be assigned to the remaining suffix of is strictly larger than the maximum possible code that can be assigned to the remaining suffix of . This can be proved by induction after characterizing such quantities. Overall, this implies that the code assigned to differs from the code assigned to , contradicting the assumption that .
Space Analysis
To show that the space usage of our encoding approaches that of Lemma 9, we introduce the following definition:
Definition 12.
Let and be positive integers with . We denote by the number of distinct normalized paths in from any to for all .
Ideally, we would like to analyze the sizes of and in terms of , but unfortunately, we are not aware of a closed formula for , and neither for the numbers .
However, we can still derive an upper bound as follows. Consider again the conceptual graph from Def. 10: As depicted by the dotted part in Fig. 2, we can extend to the left by adding levels according to the same construction rule, and only keeping the nodes lying at the same height and above node . If we consider all paths in the extended graph using only North and East steps and running from its leftmost node to its top rightmost node, these paths are in bijection with Dyck paths of length and maximum height , which are counted by . Moreover, for every path and every entry height in the original graph, there exists at least one such path in the extended graph that starts at its leftmost node and reaches that height (see the example in Fig. 2). Consequently, , and the space usage in bits of the type table can be bounded as follows.
The space usage in bits for the table storing the prefix sums can be bounded as follows:
Choosing the block size as for an arbitrarily small constant makes bits. The sample array requires bits; hence, the overall size of the encoding is bits. Queries are answered in time as shown in Eq. 2 using only and .
Construction.
Table can be computed optimally in time while using bits of space, and is only needed at construction time. The type table is filled using Alg. 1 in time. Moreover, the same algorithm can be modified easily to operate block-wise (i.e., computing and the resulting -sequence only when the -th block is processed), it never uses more than bits of working space. Lastly, the relevant entries of can be computed on-the-fly in no more than bits of space and time. Overall, we proved Thm. 3.
4 Conclusions
We introduced a new type of range query on arrays, with potential applications in text indexing as well as other algorithmic realms. Our algorithms build on techniques that are inspired by range minimum queries [7, 8], but needed substantial new ideas and adaptations to work for All-Distinct. We note that in cases where an All-Distinct query returns a negative answer, our algorithms can be easily modified to report the position of a duplicate in the query range, if desired. Future research directions include: proving a space-time trade-off lower bound in the indexing model [1]; extending the techniques to higher dimensional arrays [2]; considering upper and lower bounds for random instances [19]; compressing the data structures [9, 12]; learning from the distribution of the data [5]; or exploring relaxed variants, such as parameterized queries that return true if the query range contains at most repeated elements, with specified either at preprocessing or at query time. So far, we have considered only the simplest case of queries testing the distinctness of values in a range. More challenging problems include deciding whether a unique element exists in the query range, as well as counting or reporting distinct or repeated elements, similarly to [11].
References
- [1] Gerth Stølting Brodal, Pooya Davoodi, Moshe Lewenstein, Rajeev Raman, and Srinivasa Rao Satti. Two dimensional range minimum queries and fibonacci lattices. Theor. Comput. Sci., 638:33–43, 2016. doi:10.1016/J.TCS.2016.02.016.
- [2] Gerth Stølting Brodal, Pooya Davoodi, and S. Srinivasa Rao. On space efficient two dimensional range minimum data structures. Algorithmica, 63(4):815–830, 2012. doi:10.1007/S00453-011-9499-0.
- [3] N.G. de Bruijn, D.E. Knuth, and S.O. Rice. The average height of planted plane trees. In Ronald C. Read, editor, Graph Theory and Computing, pages 15–22. Academic Press, 1972. doi:10.1016/B978-1-4832-3187-7.50007-6.
- [4] Arash Farzan and J. Ian Munro. A uniform paradigm to succinctly encode various families of trees. Algorithmica, 68(1):16–40, 2014. doi:10.1007/S00453-012-9664-0.
- [5] Paolo Ferragina and Filippo Lari. FL-RMQ: A learned approach to range minimum queries. In Paola Bonizzoni and Veli Mäkinen, editors, 36th Annual Symposium on Combinatorial Pattern Matching, CPM 2025, June 17-19, 2025, Milan, Italy, volume 331 of LIPIcs, pages 7:1–7:23. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2025. doi:10.4230/LIPIcs.CPM.2025.7.
- [6] Johannes Fischer. Compressed range minimum queries. In Encyclopedia of Algorithms, pages 379–382. Springer, 2016. doi:10.1007/978-1-4939-2864-4_640.
- [7] Johannes Fischer and Volker Heun. Finding range minima in the middle: Approximations and applications. Math. Comput. Sci., 3(1):17–30, 2010. doi:10.1007/s11786-009-0007-8.
- [8] Johannes Fischer and Volker Heun. Space-efficient preprocessing schemes for range minimum queries on static arrays. SIAM J. Comput., 40(2):465–492, 2011. doi:10.1137/090779759.
- [9] Johannes Fischer, Volker Heun, and Horst Martin Stühler. Practical entropy-bounded schemes for o(1)-range minimum queries. In 2008 Data Compression Conference (DCC 2008), 25-27 March 2008, Snowbird, UT, USA, pages 272–281. IEEE Computer Society, 2008. doi:10.1109/DCC.2008.45.
- [10] Philippe Flajolet and Robert Sedgewick. Analytic Combinatorics. Cambridge University Press, 2009. URL: http://www.cambridge.org/uk/catalogue/catalogue.asp?isbn=9780521898065.
- [11] Travis Gagie, Juha Kärkkäinen, Gonzalo Navarro, and Simon J. Puglisi. Colored range queries and document retrieval. Theor. Comput. Sci., 483:36–50, 2013. doi:10.1016/J.TCS.2012.08.004.
- [12] Pawel Gawrychowski, Seungbum Jo, Shay Mozes, and Oren Weimann. Compressed range minimum queries. Theor. Comput. Sci., 812:39–48, 2020. doi:10.1016/J.TCS.2019.07.002.
- [13] Alexander Golynski. Optimal lower bounds for rank and select indexes. Theor. Comput. Sci., 387(3):348–359, 2007. doi:10.1016/J.TCS.2007.07.041.
- [14] Roberto Grossi. Wavelet trees. In Encyclopedia of Algorithms, pages 2355–2359. Springer, 2016. doi:10.1007/978-1-4939-2864-4_642.
- [15] Guy Jacobson. Space-efficient static trees and graphs. In 30th Annual Symposium on Foundations of Computer Science, Research Triangle Park, North Carolina, USA, 30 October - 1 November 1989, pages 549–554. IEEE Computer Society, 1989. doi:10.1109/SFCS.1989.63533.
- [16] Seungbum Jo and Srinivasa Rao Satti. Encodings for range minimum queries over bounded alphabets. Theor. Comput. Sci., 1070:115824, 2026. doi:10.1016/J.TCS.2026.115824.
- [17] Danny Krizanc, Pat Morin, and Michiel H. M. Smid. Range mode and range median queries on lists and trees. Nord. J. Comput., 12(1):1–17, 2005.
- [18] Anna Lubiw and András Rácz. A lower bound for the integer element distinctness problem. Inf. Comput., 94(1):83–92, 1991. doi:10.1016/0890-5401(91)90034-Y.
- [19] J. Ian Munro, Patrick K. Nicholson, Louisa Seelbach Benkner, and Sebastian Wild. Hypersuccinct trees - new universal tree source codes for optimal compressed tree data structures and range minima. In Petra Mutzel, Rasmus Pagh, and Grzegorz Herman, editors, 29th Annual European Symposium on Algorithms, ESA 2021, September 6-8, 2021, Lisbon, Portugal (Virtual Conference), volume 204 of LIPIcs, pages 70:1–70:18. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPIcs.ESA.2021.70.
- [20] Gonzalo Navarro. Spaces, trees, and colors: The algorithmic landscape of document retrieval on sequences. ACM Comput. Surv., 46(4):52:1–52:47, 2013. doi:10.1145/2535933.
- [21] Gonzalo Navarro. Compact Data Structures – A Practical Approach. Cambridge University Press, 2016. URL: http://www.cambridge.org/de/academic/subjects/computer-science/algorithmics-complexity-computer-algebra-and-computational-g/compact-data-structures-practical-approach?format=HB.
- [22] Mihai Pătraşcu. Succincter. In 49th Annual IEEE Symposium on Foundations of Computer Science, FOCS 2008, Philadelphia, PA, USA, October 25-28, 2008, pages 305–313. IEEE Computer Society, 2008. doi:10.1109/FOCS.2008.83.
- [23] Rajeev Raman, Venkatesh Raman, and Srinivasa Rao Satti. Succinct indexable dictionaries with applications to encoding k-ary trees, prefix sums and multisets. ACM Trans. Algorithms, 3(4):43, 2007. doi:10.1145/1290672.1290680.
- [24] Matthew Skala. Array range queries. In Andrej Brodnik, Alejandro López-Ortiz, Venkatesh Raman, and Alfredo Viola, editors, Space-Efficient Data Structures, Streams, and Algorithms – Papers in Honor of J. Ian Munro on the Occasion of His 66th Birthday, volume 8066 of Lecture Notes in Computer Science, pages 333–350. Springer, 2013. doi:10.1007/978-3-642-40273-9_21.
- [25] Ivor van der Hoog, Eva Rotenberg, and Daniel Rutschmann. Tight better-than-worst-case bounds for element distinctness and set intersection, 2025. arXiv:2511.02954.
Appendix A Proof of Lemma 11
To simplify the proof of Lemma 11, we introduce two auxiliary lemmas. Their proofs proceed by induction on the cells of , following the reverse sum order: for any two pairs of coordinates and , if and only if . Under this ordering, the smallest coordinate is , while the largest is . Intuitively, this ordering is more suited for the indexing of the -array (see Figure 2). Finally, for ease of presentation, throughout the following proofs, we assume that for every or .
Lemma 13.
For every and , .
Proof.
The base case immediately follows from Def. 3. For the inductive case, consider a generic coordinate , and assume the predicate holds for all . Then:
Lemma 14.
For every and , .
Proof.
The base case holds since by Def. 3, and (see Figure 2). For the inductive case, consider a generic coordinate , and assume the predicate holds for all . Then:
For ease of reference, we restate Lemma 11 here, and proceed with a formal proof based on the argument sketched in Sect. 3.2.2.
Lemma 15.
Let be the table produced by Algorithm 1. For all indices , if then for all , .
Proof.
We proceed by contradiction. Suppose there are two indices such that , but for some , . This difference then translates to a difference in the corresponding Dyck path fragments of the two blocks.
Let and be the paths corresponding to blocks and , and let be the first node at which they differ. For , let the portion of up to and including be called the prefix of , denoted as , and let the remaining part be its suffix, denoted as .
Without loss of generality, assume that at the path takes a North step while takes an East step. Note that and , otherwise these steps cannot be performed.
Since the paths coincide up to , we have . The key idea of our proof is then to show that, after the mismatched step, the largest possible code that can be assigned to is strictly smaller than the minimum possible code that can be assigned to . This gives a contradiction, because then the overall codes of the two blocks cannot be the same, and thus .
Let denote the smallest possible code for . After the initial North step from , which contributes to , this minimum is achieved by taking East steps and then following the lower diagonal of the graph until reaching node . It is immediate that, besides the initial North step, all the remaining steps do not contribute to , hence .
Similarly, let denote the largest possible code for . After the initial East step from , which does not contribute to , this maximum is achieved by first taking consecutive North steps. Each of these North steps contributes to for . Once these North steps are completed, the path reaches the upper diagonal of the graph and then follows that diagonal until it arrives at the node . Each North step performed while following the upper diagonal contributes to for . Overall, the value of is given by the following expression:
| (4) |
Finally, we obtain the following chain of inequalities relating to , from which our claim follows:
Appendix B An Alternative Encoding Based on Tree Covering
As mentioned in Sect. 1, it is possible to design an alternative encoding in the restricted alphabet setting of Sect. 3.2. This encoding is based on Lemma 7, which gives a bijection between the set of -arrays of length over an alphabet of size , and ordered trees with nodes and height at most . In particular, if we denote by the ordered tree corresponding to under this bijection for some input array , then each is related to the depth of the -th node visited in a pre-order traversal of .
Lemma 16.
Let be the ordered tree corresponding to under the bijection of Lemma 7 for some input array . For every and every node with pre-order rank in , .
Proof.
Let be the balanced parentheses sequence of obtained from through the bijection of Lemma 7. Because of the fictitious node, for every , a node with pre-order rank has its opening parenthesis in at position . In particular, there are opening parentheses and closing parentheses. The difference between these two quantities gives the depth of in . Therefore, , which implies .
The encoding is then based on the following observation. By Lemma 7, the height of , and hence the depth of each node, is bounded by . It follows that this bound also holds locally for any subtree in any partition of the nodes of into subtrees. The key idea of this second encoding is therefore to decompose into a certain number of subtrees and, for each possible subtree shape, store a small precomputed table that maps nodes to their corresponding depth within the subtree. By Lemma 16, this allows us to access the values of and ultimately answer All-Distinct queries on . To this end, we employ the following tree decomposition technique by Farzan & Munro [4].
Lemma 17 (Tree Covering [4]).
For any parameter , an ordinal tree with nodes can be decomposed, in linear time, into connected subtrees (so-called micro trees) having the following properties:
-
Each micro tree contains at most nodes.
-
Micro trees are pairwise disjoint aside from their roots.
-
Apart from edges leaving the micro tree root, at most one other edge leads to a node outside of this micro tree.
We apply Lemma 17 to using a parameter , to be fixed later, obtaining micro trees, each having a height bounded by . For each micro tree , we store the depth of its root, its size, and the pre-order rank of its root in .
The depths of the roots and their sizes can be stored respectively using and bits. As for the pre-order rank of the roots, we observe that these values form a set of elements from a universe of size ; therefore, we encode them using a Fully Indexable Dictionary (FID), e.g., [23, 22] in bits.
For each micro tree size , we store a precomputed table mapping the local pre-order rank of nodes inside the micro tree to their depth (local to the micro tree). Overall, these precomputed tables use the following space in bits:
This last quantity is . By setting for an arbitrarily small constant , the space usage of the tables becomes bits.
We then proceed by assigning a code to each micro tree of size using the same algorithm based on lattice paths of Sect. 3.2.2. In this setting, we apply Lemma 7 to convert each micro tree into its corresponding -array, which is then converted into a Dyck path. Notice that here, each such path starts from and reaches . Therefore, contrary to Sect. 3.2.2, we can avoid prepending bits to the obtained code, and we can directly use to analyze the resulting type table . Therefore, let denote the size of the -th micro tree, the space usage in bits of can be bounded as follows:
where the last inequality follows since each micro tree in the worst case can contain a duplicated root (i.e., , see Lemma 17). Hence .
Finally, it is possible to prove that by using bits data structures, indices of can be mapped to their corresponding micro tree in time [4].
Therefore, given an query, we first map to its corresponding micro tree . We then subtract from the pre-order rank of the root of to obtain the local pre-order rank of within . Using this value, together with the size and the type of , we access the corresponding precomputed table to retrieve the local depth of the node associated with . We add this local depth to the depth of the root of and, by Lemma 16, derive the value of . The query is then answered as usual by checking whether .
All the above operations are performed in time, and the overall space usage of the encoding is bits, which matches the lower bound of Thm. 9 up to lower-order terms.
We conclude by highlighting that the encoding of Thm. 3 presented in Sect. 3.2.2 has the same query time, but an asymptotically smaller lower-order term in the space usage, namely against . Moreover, to the best of our knowledge, we are not aware of a space-efficient construction algorithm for the tree covering of Lemma 17 as in Thm. 3. Lastly, the alternative encoding we presented here is not a mere black-box application of the technique by Farzan & Munro. In particular, our method for assigning types to height-restricted Dyck paths, and hence to trees of bounded height, is still needed for construction.
