# 3D Surface Area HackerRank Solution in C, C++, Java, Python

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 with H*W with H rows and W columns. The board is divided into cells of size 1*1 with each cell indicated by it’s 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*1*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 ith in line denotes .

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

image The surface area of cube is 6.

Sample Input 1

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

Sample Output 1

`60`

Explanation 1

The sample input corresponds to the figure described in problem statement.

## 3D Surface Area HackerRank 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 array[101][101],height,width;

int small(int x, int y)
{
if (x < y) return(x);
return(y);
}

int f(int x)
{
return(4*x+2);
}

int g(int i, int j)
{
int term1,term2;
if (i == 0) term1=0;
else term1=small(array[i-1][j],array[i][j]);
if (j == 0) term2=0;
else term2=small(array[i][j-1],array[i][j]);
//printf("term1=%d,term2=%d\n",term1,term2);
return(2*(term1+term2));
}

int main()
{
int i,j,result;
scanf("%i %i", &height, &width);
for (i = 0; i < height; ++i)
{
for (j = 0; j < width; ++j) scanf("%i",&array[i][j]);
}
result=0;
for (i=0;i<height;++i)
{
for (j=0;j<width;++j)
{
result+=f(array[i][j]);
result-=g(i,j);
//printf("%d\n",result);
}
}

printf("%d\n", result);
return 0;
}```

## 3D Surface Area HackerRank Solution in C++

```#include <bits/stdc++.h>
using namespace std;

int A[102][102][102];

int main() {
int H;
int W;
cin >> H >> W;

for(int i=1; i<=H; i++)
for(int j=1; j<=W; j++)
{
int x;
cin >> x;

for(int k=1; k<=x; k++)
A[i][j][k] = 1;
}

int result = 0;

for(int i=1; i<=100; i++)
for(int j=1; j<=100; j++)
for(int k=1; k<=100; k++)
{
if(A[i][j][k] == 0)
continue;

result += (A[i-1][j][k] == 0);
result += (A[i+1][j][k] == 0);
result += (A[i][j-1][k] == 0);
result += (A[i][j+1][k] == 0);
result += (A[i][j][k-1] == 0);
result += (A[i][j][k+1] == 0);
}

cout << result << endl;
return 0;
}```

## 3D Surface Area HackerRank 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 HackerRank 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 HackerRank Solution in C#

```using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static int surfaceArea(int[][] A)
{
int area = 0;
// Complete this function
for (int i = 0; i < A.Length; i++)
{
for (int j = 0; j < A[i].Length; j++)
{
area += A[i][j] * 4 + 2;
if(i>0)
{
area -= Math.Min(A[i - 1][j], A[i][j]) * 2;
}
if (j > 0)
{
area -= Math.Min(A[i][j - 1], A[i][j]) * 2;
}
}
}
return area;
}

static void Main(String[] args) {
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++){