Split array into minimum number of subsets with every element of a subset divisible by its minimum
Last Updated :
09 Feb, 2022
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++
#include <bits/stdc++.h>
#define LL long long
#define MM 1000000007
using namespace std;
void groupDivision( int arr[], int n)
{
LL z, i, j, ans;
sort(arr, arr + n);
ans = 0;
LL vis[n + 5] = { 0 };
for (i = 0; i < n; i++) {
if (!vis[i]) {
for (j = i + 1; j < n; j++) {
if (vis[j] == 1)
continue ;
if (arr[j] % arr[i] == 0)
vis[j] = 1;
}
ans++;
}
}
cout << ans;
}
int main()
{
int arr[] = { 10, 2, 3, 5, 4, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
groupDivision(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MM = 1000000007 ;
static void groupDivision( int arr[], int n)
{
int z, i, j, ans;
Arrays.sort(arr);
ans = 0 ;
int [] vis = new int [n + 5 ];
Arrays.fill(vis, 0 );
for (i = 0 ; i < n; i++) {
if (vis[i] == 0 ) {
for (j = i + 1 ; j < n; j++) {
if (vis[j] == 1 )
continue ;
if (arr[j] % arr[i] == 0 )
vis[j] = 1 ;
}
ans++;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 10 , 2 , 3 , 5 , 4 , 2 };
int N = arr.length;
groupDivision(arr, N);
}
}
|
Python3
MM = 1000000007
def groupDivision(arr, n):
global MM
ans = 0
arr = sorted (arr)
vis = [ 0 ] * (n + 5 )
for i in range (n):
if ( not vis[i]):
for j in range (i + 1 , n):
if (vis[j] = = 1 ):
continue
if (arr[j] % arr[i] = = 0 ):
vis[j] = 1
ans + = 1
print (ans)
if __name__ = = '__main__' :
arr = [ 10 , 2 , 3 , 5 , 4 , 2 ]
N = len (arr)
groupDivision(arr, N)
|
C#
using System;
class GFG
{
static int MM = 1000000007;
static void groupDivision( int [] arr, int n)
{
int z, i, j, ans;
Array.Sort(arr);
ans = 0;
int [] vis = new int [n + 5];
for (i = 0; i < n; i++) {
vis[i] = 0;
}
for (i = 0; i < n; i++) {
if (vis[i] == 0) {
for (j = i + 1; j < n; j++) {
if (vis[j] == 1)
continue ;
if (arr[j] % arr[i] == 0)
vis[j] = 1;
}
ans++;
}
}
Console.Write(ans);
}
static public void Main ()
{
int [] arr = { 10, 2, 3, 5, 4, 2 };
int N = arr.Length;
groupDivision(arr, N);
}
}
|
Javascript
<script>
var MM = 1000000007;
function bblSort(arr)
{
for ( var i = 0; i < arr.length; i++)
{
for ( var j = 0;
j < (arr.length - i - 1);
j++)
{
if (arr[j] > arr[j + 1])
{
var temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
return (arr);
}
function groupDivision(arr, n)
{
var z, i, j, ans;
arr = bblSort(arr);
ans = 0;
var vis = Array(n + 5).fill(0);
for (i = 0; i < n; i++)
{
if (vis[i] == 0)
{
for (j = i + 1; j < n; j++)
{
if (vis[j] == 1)
continue ;
if (arr[j] % arr[i] == 0)
vis[j] = 1;
}
ans++;
}
}
document.write(ans);
}
var arr = [ 10, 2, 3, 5, 4, 2 ];
var N = arr.length;
groupDivision(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...