Advertisement
sailorbob74133

DSA Maman 12 Q2

Apr 7th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. // compile with gcc -lm missingInt.c -std=c99 -o missingInt
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #define getIthBit(A, bit) ( ( A & ( 0x1 << bit ) ) >> bit )
  7.  
  8. // len is the number of elements in A, which is one less than n
  9. int missingInt(unsigned int A[], int len ) {
  10.     // The number of entries in B is the maximum number of values which can be
  11.     // represented by ceil(log( n+1 )) bits
  12.     int bitCount = ceil( log2( len + 1 ) );
  13.     int Bsize = pow( 2, bitCount );
  14.     unsigned int *B = malloc(sizeof(*A) * Bsize );
  15.  
  16.     // we use B to store both indexes and missing values
  17.     // on the first iteration of the outer loop, B will simply contain the
  18.     // indexes of each element in A and B in order.  On following loops, B's
  19.     // first (Bsize-1)/2 elements will contain the indexes of the remaining
  20.     // elements we want to examine, but any missing values between n and
  21.     // 2^bitCount will still be availbe in the second half of B.
  22.     for (int i = 0; i != Bsize; ++i)
  23.         B[i] = i;
  24.  
  25.     unsigned int missingInt = 0;
  26.     for (int i = 0; i != bitCount; ++i) {
  27.         printf("bitCount, Bsize == %d, %d\n", bitCount, Bsize);
  28.         unsigned int oneCount = 0;
  29.         // count the number of one's
  30.         for (int j = 0; j != Bsize; ++j) {
  31.             printf("B[%d] == %d\t", j, B[j]);
  32.             if ( B[j] < len ) {
  33.                 // look in A
  34.                 oneCount += getIthBit(A[ B[j] ], i);
  35.             } else if (B[j] > len) {
  36.                 // look in B
  37.                 oneCount += getIthBit(B[ B[j] ], i);
  38.             }
  39.         }
  40.         printf("\noneCount == %d\n", oneCount);
  41.         unsigned int evenOrOdd = ( oneCount & 0x1 );
  42.         // missingInt |= ( evenOrOdd << i );
  43.        
  44.         // now record the indexes of the remaining values we want to look at
  45.         for (int j = 0, idx = 0; j != Bsize; ++j) {
  46.             if ( B[j] < len ) {
  47.                 // look in A
  48.                 if ( getIthBit(A[ B[j] ], i) == evenOrOdd )
  49.                     B[idx++] = B[j];
  50.             } else if (B[j] > len) {
  51.                 // look in B
  52.                 if ( getIthBit(B[ B[j] ], i) == evenOrOdd )
  53.                     B[idx++] = B[j];
  54.             }
  55.         }
  56.         // adjust Bsize = (Bsize-1)/2
  57.         Bsize = ( Bsize - 1) >> 1;
  58.         // If we're missing a one, add it in
  59.         if ( oneCount != ( Bsize + 1) )
  60.             missingInt |= ( 0x1 << i );
  61.     }
  62.     return missingInt;
  63. }
  64.  
  65. int main(void) {
  66.     //unsigned int A[] = { 0, 1, 2, 3, 5 };
  67.     unsigned int A[] = { 3, 5, 2, 1, 0, 4, 6};
  68.  
  69.     printf("sizeof(A) == %d\n", sizeof(A)/sizeof(A[0]));
  70.     int mi = missingInt(A, sizeof(A)/sizeof(A[0]));
  71.  
  72.     printf("missing int is %d\n", mi);
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement