Menu Close

Find the minimum size of the sub-segment to make the array pairwise distinct.

Mukesh has given an array π‘Ž1,π‘Ž2,…,π‘Žπ‘› to Mahesh. Mahesh can remove at most one subsegment from it. The remaining elements should be pairwise distinct.

In other words, at most one time Mahesh can choose two integers 𝑙 and π‘Ÿ (1β‰€π‘™β‰€π‘Ÿβ‰€π‘›) and delete integers π‘Žπ‘™,π‘Žπ‘™+1,…,π‘Žπ‘Ÿ from the array. The remaining elements should be pairwise distinct.

Input:
The first line contains a single integer 𝑛 β€” the number of elements in the given array.

The next line contains 𝑛 spaced integers π‘Ž1,π‘Ž2,…,π‘Žπ‘› β€” the elements of the array. 

Output:
Print the output, the minimum size of the subsegment to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print 0
#include <stdio.h>
int compare(const void *a, const void *b)
{if(*(int *)a == *(int *)b){return 1;}
  else{return 0;}
}
int main()
{int n,i,j,count=0;
  scanf("%d",&n);
  int arr[n];
  for(i=0;i < n;i++){
  scanf("%d",&arr[i]);}
  for(i=0;i<n;i++){
  for(j=i+1;j<n;j++){
  if(compare(&arr[i],&arr[j]))
  count++;}
  }
  printf("%d",count);
return 0;
}

INPUT_1:
5
3  4  2  4  9

OUTPUT:
1


INPUT_2:
4
1  1  2  2

OUTPUT:
2


ILLUSTRATION:

EXECUTED USING gcc TERMINAL LINUX


Morae Q!