Longest permutation subsequence in a given array
Last Updated :
25 Aug, 2021
Given an array arr containing N elements, find the length of the longest subsequence such that it is a valid permutation of a particular length. If no such permutation sequence exists then print 0.
Examples:
Input: arr[] = {3, 2, 1, 6, 5}
Output: 3
Explanation:
Longest permutation subsequence will be [3, 2, 1].
Input: arr[]= {2, 3, 4, 5}
Output: 0
Explanation:
No valid permutation subsequence present as element 1 is missing.
Approach: The above-mentioned problem is on permutation subsequence so the order of the array elements is irrelevant, only what matter is the frequency of each element. If array is of length N then the maximum possible length for the permutation sequence is N and minimum possible length is 0. If the subsequence of length L is a valid permutation then all elements from 1 to L should be present.
- Count the frequency of the elements in the range [1, N] from the array
- Iterate through all elements from 1 to N in the array and count the iterations till a 0 frequency is observed. If the frequency of an element is '0', return the current count of iterations as the required length.
Below is the implementation of the above approach:
C++
// C++ Program to find length of
// Longest Permutation Subsequence
// in a given array
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// longest permutation subsequence
int longestPermutation(int a[], int n)
{
// Map data structure to
// count the frequency of each element
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 frequency of element is 0,
// then we can not move forward
// as every element should be present
if (freq[i] == 0) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1, 6, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << longestPermutation(arr, n)
<< "\n";
return 0;
}
Java
// Java Program to find length of
// Longest Permutation Subsequence
// in a given array
import java.util.*;
class GFG{
// Function to find the
// longest permutation subsequence
static int longestPermutation(int arr[], int n)
{
// Map data structure to
// count the frequency of each element
HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
for (int i = 0; i < n; i++) {
if(freq.containsKey(arr[i])){
freq.put(arr[i], freq.get(arr[i])+1);
}else{
freq.put(arr[i], 1);
}
}
int len = 0;
for (int i = 1; i <= n; i++) {
// If frequency of element is 0,
// then we can not move forward
// as every element should be present
if (!freq.containsKey(i)) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 2, 1, 6, 5 };
int n = arr.length;
System.out.print(longestPermutation(arr, n));
}
}
// This code is contributed by Rajput-Ji
C#
// C# Program to find length of
// longest Permutation Subsequence
// in a given array
using System;
using System.Collections.Generic;
public class GFG{
// Function to find the
// longest permutation subsequence
static int longestPermutation(int []arr, int n)
{
// Map data structure to
// count the frequency of each element
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 frequency of element is 0,
// then we can not move forward
// as every element should be present
if (!freq.ContainsKey(i)) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1, 6, 5 };
int n = arr.Length;
Console.Write(longestPermutation(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Program to find length of
# Longest Permutation Subsequence
# in a given array
from collections import defaultdict
# Function to find the
# longest permutation subsequence
def longestPermutation(a, n):
# Map data structure to
# count the frequency of each element
freq = defaultdict(int)
for i in range( n ):
freq[a[i]] += 1
length = 0
for i in range(1 , n + 1):
# If frequency of element is 0,
# then we can not move forward
# as every element should be present
if (freq[i] == 0):
break
# Increasing the length by one
length += 1
return length
# Driver Code
if __name__ == "__main__":
arr = [ 3, 2, 1, 6, 5 ]
n = len(arr)
print(longestPermutation(arr, n))
# This code is contributed by chitranayal
JavaScript
<script>
// Javascript Program to find length of
// Longest Permutation Subsequence
// in a given array
// Function to find the
// longest permutation subsequence
function longestPermutation(arr, n)
{
// Map data structure to
// count the frequency of each element
let freq = new Map();
for (let i = 0; i < n; i++) {
if(freq.has(arr[i])){
freq.set(arr[i], freq.get(arr[i])+1);
}else{
freq.set(arr[i], 1);
}
}
let len = 0;
for (let i = 1; i <= n; i++) {
// If frequency of element is 0,
// then we can not move forward
// as every element should be present
if (!freq.has(i)) {
break;
}
// Increasing the length by one
len++;
}
return len;
}
// Driver code
let arr = [ 3, 2, 1, 6, 5 ];
let n = arr.length;
document.write(longestPermutation(arr, n));
</script>
Time Complexity: O(N)
Auxiliary Space Complexity: O(N)
Similar Reads
Find the Longest Common Subsequence (LCS) in given K permutations Given K permutations of numbers from 1 to N in a 2D array arr[][]. The task is to find the longest common subsequence of these K permutations. Examples: Input: N = 4, K = 3arr[][] = {{1, 4, 2, 3}, {4, 1, 2, 3}, {1, 2, 4, 3}}Output: 3Explanation: Longest common subsequence is {1, 2, 3} which has leng
10 min read
Printing Longest Increasing Subsequence (LIS) Given a sequence of numbers, the task is to find and print the Longest Increasing Subsequence (LIS), the longest subsequence where each element is strictly greater than the previous one. If multiple LIS of the same maximum length exist, we must select the one that appears first based on the lexicogr
15+ min read
Longest alternative parity subsequence We are given an array a of size N. The task is to print the length of the longest alternative odd/even or even/odd subsequence. Examples: Input: a[] = { 13, 16, 8, 9, 32, 10 } Output: 4 {13, 16, 9, 10} or any other subsequence of length 4 can be the answer. Input: a[] = {1, 2, 3, 3, 9} Output: 3 App
7 min read
Printing Longest Bitonic Subsequence The Longest Bitonic Subsequence problem is to find the longest subsequence of a given sequence such that it is first increasing and then decreasing. A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bito
12 min read
Count Permutations in a Sequence Given an array A consisting of N positive integers, find the total number of subsequences of the given array such that the chosen subsequence represents a permutation. Note: Sequence A is a subsequence of B if A can be obtained from B by deleting some(possibly, zero) elements without changing its or
5 min read