📜  可以进行分区,以使最小元素划分该分区的所有其他元素

📅  最后修改于: 2021-06-27 02:42:17             🧑  作者: Mango

给定一个整数数组arr [] ,任务是计算可能的分区数量,以使每个分区中的最小元素划分该分区的所有其他元素。分区不必是连续的。
例子:

方法:

  1. 在数组中找到不等于INT_MAX的最小元素。
  2. 从最小元素可整除的数组中删除所有元素(用INT_MAX替换)。
  3. 由于运算而产生的有效最小元素数是所需的分区数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
4

时间复杂度: O(N 2 )

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。