Find all differences in an array in O(n)

前端 未结 7 990
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 05:39

Question: Given a sorted array A find all possible difference of elements from A.

My solution:

for (int i=0; i

        
相关标签:
7条回答
  • 2020-12-30 06:04

    For example for an array with the elements {21, 22, ..., 2n} there are n⋅(n-1)/2 possible differences, and no two of them are equal. So there are O(n2) differences.

    Since you have to enumerate all of them, you also need at least O(n2) time.

    0 讨论(0)
  • 2020-12-30 06:09

    If the interviewer is fond of theoretical games, perhaps he was thinking of using a table of inputs and results? Any problem with a limit on the size of the input, and that has a known solution, can be solved by table lookup. Given that you have first created and stored that table, which might be large.

    So if the array size is limited, the problem can be solved by table lookup, which (given some assumptions) can even be done in constant time. Granted, even for a maximum array size of two (assuming 32-bit integers) the table will not fit in a normal computer's memory, or on the disks. For larger max sizes of the array, you're into "won't fit in the known universe" size. But, theoretically, it can be done.

    (But in reality, I think that Jens Gustedt's comment is more likely.)

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

    Yes you can surely do that its a little tricky method. to find differances in O(n) you will need to use BitSet(C++) or any similar Data Structure in respective language.

    Initialize two bitset say A and B You can do as follows: For each iteration through array: 1--store consecutive differance in BitSet A 2--LeftShift B 3--store consecutive differance in BitSet B 4--take A=A or B

    for example I have given code- Here N is Size of array

    for (int i=1;i<N;i++){
        int diff = arr[i]-arr[i-1];
        A[diff]=1;
        B<<=diff;
        B[diff]=1;
        A=A | B;
    }
    

    Bits in A which are 1 will be the differances.

    0 讨论(0)
  • 2020-12-30 06:16

    You can get another counter-example by assuming the array contents are random integers before sorting. Then the chance that two differences, Ai - Aj vs Ak - Al, or even Ai - Aj vs Aj - Ak, are the same is too small for there to be only O(n) distinct differences Ai - Aj.

    Given that, the question to your interviewer is to explain the special circumstances that allow an O(n) solution. One possibility is that the array values are all numbers in the range 0..n, because in this case the maximum absolute difference is only n.

    I can do this in O(n lg n) but not O(n). Represent the array contents by an array of size n+1 with element i set to 1 where there is a value i in the array. Then use FFT to convolve the array with itself - there is a difference Ai - Aj = k where the kth element of the convolution is non-zero.

    0 讨论(0)
  • 2020-12-30 06:16

    First of all the array need to be sorted

    lets think a sorted array ar = {1,2,3,4}

    so what we were doing at the O(n^2)

    for(int i=0; i<n; i++)
    for(int j=i+1; j<n; j++) sum+=abs(ar[i]-ar[j]);
    

    if we do the operations here elaborately then it will look like below

    when i = 0 | sum = sum + {(2-1)+(3-1)+(4-1)}                   
    when i = 1 | sum = sum + {(3-2)+(4-2)}               
    when i = 2 | sum = sum + {(4-3)}
    

    if we write them all

    sum = ( -1-1-1) + (2+ -2-2) + (3+3 -3) + (4+4+4 ) 
    

    we can see that

    the number at index 0 is added to the sum for 0 time and substracted from the sum for 3 time.
    the number at index 1 is added to the sum for 1 time and substracted from the sum for 2 time.
    the number at index 2 is added to the sum for 2 time and substracted from the sum for 1 time.
    the number at index 3 is added to the sum for 3 time and substracted from the sum for 0 time.

    so for we can say that,

    the number at index i will be added to the sum for i time 
    and will be substracted from the sum for (n-i)-1 time
    

    Then the generalized expression for each element will be

    sum = sum + (i*a[i]) – ((n-i)-1)*a[i];
    
    0 讨论(0)
  • 2020-12-30 06:18

    sorted or unsorted doesn't matter, if you have to calculate each difference there is no way to do it in less then n^2,

    the question was asked wrong, or you just do O(n) and then print 42 the other N times :D

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