Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- FastReader in = new FastReader();
- int t = in.nextInt();
- for (int x = 0; x < t; x++) {
- int n = in.nextInt();
- int[] a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = in.nextInt();
- }
- int min = 1000000;
- HashMap<Integer, Integer> map = new HashMap<>();
- map.put(a[0], 0);
- boolean duplicateFound = false;
- for (int i = 1; i < n; i++) {
- int currMin = i - map.getOrDefault(a[i], -1000000) + 1;
- if (currMin == min) {
- System.out.println(-1);
- duplicateFound = true;
- break;
- }
- min = Math.min(min, currMin);
- map.put(a[i], i);
- }
- if (!duplicateFound) {
- System.out.println(min == 1000000 ? -1 : min);
- }
- }
- }
- static class FastReader {
- BufferedReader br;
- StringTokenizer st;
- public FastReader() {
- br = new BufferedReader(new
- InputStreamReader(System.in));
- }
- public int[] nextIntArray(int n) {
- int[] array = new int[n];
- for (int i = 0; i < n; ++i) array[i] = nextInt();
- return array;
- }
- public int[] nextSortedIntArray(int n) {
- int array[] = nextIntArray(n);
- Arrays.sort(array);
- return array;
- }
- public int[] nextSumIntArray(int n) {
- int[] array = new int[n];
- array[0] = nextInt();
- for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
- return array;
- }
- public long[] nextLongArray(int n) {
- long[] array = new long[n];
- for (int i = 0; i < n; ++i) array[i] = nextLong();
- return array;
- }
- public long[] nextSumLongArray(int n) {
- long[] array = new long[n];
- array[0] = nextInt();
- for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
- return array;
- }
- public long[] nextSortedLongArray(int n) {
- long array[] = nextLongArray(n);
- Arrays.sort(array);
- return array;
- }
- String next() {
- while (st == null || !st.hasMoreElements()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- String nextLine() {
- String str = "";
- try {
- str = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement