Longest dividing subsequence
Last Updated :
11 Jul, 2025
You are given an array A of size N. Your task is to find the length of largest dividing sub sequence.A dividing sequence is a sequence a1, a2, …, aN where ai divides aj whenever i < j. For example, 3, 15, 60, 720 is a dividing sequence.
input-
The first line of each test case is N, where N is the size of array.
The second line of each test case contains N space separated integers which is the input for the array.
Output-
the length of largest dividing sub sequence
examples:
Input : arr[] = {2 11 16 12 36 60 71 17 29 144 288 129 432 993}
Output : 5
2 12 36 144 288 is dividing sub sequence of largest size
Input : 1 2 4 8 16
Output : 5
Whole sequence is dividing
This problem is simply a variation of Longest Increasing Subsequence. We can solve this using Dynamic Programming. The idea is to find the longest dividing subsequence ending with every element and finally return maximum of all.
C++
/* Dynamic Programming C++ implementation of lds problem */
#include<bits/stdc++.h>
using namespace std;
/* lds() returns the length of the longest dividing
subsequence in arr[] of size n */
int lds( int arr[], int n )
{
int lds[n];
lds[0] = 1;
/* Compute optimized lds values in bottom up manner */
for (int i = 1; i < n; i++ )
{
lds[i] = 1;
for (int j = 0; j < i; j++ )
if (lds[j] != 0 && arr[i] % arr[j] == 0)
lds[i] = max(lds[i], lds[j] + 1);
}
// Return maximum value in lds[]
return *max_element(lds, lds+n);
}
/* Driver program to test above function */
int main()
{
int arr[] = { 2, 11, 16, 12, 36, 60, 71, 17,
29, 144, 288, 129, 432, 993};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Length of lds is %d\n", lds( arr, n ) );
return 0;
}
Java
/* Dynamic Programming Java implementation of lds problem */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
/* lds() returns the length of the longest dividing
subsequence in arr[] of size n */
static int lds( Integer arr[], int n )
{
Integer lds[]=new Integer[n];
lds[0] = 1;
/* Compute optimized lds values in bottom up manner */
for (int i = 1; i < n; i++ )
{
lds[i] = 1;
for (int j = 0; j < i; j++ )
if (lds[j] != 0 && arr[i] % arr[j] == 0)
lds[i] = Math.max(lds[i], lds[j] + 1);
}
// Return maximum value in lds[]
int max=(int)Collections.max(Arrays.asList(lds));
return max;
}
/* Driver program to test above function */
public static void main(String args[])
{
Integer arr[] = { 2, 11, 16, 12, 36, 60, 71, 17,
29, 144, 288, 129, 432, 993};
int n =arr.length ;
System.out.println("Length of lds is "+lds( arr, n ) );
}
}
Python3
# Dynamic Programming Python3
# implementation of lds problem
# lds() returns the length of the longest
# dividing subsequence in arr[] of size n
def lds(arr, n):
lds = [0 for i in range(n)]
lds[0] = 1
# Compute optimized lds values
# in bottom up manner
for i in range(n):
lds[i] = 1
for j in range(i):
if (lds[j] != 0 and
arr[i] % arr[j] == 0):
lds[i] = max(lds[i], lds[j] + 1)
return max(lds)
# Driver Code
arr = [2, 11, 16, 12, 36, 60, 71, 17,
29, 144, 288, 129, 432, 993]
print("Length of lds is",
lds(arr, len(arr)))
# This code is contributed
# by Mohit Kumar
C#
/* Dynamic Programming C# implementation of lds problem */
using System;
using System.Linq;
public class GFG{
/* lds() returns the length of the longest dividing
subsequence in arr[] of size n */
static int lds( int []arr, int n )
{
int []lds=new int[n];
lds[0] = 1;
/* Compute optimized lds values in bottom up manner */
for (int i = 1; i < n; i++ )
{
lds[i] = 1;
for (int j = 0; j < i; j++ )
if (lds[j] != 0 && arr[i] % arr[j] == 0)
lds[i] = Math.Max(lds[i], lds[j] + 1);
}
// Return maximum value in lds[]
int max=lds.Max();
return max;
}
/* Driver program to test above function */
public static void Main()
{
int []arr = { 2, 11, 16, 12, 36, 60, 71, 17,
29, 144, 288, 129, 432, 993};
int n =arr.Length ;
Console.Write("Length of lds is "+lds( arr, n ) );
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
/* Dynamic Programming Javascript implementation of lds problem */
/* lds() returns the length of the longest dividing
subsequence in arr[] of size n */
function lds(arr,n)
{
let lds = new Array(n);
lds[0] = 1;
/* Compute optimized lds values in bottom up manner */
for (let i = 1; i < n; i++ )
{
lds[i] = 1;
for (let j = 0; j < i; j++ )
if (lds[j] != 0 && arr[i] % arr[j] == 0)
lds[i] = Math.max(lds[i], lds[j] + 1);
}
// Return maximum value in lds[]
let max=Math.max(...lds);
return max;
}
/* Driver program to test above function */
let arr=[2, 11, 16, 12, 36, 60, 71, 17,
29, 144, 288, 129, 432, 993];
let n =arr.length ;
document.write("Length of lds is "+lds( arr, n ) );
// This code is contributed by unknown2108
</script>
Output: Length of lds is 5
Time Complexity: O(N*N )
Auxiliary Space: O(N)
Similar Reads
Longest Decreasing Subsequence Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in strictly decreasing order. Examples: Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85] Output: 3 Explanation: The longest decreasing subsequence is {
14 min read
Longest Decreasing Subsequence Given an array of N integers, find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in strictly decreasing order. Examples: Input: arr[] = [15, 27, 14, 38, 63, 55, 46, 65, 85] Output: 3 Explanation: The longest decreasing subsequence is {
14 min read
Longest Bitonic Subsequence Given an array arr[] containing n positive integers, a subsequence of numbers is called bitonic if it is first strictly increasing, then strictly decreasing. The task is to find the length of the longest bitonic subsequence. Note: Only strictly increasing (no decreasing part) or a strictly decreasin
15+ min read
Longest alternating subsequence A sequence {X1, X2, .. Xn} is an alternating sequence if its elements satisfy one of the following relations : X1 < X2 > X3 < X4 > X5 < â¦. xn or X1 > X2 < X3 > X4 < X5 > â¦. xn Examples: Input: arr[] = {1, 5, 4}Output: 3Explanation: The whole arrays is of the form x1
15+ min read
Longest Increasing Subsequence (LIS) Given an array arr[] of size n, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.Examples: Input: arr[] = [3, 10, 2, 1, 20]Output: 3Explanation: The longest increa
14 min read
Longest Consecutive Subsequence Given an array of integers, the task is to find the length of the longest subsequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order. Examples: Input: arr[] = [2, 6, 1, 9, 4, 5, 3]Output: 6Explanation: The consecutive numbers here are from
9 min read