The Wayback Machine - https://web.archive.org/web/20210212180236/https://www.geeksforgeeks.org/split-array-into-minimum-subsets-with-every-element-of-a-subset-divisible-by-its-minimum-element/
Skip to content
Related Articles

Related Articles

Split array into minimum number of subsets with every element of a subset divisible by its minimum
  • Last Updated : 12 Feb, 2021

Given an array arr[] of size N, the task is to split the array into the minimum number of subsets such that every element belongs to exactly one subset and is divisible by the minimum element present in each subset.

Examples:

Input: arr[] = {10, 2, 3, 5, 4, 2}
Output: 3
Explanation:
The three possible groups are:

  • {5, 10}, where all the element is divisible by 5(minimum element).
  • {2, 2, 4}, where all the element is divisible by 2(minimum element).
  • {3}, where all the element is divisible by 3(minimum element).

Input: arr[] = {50, 50, 50, 50, 50}
Output: 1

Approach: The problem can be solved by using Sorting and finding the minimum for each subset. Follow the steps below to solve the problem:



  • Sort the array arr[] in ascending order.
  • Initialize a variable, say ans, with 0 and an array vis[], for storing the visited array elements.
  • Mark all the positions of vis[] array with 0 that represents that the positions that have not been visited.
  • Traverse the given array arr[] and perform the following steps:
    • If the element arr[i] is not visited, then:
      • Consider it as a minimum for the new subset and increment ans by 1.
      • Iterate over the range [i + 1, N – 1] using the variable j and if the element arr[j] is not visited and is divisible by arr[i], then set vis[j] = 1.
    • Repeat the above steps for each index.
  • After completing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
#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;
}

chevron_right


Output:

3

Time Complexity: O(N2)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :