**Here is my brute force solution:
**How should I optimize it?
int Solution::solve(vector<int> &nums, int B) {
int ans=0;
int n=nums.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((j-i)<=B){
if(A[i]==A[j])
ans++;
}
else{
break;
}
}
return ans;
}