Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 3e5 + 1;
int n, a[N];
vector<int> all, sol;
map<int, int> dp;
int main() {
cin >> n;
for(int i = 0; i < n; ++i)
cin >> a[i];
int mx = 0, val;
for(int i = 0; i < n; ++i) {
dp[a[i]] = dp[a[i] - 1] + 1;
if(dp[a[i]] > mx)
mx = dp[a[i]], val = a[i];
}
cout << mx << endl;
for(int i = n - 1; i >= 0; --i)
if(a[i] == val) {
sol.push_back(i + 1);
--val;
}
reverse(sol.begin(), sol.end());
for(int i = 0; i < sol.size(); ++i)
cout << sol[i] << ' ';
cout << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
7
3 3 4 7 5 6 8
3 3 4 7 5 6 8
Output
4
2 3 5 6
2 3 5 6
Demonstration
Codeforces Solution-F. Consecutive Subsequence-Solution in C, C++, Java, Python