📌  相关文章
📜  将数组拆分为最少数量的子集,以便所有对的元素至少出现在不同的子集中一次

📅  最后修改于: 2021-09-06 05:59:03             🧑  作者: Mango

给定一个由N 个不同整数组成的数组arr[] ,任务是找到需要将数组拆分为两个子集的最小次数,使得每对的元素至少出现在两个不同的子集中一次。

例子:

方法:想法是始终将数组拆分为大小为 floor(N / 2) 和 ceil(N / 2) 的两个子集。在每个分区之前,只需将arr[i]的值与arr[N / 2 + i]交换。请按照以下步骤解决问题:

  • 如果 N 是奇数,则可能的总交换次数(arr[i], arr[N / 2 + i]) 等于N / 2 + 1 。因此,打印(N / 2 + 1)
  • 如果 N 是偶数,则总可能的 swap(arr[i], arr[N / 2 + i]) 等于N / 2 。因此,打印(N / 2)

下面是上述方法的 C++ 实现:

C++
// C++ program to to implement
// the above approach
#include 
using namespace std;
 
// Function to find minimum count of ways to split
// the array into two subset such that elements of
// each pair occurs in two different subset
int MinimumNoOfWays(int arr[], int n)
{
 
    // Stores minimum count of ways to split array
    // into two subset such that elements of
    // each pair occurs in two different subset
    int mini_no_of_ways;
 
    // If N is odd
    if (n % 2 == 0) {
        mini_no_of_ways = n / 2;
    }
    else {
        mini_no_of_ways = n / 2 + 1;
    }
    return mini_no_of_ways;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 4, 2, 1, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << MinimumNoOfWays(arr, N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
   
class GFG{
   
// Function to find minimum count of ways to split
// the array into two subset such that elements of
// each pair occurs in two different subset
static int MinimumNoOfWays(int arr[], int n)
{
  
    // Stores minimum count of ways to split array
    // into two subset such that elements of
    // each pair occurs in two different subset
    int mini_no_of_ways;
  
    // If N is odd
    if (n % 2 == 0) {
        mini_no_of_ways = n / 2;
    }
    else {
        mini_no_of_ways = n / 2 + 1;
    }
    return mini_no_of_ways;
}
   
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 4, 2, 1, 5 };
    int N = arr.length;
    System.out.print(MinimumNoOfWays(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python program to to implement
# the above approach
 
# Function to find minimum count of ways to split
# the array into two subset such that elements of
# each pair occurs in two different subset
def MinimumNoOfWays(arr, n):
   
    # Stores minimum count of ways to split array
    # into two subset such that elements of
    # each pair occurs in two different subset
    min_no_of_ways = 0
     
    # if n is even
    if (n % 2 == 0):
        mini_no_of_ways = n // 2
         
    # n is odd
    else:
        mini_no_of_ways = n // 2 + 1
         
    return mini_no_of_ways
 
# driver code
if __name__ == '__main__':
    arr = [3, 4, 1, 2, 5]
    n = len(arr)
    print(MinimumNoOfWays(arr, n))
 
# This code is contributed by MuskanKalra1


C#
// C# program to implement
// the above approach
using System;
class GFG
{
   
// Function to find minimum count of ways to split
// the array into two subset such that elements of
// each pair occurs in two different subset
static int MinimumNoOfWays(int []arr, int n)
{
  
    // Stores minimum count of ways to split array
    // into two subset such that elements of
    // each pair occurs in two different subset
    int mini_no_of_ways;
  
    // If N is odd
    if (n % 2 == 0)
    {
        mini_no_of_ways = n / 2;
    }
    else
    {
        mini_no_of_ways = n / 2 + 1;
    }
    return mini_no_of_ways;
}
   
  // Driver code
  public static void Main(string[] args)
  {
      int[] arr = { 3, 4, 2, 1, 5 };
      int N = arr.Length;
      Console.WriteLine(MinimumNoOfWays(arr, N));
  }
}
 
// This code is contributed by AnkThon


Javascript


输出:
3

时间复杂度: O(1)
空间复杂度: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live