Largest divisible subset in array


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.


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.

  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.
  2. 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.
  3. 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.

Below is C++ implementation of above steps.

// C++ program to find largest divisible
// subset in a given array
using namespace std;

// Prints largest divisble subset
void 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 code
int main()
    int arr[] = {1, 2, 17, 4};
    int n = sizeof(arr)/sizeof(int);
    findLargest(arr, n);
    return 0;


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 or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at to report any issue with the above content.

Recommended Posts:

3.2 Average Difficulty : 3.2/5.0
Based on 7 vote(s)

Writing code in comment? Please use, generate link and share the link here.