# Count minimum number of subsets (or subsequences) with consecutive numbers

Given an array of distinct positive numbers, the task is to calculate the number of subsets (or subsequences) from the array such that each subset contains consecutive numbers.

Examples:

Input :arr[] = {100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59}Output :3 {5, 6, 7}, { 56, 57, 58, 59}, {100, 101, 102, 103} are 3 subset in which numbers are consecutive.Input :arr[] = {10, 100, 105}Output :3 {10}, {100} and {105} are 3 subset in which numbers are consecutive.

The idea is to sort the array and traverse the sorted array to count the number of such subsets. To count the number of such subsets, we need to count the consecutive numbers such that difference between them is not equal to one.

Following is the algorithm for the finding number of subset containing consecutive numbers:

1. Sort the array arr[ ] and count = 1. 2. Traverse the sorted array and for each element arr[i]. If arr[i] + 1 != arr[i+1], then increment the count by one. 3. Return the count.

Below is the implementation of this approach :

## C++

`// C++ program to find number of subset containing ` `// consecutive numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns count of subsets with consecutive numbers ` `int` `numofsubset(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Sort the array so that elements which are ` ` ` `// consecutive in nature became consecutive ` ` ` `// in the array. ` ` ` `sort(arr, arr + n); ` ` ` ` ` `int` `count = 1; ` `// Initialize result ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` `// Check if there is beginning of another ` ` ` `// subset of consecutive number ` ` ` `if` `(arr[i] + 1 != arr[i + 1]) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 100, 56, 5, 6, 102, 58, 101, ` ` ` `57, 7, 103, 59 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << numofsubset(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of subset ` `// containing consecutive numbers ` `import` `java.util.*; ` `class` `GFG { ` ` ` ` ` `// Returns count of subsets with consecutive numbers ` ` ` `static` `int` `numofsubset(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Sort the array so that elements ` ` ` `// which are consecutive in nature ` ` ` `// became consecutive in the array. ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Initialize result ` ` ` `int` `count = ` `1` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) { ` ` ` `// Check if there is beginning ` ` ` `// of another subset of ` ` ` `// consecutive number ` ` ` `if` `(arr[i] + ` `1` `!= arr[i + ` `1` `]) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `100` `, ` `56` `, ` `5` `, ` `6` `, ` `102` `, ` `58` `, ` `101` `, ` ` ` `57` `, ` `7` `, ` `103` `, ` `59` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(numofsubset(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by prerna saini. ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find number of subset containing ` `# consecutive numbers ` `def` `numofsubset(arr, n): ` ` ` ` ` `# Sort the array so that elements which are consecutive ` ` ` `# in nature became consecutive in the array. ` ` ` `x ` `=` `sorted` `(arr) ` ` ` ` ` `count ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n` `-` `1` `): ` ` ` ` ` `# Check if there is beginning of another subset of ` ` ` `# consecutive number ` ` ` `if` `(x[i] ` `+` `1` `!` `=` `x[i ` `+` `1` `]): ` ` ` `count ` `=` `count ` `+` `1` ` ` ` ` `return` `count ` ` ` `# Driven Program ` `arr ` `=` `[ ` `100` `, ` `56` `, ` `5` `, ` `6` `, ` `102` `, ` `58` `, ` `101` `, ` `57` `, ` `7` `, ` `103` `, ` `59` `] ` `n ` `=` `len` `(arr) ` `print` `numofsubset(arr, n) ` ` ` `# This code is contributed by Afzal Ansari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of subset ` `// containing consecutive numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns count of subsets with ` ` ` `// consecutive numbers ` ` ` `static` `int` `numofsubset(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `// Sort the array so that elements ` ` ` `// which are consecutive in nature ` ` ` `// became consecutive in the array. ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Initialize result ` ` ` `int` `count = 1; ` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) { ` ` ` ` ` `// Check if there is beginning ` ` ` `// of another subset of ` ` ` `// consecutive number ` ` ` `if` `(arr[i] + 1 != arr[i + 1]) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 100, 56, 5, 6, 102, 58, 101, ` ` ` `57, 7, 103, 59 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(numofsubset(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number ` `// of subset containing ` `// consecutive numbers ` ` ` `// Returns count of subsets ` `// with consecutive numbers ` `function` `numofsubset( ` `$arr` `, ` `$n` `) ` `{ ` ` ` ` ` `// Sort the array so that ` ` ` `// elements which are ` ` ` `// consecutive in nature ` ` ` `// became consecutive ` ` ` `// in the array. ` ` ` `sort(` `$arr` `); ` ` ` ` ` `// Initialize result ` ` ` `$count` `= 1; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Check if there is ` ` ` `// beginning of another ` ` ` `// subset of consecutive ` ` ` `// number ` ` ` `if` `(` `$arr` `[` `$i` `] + 1 != ` `$arr` `[` `$i` `+ 1]) ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(100, 56, 5, 6, 102, 58, 101, ` ` ` `57, 7, 103, 59 ); ` ` ` `$n` `= sizeof(` `$arr` `); ` ` ` `echo` `numofsubset(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by Anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

**Time Complexity : **O(nlogn)

This article is contributed by **Anuj Chauhan(anuj0503)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count subsets having distinct even numbers
- Count number of subsets having a particular XOR value
- Count the number of subsequences of length k having equal LCM and HCF
- Count number of increasing subsequences of size k
- Minimum number of subsets with distinct elements
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Count number of subsets whose median is also present in the same subset
- Count number of binary strings without consecutive 1's
- Minimum number of consecutive sequences that can be formed in an array
- Find the number of consecutive zero at the end after multiplying n numbers
- Maximise the size of consecutive element subsets in an array
- Count Distinct Subsequences
- Count all subsequences having product less than K
- Count of 'GFG' Subsequences in the given string
- Count all increasing subsequences