Longest increasing subsequence consisting of elements from indices divisible by previously selected indices
Last Updated :
29 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int findMaxLength( int N, vector< int > arr)
{
vector< int > dp(N + 1, 1);
for ( int i = 1; i <= N; i++) {
for ( int j = 2 * i; j <= N; j += i) {
if (arr[i - 1] < arr[j - 1]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
return *max_element(dp.begin(), dp.end());
}
int main()
{
vector< int > arr{ 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.size();
cout << findMaxLength(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMaxLength( int N, int [] arr)
{
int [] dp = new int [N + 1 ];
Arrays.fill(dp, 1 );
for ( int i = 1 ; i <= N; i++)
{
for ( int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1 ] < arr[j - 1 ])
{
dp[j] = Math.max(dp[j], dp[i] + 1 );
}
}
}
return Arrays.stream(dp).max().getAsInt();
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int N = arr.length;
System.out.print(findMaxLength(N, arr));
}
}
|
Python3
def findMaxLength(N, arr):
dp = [ 1 ] * (N + 1 )
for i in range ( 1 , N + 1 ):
for j in range ( 2 * i, N + 1 , i):
if (arr[i - 1 ] < arr[j - 1 ]):
dp[j] = max (dp[j], dp[i] + 1 )
return max (dp)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
N = len (arr)
print (findMaxLength(N, arr))
|
C#
using System;
using System.Linq;
class GFG{
static int findMaxLength( int N, int [] arr)
{
int [] dp = new int [N + 1];
for ( int i = 1; i <= N; i++)
{
dp[i] = 1;
}
for ( int i = 1; i <= N; i++)
{
for ( int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1] < arr[j - 1])
{
dp[j] = Math.Max(dp[j], dp[i] + 1);
}
}
}
return dp.Max();;
}
public static void Main()
{
int [] arr = { 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.Length;
Console.WriteLine(findMaxLength(N, arr));
}
}
|
Javascript
<script>
function findMaxLength(N, arr) {
var dp = Array(N + 1).fill(1);
for (i = 1; i <= N; i++) {
for (j = 2 * i; j <= N; j += i) {
if (arr[i - 1] < arr[j - 1]) {
dp[j] = Math.max(dp[j], dp[i] + 1);
}
}
}
return Math.max.apply(Math,dp);
}
var arr = [ 2, 3, 4, 5, 6, 7, 8, 9 ];
var N = arr.length;
document.write(findMaxLength(N, arr));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...