Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    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:

    • (0, 1) since 0 < 1 and nums0 + nums1 = 0 < target

    • (0, 2) since 0 < 2 and nums0 + nums2 = 1 < target

    • (0, 4) since 0 < 4 and nums0 + nums4 = 0 < target

    Note that (0, 3) is not counted since nums0 + nums3 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:

    • (0, 1) since 0 < 1 and nums0 + nums1 = -4 < target

    • (0, 3) since 0 < 3 and nums0 + nums3 = -8 < target

    • (0, 4) since 0 < 4 and nums0 + nums4 = -13 < target

    • (0, 5) since 0 < 5 and nums0 + nums5 = -7 < target

    • (0, 6) since 0 < 6 and nums0 + nums6 = -3 < target

    • (1, 4) since 1 < 4 and nums1 + nums4 = -5 < target

    • (3, 4) since 3 < 4 and nums3 + nums4 = -9 < target

    • (3, 5) since 3 < 5 and nums3 + nums5 = -3 < target

    • (4, 5) since 4 < 5 and nums4 + nums5 = -8 < target

    • (4, 6) since 4 < 6 and nums4 + nums6 = -4 < target

    Constraints:

    • 1 &lt;= nums.length == n &lt;= 50

    • -50 &lt;= nums[i], target &lt;= 50

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer countPairs(List<Integer> nums, Integer target)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait