# Length of longest increasing index dividing subsequence

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ program to find the length of ` `// the longest increasing sub-sequence ` `// such that the index of the element is ` `// divisible by index of previous element ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the length of ` `// the longest increasing sub-sequence ` `// such that the index of the element is ` `// divisible by index of previous element ` `int` `LIIDS(``int` `arr[], ``int` `N) ` `{ ` `    ``// Initialize the dp[] array with 1 as a ` `    ``// single element will be of 1 length ` `    ``int` `dp[N + 1]; ` ` `  `    ``int` `ans = 0; ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` `        ``dp[i] = 1; ` `    ``} ` ` `  `    ``// Traverse the given array ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` ` `  `        ``// If the index is divisible by ` `        ``// the previous index ` `        ``for` `(``int` `j = i + i; j <= N; j += i) { ` ` `  `            ``// if increasing ` `            ``// subsequence identified ` `            ``if` `(arr[j] > arr[i]) { ` `                ``dp[j] = max(dp[j], dp[i] + 1); ` `            ``} ` `        ``} ` ` `  `        ``// Longest length is stored ` `        ``ans = max(ans, dp[i]); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `arr[] = { 1, 2, 3, 7, 9, 10 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << LIIDS(arr, N); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the length of ` `// the longest increasing sub-sequence ` `// such that the index of the element is ` `// divisible by index of previous element ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to find the length of ` `// the longest increasing sub-sequence ` `// such that the index of the element is ` `// divisible by index of previous element ` `static` `int` `LIIDS(``int` `arr[], ``int` `N) ` `{ ` ` `  `    ``// Initialize the dp[] array with 1 as a ` `    ``// single element will be of 1 length ` `    ``int``[] dp = ``new` `int``[N + ``1``]; ` `    ``int` `ans = ``0``; ` `     `  `    ``for``(``int` `i = ``1``; i <= N; i++) ` `    ``{ ` `       ``dp[i] = ``1``; ` `    ``} ` ` `  `    ``// Traverse the given array ` `    ``for``(``int` `i = ``1``; i <= N; i++) ` `    ``{ ` `         `  `       ``// If the index is divisible by ` `       ``// the previous index ` `       ``for``(``int` `j = i + i; j <= N; j += i) ` `       ``{ ` `            `  `          ``// If increasing ` `          ``// subsequence identified ` `          ``if` `(j < arr.length && arr[j] > arr[i]) ` `          ``{ ` `              ``dp[j] = Math.max(dp[j], dp[i] + ``1``); ` `          ``} ` `       ``} ` `        `  `       ``// Longest length is stored ` `       ``ans = Math.max(ans, dp[i]); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``, ``2``, ``3``, ``7``, ``9``, ``10` `}; ` `    ``int` `N = arr.length; ` ` `  `    ``System.out.println(LIIDS(arr, N)); ` `} ` `} ` ` `  `// This code is contributed by AbhiThakur `

Output:

```3
```

Time Complexity: O(N log(N)), where N is the length of the array. My Personal Notes arrow_drop_up 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.

Improved By : abhaysingh290895

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.