![]() |
VOOZH | about |
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.
Table of Content
The idea is to sort the array and traverse the sorted array to count the number of such subsets.
Algorithm:
0 to n-2.3
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.
3