Given an array arr[] consisting of N positive integers, the task is to find the length of the longest increasing subsequence possible by selecting elements from indices that are divisible by all the previously selected indices.
Note: Consider 1-based indexing
Examples:
Input: arr[] = {1, 4, 2, 3, 6, 4, 9}
Output: 3
Explanation: The optimal way is to select elements present at indices 1, 3 & 6. The sequence {1, 2, 4} generated by selecting elements from these indices is increasing and every index is divisible by the previously selected indices.
Therefore, the length is 3.Input: arr[] = {2, 3, 4, 5, 6, 7, 8, 9}
Output: 4
Explanation: The optimal way is to select elements present at indices 1, 2, 4 & 8. The sequence {2, 3, 5, 9} generated by selecting elements from these indices is increasing and every index is divisible by the previously selected indices.
Therefore, the length is 4.
Naive Approach: The simplest approach is to generate all subsequences of array elements possible by selecting from the sequence of indices that are divisible by all the preceding indices in the sequence. Find the length of all these subsequences and print the maximum length obtained.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming. Follow the steps below to solve the problem:
- Initialize an array dp[] of size N. The ith index in the dp[] table, i.e. dp[i], represents the length of the longest possible subsequence of the required type obtained up to ith index.
-
Traverse the array dp[] using variable i, and traverse through all the multiples of i with variable j, such that 2*i ? j ? N.
- For each j, if arr[j] > arr[i], then update the value dp[j] = max(dp[j], dp[i] + 1) to include the length of the longest subsequence.
- Otherwise, check for the next index.
- After completing the above steps, print the maximum element from the array dp[] as the length of the longest subsequence.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find length of longest // subsequence generated that // satisfies the specified conditions int findMaxLength( int N, vector< int > arr)
{ // Stores the length of longest
// subsequences of all lengths
vector< int > dp(N + 1, 1);
// Iterate through the given array
for ( int i = 1; i <= N; i++) {
// Iterate through the multiples i
for ( int j = 2 * i; j <= N; j += i) {
if (arr[i - 1] < arr[j - 1]) {
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return *max_element(dp.begin(), dp.end());
} // Driver Code int main()
{ vector< int > arr{ 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.size();
// Function Call
cout << findMaxLength(N, arr);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG{
// Function to find length of longest // subsequence generated that // satisfies the specified conditions static int findMaxLength( int N, int [] arr)
{ // Stores the length of longest
// subsequences of all lengths
int [] dp = new int [N + 1 ];
Arrays.fill(dp, 1 );
// Iterate through the given array
for ( int i = 1 ; i <= N; i++)
{
// Iterate through the multiples i
for ( int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1 ] < arr[j - 1 ])
{
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = Math.max(dp[j], dp[i] + 1 );
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return Arrays.stream(dp).max().getAsInt();
} // Driver Code public static void main(String[] args)
{ int [] arr = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int N = arr.length;
// Function Call
System.out.print(findMaxLength(N, arr));
} } // This code is contributed by sanjoy_62 |
# Python3 program for the above approach # Function to find length of longest # subsequence generated that # satisfies the specified conditions def findMaxLength(N, arr):
# Stores the length of longest
# subsequences of all lengths
dp = [ 1 ] * (N + 1 )
# Iterate through the given array
for i in range ( 1 , N + 1 ):
# Iterate through the multiples i
for j in range ( 2 * i, N + 1 , i):
if (arr[i - 1 ] < arr[j - 1 ]):
# Update dp[j] as maximum
# of dp[j] and dp[i] + 1
dp[j] = max (dp[j], dp[i] + 1 )
# Return the maximum element in dp[]
# as the length of longest subsequence
return max (dp)
# Driver Code if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
N = len (arr)
# Function Call
print (findMaxLength(N, arr))
# This code is contributed by mohit kumar 29 |
// C# program for the above approach using System;
using System.Linq;
class GFG{
// Function to find length of longest // subsequence generated that // satisfies the specified conditions static int findMaxLength( int N, int [] arr)
{ // Stores the length of longest
// subsequences of all lengths
int [] dp = new int [N + 1];
for ( int i = 1; i <= N; i++)
{
dp[i] = 1;
}
// Iterate through the given array
for ( int i = 1; i <= N; i++)
{
// Iterate through the multiples i
for ( int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1] < arr[j - 1])
{
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = Math.Max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return dp.Max();;
} // Driver Code public static void Main()
{ int [] arr = { 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.Length;
// Function Call
Console.WriteLine(findMaxLength(N, arr));
} } // This code is contributed by susmitakundugoaldanga |
<script> // javascript program for the above approach // Function to find length of longest
// subsequence generated that
// satisfies the specified conditions
function findMaxLength(N, arr) {
// Stores the length of longest
// subsequences of all lengths
var dp = Array(N + 1).fill(1);
// Iterate through the given array
for (i = 1; i <= N; i++) {
// Iterate through the multiples i
for (j = 2 * i; j <= N; j += i) {
if (arr[i - 1] < arr[j - 1]) {
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = Math.max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp
// as the length of longest subsequence
return Math.max.apply(Math,dp);
}
// Driver Code
var arr = [ 2, 3, 4, 5, 6, 7, 8, 9 ];
var N = arr.length;
// Function Call
document.write(findMaxLength(N, arr));
// This code contributed by Rajput-Ji </script> |
4
Time Complexity: O(N*log N)
Auxiliary Space: O(N)