LeetCode in Kotlin

2824. Count Pairs Whose Sum is Less than Target

Easy

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.

Example 1:

Input: nums = [-1,1,2,3,1], target = 2

Output: 3

Explanation:

There are 3 pairs of indices that satisfy the conditions in the statement:

Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.

Example 2:

Input: nums = [-6,2,5,-2,-7,-1,3], target = -2

Output: 10

Explanation:

There are 10 pairs of indices that satisfy the conditions in the statement:

Constraints:

Solution

class Solution {
    fun countPairs(nums: List<Int>, target: Int): Int {
        var cnt = 0
        for (i in nums.indices) {
            for (j in i + 1 until nums.size) {
                if (nums[i] + nums[j] < target) {
                    cnt++
                }
            }
        }
        return cnt
    }
}