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

📅  最后修改于: 2021-09-08 12:33:32             🧑  作者: 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live