Largest subsequence such that all indices and all values are multiples individually
Last Updated :
22 Nov, 2021
Given an array arr[] of N positive integers, the task is to find the largest strictly increasing subsequence of arr[] such that the indices of the selected elements in arr[], and the selected elements are multiples of each other individually.
Note: Consider 1 based indexing for the array arr[].
Examples:
Input: arr[] = {1, 4, 2, 3, 6, 4, 9}
Output: 3
Explanation:
We can choose index 1, 3, 6 and values are 1, 2, 4:
Here every greater index is divisible by smaller index and every greater index value is greater than the smaller index value.
Input: arr[] = {5, 3, 4, 6}
Output: 2
Explanation:
We can choose index 1 and 3 and values are 3 and 6:
Here, every greater index is divisible by smaller index and every greater index value is greater than the smaller index value.
Naive Approach:
The naive approach is to simply generate all possible subsequence and for each subsequence check two conditions:
- first check if elements are in strictly increasing order and
- secondly check if the index of the selected elements in arr[] is multiple of each other.
Out of all possible subsequence which satisfies the given two conditions select the largest subsequence.
Time Complexity: O( N * 2N )
Auxiliary Space: O( N )
Efficient Approach:
We can optimize the code by using Dynamic Programming by avoiding redundant calculation of the repeated sub problem by caching its result.
- Create an array dp[] of size equal to the size of arr[], where dp[i] represents size of largest subsequence till i-th index which satisfies the given conditions.
- Initialize array dp[] with 0.
- Now, iterate the array arr[] from the end.
- For each index find the indices j which divide the current index and check if the value at current index is greater than the element at index j.
- If it is then update dp[j] as:
dp[j] = max(dp[current] + 1, dp[j])
Finally, traverse the array dp[] and print the maximum value.
Below is the implementation of the efficient approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxLength( int arr[], int n)
{
vector< int > dp(n, 1);
for ( int i = n - 1; i > 1; i--) {
for ( int j = 1;
j <= sqrt (i); j++) {
if (i % j == 0) {
int s = i / j;
if (s == j) {
if (arr[i] > arr[s]) {
dp[s] = max(dp[i] + 1,
dp[s]);
}
}
else {
if (s != i
&& arr[i] > arr[s])
dp[s] = max(dp[i] + 1,
dp[s]);
if (arr[i] > arr[j]) {
dp[j] = max(dp[i] + 1,
dp[j]);
}
}
}
}
}
int max = 0;
for ( int i = 1; i < n; i++) {
if (dp[i] > max)
max = dp[i];
}
cout << max << "\n" ;
}
int main()
{
int arr[] = { 0, 1, 4, 2, 3, 6, 4, 9 };
int size = sizeof (arr) / sizeof (arr[0]);
maxLength(arr, size);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void maxLength( int arr[], int n)
{
int dp[] = new int [n];
for ( int i = 1 ; i < n; i++)
{
dp[i] = 1 ;
}
for ( int i = n - 1 ; i > 1 ; i--)
{
for ( int j = 1 ;
j <= Math.sqrt(i); j++)
{
if (i % j == 0 )
{
int s = i / j;
if (s == j)
{
if (arr[i] > arr[s])
{
dp[s] = Math.max(dp[i] + 1 ,
dp[s]);
}
}
else
{
if (s != i && arr[i] > arr[s])
dp[s] = Math.max(dp[i] + 1 ,
dp[s]);
if (arr[i] > arr[j])
{
dp[j] = Math.max(dp[i] + 1 ,
dp[j]);
}
}
}
}
}
int max = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (dp[i] > max)
max = dp[i];
}
System.out.println(max);
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 };
int size = arr.length;
maxLength(arr, size);
}
}
|
Python3
from math import *
def maxLength (arr, n):
dp = [ 1 ] * n
for i in range (n - 1 , 1 , - 1 ):
for j in range ( 1 , int (sqrt(i)) + 1 ):
if (i % j = = 0 ):
s = i / / j
if (s = = j):
if (arr[i] > arr[s]):
dp[s] = max (dp[i] + 1 , dp[s])
else :
if (s ! = i and arr[i] > arr[s]):
dp[s] = max (dp[i] + 1 , dp[s])
if (arr[i] > arr[j]):
dp[j] = max (dp[i] + 1 , dp[j])
Max = 0
for i in range ( 1 , n):
if (dp[i] > Max ):
Max = dp[i]
print ( Max )
if __name__ = = '__main__' :
arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ]
size = len (arr)
maxLength(arr, size)
|
C#
using System;
class GFG{
static void maxLength( int [] arr, int n)
{
int [] dp = new int [n];
for ( int i = 1; i < n; i++)
{
dp[i] = 1;
}
for ( int i = n - 1; i > 1; i--)
{
for ( int j = 1;
j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
int s = i / j;
if (s == j)
{
if (arr[i] > arr[s])
{
dp[s] = Math.Max(dp[i] + 1,
dp[s]);
}
}
else
{
if (s != i && arr[i] > arr[s])
dp[s] = Math.Max(dp[i] + 1,
dp[s]);
if (arr[i] > arr[j])
{
dp[j] = Math.Max(dp[i] + 1,
dp[j]);
}
}
}
}
}
int max = 0;
for ( int i = 1; i < n; i++)
{
if (dp[i] > max)
max = dp[i];
}
Console.WriteLine(max);
}
public static void Main()
{
int [] arr = new int [] { 0, 1, 4, 2,
3, 6, 4, 9 };
int size = arr.Length;
maxLength(arr, size);
}
}
|
Javascript
<script>
function maxLength(arr, n)
{
let dp = [];
for (let i = 1; i < n; i++)
{
dp[i] = 1;
}
for (let i = n - 1; i > 1; i--)
{
for (let j = 1;
j <= Math.sqrt(i); j++)
{
if (i % j == 0)
{
let s = i / j;
if (s == j)
{
if (arr[i] > arr[s])
{
dp[s] = Math.max(dp[i] + 1,
dp[s]);
}
}
else
{
if (s != i && arr[i] > arr[s])
dp[s] = Math.max(dp[i] + 1,
dp[s]);
if (arr[i] > arr[j])
{
dp[j] = Math.max(dp[i] + 1,
dp[j]);
}
}
}
}
}
let max = 0;
for (let i = 1; i < n; i++)
{
if (dp[i] > max)
max = dp[i];
}
document.write(max);
}
let arr = [ 0, 1, 4, 2, 3, 6, 4, 9 ];
let size = arr.length;
maxLength(arr, size);
</script>
|
Time Complexity: O(N*(sqrt(N)) Since, for each index of the array, we calculate its all divisor, this takes O(sqrt(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...