Given an array arr[] of size N, the task is to find the longest increasing sub-sequence such that index of any element is divisible by index of previous element (LIIDS). The following are the necessary conditions for the LIIDS:
If i, j are two indices in the given array. Then:
- i < j
- j % i = 0
- arr[i] < arr[j]
Examples:
Input: arr[] = {1, 2, 3, 7, 9, 10}
Output: 3
Explanation:
The subsequence = {1, 2, 7}
Indices of the elements of sub-sequence = {1, 2, 4}
Indices condition:
2 is divisible by 1
4 is divisible by 2
OR
Another possible sub-sequence = {1, 3, 10}
Indices of elements of sub-sequence = {1, 3, 6}
Indices condition:
3 is divisible by 1
6 is divisible by 3
Input: arr[] = {7, 1, 3, 4, 6}
Output: 2
Explanation:
The sub-sequence = {1, 4}
Indices of elements of sub-sequence = {2, 4}
Indices condition:
4 is divisible by 2
Approach: The idea is to use the concept of Dynamic programming to solve this problem.
- Create a dp[] array first and initialize the array with 1. This is because, the minimum length of the dividing subsequence is 1.
- Now, for every index ‘i’ in the array, increment the count of the values at the indices at all its multiples.
- Finally, when the above step is performed for all the values, the maximum value present in the array is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LIIDS( int arr[], int N)
{
int dp[N + 1];
int ans = 0;
for ( int i = 1; i <= N; i++) {
dp[i] = 1;
}
for ( int i = 1; i <= N; i++) {
for ( int j = i + i; j <= N; j += i) {
if (arr[j] > arr[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
ans = max(ans, dp[i]);
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 3, 7, 9, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << LIIDS(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int LIIDS( int arr[], int N)
{
int [] dp = new int [N + 1 ];
int ans = 0 ;
for ( int i = 1 ; i <= N; i++)
{
dp[i] = 1 ;
}
for ( int i = 1 ; i <= N; i++)
{
for ( int j = i + i; j <= N; j += i)
{
if (j < arr.length && arr[j] > arr[i])
{
dp[j] = Math.max(dp[j], dp[i] + 1 );
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 7 , 9 , 10 };
int N = arr.length;
System.out.println(LIIDS(arr, N));
}
}
|
Python3
def LIIDS(arr, N):
dp = []
for i in range ( 1 , N + 1 ):
dp.append( 1 )
ans = 0
for i in range ( 1 , N + 1 ):
j = i + i
while j < = N:
if j < N and i < N and arr[j] > arr[i]:
dp[j] = max (dp[j], dp[i] + 1 )
j + = i
if i < N:
ans = max (ans, dp[i])
return ans
arr = [ 1 , 2 , 3 , 7 , 9 , 10 ]
N = len (arr)
print (LIIDS(arr, N))
|
C#
using System;
class GFG{
static int LIIDS( int [] arr, int N)
{
int [] dp = new int [N + 1];
int ans = 0;
for ( int i = 1; i <= N; i++)
{
dp[i] = 1;
}
for ( int i = 1; i <= N; i++)
{
for ( int j = i + i; j <= N; j += i)
{
if (j < arr.Length && arr[j] > arr[i])
{
dp[j] = Math.Max(dp[j], dp[i] + 1);
}
}
ans = Math.Max(ans, dp[i]);
}
return ans;
}
public static void Main()
{
int [] arr = new int [] { 1, 2, 3, 7, 9, 10 };
int N = arr.Length;
Console.WriteLine(LIIDS(arr, N));
}
}
|
Javascript
<script>
function LIIDS(arr, N)
{
let dp = [];
let ans = 0;
for (let i = 1; i <= N; i++)
{
dp[i] = 1;
}
for (let i = 1; i <= N; i++)
{
for (let j = i + i; j <= N; j += i)
{
if (j < arr.length && arr[j] > arr[i])
{
dp[j] = Math.max(dp[j], dp[i] + 1);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
let arr = [ 1, 2, 3, 7, 9, 10 ];
let N = arr.length;
document.write(LIIDS(arr, N));
</script>
|
Time Complexity: O(N log(N)), where N is the length of the array.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Apr, 2021
Like Article
Save Article