Given an array of t integers we need to answer x queries.Each query describes an starting index and a ending index ,we need to print the sorted elements in the sub-array.
For Example-
Array={16,9,10,19,2}
for query 1 3 ,the answer would be
9 10 16
for query 2 5,the answer would be
2 9 10 19
Please suggest an optimal solution?Are there any advanced data structures involved?
The number of elements can be upto 10^5 .

- 13
- 4
-
All algorithms needed are implemented in the standard library, namely `std::vector` and `std::sort`. You can find references for both easily. Are these allowed to be used? If so its pretty simple. – Jan 11 '14 at 16:37
-
so here is the sorted array, `2, 9, 10, 16, 19`. query one: `9, 10, 16` makes sense. query two doesn't – Moha the almighty camel Jan 11 '14 at 16:37
-
are you allowed to sort the array ? – Moha the almighty camel Jan 11 '14 at 16:37
-
@Mhd.Tahawi I guess it should be sorted *after* getting a subset, not before. – Jan 11 '14 at 16:38
-
we need to print the sorted sub-array i.e the sorting should come later – user3092832 Jan 11 '14 at 16:39
-
I can think of a solution with n(log(n))*(queries) time.I need something better than this here – user3092832 Jan 11 '14 at 16:41
-
2@user3092832 Please include both your solution idea and the additional requirement in the question. Also is the number of queries on the order of `n`, lower or above? What are the lengths of queries? – Jan 11 '14 at 16:43
-
3@user3092832: That's the sort of requirement that _needs to go in the question_, and I don't mean 20 minutes later when we've all already spent time writing answers for you. – Lightness Races in Orbit Jan 11 '14 at 16:58
3 Answers
Tag each element with it's position:
16 1, 9 2, 10 3, 19 4, 2 5
Sort it:
2 5, 9 2, 10 3, 16 1, 19 4
For each query walk through the result and return the elements which are within the range.
After preprocessing each query takes O(N)
work.

- 94,607
- 11
- 117
- 176
-
+1: Nice, you got it. The OP threw me off by saying "the sorting has to be done afterwards" which is, of course, not true. – Lightness Races in Orbit Jan 11 '14 at 17:18
-
Are there any advanced data structures involved?
Nope, not at all. First you obtain the range given by those indexes, then you sort the resulting range. Then you print it. Seems pretty simple!
In fact, it's so simple, I'm going to show you an example:
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
template <typename T, size_t N>
size_t len(T (&)[N])
{
return N;
}
int main()
{
int array[] = {16,9,10,19,2};
const int START = 1; // user input ( 1-based index,)
const int END = 3; // user input (inclusive range)
assert((START-1) >= 0 && (START <= len(array)));
assert((END-1) >= 0 && (END <= len(array)));
// These two lines do the work.
// Everything else is just exposition.
//
// First construct a vector from the requested subrange,
// then sort that resulting vector.
//
std::vector<int> v(array+(START-1), array+END);
std::sort(std::begin(v), std::end(v));
// Output the results to console for demo.
// Uses C++ ranged-for syntax; replace with more
// verbose equivalent if required, or do something
// else with `v`.
//
for (auto elm : v) {
std::cout << elm << ' ';
}
}
// Output: 9 10 6
Live demo
You could make the code even terser (and possibly more efficient) by copying the subrange into an std::set
rather than a std::vector
, so sorting happens during insertion rather than after-the-fact. I hardly think either is ever going to be as much as O(n^2)
, contrary to your claims.
Can you do sufficient preprocessing to share information between individual queries and get your complexity down still further? I don't know. I don't think so.

- 378,754
- 76
- 643
- 1,055
-
The worst case time complexity would be O((n^2)*queries).I need to better than this here – user3092832 Jan 11 '14 at 16:49
-
-
Can there be preprocessing involved such that I can get the sorted sub array in linear time? – user3092832 Jan 11 '14 at 16:52
-
1@user3092832 There is no worst case time complexity for `std::sort` - it is [unspecified](http://stackoverflow.com/questions/4484900/what-is-the-complextity-of-sort-function-in-c-standard-library). Where did you read that? – JBentley Jan 11 '14 at 16:52
-
-
@@LightnessRacesinOrbit "Are there any advanced data structures involved? Nope, not at all" You are using advanced data structure named std::vector. – Vlad from Moscow Jan 11 '14 at 17:08
-
@VladfromMoscow When the OP used the word advanced, I think he was thinking of e.g. a data structure which performs sorting on insertion. In this context, `std::vector` isn't "advanced" compared to an array, because it does the same job. – JBentley Jan 11 '14 at 17:11
-
3@VladfromMoscow: In the UK we don't consider that advanced. If a vector is "advanced" in Moscow then fine. – Lightness Races in Orbit Jan 11 '14 at 17:13
-
std::vector is size+an external array. I wouldn't consider it "advanced". – Karoly Horvath Jan 11 '14 at 17:17
-
1@VladfromMoscow: Please don't construct strawman arguments. Some of the standard containers _are_ what I would call "advanced". I only said that vector is not one. – Lightness Races in Orbit Jan 11 '14 at 17:19
-
@Karoly Horvath It is a dynamically changing data structure. it is not an array. It looks like array only in case then its size is not changed. It is an advamced data structure that has its own methods as for example resize, reserve, erase and so on. – Vlad from Moscow Jan 11 '14 at 17:20
-
-
@VladfromMoscow: Data structures do not have methods. Data structures are structures of data. This one is a contiguous sequence of elements. Hardly advanced by any definition: in fact, it is impossible to make it simpler! The C++ type `std::vector
` is _certainly_ more advanced that the type `int[N]`, if that's what you meant. – Lightness Races in Orbit Jan 11 '14 at 17:21 -
2@VladfromMoscow It also looks like an array *when the size is changed*. It's essentially the easiest possible way to store elements; in a contiguous block of memory. BTW `std::array` also has its own methods. – Bartek Banachewicz Jan 11 '14 at 17:21
If I understood your question: Sort it.
You can use any sorting algorithm you want, Wikipedia lists a few.
Depending on which algorithm you choose, you may need extra memory / data structures.
If sorting should come later, copy the array and sort the copy. Makes more sense, than sorting over and over again for every query.

- 54,589
- 14
- 104
- 138

- 606
- 7
- 21
-
The worst case time complexity would be O((n^2)*queries).I need to better than this here – user3092832 Jan 11 '14 at 16:42