2 votes 2 votes Shivansh Gupta asked Jan 22, 2018 Shivansh Gupta 275 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply MiNiPanda commented Jan 22, 2018 reply Follow Share I am not getting the correct answer. But why can't we just sort the array, say arr, having n elements of set S in O(nlogn) time and then find the values at index K1,K2..Kr. For that we need to go through the K set once. Pick up each element and print arr[Ki]. This would take O(r) as there are r elements in K set. So O(nlogn) + O(r) = O(nlogn+r) The highest value of Ki can't be more than the number of elements in n. Because if there are 200 elements(n) is a set, we should not be asked to find the 201st smallest element. Max range of Ki can be from i=0 to i=n-1. Assuming that Ki does not repeat in the set, no. of Ki can be maximum 'n' i.e. max(r)=n. Hence, O(nlogn+r) becomes O(nlogn). Do you have the solution? Please share if yes. 0 votes 0 votes Shivansh Gupta commented Jan 22, 2018 reply Follow Share Also thought same thing, don't know how they gave r logn . there is no explanation given. 0 votes 0 votes Please log in or register to add a comment.