Find an element in an infinite length sorted array

后端 未结 7 1101
北海茫月
北海茫月 2020-12-13 21:51

Given an infinite length sorted array having both positive and negative integers. Find an element in it.

EDIT
All the elements in the array are

相关标签:
7条回答
  • 2020-12-13 22:14

    Since the array is infinite, the indexes are necessarily variable-length. That means that doing math on them is not O(1), which in turn means that "binary search with first a search for an endpoint" has a slightly different time complexity than O(log(k)).

    The index math done in the search for the endpoint is just a left shift by one, which takes O(log(k)) because indexes up to k need up to log(k) bits and shifting left by one is linear in the number of bits.

    The index math done in the binary search is all O(log(k)) as well.

    So the actual complexity of both algorithms is O(log(k)^2). The complexity of a linear search would be O(k log k), so it still loses.

    0 讨论(0)
  • 2020-12-13 22:22

    --Complete Solution-- takes O(logn) time complexity

    public class B {

    public static void main(String[] args) {
        // Assuming sorted array of infinite length
        int a[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    
        int elementToFind = 12;
    
        int finalIndex = getFinalIndex(a, elementToFind);
    
        if (finalIndex == -1) {
            System.out.println("Element not found");
        }
        System.out.println("Found element:" + a[finalIndex]);
    
    }
    
    private static int getFinalIndex(int[] a, int elementToFind) {
    
        int power = 2;
        int finalIndex = (int) Math.pow(2, power);
    
        for (int i = 0; i < finalIndex;) {
    
            if (elementToFind == a[finalIndex]) {
                return finalIndex;
            }
    
            else if (elementToFind < a[finalIndex]) {
                System.out.println("search through binary search algo");
                // taking i as starting index in binary search call
                int searchedIndex = callToBinarySearch(a, i, finalIndex);
                return searchedIndex;
            }
    
            else {
                i = finalIndex + 1;
                power = power * 2;
                finalIndex = (int) Math.pow(2, power);
    
            }
        }
        return -1;
    
    }
    

    }

    0 讨论(0)
  • 2020-12-13 22:24

    Just my 2 cents. We have an infinite array thus lets imagine that we are looking for very big number. Did you imagine? Well it's ever much more bigger. Note that length of interval to binary search in is 2^i = 2^(i+1)-2^i thus it should take log(2^i)=i time to find the number. On the other hand it takes i time to reach the target interval. So the total time complexity is O(n) again. What I'm missing?

    0 讨论(0)
  • 2020-12-13 22:25

    The first approach will be linear in the index of the element (O(k) where k is the index of the element). Actually, you are going to need k/100 iterations to find the element which is greater than the searched element, which is O(k).

    The second approach will be logarithmic in the same index. O(logk). (where k is the index of the element). In here, you are going to need log(k) iterations until you find the higher element. Then binary search between 2^(i-1), 2^i (where i is the iteration number), will be logarithmic as well, totaling in O(logk)

    Thus, the second is more efficient.

    0 讨论(0)
  • 2020-12-13 22:30

    You can apply binary search more or less directly with a small modification. This will roughly correspond to your Approach 2.

    Basically, pick some number B and set A to 0, then check if the element you're looking for is between A and B. If it is, perform the usual kind of binary search in these boundaries, otherwise set B=A and A=2*A and repeat. This will take O(log(M)), where M is the position of the element you're looking for in the array.

    0 讨论(0)
  • 2020-12-13 22:38

    If the array is well-founded, i.e. has a smallest element (i.e. you have elements x0, x1, ...), and all ele­ments are unique, then here's a simple approach: If you're looking for the number n, you can do a bi­na­ry search over the indices 0, ..., n − x0. Note that we always have the basic inequality xi ≥ i + x0 for all i ≥ 0.

    Thus you can find the value n in log2(n − x0) steps.

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