Numbers - Range Count
Given N distinct integers, the program must print the number of ranges R present. A range is defined as two or more consecutive integers.
Input Format:
The first line contains N.
The second line contains N integer values separated by a space.
The first line contains N.
The second line contains N integer values separated by a space.
Output Format:
The first line contains R.
The first line contains R.
Boundary Conditions:
2 <= N <= 100000
1 <= R <= 10000
2 <= N <= 100000
1 <= R <= 10000
Example Input/Output 1:
Input:
5
2 1 4 9 3
Input:
5
2 1 4 9 3
Output:
1
1
Explanation:
The only range which is present is 1 2 3 4.
9 is not a range (as a range needs two or more consecutive integers).
The only range which is present is 1 2 3 4.
9 is not a range (as a range needs two or more consecutive integers).
Example Input/Output 2:
Input:
7
1 3 11 -15 -20 9 5
Input:
7
1 3 11 -15 -20 9 5
Output:
#include<algorithm>
#include<iostream>
int main(){
int i,j,flag=0,n,c=0;
std::cin>>n;
int arr[n];
for(i=0;i<n;i++)
std::cin>>arr[i];
std::sort(arr,arr+n);
for(i=0;i<n;i++)
{
if(arr[i]+1==arr[i+1])
flag=1;
else
{
if(flag==1)
{
c++;
flag=0;
}
}
}
std::cout<<c;
}
#include<iostream>
int main(){
int i,j,flag=0,n,c=0;
std::cin>>n;
int arr[n];
for(i=0;i<n;i++)
std::cin>>arr[i];
std::sort(arr,arr+n);
for(i=0;i<n;i++)
{
if(arr[i]+1==arr[i+1])
flag=1;
else
{
if(flag==1)
{
c++;
flag=0;
}
}
}
std::cout<<c;
}
No comments:
Post a Comment