Algorithm to find a duplicate entry in constant space and O(n) time

后端 未结 8 2032
我在风中等你
我在风中等你 2020-12-16 04:30

Given an array of N integer such that only one integer is repeated. Find the repeated integer in O(n) time and constant space. There is no range for the value of integers or

相关标签:
8条回答
  • 2020-12-16 04:52

    If the array isn't sorted, you can only do it in O(nlogn).

    Some approaches can be found here.

    0 讨论(0)
  • 2020-12-16 04:52

    Jun Tarui has shown that any duplicate finder using O(log n) space requires at least Ω(log n / log log n) passes, which exceeds linear time. I.e. your question is provably unsolvable even if you allow logarithmic space.

    There is an interesting algorithm by Gopalan and Radhakrishnan that finds duplicates in one pass over the input and O((log n)^3) space, which sounds like your best bet a priori.

    Radix sort has time complexity O(kn) where k > log_2 n often gets viewed as a constant, albeit a large one. You cannot implement a radix sort in constant space obviously, but you could perhaps reuse your input data's space.

    There are numerical tricks if you assume features about the numbers themselves. If almost all numbers between 1 and n are present, then simply add them up and subtract n(n+1)/2. If all the numbers are primes, you could cheat by ignoring the running time of division.

    As an aside, there is a well-known lower bound of Ω(log_2(n!)) on comparison sorting, which suggests that google might help you find lower bounds on simple problems like finding duplicates as well.

    0 讨论(0)
  • 2020-12-16 04:52

    The approach that would come closest to O(N) in time is probably a conventional hash table, where the hash entries are simply the numbers, used as keys. You'd walk through the list, inserting each entry in the hash table, after first checking whether it was already in the table.

    Not strictly O(N), however, since hash search/insertion gets slower as the table fills up. And in terms of storage it would be expensive for large lists -- at least 3x and possibly 10-20x the size of the array of numbers.

    0 讨论(0)
  • 2020-12-16 04:57

    As was already mentioned by others, I don't see any way to do it in O(n).

    However, you can try a probabilistic approach by using a Bloom Filter. It will give you O(n) if you are lucky.

    0 讨论(0)
  • 2020-12-16 04:58

    We can do in linear time o(n) here as well

    public class DuplicateInOnePass {
    
        public static  void duplicate()
    
       {
            int [] ar={6,7,8,8,7,9,9,10};
            Arrays.sort(ar);
            for (int i =0 ; i <ar.length-1; i++)
            {
    
    
                if (ar[i]==ar[i+1])
                    System.out.println("Uniqie Elements are" +ar[i]);
    
            }  
    
        }
    
        public static void main(String[] args) {
            duplicate();
        }
    }
    
    0 讨论(0)
  • 2020-12-16 05:02

    Since extra space is not allowed this can't be done without comparison.The concept of lower bound on the time complexity of comparison sort can be applied here to prove that the problem in its original form can't be solved in O(n) in the worst case.

    0 讨论(0)
提交回复
热议问题