HackerRank 3D Surface Area Solution

Hello Programmers, In this post, you will learn how to solve HackerRank 3D Surface Area Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank 3D Surface Area

Madison is a little girl who is fond of toys. Her friend Mason works in a toy manufacturing factory . Mason has a 2D board A of size H x W  with H rows and W columns. The board is divided into cells of size 1 x 1 with each cell indicated by its coordinate (ij). The cell (ij) has an integer Aij written on it. To create the toy Mason stacks Aij number of cubes of size 1 x 1 x 1 on the cell (ij).

Given the description of the board showing the values of Aij and that the price of the toy is equal to the 3d surface area find the price of the toy.

Input Format

The first line contains two space-separated integers H and W the height and the width of the board respectively.

The next H lines contains W space separated integers. The jth integer in ith line denotes Aij.

Constraints

• 1 <= HW <= 100
• 1 <= Aij <= 100

Output Format

Print the required answer, i.e the price of the toy, in one line.

Sample Input 0

```1 1
1
```

Sample Output 0

```6
```

Explanation 0

The surface area of 1 x 1 x 1 cube is 6.

Sample Input 1

```3 3
1 3 4
2 2 3
1 2 4
```

Sample Output 1

```60
```

Explanation 1

The object is rotated so the front row matches column 1 of the input, heights 1, 2, and 1.

• The front face is 1 + 2 + 1 = 4 units in area.
• The top is 3 units.
• The sides are 4 units.
• None of the rear faces are exposed.
• The underside is 3 units.

The front row contributes 4 + 3 + 4 + 3 = 14 units to the surface area.

HackerRank 3D Surface Area Solution

3D Surface Area Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int surfaceArea(int h, int w, int** a) {
// Complete this function
int ans=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(i!=h-1){
if(a[i][j]>a[i+1][j]){
ans+=a[i][j]-a[i+1][j];
//printf("%d 11 %d\n",ans,a[i][j]);
}
}
else{
ans+=a[i][j];
/// printf("%d 12 %d\n",ans,a[i][j]);
}
if(i!=0){
if(a[i][j]>a[i-1][j]){
ans+=a[i][j]-a[i-1][j];
//printf("%d 21 %d\n",ans,a[i][j]);
}
}
else{
ans+=a[i][j];
// printf("%d 22 %d\n",ans,a[i][j]);
}
if(j!=w-1){
if(a[i][j]>a[i][j+1]){
ans+=a[i][j]-a[i][j+1];
//printf("%d 31 %d\n",ans,a[i][j]);
}
}
else{
ans+=a[i][j];
// printf("%d 32 %d\n",ans,a[i][j]);
}
if(j!=0){
if(a[i][j]>a[i][j-1]){
ans+=a[i][j]-a[i][j-1];
//printf("%d 41 %d\n",ans,a[i][j]);
}
}
else{
ans+=a[i][j];
//printf("%d 42 %d\n",ans,a[i][j]);
}
}
}
return ans;
}

int main() {
int H;
int W;
scanf("%i %i", &H, &W);
int **A=(int **)malloc(sizeof(int *)*H);
for (int A_i = 0; A_i < H; A_i++) {
A[A_i]=(int *)malloc(sizeof(int )*W);
for (int A_j = 0; A_j < W; A_j++) {

scanf("%i",&A[A_i][A_j]);
}
}
int result = surfaceArea(H, W, A);
printf("%d\n", result+(2*H*W));
return 0;
}```

3D Surface Area Solution in Cpp

```#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
int h,w,a[555][555];
int main()
{
cin>>h>>w;
for(int i=1;i<=h;++i)
for(int j=1;j<=w;++j)
cin>>a[i][j];
int s=h*w*2;
for(int i=1;i<=h;++i)
for(int j=1;j<=w+1;++j)
s+=abs(a[i][j]-a[i][j-1]);
for(int i=1;i<=h+1;++i)
for(int j=1;j<=w;++j)
s+=abs(a[i][j]-a[i-1][j]);
cout<<s<<"\n";
}
```

3D Surface Area Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static int surfaceArea(int[][] A) {
int cost = 0 ;
int m = A.length-2;
int k = A[0].length-2;
for(int i=1;i<=m;i++){
for(int j=1;j<=k;j++){
cost+=2;
cost+=(A[i-1][j]<A[i][j]?(A[i][j] - A[i-1][j]):0);
cost+=(A[i+1][j]<A[i][j]?(A[i][j] - A[i+1][j]):0);
cost+=(A[i][j-1]<A[i][j]?(A[i][j] - A[i][j-1]):0);
cost+=(A[i][j+1]<A[i][j]?(A[i][j] - A[i][j+1]):0);

}

}
return cost;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int H = in.nextInt();
int W = in.nextInt();
int[][] A = new int[H+2][W+2];
for(int A_i = 1; A_i <= H; A_i++){
for(int A_j = 1; A_j <= W; A_j++){
A[A_i][A_j] = in.nextInt();
}
}
int result = surfaceArea(A);
System.out.println(result);
in.close();
}
}```

