fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <memory.h>
  5.  
  6. #define bit(_x_) (1U << (_x_))
  7.  
  8. void randomfillarray( unsigned int * arr, size_t size ) {
  9. srand( time( NULL ) );
  10. for ( int i = 0; i < size; i++ )
  11. arr[i] = rand( );
  12. }
  13.  
  14. int main( ) {
  15. unsigned int arr[10];
  16. size_t size = sizeof arr / sizeof * arr;
  17. randomfillarray( arr, size );
  18.  
  19. unsigned int * resultantcouple = malloc( sizeof arr );
  20. memcpy( resultantcouple, arr, sizeof arr );
  21.  
  22. for ( int i = 0; i < size; i++ )
  23. printf( i ? " %u" : "%u", arr[i] );
  24. putchar( '\n' );
  25.  
  26. int success = 0;
  27.  
  28. for ( unsigned int thebit = bit( sizeof( int ) * 8 - 1 ); thebit; thebit >>= 1 ) {
  29. int count = 0;
  30. int * indices = NULL;
  31. for ( int i = 0; i < size; i++ ) {
  32. if ( resultantcouple[i] & thebit ) {
  33. indices = realloc( indices, ++count * sizeof * indices );
  34. indices[count - 1] = i;
  35. }
  36. }
  37. if ( count >= 2 ) {
  38. size = count;
  39. for ( int i = 0; i < size; i++ )
  40. resultantcouple[i] = resultantcouple[indices[i]];
  41. resultantcouple = realloc( resultantcouple, size * sizeof * resultantcouple );
  42. }
  43. if ( size == 2 ) {
  44. success = 1;
  45. break;
  46. }
  47. free( indices );
  48. }
  49.  
  50. if ( success )
  51. printf( "Success! %u and %u are the ones.", resultantcouple[0], resultantcouple[1] );
  52. else
  53. printf( "Failure! Either all pairs are bitwise distinct, or there are less than 2 elements, or something else..." );
  54.  
  55.  
  56. putchar( '\n' );
  57. return 0;
  58. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
112681642 1998444988 1020495738 1770693426 913084976 932014602 1144788178 95777753 1845835807 75638767
Success! 1770693426 and 1845835807 are the ones.