Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The Prime Cells problem solution

YASH PAL, 31 July 202411 February 2026
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 solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes