g2301_2400.s2364_count_number_of_bad_pairs.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2364_count_number_of_bad_pairs;
// #Medium #Array #Hash_Table #2022_08_14_Time_44_ms_(80.00%)_Space_55.3_MB_(100.00%)
import java.util.HashMap;
/**
* 2364 - Count Number of Bad Pairs\.
*
* Medium
*
* You are given a **0-indexed** integer array `nums`. A pair of indices `(i, j)` is a **bad pair** if `i < j` and `j - i != nums[j] - nums[i]`.
*
* Return _the total number of **bad pairs** in_ `nums`.
*
* **Example 1:**
*
* **Input:** nums = [4,1,3,3]
*
* **Output:** 5
*
* **Explanation:**
*
* 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.
*
* **Example 2:**
*
* **Input:** nums = [1,2,3,4,5]
*
* **Output:** 0
*
* **Explanation:** There are no bad pairs.
*
* **Constraints:**
*
* * 1 <= nums.length <= 105
* * 1 <= nums[i] <= 109
**/
public class Solution {
public long countBadPairs(int[] nums) {
HashMap seen = new HashMap<>();
long count = 0;
for (int i = 0; i < nums.length; i++) {
int diff = i - nums[i];
if (seen.containsKey(diff)) {
count += (i - seen.get(diff));
} else {
count += i;
}
seen.put(diff, seen.getOrDefault(diff, 0) + 1);
}
return count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy