3D Surface Area – HackerRank Solution

In this post, we will solve 3D Surface Area HackerRank Solution. This problem (3D Surface Area) is a part of HackerRank Problem Solving series.

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.

Solution – 3D Surface Area – HackerRank Solution

C++

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int T[200][200];

int main() {
    int H, W;
    while(cin >> H >> W) {
        for(int i=0; i<H; i++) 
            for(int j=0; j<W; j++) 
                cin >> T[i][j];
                
        int total = 2*H*W;
        for(int i=0; i<H; i++) {
            total += T[i][0];
            for(int j=1; j<W; j++)
                total += abs(T[i][j] - T[i][j-1]);
            total += T[i][W-1];
        }
        for(int i=0; i<W; i++) {
            total += T[0][i];
            for(int j=1; j<H; j++)
                total += abs(T[j][i] - T[j-1][i]);
            total += T[H-1][i];
        }
        
        cout << total << endl;
    }
}

Python

import sys

def surfaceArea(A):
    H, W = len(A), len(A[0])
    area = 2*H*W
    #print("H = {} W = {} area = {}".format(len(A), len(A[0]), area))
    
    for ind in range(H):
        for jnd in range(W):
            #print("i = {} j = {} A = {}".format(ind, jnd, A[ind][jnd]))
            # ind-1, jnd
            if ind-1 >= 0:
                #print("adding part {}".format(max(0, A[ind][jnd] - A[ind-1][jnd])))
                area += max(0, A[ind][jnd] - A[ind-1][jnd])
            else:
                #print("adding full {}".format(A[ind][jnd]))
                area += A[ind][jnd]
            # ind, jnd-1
            if jnd-1 >= 0:
                #print("adding part {}".format(max(0, A[ind][jnd] - A[ind][jnd-1])))
                area += max(0, A[ind][jnd] - A[ind][jnd-1])
            else:
                #print("adding full {}".format(A[ind][jnd]))
                area += A[ind][jnd]
            # ind+1, jnd
            if ind+1 < H:
                #print("adding part {}".format(max(0, A[ind][jnd] - A[ind+1][jnd])))
                area += max(0, A[ind][jnd] - A[ind+1][jnd])
            else:
                #print("adding full {}".format(A[ind][jnd]))
                area += A[ind][jnd]
            # ind, jnd+1
            if jnd+1 < W:
                #print("adding part {}".format(max(0, A[ind][jnd] - A[ind][jnd+1])))
                area += max(0, A[ind][jnd] - A[ind][jnd+1])
            else:
                #print("adding full {}".format(A[ind][jnd]))
                area += A[ind][jnd]
    return area

if __name__ == "__main__":
    H, W = input().strip().split(' ')
    H, W = [int(H), int(W)]
    A = []
    for A_i in range(H):
        A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
        A.append(A_t)
    result = surfaceArea(A)
    print(result)

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();
    }
}

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

Leave a Comment

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