Open In App

Split array into minimum number of subsets with every element of a subset divisible by its minimum

Last Updated : 09 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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 visited
                    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 visited
                    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 visited
                    vis[j] = 1
 
            # Increment ans by 1
            ans += 1
 
    # Print the 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 visited
                    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




<script>
 
// Javascript program for the above approach
var MM = 1000000007;
 
// Creating the bblSort function
function bblSort(arr)
{
    for(var i = 0; i < arr.length; i++)
    {
     
        // Last i elements are already in place 
        for(var j = 0;
                j < (arr.length - i - 1);
                j++)
        {
         
            // Checking if the item at present iteration
            // is greater than the next iteration
            if (arr[j] > arr[j + 1])
            {
             
                // If the condition is true
                // then swap them
                var temp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = temp
            }
        }
    }
     
    // Return the sorted array
    return (arr);
}
 
// Function to find the minimum number
// of subsets into which given array
// can be split such that the given
// conditions are satisfied
function groupDivision(arr, n)
{
    var z, i, j, ans;
 
    // Sort the given array arr
    arr = bblSort(arr);
 
    // Initialize answer
    ans = 0;
    var vis = Array(n + 5).fill(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 visited
                    vis[j] = 1;
            }
 
            // Increment ans by 1
            ans++;
        }
    }
 
    // Print the value of ans
    document.write(ans);
}
 
// Driver Code
var arr = [ 10, 2, 3, 5, 4, 2 ];
var N = arr.length;
 
groupDivision(arr, N);
 
// This code is contributed by gauravrajput1
 
</script>


Output: 

3

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads