Largest divisible subset in array

08 Jul, 2022
Given an array the task is to largest divisible subset in array. A subset is called divisible if for every pair (x, y) in subset, either x divides y or y divides x.
Examples: 
Input  : arr[] = {1, 16, 7, 8, 4}
Output : 16 8 4 1
In the output subset, for every pair,
either the first element divides second
or second divides first.

Input  : arr[] = {2, 4, 3, 8}
Output : 8 4 2
A simple solution is to generate all subsets of given set. For every generated subset, check if it is divisible or not. Finally print the largest divisible subset.
An efficient solution involves following steps. 
Sort all array elements in increasing order. The purpose of sorting is to make sure that all divisors of an element appear before it.Create an array divCount[] of same size as input array. divCount[i] stores size of divisible subset ending with arr[i] (In sorted array). The minimum value of divCount[i] would be 1.Traverse all array elements. For every element, find a divisor arr[j] with largest value of divCount[j] and store the value of divCount[i] as divCount[j] + 1.
Sort all array elements in increasing order. The purpose of sorting is to make sure that all divisors of an element appear before it.
Create an array divCount[] of same size as input array. divCount[i] stores size of divisible subset ending with arr[i] (In sorted array). The minimum value of divCount[i] would be 1.
Traverse all array elements. For every element, find a divisor arr[j] with largest value of divCount[j] and store the value of divCount[i] as divCount[j] + 1.
Implementation:
C++
Java
Python3
C#
Javascript
// C++ program to find largest divisible// subset in a given array#include<bits/stdc++.h>using namespace std; // Prints largest divisible subsetvoid findLargest(int arr[], int n){    // We first sort the array so that all divisors    // of a number are before it.    sort(arr, arr+n);     // To store count of divisors of all elements    vector <int> divCount(n, 1);     // To store previous divisor in result    vector <int> prev(n, -1);     // To store index of largest element in maximum    // size subset    int max_ind = 0;     // In i'th iteration, we find length of chain    // ending with arr[i]    for (int i=1; i<n; i++)    {        // Consider every smaller element as previous        // element.        for (int j=0; j<i; j++)        {            if (arr[i]%arr[j] == 0)            {                if (divCount[i] < divCount[j] + 1)                {                    divCount[i] = divCount[j]+1;                    prev[i] = j;                }            }        }         // Update last index of largest subset if size        // of current subset is more.        if (divCount[max_ind] < divCount[i])            max_ind = i;    }     // Print result    int k = max_ind;    while (k >= 0)    {        cout << arr[k] << " ";        k = prev[k];    }} // Driven codeint main(){    int arr[] = {1, 2, 17, 4};    int n = sizeof(arr)/sizeof(int);    findLargest(arr, n);    return 0;}
// Java program to find largest divisible// subset in a given arrayimport java.io.*;import java.util.*; class DivSubset {     public static int[][] dp;     // Prints largest divisible subset    static void findLargest(int[] arr) {         // array that maintains the maximum index        // till which the condition is satisfied        int divCount[] = new int[arr.length];                 // we will always have atleast one        // element divisible by itself        Arrays.fill(divCount, 1);         // maintain the index of the last increment        int prev[] = new int[arr.length];        Arrays.fill(prev, -1);         // index at which last increment happened        int max_ind = 0;         for (int i = 1; i < arr.length; i++) {            for (int j = 0; j < i; j++) {                 // only increment the maximum index if                // this iteration will increase it                if (arr[i] % arr[j] == 0 &&                    divCount[i] < divCount[j] + 1) {                    prev[i] = j;                    divCount[i] = divCount[j] + 1;                 }             }        // Update last index of largest subset if size        // of current subset is more.            if (divCount[i] > divCount[max_ind])                max_ind = i;        }         // Print result        int k = max_ind;        while (k >= 0) {            System.out.print(arr[k] + " ");            k = prev[k];        }     }     public static void main(String[] args) {         int[] x = { 1, 2, 17, 4};         // sort the array        Arrays.sort(x);         findLargest(x);    }}
# Python 3 program to find largest divisible# subset in a given array # Prints largest divisible subsetdef findLargest(arr, n):         # We first sort the array so that all divisors    # of a number are before it.    arr.sort(reverse = False)     # To store count of divisors of all elements    divCount = [1 for i in range(n)]     # To store previous divisor in result    prev = [-1 for i in range(n)]     # To store index of largest element in maximum    # size subset    max_ind = 0     # In i'th iteration, we find length of chain    # ending with arr[i]    for i in range(1,n):        # Consider every smaller element as previous        # element.        for j in range(i):            if (arr[i] % arr[j] == 0):                if (divCount[i] < divCount[j] + 1):                    divCount[i] = divCount[j]+1                    prev[i] = j         # Update last index of largest subset if size        # of current subset is more.        if (divCount[max_ind] < divCount[i]):            max_ind = i     # Print result    k = max_ind    while (k >= 0):        print(arr[k],end = " ")        k = prev[k] # Driven codeif __name__ == '__main__':    arr = [1, 2, 17, 4]    n = len(arr)    findLargest(arr, n) # This code is contributed by# Surendra_Gangwar
// C# program to find largest divisible// subset in a given arrayusing System; public class DivSubset{    public static int[,] dp;     // Prints largest divisible subset    static void findLargest(int[] arr)    {         // array that maintains the maximum index        // till which the condition is satisfied        int []divCount = new int[arr.Length];                 // we will always have atleast one        // element divisible by itself        for(int i = 0; i < arr.Length; i++)            divCount[i] = 1;         // maintain the index of the last increment        int []prev = new int[arr.Length];        for(int i = 0; i < arr.Length; i++)            prev[i] = -1;          // index at which last increment happened        int max_ind = 0;         for (int i = 1; i < arr.Length; i++)        {            for (int j = 0; j < i; j++)            {                 // only increment the maximum index if                // this iteration will increase it                if (arr[i] % arr[j] == 0 &&                    divCount[i] < divCount[j] + 1)                {                    prev[i] = j;                    divCount[i] = divCount[j] + 1;                 }             }                     // Update last index of largest subset if size        // of current subset is more.            if (divCount[i] > divCount[max_ind])                max_ind = i;        }         // Print result        int k = max_ind;        while (k >= 0)        {            Console.Write(arr[k] + " ");            k = prev[k];        }    }     // Driver code    public static void Main(String[] args)    {        int[] x = { 1, 2, 17, 4};         // sort the array        Array.Sort(x);         findLargest(x);    }} // This code has been contributed by 29AjayKumar
<script> // JavaScript program to find largest divisible// subset in a given arrayimport java.io.*; let dp; // Prints largest divisible subsetfunction findLargest(arr){    // array that maintains the maximum index        // till which the condition is satisfied        let divCount = new Array(arr.length);                   // we will always have atleast one        // element divisible by itself        for(let i=0;i<arr.length;i++)        {            divCount[i]=1;        }           // maintain the index of the last increment        let prev = new Array(arr.length);        for(let i=0;i<arr.length;i++)        {            prev[i]=-1;        }           // index at which last increment happened        let max_ind = 0;           for (let i = 1; i < arr.length; i++) {            for (let j = 0; j < i; j++) {                   // only increment the maximum index if                // this iteration will increase it                if (arr[i] % arr[j] == 0 &&                    divCount[i] < divCount[j] + 1) {                    prev[i] = j;                    divCount[i] = divCount[j] + 1;                   }               }        // Update last index of largest subset if size        // of current subset is more.            if (divCount[i] > divCount[max_ind])                max_ind = i;        }           // Print result        let k = max_ind;        while (k >= 0) {            document.write(arr[k] + " ");            k = prev[k];        }} let x=[1, 2, 17, 4];// sort the arrayx.sort(function(a,b){return a-b;}); findLargest(x);   // This code is contributed by rag2127 </script>
Output:  
4 2 1
Time Complexity : O(n2) Auxiliary Space : O(n)This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
sakshamcse
29AjayKumar
SURENDRA_GANGWAR
ManasChhabra2
rag2127
hardikkoriintern
subset
Arrays
Arrays
subset
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Introduction to Arrays
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
K'th Smallest/Largest Element in Unsorted Array | Set 1
Python | Using 2D arrays/lists the right way
Introduction to Data Structures