Failing in test case 4 only
Approach : finding continous distinct subarray using window sliding technique
for every such array calculate
int tempAns=2*min(left, n-right-1) + max(left, n-right-1);
ans=min(ans, tempAns)
code is
public static void f(int a[], int n) {
HashSet<Integer> set = new HashSet<>();
int left = 0, right = 0, mini = (int) Math.pow(10, 9);
set.add(a[0]);
for (int k = 1; k < n; k++) {
if (!set.add(a[k])) {
int ans = 2 * Math.min(left, n - right - 1) + Math.max(left, n - right - 1);
mini = Math.min(mini, ans);
left = right = k;
set.clear();
set.add(a[k]);
} else {
right = k;
}
}
int ans = 2 * Math.min(left, n - right - 1) + Math.max(left, n - right - 1);
mini = Math.min(mini, ans);
System.out.println(mini);
}
submission link : CodeChef: Practical coding for everyone
plz help of to find out why 4th case getting failed