CODE :
#include <bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
int n; //array length | |
cin>>n; | |
int arr[n]; //array | |
for(int i=0;i<n;i++){ //input of array | |
cin>>arr[i]; | |
} | |
int temp; //variable | |
int a; //variable | |
int count; //variable | |
int num=0; //variable | |
for(int i=0;i<n;i++){ // 1st for loop | |
count=0; | |
for(int j=0;j<n;j++){ //2nd for loop | |
temp=0; | |
a=arr[i]-arr[j]; //find differnce between element and save to a | |
if(abs(a)<=1){ //if diff is less than or equal to one | |
for(int k=0;k<n;k++){ /* | |
count how many time that two numbers are in the array | |
and save that count into temp | |
*/ | |
if(arr[k]==arr[i] || arr[k]==arr[j]){ | |
temp++; | |
} | |
} | |
} | |
/* | |
if that count is bgger thn previous count than save new count | |
*/ | |
if(temp>count){ | |
count=temp; | |
} | |
} | |
if(count>num){ | |
num=count; | |
} | |
} | |
cout<<num<<endl; | |
} |
Example
There are two subarrays meeting the criterion: and . The maximum length subarray has elements.
Function Description
Complete the pickingNumbers function in the editor below.
pickingNumbers has the following parameter(s):
- int a[n]: an array of integers
Returns
- int: the length of the longest subarray that meets the criterion
Input Format
The first line contains a single integer , the size of the array .
The second line contains space-separated integers, each an .
Constraints
- The answer will be .
Sample Input 0
6
4 6 5 3 3 1
Sample Output 0
3
Explanation 0
We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., and ), so we print the number of chosen integers, , as our answer.
Sample Input 1
6
1 2 2 3 1 2
Sample Output 1
5
Explanation 1
We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference (i.e., , , and ), so we print the number of chosen integers, , as our answer.