- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

- Related Questions & Answers
- C++ Program to find the Largest Divisible Subset in Array
- Largest Divisible Subset in C++
- Program to Find K-Largest Sum Pairs in Python
- Python program to find the size of largest subset of anagram words
- Program to find length of the largest subset where one element in every pair is divisible by other in Python
- Python program to find the size of the largest subset of anagram words
- Program to Find Out the Largest K-Divisible Subsequence Sum in Python
- C++ Program for the Largest K digit number divisible by X?
- C++ Program for Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- Java Program to Find the Largest Among Three Numbers
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4
- C# Program to find the largest element from an array
- Java program to find the largest number in an array
- C program to Find the Largest Number Among Three Numbers