fork download
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int test(int*, int);
  5. int arr[] = {0,1,1,1,1};
  6. int length = sizeof(arr)/sizeof(int);
  7. test(arr,length);
  8. return 1;
  9. }
  10.  
  11. int test(int* arr,int N)
  12. {
  13. int result1 = 0; // store the xor of arr[arr[i]]
  14. int result2 = 0; // store the xor of all numbers
  15. int result3 = 0; // store the xor of all indexes
  16.  
  17. int count = 0;
  18. int init = arr[0];
  19.  
  20. if(init==0)
  21. count++;
  22.  
  23. // checks for special case either 0 is repeated or the first value is repeated
  24. int i;
  25. for(i=1;i<N;i++)
  26. {
  27. if(arr[i]==0)
  28. count++;
  29. else if(arr[i]==init)
  30. count++;
  31.  
  32. if(count>2)
  33. {
  34. printf("Repeated");
  35. return -1;
  36. }
  37.  
  38. }
  39.  
  40. for(i=0;i<N;i++)
  41. {
  42. result1 ^= arr[arr[i]];
  43. result2 ^= arr[i];
  44. result3 ^= i;
  45. }
  46. if((result1==result2) && (result1==result3))
  47. printf("NOT REPEATED");
  48. else
  49. printf("REPEATED");
  50. return 1;
  51. }
  52.  
Runtime error #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
REPEATED