VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-minimum-number-subsets-subsequences-consecutive-numbers/

⇱ Split array into minimum subsets - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Split array into minimum subsets

Last Updated : 9 Jun, 2026

Given an array arr[] of distinct positive numbers, partition the array into minimum number of subsets (or subsequences) such that each subset contains consecutive numbers only.

Examples:

Input: arr[] = [100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59]
Output: 3
Explanation: [5, 6, 7], [ 56, 57, 58, 59] and [100, 101, 102, 103] are 3 subsets in which numbers are consecutive.

Input: arr[] = [10, 100, 105]
Output: 3
Explanation: [10], [100] and [105] are 3 subsets in which numbers are consecutive.

[Naive Approach] Sorting - O(n*log n) Time and O(1) Space

The idea is to sort the array and traverse the sorted array to count the number of such subsets.

Algorithm:

  1. Sort the array so that consecutive numbers come next to each other.
  2. Start with: count = 1, because at least one subset will always exist.
  3. Loop through the array from index 0 to n-2.
  4. At each step, compare: current with next.
  5. If arr[i] + 1 is not equal to arr[i + 1] that means sequence is broken, change count = count + 1.

Output
3

[Expected Approach] Hashing - O(n) Time and O(n) Space

The idea is to use Hashing. We first insert all elements in a Hash Set. Then, traverse over all the elements and check if the current element can be a starting element of a consecutive subsequence.


  1. Store all the elements in a hash set to allow quick lookup.
  2. Traverse through each element and check if this element is starting point or not.
  3. To check starting, check if x - 1 is present in the set or not. If, not present that means x can become the starting point of longest consecutive subsequence.
  4. Once we find a starting point, increment the count.=

Output
3


Comment
Article Tags: