Related Articles

# Print all Longest dividing subsequence in an Array

• Difficulty Level : Expert
• Last Updated : 03 Jul, 2020

Given an array arr[] of N integers, the task is to print all the element of Longest Dividing Subsequence formed by the given array. If we have more than one sequence with maximum length then print all of them.

Examples:

Input: arr[] = { 2, 11, 16, 12, 36, 60, 71 }
Output:
2 12 36
2 12 60
Explanation:
There are two subsequence with maximum length 3.
1. 2, 12, 36
2. 2, 12, 60

Input: arr[] = { 2, 4, 16, 32, 64, 60, 12 };
Output:
2 4 16 32 64
Explanation:
There is only one subsequence with maximum length 5.
1. 2 4 16 32 64

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

Approach:

1. Declare a 2D array LDS[][] which stores the Longest Dividing Subsequence.
2. Traverse the given array arr[] and find the longest dividing subsequence till current element by using the approach discussed in this article.
3. Traverse the given LDS[][] array, and print all the elements of the sequence with maximum length.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`` ` `#include "bits/stdc++.h"``using` `namespace` `std;`` ` `// Function to print LDS[i] element``void` `printLDS(vector<``int``>& Max)``{`` ` `    ``// Traverse the Max[]``    ``for` `(``auto``& it : Max) {``        ``cout << it << ``' '``;``    ``}``}`` ` `// Function to construct and print Longest``// Dividing Subsequence``void` `LongestDividingSeq(``int` `arr[], ``int` `N)``{``    ``// 2D vector for storing sequences``    ``vector > LDS(N);`` ` `    ``// Push the first element to LDS[][]``    ``LDS[0].push_back(arr[0]);`` ` `    ``// Interate over all element``    ``for` `(``int` `i = 1; i < N; i++) {`` ` `        ``// Loop for every index till i``        ``for` `(``int` `j = 0; j < i; j++) {`` ` `            ``// if current elements divides``            ``// arr[i] and length is greater``            ``// than the previous index, then``            ``// insert the current element``            ``// to the sequences LDS[i]``            ``if` `((arr[i] % arr[j] == 0)``                ``&& (LDS[i].size() < LDS[j].size() + 1))``                ``LDS[i] = LDS[j];``        ``}`` ` `        ``// L[i] ends with arr[i]``        ``LDS[i].push_back(arr[i]);``    ``}`` ` `    ``int` `maxLength = 0;`` ` `    ``// LDS stores the sequences till``    ``// each element of arr[]``    ``// Traverse the LDS[][] to find the``    ``// the maximum length``    ``for` `(``int` `i = 0; i < N; i++) {``        ``int` `x = LDS[i].size();``        ``maxLength = max(maxLength, x);``    ``}`` ` `    ``// Print all LDS with maximum length``    ``for` `(``int` `i = 0; i < N; i++) {`` ` `        ``// Find size``        ``int` `size = LDS[i].size();`` ` `        ``// If size = maxLength``        ``if` `(size == maxLength) {`` ` `            ``// Print LDS``            ``printLDS(LDS[i]);``            ``cout << ``'\n'``;``        ``}``    ``}``}`` ` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 11, 16, 12, 36, 60, 71 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`` ` `    ``// Function Call``    ``LongestDividingSeq(arr, N);`` ` `    ``return` `0;``}`

## Python3

 `# Python3 program for the above approach`` ` `# Function to print LDS[i] element``def` `printLDS(``Max``):``     ` `    ``# Traverse the Max[]``    ``for` `it ``in` `Max``:``        ``print``(it, end ``=` `" "``)`` ` `# Function to construct and print ``# Longest Dividing Subsequence``def` `LongestDividingSeq(arr, N):``     ` `    ``# 2D vector for storing sequences``    ``LDS ``=` `[[] ``for` `i ``in` `range``(N)]`` ` `    ``# Push the first element to LDS[][]``    ``LDS[``0``].append(arr[``0``])`` ` `    ``# Interate over all element``    ``for` `i ``in` `range``(``1``, N):``         ` `        ``# Loop for every index till i``        ``for` `j ``in` `range``(i):`` ` `            ``# If current elements divides``            ``# arr[i] and length is greater``            ``# than the previous index, then``            ``# insert the current element``            ``# to the sequences LDS[i]``            ``if` `((arr[i] ``%` `arr[j] ``=``=` `0``) ``and``                ``(``len``(LDS[i]) < ``len``(LDS[j]) ``+` `1``)):``                ``LDS[i] ``=` `LDS[j].copy()`` ` `        ``# L[i] ends with arr[i]``        ``LDS[i].append(arr[i])`` ` `    ``maxLength ``=` `0`` ` `    ``# LDS stores the sequences till``    ``# each element of arr[]``    ``# Traverse the LDS[][] to find ``    ``# the maximum length``    ``for` `i ``in` `range``(N):``        ``x ``=` `len``(LDS[i])``        ``maxLength ``=` `max``(maxLength, x)`` ` `    ``# Print all LDS with maximum length``    ``for` `i ``in` `range``(N):`` ` `        ``# Find size``        ``size ``=` `len``(LDS[i])`` ` `        ``# If size = maxLength``        ``if` `(size ``=``=` `maxLength):`` ` `            ``# Print LDS``            ``printLDS(LDS[i])``            ``print``()`` ` `# Driver Code``arr ``=` `[ ``2``, ``11``, ``16``, ``12``, ``36``, ``60``, ``71` `]``N ``=` `len``(arr)`` ` `# Function call``LongestDividingSeq(arr, N);`` ` `# This code is contributed by ANKITKUMAR34`
Output:
```2 12 36
2 12 60
```

Time Complexity: O(N2)
Auxiliary Space: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up