Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k.
A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1).
Return the total number of good pairs.
Example 1:
Input: nums1 = [1,3,4], nums2 = [1,3,4], k = 1
Output: 5
Explanation:
The 5 good pairs are(0, 0), (1, 0), (1, 1), (2, 0), and (2, 2).Example 2:
Input: nums1 = [1,2,4,12], nums2 = [2,4], k = 3
Output: 2
Explanation:
The 2 good pairs are (3, 0) and (3, 1).
Constraints:
1 <= n, m <= 501 <= nums1[i], nums2[j] <= 501 <= k <= 50Problem summary: You are given 2 integer arrays nums1 and nums2 of lengths n and m respectively. You are also given a positive integer k. A pair (i, j) is called good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1). Return the total number of good pairs.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,3,4] [1,3,4] 1
[1,2,4,12] [2,4] 3
count-array-pairs-divisible-by-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3162: Find the Number of Good Pairs I
class Solution {
public int numberOfPairs(int[] nums1, int[] nums2, int k) {
int ans = 0;
for (int x : nums1) {
for (int y : nums2) {
if (x % (y * k) == 0) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3162: Find the Number of Good Pairs I
func numberOfPairs(nums1 []int, nums2 []int, k int) (ans int) {
for _, x := range nums1 {
for _, y := range nums2 {
if x%(y*k) == 0 {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #3162: Find the Number of Good Pairs I
class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:
return sum(x % (y * k) == 0 for x in nums1 for y in nums2)
// Accepted solution for LeetCode #3162: Find the Number of Good Pairs I
/**
* [3162] Find the Number of Good Pairs I
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn number_of_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i32 {
let nums1: Vec<i32> = nums1
.into_iter()
.filter_map(|x| if x % k == 0 { Some(x / k) } else { None })
.collect();
let mut result = 0;
for &num in nums1.iter() {
for &num2 in nums2.iter() {
if num % num2 == 0 {
result += 1;
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3162() {
assert_eq!(
5,
Solution::number_of_pairs(vec![1, 3, 4], vec![1, 3, 4], 1)
);
assert_eq!(
2,
Solution::number_of_pairs(vec![1, 2, 4, 12], vec![2, 4], 3)
);
assert_eq!(1, Solution::number_of_pairs(vec![2, 12], vec![4, 3], 4));
}
}
// Accepted solution for LeetCode #3162: Find the Number of Good Pairs I
function numberOfPairs(nums1: number[], nums2: number[], k: number): number {
let ans = 0;
for (const x of nums1) {
for (const y of nums2) {
if (x % (y * k) === 0) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.