fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. // public int func(int a[] , int b[]){
  11. // int n = a.length;
  12. // int m = b.length;
  13.  
  14. // int len = -1;
  15. // int maxi = Integer.MIN_VALUE;
  16. // for(int i=0;i<n;i++){
  17. // int z1 = 0;
  18. // int z2 = 0;
  19. // for(int j=i;j<n;j++){
  20. // z1 = z1 ^ a[j];
  21. // z2 = z2 ^ b[j];
  22. // if(z1 == z2){
  23. // len = j-i+1;
  24. // maxi = Math.max(maxi,len);
  25. // }
  26. // }
  27. // }
  28. // return maxi;
  29. // }
  30. public int func(int a[], int b[]){
  31. int n = a.length;
  32. int c[] = new int[n];
  33. for(int i=0;i<n;i++){
  34. c[i] = a[i] ^ b[i];
  35. }
  36. int p[] = new int[n];
  37. p[0] = c[0];
  38. for(int i=1;i<p.length;i++){
  39. p[i] = p[i-1] ^ c[i];
  40. }
  41. int maxi = 0;
  42. int i=-1;
  43. HashMap<Integer,Integer>map = new HashMap<>();
  44. for(int j=0;j<p.length;j++){
  45. int req = p[j] ^ 0;
  46. if(map.containsKey(req)){
  47. i = map.getOrDefault(req,0)+1;
  48. }
  49. int len = j-i+1;
  50. maxi = Math.max(maxi,len);
  51. if(!map.containsKey(p[j])){
  52. map.put(p[j],j);
  53. }
  54. }
  55. return maxi;
  56. }
  57. public static void main (String[] args) throws java.lang.Exception
  58. {
  59. // your code goes here
  60. Ideone obj = new Ideone();
  61.  
  62. int a[] = {1, 2, 3, 4};
  63. int b[] = {1, 3, 2, 4};
  64.  
  65. int ans = obj.func(a, b);
  66.  
  67. System.out.println(ans);
  68. }
  69. }
Success #stdin #stdout 0.09s 16508KB
stdin
Standard input is empty
stdout
3