Hello Programmers, In this post, you will know how to solve the HackerRank 3D Surface Area Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank 3D Surface Area Solution
Task
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 (i, j). The cell (i, j) 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 (i, j).
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 <= H, W <= 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 Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #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(); }); function readLine() { 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_temp = readLine().split(' '); 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] = readLine().split(' '); 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); readln(h, w); for i := 1 to h do for j := 1 to w do read(a[i][j]); 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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.