[LeetCode] Count Pairs Whose Sum is Less than Target

2824. Count Pairs Whose Sum is Less than Target

Given a 0-indexed integer array nums of length n and an integer target, return the number of pairs (i, j) where 0 <= i < j < n and nums[i] + nums[j] < target.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countPairs(vector<int>& nums, int target) {
int res = 0;
for(int i = 0; i < nums.size(); i++) {
for(int j = i + 1; j < nums.size(); j++) {
if(nums[i] + nums[j] < target) res += 1;
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/20/PS/LeetCode/count-pairs-whose-sum-is-less-than-target/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.