fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* This finction sets the values of *x and *y to nonr-epeating
  5.  elements in an array arr[] of size n*/
  6. void get2NonRepeatingNos(int arr[], int n, int *x, int *y)
  7. {
  8. int xor = arr[0]; /* Will hold xor of all elements */
  9. int set_bit_no; /* Will have only single set bit of xor */
  10. int i;
  11. *x = 0;
  12. *y = 0;
  13.  
  14. /* Get the xor of all elements */
  15. for(i = 1; i < n; i++)
  16. xor ^= arr[i];
  17.  
  18. /* Get the rightmost set bit in set_bit_no */
  19. set_bit_no = xor & ~(xor-1);
  20. //printf("set_bit_no %d\n", set_bit_no);
  21. /* Now divide elements in two sets by comparing rightmost set
  22.   bit of xor with bit at same position in each element. */
  23. for(i = 0; i < n; i++)
  24. {
  25. if(arr[i] & set_bit_no){
  26. *x = *x ^ arr[i]; /*XOR of first set */
  27. //printf("*x and arr and arr[i] & set_bit_no = %d %d %d \n",*x, arr[i],arr[i] & set_bit_no);
  28. }
  29. else{
  30. *y = *y ^ arr[i]; /*XOR of second set*/
  31. //printf("*y and arr= %d %d\n",*y, arr[i]);
  32. }
  33. }
  34. }
  35.  
  36. /* Driver program to test above function */
  37. int main()
  38. {
  39. int arr[] = {2, 3, 7, 9, 11, 2, 3, 11};
  40. int *x = (int *)malloc(sizeof(int));
  41. int *y = (int *)malloc(sizeof(int));
  42. get2NonRepeatingNos(arr, 8, x, y);
  43. printf("The non-repeating elements are %d and %d", *x, *y);
  44. }
Runtime error #stdin #stdout 0s 2384KB
stdin
Standard input is empty
stdout
The non-repeating elements are 7 and 9