Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution {
  5.     public static void main(String[] args) {
  6.         FastReader in = new FastReader();
  7.         int t = in.nextInt();
  8.         for (int x = 0; x < t; x++) {
  9.             int n = in.nextInt();
  10.             int[] a = new int[n];
  11.             for (int i = 0; i < n; i++) {
  12.                 a[i] = in.nextInt();
  13.             }
  14.             int min = 1000000;
  15.             HashMap<Integer, Integer> map = new HashMap<>();
  16.             map.put(a[0], 0);
  17.             boolean duplicateFound = false;
  18.             for (int i = 1; i < n; i++) {
  19.                 int currMin = i - map.getOrDefault(a[i], -1000000) + 1;
  20.                 if (currMin == min) {
  21.                     System.out.println(-1);
  22.                     duplicateFound = true;
  23.                     break;
  24.                 }
  25.                 min = Math.min(min, currMin);
  26.                 map.put(a[i], i);
  27.             }
  28.             if (!duplicateFound) {
  29.                 System.out.println(min == 1000000 ? -1 : min);
  30.             }
  31.         }
  32.     }
  33.  
  34.     static class FastReader {
  35.         BufferedReader br;
  36.         StringTokenizer st;
  37.  
  38.         public FastReader() {
  39.             br = new BufferedReader(new
  40.                     InputStreamReader(System.in));
  41.         }
  42.  
  43.         public int[] nextIntArray(int n) {
  44.             int[] array = new int[n];
  45.             for (int i = 0; i < n; ++i) array[i] = nextInt();
  46.             return array;
  47.         }
  48.  
  49.         public int[] nextSortedIntArray(int n) {
  50.             int array[] = nextIntArray(n);
  51.             Arrays.sort(array);
  52.             return array;
  53.         }
  54.  
  55.         public int[] nextSumIntArray(int n) {
  56.             int[] array = new int[n];
  57.             array[0] = nextInt();
  58.             for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
  59.             return array;
  60.         }
  61.  
  62.         public long[] nextLongArray(int n) {
  63.             long[] array = new long[n];
  64.             for (int i = 0; i < n; ++i) array[i] = nextLong();
  65.             return array;
  66.         }
  67.  
  68.         public long[] nextSumLongArray(int n) {
  69.             long[] array = new long[n];
  70.             array[0] = nextInt();
  71.             for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
  72.             return array;
  73.         }
  74.  
  75.         public long[] nextSortedLongArray(int n) {
  76.             long array[] = nextLongArray(n);
  77.             Arrays.sort(array);
  78.             return array;
  79.         }
  80.  
  81.         String next() {
  82.             while (st == null || !st.hasMoreElements()) {
  83.                 try {
  84.                     st = new StringTokenizer(br.readLine());
  85.                 } catch (IOException e) {
  86.                     e.printStackTrace();
  87.                 }
  88.             }
  89.             return st.nextToken();
  90.         }
  91.  
  92.         int nextInt() {
  93.             return Integer.parseInt(next());
  94.         }
  95.  
  96.         long nextLong() {
  97.             return Long.parseLong(next());
  98.         }
  99.  
  100.         double nextDouble() {
  101.             return Double.parseDouble(next());
  102.         }
  103.  
  104.         String nextLine() {
  105.             String str = "";
  106.             try {
  107.                 str = br.readLine();
  108.             } catch (IOException e) {
  109.                 e.printStackTrace();
  110.             }
  111.             return str;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement