My issue
First, I used a set to count the unique elements in the array. Then, I calculated the number of remaining elements, which are duplicates, and stored that in a variable. Since the set contains all distinct elements and is a subsequence of the original array, the minimum value of K would be the size of the set. Given that the number of distinct elements is fixed, adding more elements will not increase the distinct count. Therefore, I counted how many times I could add these extra elements to increase the size of the subsequence K.
I am unable to find out why my code is not working correctly? Please help me.
My code
#include <bits/stdc++.h>
#define lp(n) for(int i=0; i<n; i++)
#define ll long long
#define endl '\n'
#define sza(x) ((int)x.size())
#define aint(a) (a).begin(), (a).end()
#define FAST_IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
int main() {
FAST_IO;
int t;
cin >>t;
while(t--){
int n;
cin >> n;
set<int> s; // unique elements
int dup; // no of duplicate elements
lp(n){
int a;
cin >> a;
s.insert(a);
}
int K = s.size(); // since the set consits of k distinct element and it's size is k
dup = n-K;
K += K*(dup/K);
cout << K << endl;
}
}
Problem Link: Largest K Practice Coding Problem