fork download
  1. import java.util.*;
  2. class subarrayLtoRcontainsSet {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5.  
  6. int n = scanner.nextInt(); // size of array b[]
  7. int[] b = new int[n + 1]; // array b[n+1]
  8. for (int i = 1; i <= n; i++) {
  9. b[i] = scanner.nextInt();
  10. }
  11.  
  12. int l = scanner.nextInt();
  13. int r = scanner.nextInt(); // given limits
  14.  
  15. int u = Integer.MAX_VALUE;
  16. for (int i = 1; i <= n; i++) {
  17. Set<Integer> s = new HashSet<>();
  18. for (int j = i; j <= n; j++) {
  19. //[i.....j]
  20. if (b[j] >= l && b[j] <= r) {
  21. s.add(b[j]);
  22. }
  23. if (s.size() == Math.abs(r - l + 1)) {
  24. //[i.....j] is a valid subarray
  25. int length = Math.abs(j - i + 1);
  26. u = Math.min(length, u);
  27. }
  28. }
  29. }
  30.  
  31. System.out.println(u);
  32. }
  33. }
  34.  
Success #stdin #stdout 0.11s 56472KB
stdin
8
1 5 2 3 5 4 2 5
2 5
stdout
4