Longest sub-sequence that satisfies the given conditions
Last Updated :
08 Dec, 2021
Given an array arr[] of N integers, the task is to find the longest sub-sequence in the given array such that for all pairs from the sub-sequence (arr[i], arr[j]) where i != j either arr[i] divides arr[j] or vice versa. If no such sub-sequence exists then print -1.
Examples:
Input: arr[] = {2, 4, 6, 1, 3, 11}
Output: 3
Longest valid sub-sequences are {1, 2, 6} and {1, 3, 6}.
Input: arr[] = {21, 22, 6, 4, 13, 7, 332}
Output: 2
Approach: This problem is a simple variation of the longest increasing sub-sequence problem. What changes is the base condition and the trick to reduce the number of computations by sorting the given array.
- First sort the given array so that we only need to check values where arr[i] > arr[j] for i > j.
- Then we move forward using two loops, outer loop runs from 1 to N and the inner loop runs from 0 to i.
- Now in the inner loop we have to find the number of arr[j] where j is from 0 to i – 1 which divides the element arr[i].
- And the recurrence relation will be dp[i] = max(dp[i], 1 + dp[j]).
- We will update the max dp[i] value in a variable named res which will be the final answer.
Below is the implementation of the above approach:
C++
CPP
#include <bits/stdc++.h>
using namespace std;
int find( int n, int a[])
{
sort(a, a + n);
int res = 1;
int dp[n];
dp[0] = 1;
for ( int i = 1; i < n; i++) {
dp[i] = 1;
for ( int j = 0; j < i; j++) {
if (a[i] % a[j] == 0) {
dp[i] = std::max(dp[i], 1 + dp[j]);
}
}
res = std::max(res, dp[i]);
}
return (res == 1) ? -1 : res;
}
int main()
{
int a[] = { 2, 4, 6, 1, 3, 11 };
int n = sizeof (a) / sizeof ( int );
cout << find(n, a);
return 0;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int find( int n, int a[])
{
Arrays.sort(a);
int res = 1 ;
int dp[] = new int [n];
dp[ 0 ] = 1 ;
for ( int i = 1 ; i < n; i++)
{
dp[i] = 1 ;
for ( int j = 0 ; j < i; j++)
{
if (a[i] % a[j] == 0 )
{
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
res = Math.max(res, dp[i]);
}
return (res == 1 ) ? - 1 : res;
}
public static void main (String[] args)
{
int a[] = { 2 , 4 , 6 , 1 , 3 , 11 };
int n = a.length;
System.out.println (find(n, a));
}
}
|
Python3
def find(n, a) :
a.sort();
res = 1 ;
dp = [ 0 ] * n;
dp[ 0 ] = 1 ;
for i in range ( 1 , n) :
dp[i] = 1 ;
for j in range (i) :
if (a[i] % a[j] = = 0 ) :
dp[i] = max (dp[i], 1 + dp[j]);
res = max (res, dp[i]);
if (res = = 1 ):
return - 1
else :
return res;
if __name__ = = "__main__" :
a = [ 2 , 4 , 6 , 1 , 3 , 11 ];
n = len (a);
print (find(n, a));
|
C#
using System;
class GFG
{
static int find( int n, int []a)
{
Array.Sort(a);
int res = 1;
int []dp = new int [n];
dp[0] = 1;
for ( int i = 1; i < n; i++)
{
dp[i] = 1;
for ( int j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
dp[i] = Math.Max(dp[i], 1 + dp[j]);
}
}
res = Math.Max(res, dp[i]);
}
return (res == 1) ? -1 : res;
}
public static void Main ()
{
int []a = { 2, 4, 6, 1, 3, 11 };
int n = a.Length;
Console.WriteLine(find(n, a));
}
}
|
javascript
<script>
function find(n , a)
{
a.sort();
var res = 1;
var dp = Array.from({length: n}, (_, i) => 0);
dp[0] = 1;
for ( var i = 1; i < n; i++)
{
dp[i] = 1;
for ( var j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
res = Math.max(res, dp[i]);
}
return (res == 1) ? -1 : res;
}
var a = [ 2, 4, 6, 1, 3, 11 ];
var n = a.length;
document.write(find(n, a));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...