SORT15
QUESTION DESCRIPTION
Given an array of distinct positive numbers, the task is to calculate the minimum number of subsets (or subsequences) from the array such that each subset contains consecutive numbers.
Mandatory variables are "int n and int t"
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
Given an array of distinct positive numbers, the task is to calculate the minimum number of subsets (or subsequences) from the array such that each subset contains consecutive numbers.
Mandatory variables are "int n and int t"
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
TEST CASE 2
INPUT
INPUT
5
12
12 23 85 11 65 22 87 24 10 86 64 63
5
1 5 3 2 6
7
6 8 1 7 2 5 4
10
45 21 74 44 72 20 4 73 5 46
8
4 2 9 5 3 7 8 1
OUTPUT4
2
2
4
2
#include <stdio.h>
int i,count=0,j;
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void noofsubset(int a[],int n)
{
sort(a,n);
for(i=0;i<n;i++)
{
if(a[i]+1!=a[i+1])
count++;
}
printf("%d\n",count);
count=0;
}
int main()
{
int a[30],n;
int t;
scanf("%d",&t);
for(j=0;j<t;j++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
noofsubset(a,n);
}
return 0;
}
Suppose the input is as follows :
ReplyDelete1
3
9 12 14
then it will give 3 as output which is totally wrong.
Though there are 3 subsets but none of them have consecutive numbers
SHOWING 40% PERCENT
ReplyDelete