📌  相关文章
📜  将数组拆分为最小数量的子集,每对之间的差异大于1

📅  最后修改于: 2021-05-17 02:56:11             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是将数组拆分为最小数量的子集,以使每个子集中的每对元素对的差严格大于1。
注意:数组中的所有元素都是不同的。

例子:

方法:这个想法是观察如果没有这样的对ij使得| arr [i] – arr [j] | = 1 ,则可以将所有元素放在同一分区中,否则将它们分为两个分区。因此,所需的最小分区数始终为12

  1. 对给定的数组进行排序。
  2. 比较相邻元素。如果在任何时候它们的差等于1,则打印“ 2”,因为所需的子集分区数将始终为2,因为我们可以将上述对中的一个元素放入另一个子集中。
  3. 如果遍历所有数组都没有发现任何相邻对的差小于2,则打印“ 1”而不将数组分成子集,我们可以使所有可能的对差至少为2。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to Split the array into
// minimum number of subsets with
// difference strictly > 1
void split(int arr[], int n)
{
    // Sort the array
    sort(arr, arr + n);
    int count = 1;
 
    // Traverse through the sorted array
    for (int i = 1; i < n; i++) {
 
        // Check the pairs of elements
        // with difference 1
        if (arr[i] - arr[i - 1] == 1) {
 
            // If we find even a single
            // pair with difference equal
            // to 1, then 2 partitions
            // else only 1 partiton
            count = 2;
            break;
        }
    }
 
    // Print the count of partitions
    cout << count << endl;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 4, 6 };
 
    // Size of the array
    int n = sizeof(arr) / sizeof(int);
 
    // Function Call
    split(arr, n);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to split the array into
// minimum number of subsets with
// difference strictly > 1
static void split(int arr[], int n)
{
     
    // Sort the array
    Arrays.sort(arr);
    int count = 1;
 
    // Traverse through the sorted array
    for(int i = 1; i < n; i++)
    {
         
        // Check the pairs of elements
        // with difference 1
        if (arr[i] - arr[i - 1] == 1)
        {
             
            // If we find even a single
            // pair with difference equal
            // to 1, then 2 partitions
            // else only 1 partiton
            count = 2;
            break;
        }
    }
 
    // Print the count of partitions
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 2, 4, 6 };
 
    // Size of the array
    int n = arr.length;
 
    // Function call
    split(arr, n);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 implementation of
# the above approach
 
# Function to Split the array into
# minimum number of subsets with
# difference strictly > 1
def split(arr, n):
 
    # Sort the array
    arr.sort()
    count = 1
 
    # Traverse through the sorted array
    for i in range(1, n):
 
        # Check the pairs of elements
        # with difference 1
        if(arr[i] - arr[i - 1] == 1):
 
            # If we find even a single
            # pair with difference equal
            # to 1, then 2 partitions
            # else only 1 partiton
            count = 2
            break
 
    # Print the count of partitions
    print(count)
 
# Driver Code
if __name__ == '__main__':
 
    # Given array
    arr = [ 2, 4, 6 ]
 
    # Size of the array
    n = len(arr)
 
    # Function call
    split(arr, n)
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
class GFG{
      
// Function to split the array into
// minimum number of subsets with
// difference strictly > 1
static void split(int []arr, int n)
{
    // Sort the array
    Array.Sort(arr);
    int count = 1;
  
    // Traverse through the sorted array
    for(int i = 1; i < n; i++)
    {
          
        // Check the pairs of elements
        // with difference 1
        if (arr[i] - arr[i - 1] == 1)
        {
            // If we find even a single
            // pair with difference equal
            // to 1, then 2 partitions
            // else only 1 partiton
            count = 2;
            break;
        }
    }
  
    // Print the count of partitions
    Console.Write(count);
}
  
// Driver Code
public static void Main(string[] args)
{
      
    // Given array
    int[] arr = new int[]{ 2, 4, 6 };
  
    // Size of the array
    int n = arr.Length;
  
    // Function call
    split(arr, n);
}
}
  
// This code is contributed by Ritik Bansal


输出:
1


时间复杂度: O(N log N),其中N是数组的长度。
辅助空间: O(1)