-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPickingNumbers.cpp
More file actions
54 lines (51 loc) · 974 Bytes
/
PickingNumbers.cpp
File metadata and controls
54 lines (51 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std;
//Sorting the given array using selection sort
void sort(int arr[],int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
j = i-1;
key = arr[i];
while(j>=0 && arr[j]>key)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
//Finding the Maximum Length of Sub-Array
int maxlen(int arr[],int n)
{
int i,j,c,max,d;
c=0;
max=0;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
d = arr[j]-arr[i];
if(d<=1)
c++;
else
break;
}
if(c>max)
max=c;
c=0;
}
return (max+1);
}
//The MAIN Function
int main()
{
int n,arr[100],i;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
sort(arr,n);
int ans = maxlen(arr,n);
cout<<ans;
}