Length of longest increasing prime subsequence from a given array
Given an array arr[] consisting of N positive integers, the task is to find the length of the longest increasing subsequence consisting of Prime Numbers in the given array.
Examples:
Input: arr[] = {1, 2, 5, 3, 2, 5, 1, 7}
Output: 4
Explanation:
The Longest Increasing Prime Subsequence is {2, 3, 5, 7}.
Therefore, the answer is 4.
Input: arr[] = {6, 11, 7, 13, 9, 25}
Output: 2
Explanation:
The Longest Increasing Prime Subsequence is {11, 13} and {7, 13}.
Therefore, the answer is 2.
Naive Approach: The simplest approach is to generate all possible subsequence of the given array and print the length of the longest subsequence consisting of prime numbers in increasing order.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use the Dynamic Programming approach to optimize the above approach. This problem is a basic variation of the Longest Increasing Subsequence (LIS) problem. Below are the steps:
- Initialize an auxiliary array dp[] of size N such that dp[i] will store the length of LIS of prime numbers ending at index i.
- Below is the recurrence relation for finding the longest increasing Prime Numbers:
If arr[i] is prime then
dp[i] = 1 + max(dp[j], for j belongs (0, i – 1)), where 0 < j < i and arr[j] < arr[i];
dp[i] = 1, if no such j exists
else if arr[i] is non-prime then
dp[i] = 0
- Using Sieve of Eratosthenes store all the prime numbers to till 105.
- Iterate a two nested loop over the given array and update the array dp[] according to the above recurrence relation.
- After all the above steps, the maximum element in the array dp[] is the length of the longest increasing subsequence of Prime Numbers in the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
void SieveOfEratosthenes( bool prime[],
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++) {
if (prime[p]) {
for ( int i = p * 2;
i <= p_size; i += p)
prime[i] = false ;
}
}
}
int LISPrime( int arr[], int n)
{
int lisp[n];
bool prime[N + 1];
memset (prime, true , sizeof (prime));
SieveOfEratosthenes(prime, N);
lisp[0] = prime[arr[0]] ? 1 : 0;
for ( int i = 1; i < n; i++) {
if (!prime[arr[i]]) {
lisp[i] = 0;
continue ;
}
lisp[i] = 1;
for ( int j = 0; j < i; j++) {
if (prime[arr[j]]
&& arr[i] > arr[j]
&& lisp[i] < lisp[j] + 1) {
lisp[i] = lisp[j] + 1;
}
}
}
return *max_element(lisp, lisp + n);
}
int main()
{
int arr[] = { 1, 2, 5, 3, 2, 5, 1, 7 };
int M = sizeof (arr) / sizeof (arr[0]);
cout << LISPrime(arr, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 100005 ;
static void SieveOfEratosthenes( boolean prime[],
int p_size)
{
prime[ 0 ] = false ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2 ;
i <= p_size;
i += p)
prime[i] = false ;
}
}
}
static int LISPrime( int arr[], int n)
{
int []lisp = new int [n];
boolean []prime = new boolean [N + 1 ];
for ( int i = 0 ; i < prime.length; i++)
prime[i] = true ;
SieveOfEratosthenes(prime, N);
lisp[ 0 ] = prime[arr[ 0 ]] ? 1 : 0 ;
for ( int i = 1 ; i < n; i++)
{
if (!prime[arr[i]])
{
lisp[i] = 0 ;
continue ;
}
lisp[i] = 1 ;
for ( int j = 0 ; j < i; j++)
{
if (prime[arr[j]] &&
arr[i] > arr[j] &&
lisp[i] < lisp[j] + 1 )
{
lisp[i] = lisp[j] + 1 ;
}
}
}
return Arrays.stream(lisp).max().getAsInt();
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 };
int M = arr.length;
System.out.print(LISPrime(arr, M));
}
}
|
Python3
N = 100005
def SieveOfEratosthenes(prime, p_size):
prime[ 0 ] = False
prime[ 1 ] = False
p = 2
while p * p < = p_size:
if (prime[p]):
for i in range (p * 2 ,
p_size + 1 , p):
prime[i] = False
p + = 1
def LISPrime(arr, n):
lisp = [ 0 ] * n
prime = [ True ] * (N + 1 )
SieveOfEratosthenes(prime, N)
if prime[arr[ 0 ]]:
lisp[ 0 ] = 1
else :
lisp[ 0 ] = 0
for i in range ( 1 , n):
if ( not prime[arr[i]]):
lisp[i] = 0
continue
lisp[i] = 1
for j in range (i):
if (prime[arr[j]] and
arr[i] > arr[j] and
lisp[i] < lisp[j] + 1 ):
lisp[i] = lisp[j] + 1
return max (lisp)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 3 ,
2 , 5 , 1 , 7 ]
M = len (arr)
print (LISPrime(arr, M))
|
C#
using System;
using System.Linq;
class GFG{
static readonly int N = 100005;
static void SieveOfEratosthenes( bool []prime,
int p_size)
{
prime[0] = false ;
prime[1] = false ;
for ( int p = 2; p * p <= p_size; p++)
{
if (prime[p])
{
for ( int i = p * 2;
i <= p_size;
i += p)
prime[i] = false ;
}
}
}
static int LISPrime( int []arr, int n)
{
int []lisp = new int [n];
bool []prime = new bool [N + 1];
for ( int i = 0; i < prime.Length; i++)
prime[i] = true ;
SieveOfEratosthenes(prime, N);
lisp[0] = prime[arr[0]] ? 1 : 0;
for ( int i = 1; i < n; i++)
{
if (!prime[arr[i]])
{
lisp[i] = 0;
continue ;
}
lisp[i] = 1;
for ( int j = 0; j < i; j++)
{
if (prime[arr[j]] &&
arr[i] > arr[j] &&
lisp[i] < lisp[j] + 1)
{
lisp[i] = lisp[j] + 1;
}
}
}
return lisp.Max();
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 5, 3, 2, 5, 1, 7 };
int M = arr.Length;
Console.Write(LISPrime(arr, M));
}
}
|
Javascript
<script>
let N = 100005
function SieveOfEratosthenes(prime, p_size)
{
prime[0] = false ;
prime[1] = false ;
for (let p = 2; p * p <= p_size; p++) {
if (prime[p]) {
for (let i = p * 2;
i <= p_size; i += p)
prime[i] = false ;
}
}
}
function LISPrime(arr, n)
{
let lisp = new Array(n);
let prime = new Array(N + 1);
prime.fill( true );
SieveOfEratosthenes(prime, N);
lisp[0] = prime[arr[0]] ? 1 : 0;
for (let i = 1; i < n; i++) {
if (!prime[arr[i]]) {
lisp[i] = 0;
continue ;
}
lisp[i] = 1;
for (let j = 0; j < i; j++) {
if (prime[arr[j]]
&& arr[i] > arr[j]
&& lisp[i] < lisp[j] + 1) {
lisp[i] = lisp[j] + 1;
}
}
}
return lisp.sort((a, b) => b - a)[0];
}
let arr = [ 1, 2, 5, 3, 2, 5, 1, 7 ];
let M = arr.length;
document.write(LISPrime(arr, M));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
12 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...