{"id":10070,"date":"2023-08-23T18:09:34","date_gmt":"2023-08-24T01:09:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10070"},"modified":"2023-08-23T18:20:43","modified_gmt":"2023-08-24T01:20:43","slug":"leetcode-2824-count-pairs-whose-sum-is-less-than-target","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2824-count-pairs-whose-sum-is-less-than-target\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2824.\u00a0Count Pairs Whose Sum is Less than Target"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;and an integer&nbsp;<code>target<\/code>, return&nbsp;<em>the number of pairs<\/em><code>(i, j)<\/code><em>where<\/em><code>0 &lt;= i &lt; j &lt; n<\/code><em>and<\/em><code>nums[i] + nums[j] &lt; target<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-1,1,2,3,1], target = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> There are 3 pairs of indices that satisfy the conditions in the statement:\n- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = 0 &lt; target\n- (0, 2) since 0 &lt; 2 and nums[0] + nums[2] = 1 &lt; target \n- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = 0 &lt; target\nNote that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [-6,2,5,-2,-7,-1,3], target = -2\n<strong>Output:<\/strong> 10\n<strong>Explanation:<\/strong> There are 10 pairs of indices that satisfy the conditions in the statement:\n- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = -4 &lt; target\n- (0, 3) since 0 &lt; 3 and nums[0] + nums[3] = -8 &lt; target\n- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = -13 &lt; target\n- (0, 5) since 0 &lt; 5 and nums[0] + nums[5] = -7 &lt; target\n- (0, 6) since 0 &lt; 6 and nums[0] + nums[6] = -3 &lt; target\n- (1, 4) since 1 &lt; 4 and nums[1] + nums[4] = -5 &lt; target\n- (3, 4) since 3 &lt; 4 and nums[3] + nums[4] = -9 &lt; target\n- (3, 5) since 3 &lt; 5 and nums[3] + nums[5] = -3 &lt; target\n- (4, 5) since 4 &lt; 5 and nums[4] + nums[5] = -8 &lt; target\n- (4, 6) since 4 &lt; 6 and nums[4] + nums[6] = -4 &lt; target\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length == n &lt;= 50<\/code><\/li><li><code>-50 &lt;= nums[i], target &lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute force<\/strong><\/h2>\n\n\n\n<p>Enumerate all pairs.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countPairs(vector<int>& nums, int target) {\n    const int n = nums.size();\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        ans += nums[i] + nums[j] < target;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Sort the numbers.<\/p>\n\n\n\n<p>Use two pointers i, and j.<br>Set i to 0 and j to n - 1.<br>while (nums[i] + nums[j] >= target) --j<br>then we have nums[i] + nums[k] &lt; target (i &lt; k &lt;= j), in total (j - i) pairs.<br>++i, move to the next starting number.<br>Time complexity: O(nlogn + n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countPairs(vector<int>& nums, int target) {\n    const int n = nums.size();\n    sort(begin(nums), end(nums));\n    int ans = 0;\n    for (int i = 0, j = n - 1; i < n; ++i) {\n      while (j >= i && nums[i] + nums[j] >= target) --j;      \n      ans += max(0, j - i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;of length&nbsp;n&nbsp;and an integer&nbsp;target, return&nbsp;the number of pairs(i, j)where0 &lt;= i &lt; j &lt; nandnums[i] + nums[j] &lt; target. Example 1: Input: nums&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,175],"class_list":["post-10070","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10070"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10070\/revisions"}],"predecessor-version":[{"id":10076,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10070\/revisions\/10076"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}