Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth The Prime Cells problem solution

YASH PAL, 31 July 2024
In this HackerEarth The Prime Cells problem solution You are given a grid of size n x n filled with numbers in each of its cells. Now you need to count total cells in the grid such that the sum of the numbers on its top, left, right and bottom cells is a prime number. In case there is no cell in a particular direction assume the number to be 0.
HackerEarth The Prime Cells problem solution

HackerEarth The Prime Cells problem solution.

#include <bits/stdc++.h>
#define ll long long
#define endl 'n'
const int mod = 1e9 + 7;
using namespace std;

int main() {

int prime[401];

for(int i=0;i<=401;i++)

prime[i]=1;
prime[0]=0;
prime[1]=0;
for(int i=2;i<=401;i++){
if(prime[i]==1){
for(int j=2*i;j<=401;j=j+i)
prime[j]=0;

}

}
int n;

cin>>n;

int a[n+2][n+2];

for(int i=0;i<n+2;i++){

for(int j=0;j<n+2;j++)

a[i][j]=0;

}

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++)

cin>>a[i][j];

}
int sum=0;
int count=0;
for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){
int k=a[i][j-1];
int d=a[i][j+1];
int e=a[i-1][j];
int f=a[i+1][j];
sum=k+d+e+f;
if(prime[sum]==1)
count++;

}

}

cout<<count<<endl;

}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes