Size of the largest divisible subset in an Array
Given an array arr[] of size N. The task is to find the size of the set of numbers from the given array such that each number divides another or is divisible by another.
Examples:
Input : arr[] = {3, 4, 6, 8, 10, 18, 21, 24}
Output : 3
One of the possible sets with a maximum size is {3, 6, 18}
Input : arr[] = {2, 3, 4, 8, 16}
Output : 4
Approach:
- Let’s take all the numbers in increasing order.
- Note that set X = xi, …, ?xk} is acceptable if xi divides xi+1 for (1 ? i ? k – 1).
- Therefore, dp[x] is equal to the length of the longest suitable increasing subsequence starting at the number x.
- DP Relation: dp[x] = max(dp[x], 1 + dp[y]) if x divides y.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
int maximum_set( int a[], int n)
{
int dp[N] = { 0 };
for ( int i = 0; i < n; i++)
dp[a[i]] = 1;
int ans = 1;
for ( int i = N - 1; i >= 1; i--) {
if (dp[i] != 0) {
for ( int j = 2 * i; j < N; j += i) {
dp[i] = max(dp[i], 1 + dp[j]);
ans = max(ans, dp[i]);
}
}
}
return ans;
}
int main()
{
int arr[] = { 3, 4, 6, 8, 10, 18, 21, 24 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maximum_set(arr, n);
return 0;
}
|
Java
class GFG
{
final static int N = 1000005 ;
static int maximum_set( int a[], int n)
{
int dp[] = new int [N] ;
for ( int i = 0 ; i < n; i++)
dp[a[i]] = 1 ;
int ans = 1 ;
for ( int i = N - 1 ; i >= 1 ; i--)
{
if (dp[i] != 0 )
{
for ( int j = 2 * i; j < N; j += i)
{
dp[i] = Math.max(dp[i], 1 + dp[j]);
ans = Math.max(ans, dp[i]);
}
}
}
return ans;
}
public static void main (String[] args)
{
int arr[] = { 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 };
int n = arr.length;
System.out.println(maximum_set(arr, n));
}
}
|
Python
N = 1000005
def maximum_set(a, n):
dp = [ 0 for i in range (N)]
for i in a:
dp[i] = 1
ans = 1
for i in range (N - 1 , 0 , - 1 ):
if (dp[i] ! = 0 ):
for j in range ( 2 * i, N, i):
dp[i] = max (dp[i], 1 + dp[j])
ans = max (ans, dp[i])
return ans
arr = [ 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 ]
n = len (arr)
print (maximum_set(arr, n))
|
C#
using System;
class GFG
{
static int N = 1000005 ;
static int maximum_set( int []a, int n)
{
int []dp = new int [N] ;
for ( int i = 0; i < n; i++)
dp[a[i]] = 1;
int ans = 1;
for ( int i = N - 1; i >= 1; i--)
{
if (dp[i] != 0)
{
for ( int j = 2 * i; j < N; j += i)
{
dp[i] = Math.Max(dp[i], 1 + dp[j]);
ans = Math.Max(ans, dp[i]);
}
}
}
return ans;
}
public static void Main()
{
int []arr = { 3, 4, 6, 8, 10, 18, 21, 24 };
int n = arr.Length;
Console.WriteLine(maximum_set(arr, n));
}
}
|
Javascript
<script>
let N = 1000005
function maximum_set(a, n) {
let dp = new Array(N).fill(0);
for (let i = 0; i < n; i++)
dp[a[i]] = 1;
let ans = 1;
for (let i = N - 1; i >= 1; i--) {
if (dp[i] != 0) {
for (let j = 2 * i; j < N; j += i) {
dp[i] = Math.max(dp[i], 1 + dp[j]);
ans = Math.max(ans, dp[i]);
}
}
}
return ans;
}
let arr = [3, 4, 6, 8, 10, 18, 21, 24];
let n = arr.length;
document.write(maximum_set(arr, n));
</script>
|
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(n), where n is the size of the given array.
Last Updated :
06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...