Count of cells in a matrix whose adjacent cells’s sum is prime Number
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) = 5
Input : 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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
bool prime[MAX];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
prime[0] = prime[1] = false ;
for ( int p = 2; p * p < MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
int PrimeSumCells(vector<vector< int > >& mat)
{
int count = 0;
int N = mat.size();
int M = mat[0].size();
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
int sum = 0;
if (i - 1 >= 0)
sum += mat[i - 1][j];
if (i + 1 < N)
sum += mat[i + 1][j];
if (j - 1 >= 0)
sum += mat[i][j - 1];
if (j + 1 < M)
sum += mat[i][j + 1];
if (prime[sum])
count++;
}
}
return count;
}
int main()
{
SieveOfEratosthenes();
vector<vector< int > > mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
cout << PrimeSumCells(mat) << endl;
}
|
Java
class GFG{
static final int MAX = 100005 ;
static boolean []prime = new boolean [MAX];
static void SieveOfEratosthenes()
{
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] == true )
{
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
static int PrimeSumCells( int [][]mat)
{
int count = 0 ;
int N = mat.length;
int M = mat[ 0 ].length;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
int sum = 0 ;
if (i - 1 >= 0 )
sum += mat[i - 1 ][j];
if (i + 1 < N)
sum += mat[i + 1 ][j];
if (j - 1 >= 0 )
sum += mat[i][j - 1 ];
if (j + 1 < M)
sum += mat[i][j + 1 ];
if (prime[sum])
count++;
}
}
return count;
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int [][]mat = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 } };
System.out.print(PrimeSumCells(mat) + "\n" );
}
}
|
Python3
MAX = 100005
prime = [ True ] * MAX
def SieveOfEratosthenes():
global prime
prime[ 0 ] = prime[ 1 ] = False
p = 2
while p * p < MAX :
if (prime[p] = = True ):
for i in range (p * p, MAX , p):
prime[i] = False
p + = 1
def PrimeSumCells(mat):
count = 0
N = len (mat)
M = len (mat[ 0 ])
for i in range (N):
for j in range (M):
sum = 0
if (i - 1 > = 0 ):
sum + = mat[i - 1 ][j]
if (i + 1 < N):
sum + = mat[i + 1 ][j]
if (j - 1 > = 0 ):
sum + = mat[i][j - 1 ]
if (j + 1 < M):
sum + = mat[i][j + 1 ]
if (prime[ sum ]):
count + = 1
return count
if __name__ = = "__main__" :
SieveOfEratosthenes()
mat = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ]]
print (PrimeSumCells(mat))
|
C#
using System;
class GFG{
static readonly int MAX = 100005;
static bool []prime = new bool [MAX];
static void SieveOfEratosthenes()
{
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] == true )
{
for ( int i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
static int PrimeSumCells( int [,]mat)
{
int count = 0;
int N = mat.GetLength(0);
int M = mat.GetLength(1);
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
int sum = 0;
if (i - 1 >= 0)
sum += mat[i - 1, j];
if (i + 1 < N)
sum += mat[i + 1, j];
if (j - 1 >= 0)
sum += mat[i, j - 1];
if (j + 1 < M)
sum += mat[i, j + 1];
if (prime[sum])
count++;
}
}
return count;
}
public static void Main(String[] args)
{
SieveOfEratosthenes();
int [,]mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
Console.Write(PrimeSumCells(mat) + "\n" );
}
}
|
Javascript
<script>
let MAX = 100005
let prime = new Array(MAX);
function SieveOfEratosthenes()
{
prime.fill( true )
prime[0] = prime[1] = false ;
for (let p = 2; p * p < MAX; p++) {
if (prime[p] == true ) {
for (let i = p * p; i < MAX; i += p)
prime[i] = false ;
}
}
}
function PrimeSumCells(mat)
{
let count = 0;
let N = mat.length;
let M = mat[0].length;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
let sum = 0;
if (i - 1 >= 0)
sum += mat[i - 1][j];
if (i + 1 < N)
sum += mat[i + 1][j];
if (j - 1 >= 0)
sum += mat[i][j - 1];
if (j + 1 < M)
sum += mat[i][j + 1];
if (prime[sum])
count++;
}
}
return count;
}
SieveOfEratosthenes();
let mat = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ] ];
document.write(PrimeSumCells(mat) + "<br>" );
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(MAX)
Last Updated :
30 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...