Count minimum number of subsets (or subsequences) with consecutive numbers
Last Updated :
07 Dec, 2023
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 the 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++
#include <bits/stdc++.h>
using namespace std;
int numofsubset( int arr[], int n)
{
sort(arr, arr + n);
int count = 1;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
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;
}
|
Java
import java.util.*;
class GFG {
static int numofsubset( int arr[], int n)
{
Arrays.sort(arr);
int count = 1 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] + 1 != arr[i + 1 ])
count++;
}
return count;
}
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));
}
}
|
Python3
def numofsubset(arr, n):
x = sorted (arr)
count = 1
for i in range ( 0 , n - 1 ):
if (x[i] + 1 ! = x[i + 1 ]):
count = count + 1
return count
arr = [ 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 , 59 ]
n = len (arr)
print (numofsubset(arr, n))
|
C#
using System;
class GFG {
static int numofsubset( int [] arr, int n)
{
Array.Sort(arr);
int count = 1;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
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));
}
}
|
Javascript
<script>
function numofsubset(arr , n) {
arr.sort((a,b)=>a-b);
var count = 1;
for (i = 0; i < n - 1; i++) {
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
var arr = [ 100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59 ];
var n = arr.length;
document.write(numofsubset(arr, n));
</script>
|
PHP
<?php
function numofsubset( $arr , $n )
{
sort( $arr );
$count = 1;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $arr [ $i ] + 1 != $arr [ $i + 1])
$count ++;
}
return $count ;
}
$arr = array (100, 56, 5, 6, 102, 58, 101,
57, 7, 103, 59 );
$n = sizeof( $arr );
echo numofsubset( $arr , $n );
?>
|
Time Complexity : O(nlogn)
Auxiliary Space :O(1) , since no extra space required.
Using Hashing:
Follow the steps below to implement the approach:
- Create an unordered set to keep track of the presence of each element in the array.
- Iterate through the array and add each element to the set.
- Iterate through the array again and for each element, check if its previous element is present in the subset. If not, start a new subset and add all consecutive elements to it.
- Increment the count variable for each new subset.
- Return the count variable which represents the minimum number of subsets required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsets( int arr[], int n) {
unordered_set< int > s;
int count = 0;
for ( int i = 0; i < n; i++) {
s.insert(arr[i]);
}
for ( int i = 0; i < n; i++) {
if (s.find(arr[i]-1) == s.end()) {
int j = arr[i];
while (s.find(j) != s.end()) {
j++;
}
count++;
}
}
return count;
}
int main() {
int arr[] = {100, 56, 5, 6, 102, 58, 101, 57, 7, 103};
int n = sizeof (arr)/ sizeof (arr[0]);
int subsets = countSubsets(arr, n);
cout << subsets << endl;
return 0;
}
|
Java
import java.util.HashSet;
public class GFG {
public static int countSubsets( int [] arr, int n) {
HashSet<Integer> set = new HashSet<>();
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
set.add(arr[i]);
}
for ( int i = 0 ; i < n; i++) {
if (!set.contains(arr[i] - 1 )) {
int j = arr[i];
while (set.contains(j)) {
j++;
}
count++;
}
}
return count;
}
public static void main(String[] args) {
int [] arr = { 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 };
int n = arr.length;
int subsets = countSubsets(arr, n);
System.out.println(subsets);
}
}
|
Python3
def count_subsets(arr, n):
s = set ()
count = 0
for i in range (n):
s.add(arr[i])
for i in range (n):
if (arr[i] - 1 ) not in s:
j = arr[i]
while j in s:
j + = 1
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 ]
n = len (arr)
subsets = count_subsets(arr, n)
print (subsets)
|
C#
using System;
using System.Collections.Generic;
class SubsetsCount
{
static int CountSubsets( int [] arr, int n)
{
HashSet< int > set = new HashSet< int >();
int count = 0;
for ( int i = 0; i < n; i++)
{
set .Add(arr[i]);
}
for ( int i = 0; i < n; i++)
{
if (! set .Contains(arr[i] - 1))
{
int j = arr[i];
while ( set .Contains(j))
{
j++;
}
count++;
}
}
return count;
}
static void Main()
{
int [] arr = { 100, 56, 5, 6, 102, 58, 101, 57, 7, 103 };
int n = arr.Length;
int subsets = CountSubsets(arr, n);
Console.WriteLine(subsets);
}
}
|
Javascript
function countSubsets(arr) {
let s = new Set();
let count = 0;
for (let i = 0; i < arr.length; i++) {
s.add(arr[i]);
}
for (let i = 0; i < arr.length; i++) {
if (!s.has(arr[i]-1)) {
let j = arr[i];
while (s.has(j)) {
j++;
}
count++;
}
}
return count;
}
let arr = [100, 56, 5, 6, 102, 58, 101, 57, 7, 103];
let subsets = countSubsets(arr);
console.log(subsets);
|
Time Complexity : O(n) , where n is the number of element in the array
Auxiliary Space :O(n) , As we need to store all the element in the Unordered set.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...