LeetCode 3162: Find the Number of Good Pairs I Solution

Master LeetCode problem 3162 (Find the Number of Good Pairs I), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3162. Find the Number of Good Pairs I

Problem Explanation

Explanation

To solve this problem, we can iterate through all possible pairs of indices (i, j) and check if nums1[i] is divisible by nums2[j]*k. If it is divisible, we increment a counter to keep track of the number of good pairs. We can do this by using nested loops to iterate through both arrays.

Algorithm:

  1. Initialize a counter variable goodPairs to 0.
  2. Iterate through each pair of indices (i, j) using nested loops.
  3. Check if nums1[i] is divisible by nums2[j]*k.
  4. If it is divisible, increment goodPairs.
  5. Return the total number of good pairs.

Time Complexity: O(n*m), where n and m are the lengths of nums1 and nums2 respectively. Space Complexity: O(1)

Solution Code

class Solution {
    public int countGoodPairs(int[] nums1, int[] nums2, int k) {
        int goodPairs = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if (nums1[i] % (nums2[j] * k) == 0) {
                    goodPairs++;
                }
            }
        }
        return goodPairs;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3162 (Find the Number of Good Pairs I)?

This page provides optimized solutions for LeetCode problem 3162 (Find the Number of Good Pairs I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3162 (Find the Number of Good Pairs I)?

The time complexity for LeetCode 3162 (Find the Number of Good Pairs I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3162 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3162 in Java, C++, or Python.

Back to LeetCode Solutions