📌  相关文章
📜  将数组拆分为最小数量的子集,其中子集的每个元素都可以被其最小值整除

📅  最后修改于: 2021-05-14 07:31:35             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是将数组拆分为最小数量的子集,以使每个元素恰好属于一个子集,并且可以被每个子集中存在的最小元素整除。

例子:

方法:可以通过使用“排序”并找到每个子集的最小值来解决该问题。请按照以下步骤解决问题:

  • 以升序对数组arr []进行排序。
  • 使用0和一个数组vis []初始化一个变量ans ,用于存储访问的数组元素。
  • vis []数组的所有位置标记为0 ,表示尚未访问的位置。
  • 遍历给定数组arr []并执行以下步骤:
    • 如果未访问元素arr [i] ,则:
      • 将其视为新子集的最小值,并将ans递增1
      • 使用变量j[i +1,N – 1]范围内进行迭代,如果元素arr [j]没有被访问且可被arr [i]整除,则将vis [j] = 1设置
    • 对每个索引重复上述步骤。
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#define LL long long
#define MM 1000000007
using namespace std;
 
// Function to find the minimum number
// of subsets into which given array
// can be split such that the given
// conditions are satisfied
void groupDivision(int arr[], int n)
{
    LL z, i, j, ans;
 
    // Sort the given array arr[]
    sort(arr, arr + n);
 
    // Initialize answer
    ans = 0;
    LL vis[n + 5] = { 0 };
 
    // Iterate for the smaller value
    // which has not been visited
    for (i = 0; i < n; i++) {
 
        if (!vis[i]) {
 
            // Mark all elements that
            // are divisible by arr[i]
            for (j = i + 1; j < n; j++) {
 
                // If jth index has already
                // been visited
                if (vis[j] == 1)
                    continue;
 
                if (arr[j] % arr[i] == 0)
 
                    // Mark the jth index
                    // as visisted
                    vis[j] = 1;
            }
 
            // Increment ans by 1
            ans++;
        }
    }
 
    // Print the value of ans
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 2, 3, 5, 4, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    groupDivision(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
static int MM = 1000000007;
 
// Function to find the minimum number
// of subsets into which given array
// can be split such that the given
// conditions are satisfied
static void groupDivision(int arr[], int n)
{
    int z, i, j, ans;
 
    // Sort the given array arr[]
    Arrays.sort(arr);
 
    // Initialize answer
    ans = 0;
    int[] vis = new int[n + 5];
    Arrays.fill(vis, 0);
 
    // Iterate for the smaller value
    // which has not been visited
    for (i = 0; i < n; i++) {
 
        if (vis[i] == 0) {
 
            // Mark all elements that
            // are divisible by arr[i]
            for (j = i + 1; j < n; j++) {
 
                // If jth index has already
                // been visited
                if (vis[j] == 1)
                    continue;
 
                if (arr[j] % arr[i] == 0)
 
                    // Mark the jth index
                    // as visisted
                    vis[j] = 1;
            }
 
            // Increment ans by 1
            ans++;
        }
    }
 
    // Print the value of ans
    System.out.println(ans);
}
 
 
// Driver Code
public static void main(String[] args)
{
     
    int arr[] = { 10, 2, 3, 5, 4, 2 };
    int N = arr.length;
    groupDivision(arr, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
MM = 1000000007
 
# Function to find the minimum number
# of subsets into which given array
# can be split such that the given
# conditions are satisfied
def groupDivision(arr, n):
    global MM
    ans = 0
 
    # Sort the given array arr[]
    arr = sorted(arr)
    vis = [0]*(n + 5)
 
    # Iterate for the smaller value
    # which has not been visited
    for i in range(n):
        if (not vis[i]):
 
            # Mark all elements that
            # are divisible by arr[i]
            for j in range(i + 1, n):
 
                # If jth index has already
                # been visited
                if (vis[j] == 1):
                    continue
                if (arr[j] % arr[i] == 0):
 
                    # Mark the jth index
                    # as visisted
                    vis[j] = 1
 
            # Increment ans by 1
            ans += 1
 
    # Prthe value of ans
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    arr =[10, 2, 3, 5, 4, 2]
    N = len(arr)
    groupDivision(arr, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
static int MM = 1000000007;
 
// Function to find the minimum number
// of subsets into which given array
// can be split such that the given
// conditions are satisfied
static void groupDivision(int[] arr, int n)
{
    int z, i, j, ans;
 
    // Sort the given array arr[]
    Array.Sort(arr);
 
    // Initialize answer
    ans = 0;
    int[] vis = new int[n + 5];
    for (i = 0; i < n; i++) {
        vis[i] = 0;
    }
 
    // Iterate for the smaller value
    // which has not been visited
    for (i = 0; i < n; i++) {
 
        if (vis[i] == 0) {
 
            // Mark all elements that
            // are divisible by arr[i]
            for (j = i + 1; j < n; j++) {
 
                // If jth index has already
                // been visited
                if (vis[j] == 1)
                    continue;
 
                if (arr[j] % arr[i] == 0)
 
                    // Mark the jth index
                    // as visisted
                    vis[j] = 1;
            }
 
            // Increment ans by 1
            ans++;
        }
    }
 
    // Print the value of ans
    Console.Write(ans);
}
 
// Driver Code
static public void Main ()
{
    int[] arr = { 10, 2, 3, 5, 4, 2 };
    int N = arr.Length;
    groupDivision(arr, N);
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
3

时间复杂度: O(N 2 )
辅助空间: O(N)