2824. Count Pairs Whose Sum is Less than Target Easy

@problem@discussion
#Array#Two Pointers#Binary Search#Sorting



1/**
2 * [2824] Count Pairs Whose Sum is Less than Target
3 *
4 * 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.
5 *  
6 * <strong class="example">Example 1:
7 * 
8 * Input: nums = [-1,1,2,3,1], target = 2
9 * Output: 3
10 * Explanation: There are 3 pairs of indices that satisfy the conditions in the statement:
11 * - (0, 1) since 0 < 1 and nums[0] + nums[1] = 0 < target
12 * - (0, 2) since 0 < 2 and nums[0] + nums[2] = 1 < target 
13 * - (0, 4) since 0 < 4 and nums[0] + nums[4] = 0 < target
14 * Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
15 * 
16 * <strong class="example">Example 2:
17 * 
18 * Input: nums = [-6,2,5,-2,-7,-1,3], target = -2
19 * Output: 10
20 * Explanation: There are 10 pairs of indices that satisfy the conditions in the statement:
21 * - (0, 1) since 0 < 1 and nums[0] + nums[1] = -4 < target
22 * - (0, 3) since 0 < 3 and nums[0] + nums[3] = -8 < target
23 * - (0, 4) since 0 < 4 and nums[0] + nums[4] = -13 < target
24 * - (0, 5) since 0 < 5 and nums[0] + nums[5] = -7 < target
25 * - (0, 6) since 0 < 6 and nums[0] + nums[6] = -3 < target
26 * - (1, 4) since 1 < 4 and nums[1] + nums[4] = -5 < target
27 * - (3, 4) since 3 < 4 and nums[3] + nums[4] = -9 < target
28 * - (3, 5) since 3 < 5 and nums[3] + nums[5] = -3 < target
29 * - (4, 5) since 4 < 5 and nums[4] + nums[5] = -8 < target
30 * - (4, 6) since 4 < 6 and nums[4] + nums[6] = -4 < target
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= nums.length == n <= 50
36 * 	-50 <= nums[i], target <= 50
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/
42// discuss: https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn count_pairs(nums: Vec<i32>, target: i32) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2824() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.