#include <iostream>                                                                                                                                                                                                                                                                
#include <algorithm>                                                                                                                                                                                                                                                               
#include <stdlib.h>                                                                                                                                                                                                                                                                
#include <vector>                                                                                                                                                                                                                                                                  
using namespace std;                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                   
int main()                                                                                                                                                                                                                                                                         
{                                                                                                                                                                                                                                                                                  
    long long n;                                                                                                                                                                                                                                                                   
    cin >> n;                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                   
    vector<long long> a(n);                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                   
    for (int i=0; i<n; ++i)                                                                                                                                                                                                                                                        
        cin >> a[i];                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                   
    sort(a.begin(), a.end());                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                   
    long long ans = 0;                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                   
    long long count = 0;                                                                                                                                                                                                                                                           
    long long prev = a[0], prev_c = 0, c=0;                                                                                                                                                                                                                                        
    int i = 0, j =0;                                                                                                                                                                                                                                                               
    for (i=0; i < n; ++i) {                                                                                                                                                                                                                                                        
        if (a[i] == a[0]){                                                                                                                                                                                                                                                         
            prev_c++;                                                                                                                                                                                                                                                              
            count++;                                                                                                                                                                                                                                                               
        } else if (a[i] == a[0] + 1) {                                                                                                                                                                                                                                             
            c++;                                                                                                                                                                                                                                                                   
            count++;                                                                                                                                                                                                                                                               
        }                                                                                                                                                                                                                                                                          
        else break;                                                                                                                                                                                                                                                                
    }                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                   
    ans = max(ans, count);                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                   
    for (i=(c==0) ? i: i-1; i < n; i=(c==0) ? j : j-1) {                                                                                                                                                                                                                           
        count = c;                                                                                                                                                                                                                                                                 
        if (prev == a[i] - 1) {                                                                                                                                                                                                                                                    
            count += prev_c;                                                                                                                                                                                                                                                       
        }                                                                                                                                                                                                                                                                          
        prev = a[i];                                                                                                                                                                                                                                                               
        prev_c = c;                                                                                                                                                                                                                                                                
        c = 0;                                                                                                                                                                                                                                                                     
        for (j = i+1; j < n; ++j) {                                                                                                                                                                                                                                                
            if (a[j] == a[i] + 1) {                                                                                                                                                                                                                                                
                c++;                                                                                                                                                                                                                                                               
                count++;                                                                                                                                                                                                                                                           
            } else break;                                                                                                                                                                                                                                                          
        }                                                                                                                                                                                                                                                                          
        ans = max(ans, count);                                                                                                                                                                                                                                                     
    }                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                   
    cout << ans << endl;;                                                                                                                                                                                                                                                          
}  

Measured memory_bytes: 1024