LeetCode 3044: Most Frequent Prime Solution

Master LeetCode problem 3044 (Most Frequent Prime), a medium 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.

Problem Explanation

Explanation:

To solve this problem, we need to traverse the 2D matrix and generate all possible numbers by moving in 8 directions from each cell. Then, we count the frequency of each prime number greater than 10 and return the most frequent prime number or -1 if no such prime number exists.

  1. Create a helper function to check if a number is prime.
  2. Initialize a map to store the frequency of prime numbers greater than 10.
  3. Traverse the matrix and generate all possible numbers from each cell by moving in 8 directions.
  4. Count the frequency of prime numbers greater than 10.
  5. Find the most frequent prime number greater than 10 or return -1 if none exists.

Time Complexity: O(m * n * 8 * log(max_element)) Space Complexity: O(m * n)

:

Solution Code

class Solution {
    public int mostFrequentPrime(int[][] mat) {
        // Helper function to check if a number is prime
        boolean isPrime(int num) {
            if (num <= 1) return false;
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) return false;
            }
            return true;
        }
        
        Map<Integer, Integer> freqMap = new HashMap<>();
        
        int m = mat.length;
        int n = mat[0].length;
        
        int[] dx = {1, 1, 0, -1, -1, -1, 0, 1};
        int[] dy = {0, 1, 1, 1, 0, -1, -1, -1};
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < 8; k++) {
                    int num = 0;
                    int x = i, y = j;
                    while (x >= 0 && x < m && y >= 0 && y < n) {
                        num = num * 10 + mat[x][y];
                        if (num > 10 && isPrime(num)) {
                            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
                        }
                        x += dx[k];
                        y += dy[k];
                    }
                }
            }
        }
        
        int maxFreq = 0;
        int mostFreqPrime = -1;
        for (int prime : freqMap.keySet()) {
            if (freqMap.get(prime) > maxFreq || (freqMap.get(prime) == maxFreq && prime > mostFreqPrime)) {
                maxFreq = freqMap.get(prime);
                mostFreqPrime = prime;
            }
        }
        
        return mostFreqPrime;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3044 (Most Frequent Prime)?

This page provides optimized solutions for LeetCode problem 3044 (Most Frequent Prime) 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 3044 (Most Frequent Prime)?

The time complexity for LeetCode 3044 (Most Frequent Prime) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3044 on DevExCode?

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

Back to LeetCode Solutions