Similar Pairs
Anonymous User
400

** Given an integer array nums of size N and an integer B ,you need to find the count of pairs(i,j) such that i<j,(j-i)<=B and nums[i]==nums[j].**

**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;
	}
Comments (2)