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
using System ; using System . Collections . Generic ; public class GFG { static int longestPermutation ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } } int len = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! freq . ContainsKey ( i ) ) { break ; } len ++ ; } return len ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 6 , 5 } ; int n = arr . Length ; Console . Write ( longestPermutation ( arr , n ) ) ; } }