mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
34 lines
1.2 KiB
HTML
34 lines
1.2 KiB
HTML
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of indices <code>(i, j)</code> is a <strong>bad pair</strong> if <code>i < j</code> and <code>j - i != nums[j] - nums[i]</code>.</p>
|
|
|
|
<p>Return<em> the total number of <strong>bad pairs</strong> in </em><code>nums</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [4,1,3,3]
|
|
<strong>Output:</strong> 5
|
|
<strong>Explanation:</strong> The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
|
|
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
|
|
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
|
|
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
|
|
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
|
|
There are a total of 5 bad pairs, so we return 5.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,2,3,4,5]
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong> There are no bad pairs.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
|
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
|
</ul>
|