#include <iostream>
#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 t, n, *arr;
cin >> t;
for(int i=0; i<t; i++){
cin >> n;
arr = new int [n];
for(int j=0; j<n; j++){
cin >> arr[j];
}
cout << numofsubset(arr, n) << endl;
}
return 0;
}
#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 t, n, *arr;
cin >> t;
for(int i=0; i<t; i++){
cin >> n;
arr = new int [n];
for(int j=0; j<n; j++){
cin >> arr[j];
}
cout << numofsubset(arr, n) << endl;
}
return 0;
}
20%
ReplyDeleteThis comment has been removed by the author.
ReplyDelete#include
ReplyDelete#include
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 t;
int n;
int *arr;
cin >> t;
for(int i=0; i> n;
arr = new int [n];
for(int j=0; j> arr[j];
}
cout << numofsubset(arr, n) << endl;
}
return 0;
}
wrong
DeleteSORT15
ReplyDeleteCODE:::::
#include
void swap(int *xp,int *yp)
{
int temp=*xp;
*xp=*yp;
*yp=temp;
}
void bubbleSort(int arr[],int n)
{
int i, j;
for (i=0;i arr[j+1]){
swap(&arr[j],&arr[j+1]);
}
}
}
}
int numofsubset(int arr[],int n)
{
int count = 1;
int i;
for (i=0;i<n-1;i++) {
if (arr[i]+1==arr[i + 1]){
continue;
}
count++;
if(n==count){
return 0;
}
}
return count;
}
int main(){
int n;
int t;
int array[50], i, j;
scanf("%d", &t);
for(i=0;i<t;i++){
scanf("%d", &n);
for(j=0;j<n;j++){
scanf("%d", &array[j]);
}
bubbleSort(array, n);
printf("%d\n", numofsubset(array,n));
}
return 0;
}
Thank me later ;)