Find the Surface area of a 3D figure
Given a N*M matrix A[][] representing a 3D figure. The height of the building at is . Find the surface area of the figure.
Examples :
Input : N = 1, M = 1 A[][] = { {1} }
Output : 6
Explanation :
The total surface area is 6 i.e 6 side of
the figure and each are of height 1.
Input : N = 3, M = 3 A[][] = { {1, 3, 4},
{2, 2, 3},
{1, 2, 4} }
Output : 60
Approach : To find the surface area we need to consider the contribution of all the six sides of the given 3D figure. We will solve the questions in part to make it easy. The base of the Figure will always contribute N*M to the total surface area of the figure, and same N*M area will be contributed by the top of the figure. Now, to calculate the area contributed by the walls, we will take out the absolute difference between the height of two adjacent wall. The difference will be the contribution in the total surface area.
Below is the implementation of the above idea :
C++
#include <bits/stdc++.h>
using namespace std;
const int M = 3;
const int N = 3;
int contribution_height( int current, int previous)
{
return abs (current - previous);
}
int surfaceArea( int A[N][M])
{
int ans = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
int up = 0;
int left = 0;
if (i > 0)
up = A[i - 1][j];
if (j > 0)
left = A[i][j - 1];
ans += contribution_height(A[i][j], up)
+ contribution_height(A[i][j], left);
if (i == N - 1)
ans += A[i][j];
if (j == M - 1)
ans += A[i][j];
}
}
ans += N * M * 2;
return ans;
}
int main()
{
int A[N][M] = { { 1, 3, 4 },
{ 2, 2, 3 },
{ 1, 2, 4 } };
cout << surfaceArea(A) << endl;
return 0;
}
|
Java
class GFG
{
static final int M= 3 ;
static final int N= 3 ;
static int contribution_height( int current, int previous)
{
return Math.abs(current - previous);
}
static int surfaceArea( int A[][])
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++) {
int up = 0 ;
int left = 0 ;
if (i > 0 )
up = A[i - 1 ][j];
if (j > 0 )
left = A[i][j - 1 ];
ans += contribution_height(A[i][j], up)
+ contribution_height(A[i][j], left);
if (i == N - 1 )
ans += A[i][j];
if (j == M - 1 )
ans += A[i][j];
}
}
ans += N * M * 2 ;
return ans;
}
public static void main (String[] args)
{
int A[][] = {{ 1 , 3 , 4 },
{ 2 , 2 , 3 },
{ 1 , 2 , 4 } };
System.out.println(surfaceArea(A));
}
}
|
Python3
M = 3 ;
N = 3 ;
def contribution_height(current, previous):
return abs (current - previous);
def surfaceArea(A):
ans = 0 ;
for i in range (N):
for j in range (M):
up = 0 ;
left = 0 ;
if (i > 0 ):
up = A[i - 1 ][j];
if (j > 0 ):
left = A[i][j - 1 ];
ans + = contribution_height(A[i][j], up) + contribution_height(A[i][j], left);
if (i = = N - 1 ):
ans + = A[i][j];
if (j = = M - 1 ):
ans + = A[i][j];
ans + = N * M * 2 ;
return ans;
A = [[ 1 , 3 , 4 ],[ 2 , 2 , 3 ],[ 1 , 2 , 4 ]];
print (surfaceArea(A));
|
C#
using System;
class GFG
{
static int M=3;
static int N=3;
static int contribution_height( int current, int previous)
{
return Math.Abs(current - previous);
}
static int surfaceArea( int [,]A)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++) {
int up = 0;
int left = 0;
if (i > 0)
up = A[i - 1,j];
if (j > 0)
left = A[i,j - 1];
ans += contribution_height(A[i,j], up)
+ contribution_height(A[i,j], left);
if (i == N - 1)
ans += A[i,j];
if (j == M - 1)
ans += A[i,j];
}
}
ans += N * M * 2;
return ans;
}
public static void Main ()
{
int [,]A = {{ 1, 3, 4 },
{ 2, 2, 3 },
{ 1, 2, 4 } };
Console.WriteLine(surfaceArea(A));
}
}
|
PHP
<?php
$M = 3;
$N = 3;
function contribution_height( $current ,
$previous )
{
return abs ( $current - $previous );
}
function surfaceArea( $A )
{
global $M ;
global $N ;
$ans = 0;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $M ; $j ++)
{
$up = 0;
$left = 0;
if ( $i > 0)
$up = $A [ $i - 1][ $j ];
if ( $j > 0)
$left = $A [ $i ][ $j - 1];
$ans += contribution_height( $A [ $i ][ $j ], $up ) +
contribution_height( $A [ $i ][ $j ], $left );
if ( $i == $N - 1)
$ans += $A [ $i ][ $j ];
if ( $j == $M - 1)
$ans += $A [ $i ][ $j ];
}
}
$ans += $N * $M * 2;
return $ans ;
}
$A = array ( array (1, 3, 4),
array (2, 2, 3),
array (1, 2, 4));
echo surfaceArea( $A );
?>
|
Javascript
<script>
let M=3;
let N=3;
function contribution_height(current, previous)
{
return Math.abs(current - previous);
}
function surfaceArea( A)
{
let ans = 0;
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++) {
let up = 0;
let left = 0;
if (i > 0)
up = A[i - 1][j];
if (j > 0)
left = A[i][j - 1];
ans += contribution_height(A[i][j], up)
+ contribution_height(A[i][j], left);
if (i == N - 1)
ans += A[i][j];
if (j == M - 1)
ans += A[i][j];
}
}
ans += N * M * 2;
return ans;
}
let A = [[ 1, 3, 4 ],
[ 2, 2, 3 ],
[ 1, 2, 4 ]];
document.write(surfaceArea(A));
</script>
|
Time Complexity: O(N*M), as the above code is been iterating over through two loops.
Auxiliary Space: O(1)
Last Updated :
11 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...