Given a M x N matrix mat[][], the task is to count the number of cells which have the sum of its adjacent cells equal to a prime number. For a cell x[i][j], only x[i+1][j], x[i-1][j], x[i][j+1] and x[i][j-1] are the adjacent cells.
Examples:
Input : mat[][] = {{1, 3}, {2, 5}}
Output :2
Explanation: Only the cells mat[0][0] and mat[1][1] satisfying the condition.
i.e for mat[0][0]:(3+2) = 5, for mat[1][1]: (3+2) = 5Input : mat[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
Output : 6
Explanation: Cells mat[0][0], mat[0][2], mat[0][3], mat[1][3], mat[2][2] and mat[2][3] are satisfying the condition.
Prerequisites: Sieve of Eratosthenes
Approach:
- Precompute and store the prime numbers using Sieve.
- Iterate the entire matrix and for each cell find the sum of all adjacent cells.
- If the sum of adjacent cells equal to a prime number then increments the count.
- Return the value of the count.
Below is the implementation of the above approach.
C++
// CPP program to find the cells whose // adjacent cells's sum is prime Number #include <bits/stdc++.h> using namespace std; #define MAX 100005 bool prime[MAX]; void SieveOfEratosthenes() { // Create a boolean array "prime[0..MAX-1]" // and initialize all entries it as true. // A value in prime[i] will finally // be false if i is Not a prime, else true. memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p // greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < MAX; i += p) prime[i] = false; } } } // Function to count the cells having // adjacent cell's sum // is equal to prime int PrimeSumCells(vector<vector<int> >& mat) { int count = 0; int N = mat.size(); int M = mat[0].size(); // Traverse for all the cells for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int sum = 0; // i-1, j if (i - 1 >= 0) sum += mat[i - 1][j]; // i+1, j if (i + 1 < N) sum += mat[i + 1][j]; // i, j-1 if (j - 1 >= 0) sum += mat[i][j - 1]; // i, j+1 if (j + 1 < M) sum += mat[i][j + 1]; // If the sum is a prime number if (prime[sum]) count++; } } // Return the count return count; } // Driver Program int main() { SieveOfEratosthenes(); vector<vector<int> > mat = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; // Function call cout << PrimeSumCells(mat) << endl; } |
Java
// Java program to find the cells whose // adjacent cells's sum is prime Number class GFG{ static final int MAX = 100005; static boolean []prime = new boolean[MAX]; static void SieveOfEratosthenes() { // Create a boolean array "prime[0..MAX-1]" // and initialize all entries it as true. // A value in prime[i] will finally // be false if i is Not a prime, else true. for (int i = 0; i < prime.length; i++) prime[i] = true; prime[0] = prime[1] = false; for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p // greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < MAX; i += p) prime[i] = false; } } } // Function to count the cells having // adjacent cell's sum // is equal to prime static int PrimeSumCells(int [][]mat) { int count = 0; int N = mat.length; int M = mat[0].length; // Traverse for all the cells for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int sum = 0; // i-1, j if (i - 1 >= 0) sum += mat[i - 1][j]; // i+1, j if (i + 1 < N) sum += mat[i + 1][j]; // i, j-1 if (j - 1 >= 0) sum += mat[i][j - 1]; // i, j+1 if (j + 1 < M) sum += mat[i][j + 1]; // If the sum is a prime number if (prime[sum]) count++; } } // Return the count return count; } // Driver Code public static void main(String[] args) { SieveOfEratosthenes(); int [][]mat = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; // Function call System.out.print(PrimeSumCells(mat) + "\n"); } } // This code is contributed by sapnasingh4991 |
C#
// C# program to find the cells whose // adjacent cells's sum is prime Number using System; class GFG{ static readonly int MAX = 100005; static bool []prime = new bool[MAX]; static void SieveOfEratosthenes() { // Create a bool array "prime[0..MAX-1]" // and initialize all entries it as true. // A value in prime[i] will finally // be false if i is Not a prime, else true. for (int i = 0; i < prime.Length; i++) prime[i] = true; prime[0] = prime[1] = false; for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p // greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < MAX; i += p) prime[i] = false; } } } // Function to count the cells having // adjacent cell's sum // is equal to prime static int PrimeSumCells(int [,]mat) { int count = 0; int N = mat.GetLength(0); int M = mat.GetLength(1); // Traverse for all the cells for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int sum = 0; // i-1, j if (i - 1 >= 0) sum += mat[i - 1, j]; // i+1, j if (i + 1 < N) sum += mat[i + 1, j]; // i, j-1 if (j - 1 >= 0) sum += mat[i, j - 1]; // i, j+1 if (j + 1 < M) sum += mat[i, j + 1]; // If the sum is a prime number if (prime[sum]) count++; } } // Return the count return count; } // Driver Code public static void Main(String[] args) { SieveOfEratosthenes(); int [,]mat = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; // Function call Console.Write(PrimeSumCells(mat) + "\n"); } } // This code is contributed by sapnasingh4991 |
6
Recommended Posts:
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Count of ways to select K consecutive empty cells from a given Matrix
- Queries to find the count of connected Non-Empty Cells in a Matrix with updates
- Find Number of Even cells in a Zero Matrix after Q queries
- Number of cells in matrix which are equidistant from given two points
- Number of cells in a matrix that satisfy the given condition
- Total number of cells covered in a matrix after D days
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Sum of the count of number of adjacent squares in an M X N grid
- Find safe cells in a matrix
- Unique cells in a binary matrix
- Find whether there is path between two cells in matrix
- Shortest distance between two cells in a matrix or grid
- Print cells with same rectangular sums in a matrix
- Size of all connected non-empty cells of a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Find cells in Matrix that are not visited by Robot for given movements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sapnasingh4991


