Correct Answer in N^2


#1
int Solution::solve(vector<int> &a, int b) {
    int len=0, n=a.size();
    for (int i=0; i<n; i++) {
        int mn=a[i], mx=a[i], ln=1;
        for (int j=i; j<n; j++) {
            if (a[j] < mn) mn = a[j];
            if (a[j] > mx) mx = a[j];
            if (mx - mn < b) ln = j-i+1;
            else break;
        }
        if (ln > len) len = ln;
    }
    return len;
}