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.
Move from brute-force thinking to an efficient approach using array strategy.
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
nums1[i]2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length.nums2[i]2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9] Output: 1 Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1] Output: 9 Explanation: All Triplets are valid, because 12 = 1 * 1. Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]2 = nums2[j] * nums2[k]. Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7] Output: 2 Explanation: There are 2 valid triplets. Type 1: (3,0,2). nums1[3]2 = nums2[0] * nums2[2]. Type 2: (3,0,1). nums2[3]2 = nums1[0] * nums1[1].
Constraints:
1 <= nums1.length, nums2.length <= 10001 <= nums1[i], nums2[i] <= 105Problem summary: Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules: Type 1: Triplet (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length. Type 2: Triplet (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math · Two Pointers
[7,4] [5,2,8,9]
[1,1] [1,1,1]
[7,7,8,3] [1,2,9,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers
class Solution {
public int numTriplets(int[] nums1, int[] nums2) {
var cnt1 = count(nums1);
var cnt2 = count(nums2);
return cal(cnt1, nums2) + cal(cnt2, nums1);
}
private Map<Long, Integer> count(int[] nums) {
Map<Long, Integer> cnt = new HashMap<>();
int n = nums.length;
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
long x = (long) nums[j] * nums[k];
cnt.merge(x, 1, Integer::sum);
}
}
return cnt;
}
private int cal(Map<Long, Integer> cnt, int[] nums) {
int ans = 0;
for (int x : nums) {
long y = (long) x * x;
ans += cnt.getOrDefault(y, 0);
}
return ans;
}
}
// Accepted solution for LeetCode #1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers
func numTriplets(nums1 []int, nums2 []int) int {
cnt1 := count(nums1)
cnt2 := count(nums2)
return cal(cnt1, nums2) + cal(cnt2, nums1)
}
func count(nums []int) map[int]int {
cnt := map[int]int{}
for j, x := range nums {
for _, y := range nums[j+1:] {
cnt[x*y]++
}
}
return cnt
}
func cal(cnt map[int]int, nums []int) (ans int) {
for _, x := range nums {
ans += cnt[x*x]
}
return
}
# Accepted solution for LeetCode #1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def count(nums: List[int]) -> Counter:
cnt = Counter()
for j in range(len(nums)):
for k in range(j + 1, len(nums)):
cnt[nums[j] * nums[k]] += 1
return cnt
def cal(nums: List[int], cnt: Counter) -> int:
return sum(cnt[x * x] for x in nums)
cnt1 = count(nums1)
cnt2 = count(nums2)
return cal(nums1, cnt2) + cal(nums2, cnt1)
// Accepted solution for LeetCode #1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers
struct Solution;
use std::collections::HashMap;
impl Solution {
fn num_triplets(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let nums1: Vec<i64> = nums1.into_iter().map(|x| x as i64).collect();
let nums2: Vec<i64> = nums2.into_iter().map(|x| x as i64).collect();
let nums1_square: Vec<i64> = nums1.iter().map(|x| x * x).collect();
let nums2_square: Vec<i64> = nums2.iter().map(|x| x * x).collect();
let mut res = 0;
for x in nums1_square {
res += Self::product(x, &nums2);
}
for x in nums2_square {
res += Self::product(x, &nums1);
}
res
}
fn product(x: i64, nums: &[i64]) -> i32 {
let mut count: HashMap<i64, i32> = HashMap::new();
let mut res = 0;
for &y in nums {
if x % y == 0 {
if let Some(t) = count.get(&(x / y)) {
res += t;
}
}
*count.entry(y).or_default() += 1;
}
res
}
}
#[test]
fn test() {
let nums1 = vec![7, 4];
let nums2 = vec![5, 2, 8, 9];
let res = 1;
assert_eq!(Solution::num_triplets(nums1, nums2), res);
let nums1 = vec![1, 1];
let nums2 = vec![1, 1, 1];
let res = 9;
assert_eq!(Solution::num_triplets(nums1, nums2), res);
let nums1 = vec![7, 7, 8, 3];
let nums2 = vec![1, 2, 9, 7];
let res = 2;
assert_eq!(Solution::num_triplets(nums1, nums2), res);
let nums1 = vec![4, 7, 9, 11, 23];
let nums2 = vec![3, 5, 1024, 12, 18];
let res = 0;
assert_eq!(Solution::num_triplets(nums1, nums2), res);
let nums1 = vec![43024, 99908];
let nums2 = vec![1864];
let res = 0;
assert_eq!(Solution::num_triplets(nums1, nums2), res);
}
// Accepted solution for LeetCode #1577: Number of Ways Where Square of Number Is Equal to Product of Two Numbers
function numTriplets(nums1: number[], nums2: number[]): number {
const cnt1 = count(nums1);
const cnt2 = count(nums2);
return cal(cnt1, nums2) + cal(cnt2, nums1);
}
function count(nums: number[]): Map<number, number> {
const cnt: Map<number, number> = new Map();
for (let j = 0; j < nums.length; ++j) {
for (let k = j + 1; k < nums.length; ++k) {
const x = nums[j] * nums[k];
cnt.set(x, (cnt.get(x) || 0) + 1);
}
}
return cnt;
}
function cal(cnt: Map<number, number>, nums: number[]): number {
return nums.reduce((acc, x) => acc + (cnt.get(x * x) || 0), 0);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.