Length of the longest increasing subsequence such that no two adjacent elements are coprime

Given an array arr[] of size N. The task is to find the length of the longest subsequence from the given array such that the sequence is strictly increasing and no two adjacent elements are coprime.

Note: The elements in the given array are strictly increasing in order (1 <= a[i] <= 105)

Examples:

Input : a[] = { 1, 2, 3, 4, 5, 6}
Output : 3
Explanation : Possible sub sequences are {1}, {2}, {3}, {4}, {5}, {6}, {2, 4}, {2, 4, 6}, {2, 6}, {4, 6}, {3, 6}.
The subsequence {2, 4, 6} has the longest length.

Input : a[] = { 1, 1, 1, 1}
Output : 1

Approach : The main idea is to use the concept of Dynamic progrraming. Let’s define dp[x] as the maximal value of the length of the subsequence whose last element is x, and define d[i] as the (maximal value of dp[x] where x is divisible by i).

We should calculate dp[x] in the increasing order of x. The value of dp[x] is (maximal value of d[i] where i is a divisor of x) + 1. After we calculate dp[x], for each divisor i of x, we should update d[i] too.

This algorithm works in O(N*logN) because the sum of the number of the divisor from 1 to N is O(N*logN).

Note : There is a corner case. When the set is {1}, you should output 1.

Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the length of the
// longest  increasing sub sequence from the given array
// such that no two adjacent elements are co prime
  
#include <bits/stdc++.h>
using namespace std;
#define N 100005
  
// Function to find the length of the
// longest  increasing sub sequence from the given array
// such that no two adjacent elements are co prime
int LIS(int a[], int n)
{
    // To store dp and d value
    int dp[N], d[N];
  
    // To store required answer
    int ans = 0;
  
    // For all elements in the array
    for (int i = 0; i < n; i++) {
        // Initially answer is one
        dp[a[i]] = 1;
  
        // For all it's divisors
        for (int j = 2; j * j <= a[i]; j++) {
            if (a[i] % j == 0) {
                // Update the dp value
                dp[a[i]] = max(dp[a[i]], dp[d[j]] + 1);
                dp[a[i]] = max(dp[a[i]], dp[d[a[i] / j]] + 1);
  
                // Update the divisor value
                d[j] = a[i];
                d[a[i] / j] = a[i];
            }
        }
  
        // Check for required answer
        ans = max(ans, dp[a[i]]);
  
        // Update divisor of a[i]
        d[a[i]] = a[i];
    }
  
    // Return reqired answer
    return ans;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5, 6 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << LIS(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the length 
// of the longest increasing sub
// sequence from the given array 
// such that no two adjacent 
// elements are co prime 
  
class GFG
{
    static int N=100005
  
// Function to find the length of the 
// longest increasing sub sequence 
// from the given array such that 
// no two adjacent elements are co prime 
static int LIS(int a[], int n) 
    // To store dp and d value 
    int dp[]=new int[N], d[]=new int[N]; 
  
    // To store required answer 
    int ans = 0
  
    // For all elements in the array 
    for (int i = 0; i < n; i++) 
    
        // Initially answer is one 
        dp[a[i]] = 1
  
        // For all it's divisors 
        for (int j = 2; j * j <= a[i]; j++) 
        
            if (a[i] % j == 0
            
                // Update the dp value 
                dp[a[i]] = Math.max(dp[a[i]], dp[d[j]] + 1); 
                dp[a[i]] = Math.max(dp[a[i]], dp[d[a[i] / j]] + 1); 
  
                // Update the divisor value 
                d[j] = a[i]; 
                d[a[i] / j] = a[i]; 
            
        
  
        // Check for required answer 
        ans = Math.max(ans, dp[a[i]]); 
  
        // Update divisor of a[i] 
        d[a[i]] = a[i]; 
    
  
    // Return reqired answer 
    return ans; 
  
// Driver code 
public static void main(String args[]) 
    int a[] = { 1, 2, 3, 4, 5, 6 }; 
  
    int n = a.length; 
  
    System.out.print( LIS(a, n)); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the length of the
# longest increasing sub sequence from the 
# given array such that no two adjacent 
# elements are co prime
N = 100005
  
# Function to find the length of the
# longest increasing sub sequence from 
# the given array such that no two 
# adjacent elements are co prime
def LIS(a, n):
      
    # To store dp and d value
    dp = [0 for i in range(N)]
    d = [0 for i in range(N)]
  
    # To store required answer
    ans = 0
  
    # For all elements in the array
    for i in range(n):
          
        # Initially answer is one
        dp[a[i]] = 1
  
        # For all it's divisors
        for j in range(2, a[i]):
            if j * j > a[i]:
                break
            if (a[i] % j == 0):
                  
                # Update the dp value
                dp[a[i]] = max(dp[a[i]], 
                               dp[d[j]] + 1)
                dp[a[i]] = max(dp[a[i]], 
                               dp[d[a[i] // j]] + 1)
  
                # Update the divisor value
                d[j] = a[i]
                d[a[i] // j] = a[i]
  
        # Check for required answer
        ans = max(ans, dp[a[i]])
  
        # Update divisor of a[i]
        d[a[i]] = a[i]
  
    # Return reqired answer
    return ans
  
# Driver code
a = [1, 2, 3, 4, 5, 6]
  
n = len(a)
  
print(LIS(a, n))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the length 
// of the longest increasing sub 
// sequence from the given array 
// such that no two adjacent 
// elements are co prime 
using System;
  
class GFG 
    static int N = 100005; 
  
    // Function to find the length of the 
    // longest increasing sub sequence 
    // from the given array such that 
    // no two adjacent elements are co prime 
    static int LIS(int []a, int n) 
    
        // To store dp and d value 
        int []dp = new int[N];
        int []d = new int[N]; 
      
        // To store required answer 
        int ans = 0; 
      
        // For all elements in the array 
        for (int i = 0; i < n; i++) 
        
            // Initially answer is one 
            dp[a[i]] = 1; 
      
            // For all it's divisors 
            for (int j = 2; j * j <= a[i]; j++) 
            
                if (a[i] % j == 0) 
                
                    // Update the dp value 
                    dp[a[i]] = Math.Max(dp[a[i]], dp[d[j]] + 1); 
                    dp[a[i]] = Math.Max(dp[a[i]], dp[d[a[i] / j]] + 1); 
      
                    // Update the divisor value 
                    d[j] = a[i]; 
                    d[a[i] / j] = a[i]; 
                
            
      
            // Check for required answer 
            ans = Math.Max(ans, dp[a[i]]); 
      
            // Update divisor of a[i] 
            d[a[i]] = a[i]; 
        
      
        // Return reqired answer 
        return ans; 
    
      
    // Driver code 
    public static void Main() 
    
        int []a = { 1, 2, 3, 4, 5, 6 }; 
      
        int n = a.Length; 
      
        Console.WriteLine(LIS(a, n)); 
    }
  
// This code is contributed by Ryuga

chevron_right


PHP

Output:

3


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.