Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmPartitions possible such that the minimum element divides all the other elements...

Partitions possible such that the minimum element divides all the other elements of the partition

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

 

Approach: 
 

  1. Find the minimum element in the array which is not equal to INT_MAX.
  2. Remove all the elements (replace by INT_MAX) from the array divisible by the minimum element.
  3. 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count partitions
// possible from the given array such that
// the minimum element of any partition
// divides all the other elements
// of that partition
int countPartitions(int A[], int N)
{
    // Initialize the count variable
    int count = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Find the minimum element
        int min_elem = *min_element(A, A + N);
 
        // Break if no minimum element present
        if (min_elem == INT_MAX)
            break;
 
        // Increment the count if
        // minimum element present
        count++;
 
        // Replace all the element
        // divisible by min_elem
        for (int i = 0; i < N; i++) {
            if (A[i] % min_elem == 0)
                A[i] = INT_MAX;
        }
    }
    return count;
}
 
// Driver code
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




// Java implementation of the approach
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;
    }
     
    // Function to return the count partitions
    // possible from the given array such that
    // the minimum element of any partition
    // divides all the other elements
    // of that partition
    static int countPartitions(int []A, int N)
    {
        // Initialize the count variable
        int count = 0;
        int i, j;
         
        for (i = 0; i < N; i++)
        {
     
            // Find the minimum element
            int min_elem = min_element(A, N);
     
            // Break if no minimum element present
            if (min_elem == INT_MAX)
                break;
     
            // Increment the count if
            // minimum element present
            count++;
     
            // Replace all the element
            // divisible by min_elem
            for (j = 0; j < N; j++)
            {
                if (A[j] % min_elem == 0)
                    A[j] = INT_MAX;
            }
        }
        return count;
    }
     
    // Driver code
    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));
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation of the approach
import sys
 
INT_MAX = sys.maxsize;
 
# Function to return the count partitions
# possible from the given array such that
# the minimum element of any partition
# divides all the other elements
# of that partition
def countPartitions(A, N) :
 
    # Initialize the count variable
    count = 0;
 
    for i in range(N) :
 
        # Find the minimum element
        min_elem = min(A);
 
        # Break if no minimum element present
        if (min_elem == INT_MAX) :
            break;
 
        # Increment the count if
        # minimum element present
        count += 1;
 
        # Replace all the element
        # divisible by min_elem
        for i in range(N) :
            if (A[i] % min_elem == 0) :
                A[i] = INT_MAX;
 
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 7, 6, 5, 4, 3, 2, 2, 3 ];
    N = len(arr);
 
    print(countPartitions(arr, N));
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
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;
    }
     
    // Function to return the count partitions
    // possible from the given array such that
    // the minimum element of any partition
    // divides all the other elements
    // of that partition
    static int countPartitions(int []A, int N)
    {
        // Initialize the count variable
        int count = 0;
        int i, j;
         
        for (i = 0; i < N; i++)
        {
     
            // Find the minimum element
            int min_elem = min_element(A, N);
     
            // Break if no minimum element present
            if (min_elem == INT_MAX)
                break;
     
            // Increment the count if
            // minimum element present
            count++;
     
            // Replace all the element
            // divisible by min_elem
            for (j = 0; j < N; j++)
            {
                if (A[j] % min_elem == 0)
                    A[j] = INT_MAX;
            }
        }
        return count;
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = { 7, 6, 5, 4, 3, 2, 2, 3 };
        int N = arr.Length;
     
        Console.WriteLine(countPartitions(arr, N));
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
// Javascript implementation of the approach
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 to return the count partitions
// possible from the given array such that
// the minimum element of any partition
// divides all the other elements
// of that partition
function countPartitions(A, N)
{
    // Initialize the count variable
    var count = 0;
    var i, j;
     
    for (i = 0; i < N; i++)
    {
 
        // Find the minimum element
        var min_elem = min_element(A, N);
 
        // Break if no minimum element present
        if (min_elem == INT_MAX)
            break;
 
        // Increment the count if
        // minimum element present
        count++;
 
        // Replace all the element
        // divisible by min_elem
        for (j = 0; j < N; j++)
        {
            if (A[j] % min_elem == 0)
                A[j] = INT_MAX;
        }
    }
    return count;
}
 
// Driver code
var arr = [ 7, 6, 5, 4, 3, 2, 2, 3 ];
var N = arr.length;
document.write(countPartitions(arr, N)); 
 
// This code is contributed by rutvik_56.
</script>


Output: 

4

 

Time Complexity: O(N2)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments