𝐁𝐄𝐀𝐓𝐒 𝟏𝟎𝟎% 🏎️💨🏁🏆 𝐉𝐀𝐕𝐀𝐒𝐂𝐑𝐈𝐏𝐓 🔥❤️‍🔥💥 𝐁𝐄𝐆𝐈𝐍𝐍𝐄𝐑 𝐅𝐑𝐈𝐄𝐍𝐃𝐋𝐘 🌱💚💹
2824. Count Pairs Whose Sum is Less than Target\n\n\uD83C\uDF31 Difficulty: Easy\n\n\uD83D\uDC81\uD83C\uDFFB\u200D\u2640\uFE0F All my solved LeetCode problems o
# 2824. Count Pairs Whose Sum is Less than Target\n\n\uD83C\uDF31 Difficulty: `Easy`\n\n\uD83D\uDC81\uD83C\uDFFB\u200D\u2640\uFE0F All my solved LeetCode problems on GitHub: [rayanasales/leetcode](https://github.com/rayanasales/leetcode)\n\n\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25 If it\'s help, please up \uD83D\uDD1D vote! \u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\n\n---\n\n## \uD83D\uDE80 Code Solution\n\n```javascript []\nvar countPairs = function (nums, target) {\n  nums.sort((a, b) => a - b);\n  let count = 0;\n  let left = 0;\n  let right = nums.length - 1;\n  while (left < right) {\n    if (nums[left] + nums[right] < target) {\n      count += right - left;\n      left++;\n    } else {\n      right--;\n    }\n  }\n  return count;\n};\n```\n\n## \uD83D\uDC8E Strategy\n\nThis solution uses a combination of sorting and the two-pointer technique to efficiently count the pairs whose sum is less than a given target. Here\'s how the strategy works in detail:\n\n1. Sort the Array: Start by sorting the array in ascending order. This ordering is crucial as it allows the two-pointer technique to be applied effectively by ensuring that any number to the right of a particular number is greater than or equal to it.\n\n2. Initialize Pointers and Counter: Initialize two pointers, `left` at the start of the array and `right` at the end, along with a counter `count` set to zero. These pointers help in traversing the array from both ends towards the center.\n\n3. Using Two Pointers: The main idea is to move the `left` pointer from the start to just before the `right` pointer while adjusting the `right` pointer based on the sum comparison:\n   - If the sum of the numbers at the `left` and `right` pointers is less than the target, then all pairs formed by the `left` pointer and any pointer between the `left` and `right` (exclusive) are valid. This is because the array is sorted, so adding the `left` number to any number between it and `right` will also produce a sum less than the target.\n   - Increment the `left` pointer after adding the count of valid pairs, which is `right - left`.\n   - If the sum is not less than the target, decrement the `right` pointer to try and find a smaller sum.\n\n## \uD83D\uDD0E Step-by-Step Debugging\n\nLet\u2019s consider debugging the given examples:\n\n- Example 1: `nums = [-1, 1, 2, 3, 1], target = 2`\n  - After sorting: `nums = [-1, 1, 1, 2, 3]`\n  - Pairs found: (-1, 1), (-1, 1), (-1, 2) leading to a count of 3 pairs.\n- Example 2: `nums = [-6, 2, 5, -2, -7, -1, 3], target = -2`\n  - After sorting: `nums = [-7, -6, -2, -1, 2, 3, 5]`\n  - Pairs found include: (-7, -6), (-7, -2), (-7, -1), and several others, totaling 10 pairs.\n\n## \uD83D\uDCCA Time and Space Complexity Analysis\n\n- Time Complexity: O(n log n) due to the sorting step, which dominates the computational cost. The two-pointer traversal of the array is O(n), as each pointer only scans the array once from opposite ends towards the center.\n\n- Space Complexity: O(1) if the sort is done in place, depending on the implementation specifics of JavaScript\'s sort method. In practice, sorting may require additional space.\n\nWhile the overall complexity might not strictly be O(n) due to the sorting step, the two-pointer approach after sorting is linear, making this solution highly efficient for the problem\'s constraints.\n\n---\n\n# Please UPVOTE if this was helpful \uD83D\uDD1D\uD83D\uDD1D\uD83D\uDD1D\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\nand check out all my solved LeetCode problems on GitHub: [rayanasales/leetcode](https://github.com/rayanasales/leetcode) \uD83E\uDD19\uD83D\uDE1A\uD83E\uDD18\n\n![image.png](https://assets.leetcode.com/users/images/57bce3b1-56e2-4c20-9cdf-b61fef26b93b_1725494158.6252415.png)\n
['Array', 'Two Pointers', 'Binary Search', 'Sorting', 'JavaScript']