Longest permutation subsequence in a given array | C ++ Program to find length of Longest Permutation Subsequence in a given array ; Function to find the longest permutation subsequence ; Map data structure to count the frequency of each element ; If frequency of element is 0 , then we can not move forward as every element should be present ; Increasing the length by one ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestPermutation ( int a [ ] , int n ) { map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; } int len = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( freq [ i ] == 0 ) { break ; } len ++ ; } return len ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestPermutation ( arr , n ) << " STRNEWLINE " ; return 0 ; }