Given an array of n distinct elements, find length of the largest subset such that every pair in the subset is such that the larger element of the pair is divisible by smaller element. **Examples:**

Input : arr[] = {10, 5, 3, 15, 20} Output : 3 Explanation: The largest subset is 10, 5, 20. 10 is divisible by 5, and 20 is divisible by 10. Input : arr[] = {18, 1, 3, 6, 13, 17} Output : 4 Explanation: The largest subset is 18, 1, 3, 6, In the subsequence, 3 is divisible by 1, 6 by 3 and 18 by 6.

This can be solved using Dynamic Programming. We traverse the sorted array from the end. For every element a[i], we compute dp[i] where dp[i] indicates size of largest divisible subset where a[i] is the smallest element. We can compute dp[i] in array using values from dp[i+1] to dp[n-1]. Finally, we return the maximum value from dp[].

Below is the implementation of the above approach:

## C++

`// CPP program to find the largest subset which` `// where each pair is divisible.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// function to find the longest Subsequence` `int` `largestSubset(` `int` `a[], ` `int` `n)` `{` ` ` `// dp[i] is going to store size of largest` ` ` `// divisible subset beginning with a[i].` ` ` `int` `dp[n];` ` ` `// Since last element is largest, d[n-1] is 1` ` ` `dp[n - 1] = 1;` ` ` `// Fill values for smaller elements.` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) {` ` ` `// Find all multiples of a[i] and consider` ` ` `// the multiple that has largest subset` ` ` `// beginning with it.` ` ` `int` `mxm = 0;` ` ` `for` `(` `int` `j = i + 1; j < n; j++)` ` ` `if` `(a[j] % a[i] == 0 || a[i] % a[j] == 0)` ` ` `mxm = max(mxm, dp[j]);` ` ` `dp[i] = 1 + mxm;` ` ` `}` ` ` `// Return maximum value from dp[]` ` ` `return` `*max_element(dp, dp + n);` `}` `// driver code to check the above function` `int` `main()` `{` ` ` `int` `a[] = { 1, 3, 6, 13, 17, 18 };` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]);` ` ` `cout << largestSubset(a, n) << endl;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`import` `java.util.Arrays;` `// Java program to find the largest` `// subset which was each pair` `// is divisible.` `class` `GFG {` ` ` `// function to find the longest Subsequence` ` ` `static` `int` `largestSubset(` `int` `[] a, ` `int` `n)` ` ` `{` ` ` `// dp[i] is going to store size of largest` ` ` `// divisible subset beginning with a[i].` ` ` `int` `[] dp = ` `new` `int` `[n];` ` ` `// Since last element is largest, d[n-1] is 1` ` ` `dp[n - ` `1` `] = ` `1` `;` ` ` `// Fill values for smaller elements.` ` ` `for` `(` `int` `i = n - ` `2` `; i >= ` `0` `; i--) {` ` ` `// Find all multiples of a[i] and consider` ` ` `// the multiple that has largest subset` ` ` `// beginning with it.` ` ` `int` `mxm = ` `0` `;` ` ` `for` `(` `int` `j = i + ` `1` `; j < n; j++) {` ` ` `if` `(a[j] % a[i] == ` `0` `|| a[i] % a[j] == ` `0` `) {` ` ` `mxm = Math.max(mxm, dp[j]);` ` ` `}` ` ` `}` ` ` `dp[i] = ` `1` `+ mxm;` ` ` `}` ` ` `// Return maximum value from dp[]` ` ` `return` `Arrays.stream(dp).max().getAsInt();` ` ` `}` ` ` `// driver code to check the above function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `[] a = { ` `1` `, ` `3` `, ` `6` `, ` `13` `, ` `17` `, ` `18` `};` ` ` `int` `n = a.length;` ` ` `System.out.println(largestSubset(a, n));` ` ` `}` `}` `/* This JAVA code is contributed by Rajput-Ji*/` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the largest ` `# subset where each pair is divisible.` `# function to find the longest Subsequence` `def` `largestSubset(a, n):` ` ` ` ` `# dp[i] is going to store size ` ` ` `# of largest divisible subset ` ` ` `# beginning with a[i].` ` ` `dp ` `=` `[` `0` `for` `i ` `in` `range` `(n)]` ` ` ` ` `# Since last element is largest,` ` ` `# d[n-1] is 1` ` ` `dp[n ` `-` `1` `] ` `=` `1` `; ` ` ` `# Fill values for smaller elements` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `, ` `-` `1` `, ` `-` `1` `):` ` ` ` ` `# Find all multiples of a[i] ` ` ` `# and consider the multiple ` ` ` `# that has largest subset ` ` ` `# beginning with it. ` ` ` `mxm ` `=` `0` `;` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n):` ` ` `if` `a[j] ` `%` `a[i] ` `=` `=` `0` `or` `a[i] ` `%` `a[j] ` `=` `=` `0` `:` ` ` `mxm ` `=` `max` `(mxm, dp[j])` ` ` `dp[i] ` `=` `1` `+` `mxm` ` ` ` ` `# Return maximum value from dp[] ` ` ` `return` `max` `(dp)` `# Driver Code` `a ` `=` `[ ` `1` `, ` `3` `, ` `6` `, ` `13` `, ` `17` `, ` `18` `]` `n ` `=` `len` `(a)` `print` `(largestSubset(a, n))` `# This code is contributed by` `# sahil shelangia` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the largest` `// subset which where each pair` `// is divisible.` `using` `System;` `using` `System.Linq;` `public` `class` `GFG {` ` ` `// function to find the longest Subsequence` ` ` `static` `int` `largestSubset(` `int` `[] a, ` `int` `n)` ` ` `{` ` ` `// dp[i] is going to store size of largest` ` ` `// divisible subset beginning with a[i].` ` ` `int` `[] dp = ` `new` `int` `[n];` ` ` `// Since last element is largest, d[n-1] is 1` ` ` `dp[n - 1] = 1;` ` ` `// Fill values for smaller elements.` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) {` ` ` `// Find all multiples of a[i] and consider` ` ` `// the multiple that has largest subset` ` ` `// beginning with it.` ` ` `int` `mxm = 0;` ` ` `for` `(` `int` `j = i + 1; j < n; j++)` ` ` `if` `(a[j] % a[i] == 0 | a[i] % a[j] == 0)` ` ` `mxm = Math.Max(mxm, dp[j]);` ` ` `dp[i] = 1 + mxm;` ` ` `}` ` ` `// Return maximum value from dp[]` ` ` `return` `dp.Max();` ` ` `}` ` ` `// driver code to check the above function` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `int` `[] a = { 1, 3, 6, 13, 17, 18 };` ` ` `int` `n = a.Length;` ` ` `Console.WriteLine(largestSubset(a, n));` ` ` `}` `}` `// This code is contributed by vt_m.` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// PHP program to find the` `// largest subset which` `// where each pair is` `// divisible.` `// function to find the` `// longest Subsequence` `function` `largestSubset(` `$a` `, ` `$n` `)` `{` ` ` `// dp[i] is going to ` ` ` `// store size of largest` ` ` `// divisible subset ` ` ` `// beginning with a[i].` ` ` `$dp` `= ` `array` `();` ` ` `// Since last element is ` ` ` `// largest, d[n-1] is 1` ` ` `$dp` `[` `$n` `- 1] = 1;` ` ` `// Fill values for ` ` ` `// smaller elements.` ` ` `for` `(` `$i` `= ` `$n` `- 2; ` `$i` `>= 0; ` `$i` `--) ` ` ` `{` ` ` `// Find all multiples of` ` ` `// a[i] and consider` ` ` `// the multiple that ` ` ` `// has largest subset ` ` ` `// beginning with it.` ` ` `$mxm` `= 0;` ` ` `for` `(` `$j` `= ` `$i` `+ 1; ` `$j` `< ` `$n` `; ` `$j` `++)` ` ` `if` `(` `$a` `[` `$j` `] % ` `$a` `[` `$i` `] == 0 ` `or` `$a` `[` `$i` `] % ` `$a` `[` `$j` `] == 0)` ` ` `$mxm` `= max(` `$mxm` `, ` `$dp` `[` `$j` `]);` ` ` `$dp` `[` `$i` `] = 1 + ` `$mxm` `;` ` ` `}` ` ` `// Return maximum value` ` ` `// from dp[]` ` ` `return` `max(` `$dp` `);` `}` ` ` `// Driver Code` ` ` `$a` `= ` `array` `(1, 3, 6, 13, 17, 18);` ` ` `$n` `= ` `count` `(` `$a` `);` ` ` `echo` `largestSubset(` `$a` `, ` `$n` `);` ` ` `// This code is contributed by anuj_67.` `?>` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(n*n)

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.

## Recommended Posts:

- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Largest divisible subset in array
- Size of the largest divisible subset in an Array
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Subset with no pair sum divisible by K
- Subset with sum divisible by m
- Largest subset whose all elements are Fibonacci numbers
- Largest subset with sum of every pair as prime
- Largest subset with M as smallest missing number
- Largest subset with maximum difference as 1
- Largest subset where absolute difference of any two element is a power of 2
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Find the Largest divisor Subset in the Array
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Largest subset having with sum less than equal to sum of respective indices
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Largest K digit number divisible by X

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.