fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. // your code goes here
  12. int[] arr={1,1,2,2,2,1};
  13. HashMap<Integer,Integer> hm = new HashMap<>();
  14. int ans=0;
  15. for(int i =0;i<arr.length;i++){
  16. if(hm.containsKey(arr[i])){
  17. ans=Math.max(ans,i-hm.get(arr[i]));
  18. }
  19. else{
  20. hm.put(arr[i],i);
  21. }
  22. }
  23. System.out.print(ans);
  24. }
  25. }
Success #stdin #stdout 0.07s 52536KB
stdin
Standard input is empty
stdout
5