Algorithm to find two repeated numbers in an array, without sorting

前端 未结 24 2175
南方客
南方客 2020-11-28 06:58

There is an array of size n (numbers are between 0 and n - 3) and only 2 numbers are repeated. Elements are placed randomly in the array.

E.g. in {2, 3, 6, 1, 5, 4

24条回答
  •  广开言路
    2020-11-28 07:13

    OK, seems I just can't give it a rest :)

    Simplest solution

    int A[N] = {...};
    
    int signed_1(n) { return n%2<1 ? +n : -n;  } // 0,-1,+2,-3,+4,-5,+6,-7,...
    int signed_2(n) { return n%4<2 ? +n : -n;  } // 0,+1,-2,-3,+4,+5,-6,-7,...
    
    long S1 = 0;  // or int64, or long long, or some user-defined class
    long S2 = 0;  // so that it has enough bits to contain sum without overflow
    
    for (int i=0; i

    One sum (S1 or S2) contains p and q with the same sign, the other sum - with opposite signs, all other members are eliminated.
    S1 and S2 must have enough bits to accommodate sums, the algorithm does not stand for overflow because of abs().

    if abs(S1)==abs(S2) then the algorithm fails, though this value will still be the difference between p and q (i.e. abs(p - q) == abs(S1)).

    Previous solution

    I doubt somebody will ever encounter such a problem in the field ;)
    and I guess, I know the teacher's expectation:

    Lets take array {0,1,2,...,n-2,n-1},
    The given one can be produced by replacing last two elements n-2 and n-1 with unknown p and q (less order)

    so, the sum of elements will be (n-1)n/2 + p + q - (n-2) - (n-1)
    the sum of squares (n-1)n(2n-1)/6 + p^2 + q^2 - (n-2)^2 - (n-1)^2

    Simple math remains:

      (1)  p+q = S1  
      (2)  p^2+q^2 = S2
    

    Surely you won't solve it as math classes teach to solve square equations.

    First, calculate everything modulo 2^32, that is, allow for overflow.
    Then check pairs {p,q}: {0, S1}, {1, S1-1} ... against expression (2) to find candidates (there might be more than 2 due to modulo and squaring)
    And finally check found candidates if they really are present in array twice.

提交回复
热议问题