The candies supermarket sells m different types of candies and has n consecutive boxes, each box contains a single type of candy, you are allowed to buy only some contiguous boxes of candies (i.e. the boxes ordered as an array and you are allowed to buy only sub-array of this array).
What is the minimum number of boxes you should buy to be sure that you have at least one box of each type of candies.
Input
The first line contains two integers n and m (1 ≤ n ≤ 105 and 1 ≤ m ≤ 10).
The second line contains n integers, the ith integer is ci (1 ≤ ci ≤ m) – The type of the ith box of candies.
Output
The minimum number of boxes you should buy to ensure that you have at least one box of each type, or print “-1” (without quotes) if it’s impossible to buy at least one box of each type.
Example
input
9 3
1 1 2 1 1 3 1 1 2
output
4
my code:
main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// srand((unsigned)time(NULL));
vector <int> Vector;
int n,m;
cin >> n >> m;
int* array = new int[n];
sort(array, array + n);
Vector.push_back(array[0]);
for (int i = 1; i < n; i++) {
if(array [i] != array[i-1])
Vector.push_back(array[i]);
}
if (Vector.size() != m)
{
cout << -1;
return 0;
}
return 0;
}
Abood Elean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.