Min Subsets with Consecutive Numbers

PROBLEM :
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.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N, denoting the length of the array. Next line contains N space seperated integers of the array.

Output:
For each test case output a new line denoting count of number of such subset's that contains consecutive numbers.
Constraints:
1<=T<=100
1<=N<=50

Example:
Input
2
11
100 56 5 6 102 58 101 57 7 103 5
3
10 100 105

Output
3
3

Explanation
Test Case 1 -
{5, 6, 7}, { 56, 57, 58, 59}, {100, 101, 102, 103} are 3 subset in which numbers are consecutive.

Test Case 2 -
{10}, {100} and {105} are 3 subset in which numbers are consecutive.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/#include<iostream>
using namespace std;
#include<algorithm>
int Subsets_with_Consecutive(int [],int ) ;
int main()
 {
int t,no,i ;
int arr[100] ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   cout<<Subsets_with_Consecutive(arr,no) ;
   cout<<endl ;
}
return 0;
}

int Subsets_with_Consecutive(int arr[],int no)
{
    int i,count ;
    sort(arr,arr+no) ;
    count=1 ;
   
    for(i=0;i<no-1;i++)
    {
        if(arr[i+1]-arr[i]>1)
           count++ ;
    }
    return count ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )