1,117 views
5 votes
5 votes

You are given a linked list, $L$ of $n$ integers, and another linked list, $P$, of $k$ integers where $k <<< n$. $L$ is sorted in ascending order but $P$ is not necessarily sorted. The operation $\text{print_lots}(L,P)$ will print the elements in $L$ that are in positions specified by $P$. For instance, if $P = 2, 1, 3, 6$, the second, first, third, and sixth elements in $L$ are printed in the same order. If you write the routine function $\text{print_lots}(L,P)$, the best running time of your routine function will be (assume $O(n)$ auxiliary space is available)  ______________ ?

  1. $\Theta(n)$
  2. $\Theta(n \log k)$
  3. $\Theta(n k)$
  4. $\Theta (n k\log k)$

3 Answers

Best answer
6 votes
6 votes

It looks O(NK) ?

But, it is given that k<<<N and auxiliary space of O(N) is there.

So, sort P array in O(K log K) and copy/store the order of P array in an auxiliary array, in which we have to print the elements of L.

Hence, in one traversal of head pointer throughout the list, this can be done.

Total Time Complexity = O(K Log K)  +  O(K)  + O(N) = O(N) as k<<<<N

selected by
6 votes
6 votes
We are given with the linked list $L$ which contains $n$ elements. It is also mentioned that we have $\theta (n)$ space.

One thing that we can do is, traverse the whole list $L$, and save it to an array of size $n$, hence using that auxiliary space of $\theta (n)$. This will take $\theta (n)$ time.

Now traverse the list $P$ having $k$ elements, and for each element in $P$ print the element in that index in our array.

Say $P$ is $2,1,6$. Then we start traversing $P$, first is $2$, so we print $2nd$ element of our array, then $1st$ element, and then $6th$ element. And so on. This will take $\theta (k)$.

The whole procedure will take  $\theta (n) + \theta (k)$, ie $ = \theta (n)$. Since, $k<<<n$.
0 votes
0 votes
For each entry in P we have to search Linked List (L).
No of entries in P is 'k' and average search time in Linked List is O(n).

Total time is k*O(n) i.e. O(kn).
Sir why answer is 'n' why not 'nk' ?
Answer:

Related questions

1.6k
views
2 answers
4 votes
Arjun asked Oct 10, 2016
1,631 views
For a singly linked list where each node has a pointer to a data array as well as a next pointer, what would be the worst case time complexity to delete an intermediate node $x$ (given a pointer to it)?$O(n)$O(1)$\Omega(n)$O(\log n)$
588
views
1 answers
2 votes
Arjun asked Oct 10, 2016
588 views
The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 ... ,72,1,4,3,6,5,71,3,2,5,4,7,62,3,4,5,6,7,1
837
views
1 answers
4 votes
Arjun asked Oct 10, 2016
837 views
In a Network where bytes are continuously being transferred, it is required to identify the most frequently transferred byte. What would be an appropriate data structure for finding this?Linked ListArrayDynamically growing ArraySet
936
views
2 answers
1 votes
Arjun asked Oct 10, 2016
936 views
Which of the following is false?Arrays are better than linked lists for sorting due to better data locality.Asymptotic time complxity for FindMax is same on an ... a fixed maximum size, a circular queue is preferrable to a normal queue