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

📅  最后修改于: 2021-09-07 02:32:11             🧑  作者: Mango

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

例子:

方法:这个想法是观察如果没有这样的对i , j使得|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


Javascript


输出:
1

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

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