Skip to main content
Back to problems
#2824
Easy Algorithms

Count pairs whose sum is less than target

Array Two Pointers Binary Search Sorting
87.7% acceptance
Feb 25, 2026
829
90
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.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_pairs(nums: Vec<i32>, target: i32) -> i32 {
    let n = nums.len();
    let mut count = 0;
    for i in 0..n { for j in i+1..n { if nums[i] + nums[j] < target { count += 1; } } }
    count
  }
}