C++ Program to find the Largest Divisible Pairs Subset


To solve a problem in which we are given an array consisting of distinct elements. Now our task is to find the subset such that every pair is evenly divisible, i.e, every large element is divisible by every smaller element, for example.

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.

We will apply dynamic programming to find our answer to this problem, and we’ll see how.

Approach to Find the Solution

In this approach, we are going to sort our array in ascending order. Now we run through the array, starting for the end. Now we maintain a dp array that will contain the size of the largest subset where the ith element is the smallest. Then we return the maximum value from our dp array.

Example

#include <bits/stdc++.h>
using namespace std;
int largestSubsetPair(int *a, int n){
    int dp[n]; // it is going to store the largest subset starting from ith index
    dp[n - 1] = 1; // as last element is the largest so its subset size is 1
    int largest = 0; // ans
    for (int i = n - 2; i >= 0; i--) {
        int maxi = 0; // taking max = 0;
        for (int j = i + 1; j < n; j++)
            if (a[j] % a[i] == 0 || a[i] % a[j] == 0)
                maxi = max(maxi, dp[j]); // if a[j] is divisible by a[i]
                 //so all the elements divisible by a[j] should also follow

        dp[i] = 1 + maxi;
        largest = max(largest, dp[i]);
    }
    return largest;
}
int main(){
    int a[] = { 1, 3, 6, 13, 17, 18 }; // given array
    int n = sizeof(a) / sizeof(int); // size of our array
    cout << largestSubsetPair(a, n) << "\n";
    return 0;
}

Output

4

Explanation of the Above Code

In this approach, we solve the problem by using dynamic programming now. Firstly, we sort our array now. As we sorted our array now, we created an array dp that will store all the previous largest subsets.

Now we start from the end of our array in this. We first assume that our current element is minimum and check for other multiples now as we encounter a multiple ahead of it. We are traveling in reverse, so that means we have encountered that element before. We also have saved their largest subset size inside our dp array now. We add this element in the dp of our current element, and that’s how we proceed.

Conclusion

In this tutorial, we solve a problem to find the Largest divisible pairs subset using Dynamic Programming. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 25-Nov-2021

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements