Looking for all unique elements from a large sorted array in log n time?

I have a very large sorted array. How can I count or print all the unique elements of an array?

Suppose my array is [2,3,3,3,4,6,6,7] then the output should be 2,3,4,6,7

I know to do this in n (complexity) time. But the interviewer asked me to do this at log time? Is it possible?

+4
source share
2 answers

Here is an algorithm that requires O(logn*k)where k are unique elements: -

set uniQ
int ind = 0;

do {

 uniQ.add(arr[i]);
 ind = BinSearchGreater(arr,arr[ind],ind+1);
 if(ind >= arr.length)
   break;

} while(true);


BinSearchGreater(arr,key,start_ind) : returns index of first element greater than key in subarray starting at start_ind 

: - , , . O(n*logn), , .

+6

, () [1,2,3,4,5] . , O (n). , O (log n), .

0

Source: https://habr.com/ru/post/1545191/


All Articles