Given an array **arr[]** of size **N**, the task is to split the array into a **minimum **number of subset such that each pair of elements in each subset have the difference strictly greater than 1.**Note: **All elements in the array are distinct.

**Examples:**

Input:arr = {5, 10, 6, 50}Output:2Explanation:

Possible partitions are: {5, 10, 50}, { 6 }

Input:arr = { 2, 4, 6 }Output:1Explanation:

Possible partitions are: {2, 4, 6}

**Approach:** The idea is to observe that if there is no such pair **i**, **j** such that **|arr[i] – arr[j]| = 1**, then it is possible to put all the elements in the same partition, otherwise divide them into two partitions. So the required minimum number of partitions is always **1 **or **2**.

- Sort the given array.
- Compare the adjacent elements. If at any point their difference to be equal to 1, then print
**“2”**as the required number of subset partition will always be 2 as we can put one of the elements from the above pair into another subset. - If we traversed all the array didn’t found any adjacent pair with a difference less than 2 then print
**“1”**without splitting the array into subsets we can have all possible pairs difference at least 2.

Below is the implementation for the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(N log N), where N is the length of the array.**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of indices pair such that element pair sum from first Array is greater than second Array
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Check if it is possible to split given Array into K odd-sum subsets
- Split array into K subsets to maximize their sum of maximums and minimums
- Check if an array can be split into subsets of K consecutive elements
- Min steps to empty an Array by removing a pair each time with sum at most K
- Count of subsets having sum of min and max element less than K
- Minimum difference between max and min of all K-size subsets
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Sum of subsets of all the subsets of an array | O(2^N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Partition N into M parts such that difference between Max and Min part is smallest

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.