Source Code
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <math.h>

#define pairi pair <int,int>
#define pairll pair <long long,long long>
#define vi vector <int> 
#define vll vector <long long> 
#define mapi map <int,int> 
#define mapill map <int,long long> 
#define seti set <int> 
#define setll set <long long> 
#define pb push_back
#define fr(i,a,b) for(int i = a ; i < b ; i++)
#define fir(i,a,b) for(int i = b ; i >= a ; i--)
typedef long long ll ;


using namespace std;

int main()
{
    int t , n , j , i , k , *a , k1 , k2 , k3 , ans ;
    t = 1 ;
    while(t--)
    {
        cin >> n ;
        cin >> k1 >> k2 >> k3 ;
        seti visited ;
        a = new int[n] ;
        fr(i,0,n)
            cin >> a[i] ;
        ans = 0 ;
        fr(c,0,n-1)
        {
            int p = a[c] , temp = 1 ;
            i = c ;
            if(visited.find(c) == visited.end())
            {
                visited.insert(i) ;
                while(i++ < n - 1)
                    {
                        int x = a[i+1] ;
                        if(abs(x%4 - p%4) <= k1 && abs(x%5 - p%5) <= k2 && abs(x%8 - p%8) <= k3)
                            temp++ ;
                    }
                ans = max(ans,temp) ;
            }
        }
        cout << ans << endl ;
    }
}
Copy
Band Song 2 Mahmoud
GNU G++17
1597 ms
1.3 MB
Time Limit Exceeded