fork download
  1. import java.util.*;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. int[] arr = {1, 1, 2, 2, 2, 1};
  6. int maxDistance = 0;
  7. Map<Integer, Integer> firstIndex = new HashMap<>();
  8.  
  9. for (int i = 0; i < arr.length; i++) {
  10. if (!firstIndex.containsKey(arr[i])) {
  11. firstIndex.put(arr[i], i);
  12. } else {
  13. maxDistance = Math.max(maxDistance, i - firstIndex.get(arr[i]));
  14. }
  15. }
  16.  
  17. System.out.println(maxDistance);
  18. }
  19. }
  20.  
Success #stdin #stdout 0.08s 52532KB
stdin
Standard input is empty
stdout
5