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