HackerRank 3D Surface Area Problem Solution Yashwant Parihar, April 19, 2023April 28, 2023 In this post, we will HackerRank 3D Surface Area Problem Solution. 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 Ai, written on it. To create the toy Mason stacks A; 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 Ai; and that the price of the toy is equal to the 3d surface area find the price of the toy.Input FormatThe 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 denotesAij. 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 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 Problem Solution 3D Surface Area C Solution #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main() { long H,W,r,c,result; long **data; // initialize the final answer result = 0; // get the base size scanf("%ld %ld",&H,&W); // allocate space to hold the information // NOTE - We will allocate the space with an extra zero // around all sides data = (long **)malloc((H+2)*sizeof(long *)); // add an extra rows and extra columns (of zeros!) data[0] = (long *)malloc((W+2)*sizeof(long)); memset(data[0],0,(W+2)*sizeof(long)); data[H+1] = (long *)malloc((W+2)*sizeof(long)); memset(data[H+1],0,(W+2)*sizeof(long)); // read in the data and process each row for (r=1;r<=H;r++) { //printf("Process row %ld\n",r); fflush(stdout); // allocate each row data[r] = (long *)malloc((W+2)*sizeof(long)); memset(data[r],0,(W+2)*sizeof(long)); // read each row and process top and bottom surfaces for (c=1;c<=W;c++) { scanf("%ld",&data[r][c]); //printf(" Read %ld,%ld = %ld\n",r,c,data[r][c]); fflush(stdout); // add the top and bottom areas if (data[r][c]>0) { result += 2; } } // the difference in each col represents the amount of exposed // surface area for (c=0;c<((W+2)-1);c++) { result += abs(data[r][c]-data[r][c+1]); } } // the difference in each row represents the amount of exposed // surface area for (c=1;c<=W;c++) { for (r=0;r<((H+2)-1);r++) { result += abs(data[r][c]-data[r+1][c]); } } // output the result printf("%ld\n", result); return 0; } 3D Surface Area C++ Solution #include <ios> #include <iostream> inline long long int maximum(long long int a, long long int b) { return (a > b ? a : b); } long long int a[105][105] = {}; int main() { int n, m; std::cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) std::cin >> a[i][j]; long long int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans += 2; if (i == 0) ans += a[i][j]; else ans += maximum(0, a[i][j]-a[i-1][j]); if (i == n-1) ans += a[i][j]; else ans += maximum(0, a[i][j]-a[i+1][j]); if (j == 0) ans += a[i][j]; else ans += maximum(0, a[i][j]-a[i][j-1]); if (j == m-1) ans += a[i][j]; else ans += maximum(0, a[i][j]-a[i][j+1]); } } std::cout << ans << '\n'; } 3D Surface Area C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static int surfaceArea(int[][] A) { // Complete this function int result = 0; int tH = A.Length; int tJoinPoint = 0; int totalNum = 0; for (int i = 0; i < tH; i++) { int tW = A[i].Length; for (int j = 0; j < tW; j++) { tJoinPoint += A[i][j] - 1; totalNum += A[i][j]; if (j + 1 < tW) { if (A[i][j + 1] > A[i][j]) tJoinPoint += A[i][j]; else tJoinPoint += A[i][j + 1]; } if (i + 1 < tH) { if (A[i + 1][j] > A[i][j]) tJoinPoint += A[i][j]; else tJoinPoint += A[i + 1][j]; } } } result = totalNum * 6 - tJoinPoint * 2; return result; } static void Main(String[] args) { string[] tokens_H = Console.ReadLine().Split(' '); int H = Convert.ToInt32(tokens_H[0]); int W = Convert.ToInt32(tokens_H[1]); int[][] A = new int[H][]; for(int A_i = 0; A_i < H; A_i++){ string[] A_temp = Console.ReadLine().Split(' '); A[A_i] = Array.ConvertAll(A_temp,Int32.Parse); } int result = surfaceArea(A); Console.WriteLine(result); } } 3D Surface Area Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'surfaceArea' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY A as parameter. */ public static int surfaceArea(List<List<Integer>> A) { int H = A.size(); int W = A.get(0).size(); int area = 2 * W * H; for (int i = 0; i < H; i++) { int max_h = 0; for (int j = 0; j < W; j++) { int h = A.get(i).get(j); max_h = Math.max(max_h, h); area += 4 * h; // Take away overlapping surface area if (i > 0) { area -= 2 * Math.min(h, A.get(i - 1).get(j)); } if (j > 0) { area -= 2 * Math.min(h, A.get(i).get(j - 1)); } } } return area; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int H = Integer.parseInt(firstMultipleInput[0]); int W = Integer.parseInt(firstMultipleInput[1]); List<List<Integer>> A = new ArrayList<>(); IntStream.range(0, H).forEach(i -> { try { A.add( Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()) ); } catch (IOException ex) { throw new RuntimeException(ex); } }); int result = Result.surfaceArea(A); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } } 3D Surface Area JavaScript Solution 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) { // Complete this function var grid=[]; var count = 2*(A.length*A[0].length+A.length+A[0].length); // var count =30; for(var s = 0; s<A.length;s++){ grid.push(Array(A[0].length).fill(0)); } for (var i = 0; i < A.length; i++) { for (var j = 0; j < A[i].length; j++) { for(var k=2;k<=A[i][j];k++){ var side = 0; if (i - 1 > -1 && grid[i - 1][j] >= k) side++; if (i + 1 < A.length && grid[i + 1][j] >= k) side++; if (j - 1 > -1 && grid[i][j - 1] >=k) side++; if (j + 1 < A[i].length && grid[i][j+1] >= k) side++; switch (side) { case 0: count += 4; break; case 1: count += 2; break; case 2: count += 0; break; case 3: count += -2; break; case 4: count += -4; break; } } grid[i][j]=A[i][j]; } } return count; } 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 Python Solution import sys def main(): buffer = map(int, sys.stdin.read().split()) H = next(buffer) W = next(buffer) board = [[0] * (W + 2) for _ in range(H + 2)] for i in range(1, H + 1): for j in range(1, W + 1): board[i][j] = next(buffer) total = 2 * H * W for i in range(1, H + 1): for j in range(1, W + 1): total += max(0, board[i][j] - board[i + 1][j]) total += max(0, board[i][j] - board[i - 1][j]) total += max(0, board[i][j] - board[i][j + 1]) total += max(0, board[i][j] - board[i][j - 1]) print(total) if __name__ == '__main__': main() Other Solutions HackerRank Absolute Permutation Solution HackerRank The Bomberman Game Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython