-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathK-diffPairsInAnArray.cpp
More file actions
49 lines (39 loc) · 1.08 KB
/
Copy pathK-diffPairsInAnArray.cpp
File metadata and controls
49 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
int ans = 0, i = 0, n = nums.size();
if(n == 0){
return ans;
}
sort(nums.begin(), nums.end());
while(i < n){
bool same = false;
while((i+1 < n) && (nums[i] == nums[i+1])){
same = true;
i++;
}
if(k == 0){
if(same){
ans++;
}
}
else{
int j = i+1;
while(j < n){
bool found = false;
while(nums[j] == (nums[i] + k)){
found = true;
j++;
}
if(found){
ans++;
break;
}
j++;
}
}
i++;
}
return ans;
}
};