3D Surface Area Solution in Python

```#!/bin/python

import sys

def surfaceArea(A, h, w):
res = 0
for r in A:
for a in r:
if a:
res += 2

for i in xrange(h):
res += A[i][0]
res += A[i][-1]
for j in xrange(w - 1):
res += abs(A[i][j] - A[i][j + 1])

for j in xrange(w):
res += A[0][j]
res += A[-1][j]
for i in xrange(h - 1):
res += abs(A[i][j] - A[i+1][j])
return res

# Complete this function

if __name__ == "__main__":
H, W = raw_input().strip().split(' ')
H, W = [int(H), int(W)]
A = []
for A_i in xrange(H):
A_temp = map(int,raw_input().strip().split(' '))
A.append(A_temp)
result = surfaceArea(A, H, W)
print result```

3D Surface Area Solution using JavaScript

```process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
input_stdin += data;
});

process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});

return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function surfaceArea(A) {
let h = A.length;
let w = A[0].length;

let arr = Array(h + 2).fill(null).map(() => Array(w + 2).fill(0));

for (let j = 0; j < h; j++) {
arr[j + 1] = [0].concat(A[j], 0);
}

let area = 0;

for (let j = 1; j < h + 1; j++) {
for (let i = 1; i < w + 1; i++) {
if (arr[j][i] > arr[j][i - 1]) {
area += arr[j][i] - arr[j][i - 1];
}

if (arr[j][i] > arr[j][i + 1]) {
area += arr[j][i] - arr[j][i + 1];
}

if (arr[j][i] > arr[j - 1][i]) {
area += arr[j][i] - arr[j - 1][i];
}

if (arr[j][i] > arr[j + 1][i]) {
area += arr[j][i] - arr[j + 1][i];
}

area += 2;
}
}

return area;
}

function main() {
var H = parseInt(H_temp[0]);
var W = parseInt(H_temp[1]);
var A = [];
for(A_i = 0; A_i < H; A_i++){
A[A_i] = A[A_i].map(Number);
}
var result = surfaceArea(A);
process.stdout.write("" + result + "\n");

}```

3D Surface Area Solution in Scala

```object Solution {

def main(args: Array[String]) {
val sc = new java.util.Scanner(System.in)
val h = sc.nextInt()
val w = sc.nextInt()
val matrix = new Array[Array[Int]](h)
sc.nextLine()
for (i <- matrix.indices)
matrix(i) = sc.nextLine().split(" ").map(_.toInt)
val totalSides = (for {
i <- 0 until w
j <- 0 until h
} yield getSides(matrix, i, j, h, w)).sum
println(totalSides)
}

def getSides(matrix: Array[Array[Int]], x: Int, y: Int, h: Int, w: Int): Int = {
var sides = 0
// west
if (x == 0)
sides = sides + matrix(y)(x)
else if (matrix(y)(x) > matrix(y)(x - 1))
sides = sides + (matrix(y)(x) - matrix(y)(x - 1))
// north
if (y == 0)
sides = sides + matrix(y)(x)
else if (matrix(y)(x) > matrix(y - 1)(x))
sides = sides + (matrix(y)(x) - matrix(y - 1)(x))
// east
if (x == w - 1)
sides = sides + matrix(y)(x)
else if (matrix(y)(x) > matrix(y)(x + 1))
sides = sides + (matrix(y)(x) - matrix(y)(x + 1))
// south
if (y == h - 1)
sides = sides + matrix(y)(x)
else if (matrix(y)(x) > matrix(y + 1)(x))
sides = sides + (matrix(y)(x) - matrix(y + 1)(x))
// top and bottom
sides + 2
}
}```

3D Surface Area Solution in Pascal

```uses math;

var a: array [0..105, 0..105] of longint;
h, w, i, j, cnt: longint;

begin
fillchar(a, sizeof(a), 0);
for i := 1 to h do
for j := 1 to w do
cnt := 0;
for i := 1 to h do
for j := 1 to w do
begin
inc(cnt, 1);
inc(cnt, 1);
inc(cnt, max(0, a[i][j] - a[i][j - 1]));
inc(cnt, max(0, a[i][j] - a[i][j + 1]));
inc(cnt, max(0, a[i][j] - a[i - 1][j]));
inc(cnt, max(0, a[i][j] - a[i + 1][j]));
end;
writeln(cnt);
end.```

Disclaimer: This problem (3D Surface Area) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.