In this HackerRank 3D Surface Area problem, you have 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.

HackerRank 3D Surface Area problem solution


Problem solution in Python programming.

#!/bin/python3

import sys

def surfaceArea(A):
    H = len(A)
    W = len(A[0])
    ans = 2*H*W
    for i in range(H):
        for j in range(W):
            # top
            if i == 0:
                ans += A[i][j]
            
            # left
            if j == 0:
                ans += A[i][j]
            
            # right
            if j == W-1:
                ans += A[i][j]
            else:
                ans += abs(A[i][j]-A[i][j+1])
            
            # bottom
            if i == H-1:
                ans += A[i][j]
            else:
                ans += abs(A[i][j]-A[i+1][j])
    return ans

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)


Problem solution in Java Programming.

import java.util.Scanner;

public class Solution {
    private static int m;
    private static int n;
    private static int[][][] a;

    private static int surfaceArea() {
        int result = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= 100; k++) {
                    if (a[i][j][k] == 0) {
                        continue;
                    }
                    if (a[i + 1][j][k] == 0) {
                        result++;
                    }
                    if (a[i - 1][j][k] == 0) {
                        result++;
                    }
                    if (a[i][j + 1][k] == 0) {
                        result++;
                    }
                    if (a[i][j - 1][k] == 0) {
                        result++;
                    }
                    if (a[i][j][k + 1] == 0) {
                        result++;
                    }
                    if (a[i][j][k - 1] == 0) {
                        result++;
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        a = new int[m + 2][n + 2][102];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                int height = sc.nextInt();
                for (int k = 1; k <= height; k++) {
                    a[i][j][k] = 1;
                }
            }
        }
        int result = surfaceArea();
        System.out.println(result);
        sc.close();
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

int surfaceArea(vector < vector<int> > A) {
    int res = 0;
    for(int i = 1; i < A.size() -1; i++) {
        for(int j = 1; j < A[0].size()-1; j++) {
            for(int k = 1; k <= A[i][j]; k++){
                if(A[i-1][j] < k) res++;
                if(A[i+1][j] < k) res++;
                if(A[i][j+1] < k) res++;
                if(A[i][j-1] < k) res++;
            }
            res+=2;
        }
     }
    return res;
}

int main() {
    int H;
    int W;
    cin >> H >> W;
    vector< vector<int> > A(H + 2,vector<int>(W + 2, 0));
    for(int A_i = 1;A_i <= H;A_i++){
       for(int A_j = 1;A_j <= W;A_j++){
          cin >> A[A_i][A_j];
       }
    }
    int result = surfaceArea(A);
    cout << result << endl;
    return 0;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
    int row; 
    int column; 
    scanf("%d %d",&row,&column);
    int a[100][100];
    for (int A_i = 0; A_i < row; A_i++) {
       for (int A_j = 0; A_j < column; A_j++) {
      
          scanf("%d ",&a[A_i][A_j]);
       }
    }
    int left=0,right=0,i,j,min=0;
    for(i=0;i<row;i++)
    {   
        for(j=0;j<column;j++)
        {
            if((a[i][j]-min)>=0)
            right+=(a[i][j]-min);
            min=a[i][j];
         
        }
        min=0;
    }
    min=0;
   for(i=0;i<column;i++)
    {   
        for(j=0;j<row;j++)
        {
            if((a[j][i]-min)>=0)
            left+=(a[j][i]-min);
            min=a[j][i];
         
        }
        min=0;
    }
    min=0;
    //printf("%d %d\n",left,right);
    printf("%d",(2*left)+(2*right)+2*(row*column));
    return 0;
}


Problem solution in JavaScript programming.

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, H, W) {
    let count = 0;
    for (let i = 0; i < H; i++) {
        for (let j = 0; j < W; j++) {
            var L = A[i][j];
            count += 2;
            if (!i) {
                count += L;
            } else if (L > A[i-1][j]) {
                count += L - A[i-1][j];
            }
            if (i === H - 1) {
                count += L;
            } else if (L > A[i+1][j]) {
                count += L - A[i+1][j];    
            }

            if (!j) {
                count += L;
            } else if (L > A[i][j-1]) {
                count += L - A[i][j-1];
            }
            if (j === W - 1) {
                count += L;
            } else if (L > A[i][j+1]) {
                count += L - A[i][j+1];    
            }
        }
    }
    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, H, W);
    process.stdout.write("" + result + "\n");

}