Partitions possible such that the minimum element divides all the other elements of the partition
Last Updated :
15 Feb, 2022
Given an integer array arr[], the task is to count the number of partitions possible such that in each partition the minimum element divides all the other elements of the partition. The partition need not be continuous.
Examples:
Input: arr[] = {10, 7, 20, 21, 13}
Output: 3
The possible partitions are {10, 20}, {7, 21} and {13}.
In each partition, all the elements are divisible by
the minimum element of the partition.
Input: arr[] = {7, 6, 5, 4, 3, 2, 2, 3}
Output: 4
Approach:
- Find the minimum element in the array which is not equal to INT_MAX.
- Remove all the elements (replace by INT_MAX) from the array divisible by the minimum element.
- The number of valid minimum elements as a result of the operations is the required number of partitions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPartitions( int A[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
int min_elem = *min_element(A, A + N);
if (min_elem == INT_MAX)
break ;
count++;
for ( int i = 0; i < N; i++) {
if (A[i] % min_elem == 0)
A[i] = INT_MAX;
}
}
return count;
}
int main()
{
int arr[] = { 7, 6, 5, 4, 3, 2, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPartitions(arr, N);
return 0;
}
|
Java
class GFG
{
static int INT_MAX = Integer.MAX_VALUE ;
static int min_element( int []A, int N)
{
int min = A[ 0 ];
int i;
for ( i = 1 ; i < N ; i++)
{
if (min > A[i])
{
min = A[i];
}
}
return min;
}
static int countPartitions( int []A, int N)
{
int count = 0 ;
int i, j;
for (i = 0 ; i < N; i++)
{
int min_elem = min_element(A, N);
if (min_elem == INT_MAX)
break ;
count++;
for (j = 0 ; j < N; j++)
{
if (A[j] % min_elem == 0 )
A[j] = INT_MAX;
}
}
return count;
}
public static void main (String[] args)
{
int arr[] = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 };
int N = arr.length;
System.out.println(countPartitions(arr, N));
}
}
|
Python3
import sys
INT_MAX = sys.maxsize;
def countPartitions(A, N) :
count = 0 ;
for i in range (N) :
min_elem = min (A);
if (min_elem = = INT_MAX) :
break ;
count + = 1 ;
for i in range (N) :
if (A[i] % min_elem = = 0 ) :
A[i] = INT_MAX;
return count;
if __name__ = = "__main__" :
arr = [ 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 ];
N = len (arr);
print (countPartitions(arr, N));
|
C#
using System;
class GFG
{
static int INT_MAX = int .MaxValue ;
static int min_element( int []A, int N)
{
int min = A[0];
int i;
for ( i = 1; i < N ; i++)
{
if (min > A[i])
{
min = A[i];
}
}
return min;
}
static int countPartitions( int []A, int N)
{
int count = 0;
int i, j;
for (i = 0; i < N; i++)
{
int min_elem = min_element(A, N);
if (min_elem == INT_MAX)
break ;
count++;
for (j = 0; j < N; j++)
{
if (A[j] % min_elem == 0)
A[j] = INT_MAX;
}
}
return count;
}
public static void Main()
{
int []arr = { 7, 6, 5, 4, 3, 2, 2, 3 };
int N = arr.Length;
Console.WriteLine(countPartitions(arr, N));
}
}
|
Javascript
<script>
var INT_MAX = 1000000000;
function min_element(A, N)
{
var min = A[0];
var i;
for ( i = 1; i < N ; i++)
{
if (min > A[i])
{
min = A[i];
}
}
return min;
}
function countPartitions(A, N)
{
var count = 0;
var i, j;
for (i = 0; i < N; i++)
{
var min_elem = min_element(A, N);
if (min_elem == INT_MAX)
break ;
count++;
for (j = 0; j < N; j++)
{
if (A[j] % min_elem == 0)
A[j] = INT_MAX;
}
}
return count;
}
var arr = [ 7, 6, 5, 4, 3, 2, 2, 3 ];
var N = arr.length;
document.write(countPartitions(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...