PROBLEM LINK:
Author: Ritesh Gupta
Tester: Sachin Yadav
Editorialist: Ritesh Gupta
DIFFICULTY:
CAKEWALK
PREREQUISITES:
NONE
PROBLEM:
You are given a sequence A_1, A_2, \ldots, A_N (1 \le N \le 10^3 and 1 \le A_i \le 10^3). You have to find out the minimum element of the given sequence which makes the length of the valid subsequence is maximum possible. A valid subsequence defines as:
- It should not be empty.
- All the values of the subsequence should be the same.
- No two values are selected which are adjacent in the given sequence.
QUICK EXPLANATION:
- As A_i is almost 1000. So, for each i from 1 to 1000, we can find the largest valid subsequence is possible with i as it’s an element. If there are many valid i for which length of the valid subsequence is the same then the answer is minimum possible i.
EXPLANATION:
OBSERVATION:
- As the valid subsequences are uni-valued. So, we can solve the question for each distinct value independently.
- It is optimum to select the alternate numbers to start from the first element in a layer of equivalent numbers i.e. consider \{2, 2, 2, 2, 2, 2, 2\}, we can choose 4(1^{st}, \space 3^{rd}, \space 5^{th}, and 7^{th}) out of 7 values.
Let suppose there are D distinct values d_1, d_2, \ldots, d_D and frequency of each is f_1, f_2, \ldots, f_D respectively.
For each valid d_i, we can hash the index values for which the value present at these indexes is d_i in the given sequence. If the index values are p_{i,1}, p_{i,2}, \ldots, p_{i,f_i} for some d_i then we just need to pick some of them according to the third conditions mentioned in the definition of the valid subsequence.
Let suppose index values are \{1,3,4,7,8,9,11\} then we greedily select the \{1,3,7,9,11\}. Here, from 3 and 4, we can select any one of them but from 7, 8 and 9, the only way to select two indices are 7 and 9.
It is easy to prove that we should pick the elements greedily from start(left the proof as an exercise) and it gives the optimum answer.
Now, the answer is the minimum of all d_i for which the length of the valid subsequence is maximum possible.
Bonus Problem
You are given a sequence A_1, A_2, \ldots, A_N (1 \le N \le 10^5 and 1 \le A_i \le 10^9). You have to find the count of the valid subsequence. A valid subsequence defines as:
- It should not be empty.
- All the values of the subsequence should be the same.
- No two values are selected which are adjacent in the given sequence.
COMPLEXITY:
TIME: O(N)
SPACE: O(N)
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
vector <int> v[1001];
for(int i=0;i<n;i++)
{
int x;
cin >> x;
v[x].push_back(i);
}
int curr = 0;
int ans = -1;
for(int i=1;i<=1000;i++)
{
if(v[i].empty())
continue;
int cnt = 0;
int prev = -1;
for(int j:v[i])
{
if(j > prev)
{
cnt++;
prev = j + 1;
}
}
if(curr < cnt)
{
curr = cnt;
ans = i;
}
}
cout << ans << endl;
}
}
Tester's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int t;
cin>>t;
while(t--)
{
int n, freq[1001]={0};
cin>>n;
int last=-1, x;
bool take=1;
for(int i=0; i<n; ++i)
{
cin>>x;
if(last==x)
take=!take;
else
take=1;
if(take) ++freq[x];
last=x;
}
int ans=0, mfreq=0;
for(int i=1; i<=1000; ++i)
if(freq[i]>mfreq)
{
ans=i; mfreq=freq[i];
}
cout<<ans<<"\n";
}
return 0;
}