fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C/C++.
  5. Code, Compile, Run and Debug online from anywhere in world.
  6.  
  7. *******************************************************************************/
  8. #include <iostream>
  9. #include <vector>
  10. #include <unordered_map>
  11. using namespace std;
  12.  
  13. int longestXorSubarray(int N, vector<int>& arr) {
  14. int maxLength = 0;
  15. int currXor = 0;
  16. unordered_map<int, int> xorMap;
  17. xorMap[0] = -1;
  18.  
  19. for (int i = 0; i < N; i++) {
  20. currXor ^= arr[i];
  21. if (xorMap.find(currXor) != xorMap.end()) {
  22. maxLength = max(maxLength, i - xorMap[currXor]);
  23. } else {
  24. xorMap[currXor] = i;
  25. }
  26. }
  27.  
  28. return (maxLength > 0) ? maxLength : 0;
  29. }
  30.  
  31. int main() {
  32. int N;
  33. cin >> N;
  34. vector<int> arr(N);
  35.  
  36. for (int i = 0; i < N; i++) {
  37. cin >> arr[i];
  38. }
  39.  
  40. int result = longestXorSubarray(N, arr);
  41. cout << result << endl;
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5472KB
stdin
45
stdout
45