📜  数组中最大可分割子集的大小

📅  最后修改于: 2021-05-06 08:23:46             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是从给定的数组中找到一组数字的大小,以使每个数字相除或被另一个整除。

例子:

方法:

  1. 让我们按升序排列所有数字。
  2. 请注意,如果x ix i + 1除以(1≤i≤k – 1) ,则设置X = x i ,…,?x k }是可以接受的。
  3. 因此, dp [x]等于从数字x开始的最长的适当递增子序列的长度。
  4. DP关系:如果x除以y,dp [x] = max(dp [x],1 + dp [y])

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
#include 
using namespace std;
  
#define N 1000005
  
// Function to find the size of the 
//largest divisible subarray
int maximum_set(int a[], int n)
{
    int dp[N] = { 0 };
  
    // Mark all elements of the array
    for (int i = 0; i < n; i++)
        dp[a[i]] = 1;
  
    int ans = 1;
  
    // Traverse reverse
    for (int i = N - 1; i >= 1; i--) {
  
        if (dp[i] != 0) {
            // For all multiples of i
            for (int j = 2 * i; j < N; j += i) {
                dp[i] = max(dp[i], 1 + dp[j]);
                ans = max(ans, dp[i]);
            }
        }
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 4, 6, 8, 10, 18, 21, 24 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << maximum_set(arr, n);
  
    return 0;
}


Java
// Java implementation of the above approach 
class GFG
{
      
    final static int N = 1000005 ;
      
    // Function to find the size of the 
    //largest divisible subarray 
    static int maximum_set(int a[], int n) 
    { 
        int dp[] = new int[N] ; 
      
        // Mark all elements of the array 
        for (int i = 0; i < n; i++) 
            dp[a[i]] = 1; 
      
        int ans = 1; 
      
        // Traverse reverse 
        for (int i = N - 1; i >= 1; i--) 
        { 
      
            if (dp[i] != 0) 
            { 
                // For all multiples of i 
                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 the required answer 
        return ans; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 3, 4, 6, 8, 10, 18, 21, 24 }; 
      
        int n = arr.length; 
      
        // Function call 
        System.out.println(maximum_set(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the above approach
  
N = 1000005
  
# Function to find the size of the
# largest divisible subarray
def maximum_set(a, n):
    dp = [0 for i in range(N)]
  
    # Mark all elements of the array
    for i in a:
        dp[i] = 1
  
    ans = 1
  
    # Traverse reverse
    for i in range(N - 1, 0, -1):
  
        if (dp[i] != 0):
              
            # For all multiples of i
            for j in range(2 * i, N, i):
                dp[i] = max(dp[i], 1 + dp[j])
                ans = max(ans, dp[i])
  
    # Return the required answer
    return ans
  
# Driver code
  
arr = [3, 4, 6, 8, 10, 18, 21, 24]
  
n = len(arr)
  
# Function call
print(maximum_set(arr, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach 
using System;
  
class GFG 
{ 
    static int N = 1000005 ; 
      
    // Function to find the size of the 
    //largest divisible subarray 
    static int maximum_set(int []a, int n) 
    { 
        int []dp = new int[N] ; 
      
        // Mark all elements of the array 
        for (int i = 0; i < n; i++) 
            dp[a[i]] = 1; 
      
        int ans = 1; 
      
        // Traverse reverse 
        for (int i = N - 1; i >= 1; i--) 
        { 
      
            if (dp[i] != 0) 
            { 
                // For all multiples of i 
                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 the required answer 
        return ans; 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 3, 4, 6, 8, 10, 18, 21, 24 }; 
        int n = arr.Length; 
      
        // Function call 
        Console.WriteLine(maximum_set(arr, n)); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
3

时间复杂度: O(n * sqrt(n))