Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created April 4, 2016 10:02
Show Gist options
  • Save rishi93/8524ee9817d22a5260da4b6975157d16 to your computer and use it in GitHub Desktop.
Save rishi93/8524ee9817d22a5260da4b6975157d16 to your computer and use it in GitHub Desktop.
Supernumbers in a permutation - SUPPER using C++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int maximum(int arr[],int n){
int i,max_val;
max_val = arr[0];
for(i=0; i<n; i++){
if(arr[i] > max_val){
max_val = arr[i];
}
}
return max_val;
}
vector<int> lis(int arr[], int n){
int i,j,max_value;
vector<int> result;
int dp[n] = {1};
for(i = 1; i < n; i++){
for(j = 0; j < i; j++){
if(dp[j] >= dp[i] && arr[j] <= arr[i]){
dp[i] += 1;
}
}
}
max_value = maximum(dp,n);
i = n-1;
while(i>=0){
if(dp[i] == max_value){
while(dp[i] == max_value){
result.push_back(arr[i]);
i -= 1;
}
max_value -= 1;
}
else{
i -= 1;
}
}
sort(result.begin(),result.end());
return result;
}
int main() {
int elem;
int arr[5] = {2,1,4,5,3};
vector<int> answer;
answer = lis(arr,5);
for(elem=0; elem<answer.size(); elem++){
cout<< answer[elem];
}
cout<< endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment