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

HackerEarth Matrix and Volume problem solution

YASH PAL, 31 July 202416 February 2026
In this HackerEarth Matrix and Volume problem solution You are given a large box of length X, width Y and height Z. This box is entirely made up of small cubic boxes of 1 unit volume each i.e. length of cube is 1 unit. Now some cubes were removed to create empty spaces. All the cubes that were not removed stay fixed in their initial position.
 
Now you are given the information of the new state of the large box i.e. you are given the status of each cube that was used in making the larger box. If the status is 1 then it means that this cubic block was not removed or else if the status is 0 then it corresponds to the fact that this cubic block was removed.
 
 
HackerEarth Matrix and Volume problem solution

 

 

HackerEarth Matrix and Volume problem solution.

#include<bits/stdc++.h>
#include <stdint.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define inf 1e18
#define PI 3.14159265
#define mset(A,num,n); memset(A,num,n);
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define nl cout << endl
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
//
#define trace1(x) cout <<#x<<": "<<x<< endl;
#define trace2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int MOD = 1e9+7;
const int N = 1e6+5;

typedef long long int ll;
typedef int64_t lln;

ll POWER[65];
ll aonb(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}

void precompute() {
POWER[0]=1;
for(int i=1;i<63;i++) POWER[i]=POWER[i-1]<<1LL;
}

int x,y,z;
int A[105][105][105];
bool dp[105][105][105];

bool check(int ii,int jj, int kk){
if (ii<0 || ii>=x || jj <0 || jj>=y || kk<0 || kk>=z){
return false;
}
return true;
}

int a[] = {1,-1,0,0,0,0};
int b[] = {0,0,1,-1,0,0};
int c[] = {0,0,0,0,1,-1};

void dfs(int ii, int jj, int kk){
if (!check(ii,jj,kk))
return ;
if (A[ii][jj][kk] == 1)
return ;
A[ii][jj][kk] = 1;
for (int i=0;i<6;i++){
dfs(ii+a[i],jj+b[i],kk+c[i]);
}
}

int main()
{
std::ios_base::sync_with_stdio(false);
freopen("input_format","rt",stdin);
freopen("output_format","wt",stdout);
// std::cout << std::setprecision(10) << std::fixed;

int t;
cin >> t;
while(t--){
cin >> x >> y >> z;
for (int k=0;k<z;k++){
for (int i=0;i<x;i++){
for (int j=0;j<y;j++){
cin >> A[i][j][k];
}
}
}

for (int i=0;i<x;i++){
for (int j=0;j<y;j++){
if (A[i][j][0] == 0){
dfs(i,j,0);
}
if (A[i][j][z-1] == 0){
dfs(i,j,z-1);
}
}
}
for (int i=0;i<x;i++){
for (int k=0;k<z;k++){
if (A[i][0][k] == 0){
dfs(i,0,k);
}
if (A[i][y-1][k] == 0){
dfs(i,y-1,k);
}
}
}
for (int j=0;j<y;j++){
for (int k=0;k<z;k++){
if (A[0][j][k] == 0){
dfs(0,j,k);
}
if (A[x-1][j][k] == 0){
dfs(x-1,j,k);
}
}
}

int count=0;
for (int i=0;i<x;i++){
for (int j=0;j<y;j++){
for (int k=0;k<z;k++){
if (A[i][j][k] == 0){
count++;
// trace4(i,j,k,count);
}
}
}
}
cout << count << endl;
}
return 0;
}


/*
4 4 3
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

1 1 1 1
1 1 1 1
1 1 0 1
1 1 1 1

ans = 4;
 

Second solution

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define MM 1000000009
#define reset(a) memset(a,0,sizeof(a))
#define rep(i,j,k) for(i=j;i<=k;++i)
#define per(i,j,k) for(i=j;i>=k;--i)
#define print(a,start,end) for(i=start;i<=end;++i) cout<<a[i];
#define endl "n"
#define inf 100000000000000
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
int p , q , r;
set<int> s;
int ct = 0;
int dp[101][101][101];
bool visit[101][101][101];
void dfs(int x,int y,int z)
{
if(x < 1 || y < 1 || z < 1 || x > p || y > q|| z > r|| dp[x][y][z] == 1 || visit[x][y][z] == 1)
return;
++ct;
s.insert(x);
s.insert(y);
s.insert(z);
dfs(x + 1, y , z);
dfs(x - 1, y , z);
dfs(x , y + 1, z);
dfs(x , y - 1, z);
dfs(x , y , z - 1);
dfs(x , y , z + 1);

}
int main()
{
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while(t--)
{
int x , y , z;
cin >> x >> y >> z;

p = x , q = y , r = z;
for(int i = 1; i <= z ; i++)
{
for(int j = 1; j <= x; j++)
{
for(int k = 1; k <= y ; k++)
{
visit[i][j][k] = 0;
cin >> dp[j][k][i];
}
}
}
int ans = 0;
for(int i = 1; i <= x ; i++)
{
for(int j = 1; j <= y ; j++)
{
for(int k = 1; k <= z ; k++)
{
s.clear();
if(dp[i][j][k] == 0 && visit[i][j][k] == 0)
{
ct = 0;
s.insert(i);
s.insert(j);
s.insert(k);
dfs(i , j , k);
}
if(s.find(1) == s.end() && s.find(x) == s.end() && s.find(y) == s.end() && s.find(z) == s.end())
{
ans = ans + ct;
}
}
}
}
cout << ans << 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 *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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