# Calculate total wall area of houses painted

• Last Updated : 29 Jul, 2022

Given integers N, L and W representing number of rows and length and width of houses in each row, and an array Heights[], representing the height of each house, the task is to find the total area of the outer walls required to be painted if the adjacent houses share a common wall.

Examples:

Input: N = 4, W = 1, L = 1, Heights[] = {1, 2, 3, 4}
Output: 28
Explanation:
The area of house-1 painted = 2 * 1 + 1 * 1 = 3
The area of house-2 painted = 2 * 2 + 1 * 1 = 5
The area of house-3 painted = 2 * 3 + 1 * 1 = 7
The area of house-4 painted = 2 * 4 + 1 * 1 + 1 * 4 = 13
Therefore, the total area painted = 28

Input: N = 7, W = 1, L = 1, Heights[] = {4, 3, 1, 2, 3, 4, 2}
Output: 52

Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:

• The total area of the wall of the first house painted is equal to 2 * w * h1 + l * (h1 + max(0, h2 – h1)).
• The total area of the wall of the last house painted is equal to 2 * w * hn + l * (hn + max(0, hn – hn-1)).
• The total area of the wall of the house painted other than the first and the last houses is equal to 2 * w * hi + l * (max(0, hi – hi + 1) + max(0, hi – hi-1)).
• Therefore, the total area of all the houses painted will be calculated using the following formula:

Total area printed = 2 * w(h1 + …+hn) + l(h1 + hn) + l * Î£abs(hi – hi-1).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the total area``// of walls painted in N row-houses``void` `areaToPaint(``int` `N, ``int` `W, ``int` `L,``                 ``int` `Heights[])``{` `    ``// Stores total area of N row-houses``    ``// that needs to be painted``    ``int` `total = 0;` `    ``// Traverse the array of wall heights``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update total area painted``        ``total += 2 * Heights[i] * W;``    ``}` `    ``// Update total``    ``total += L * (Heights[0]``                  ``+ Heights[N - 1]);` `    ``// Traverse all the houses and``    ``// print the shared walls``    ``for` `(``int` `i = 1; i < N; i++) {` `        ``// Update total``        ``total += L * ``abs``(Heights[i]``                         ``- Heights[i - 1]);``    ``}` `    ``// Print total area needs to paint``    ``cout << total;``}` `// Driver Code``int` `main()``{` `    ``// Given N, W & L``    ``int` `N = 7, W = 1, L = 1;` `    ``// Given heights of houses``    ``int` `Heights[N]``        ``= { 4, 3, 1, 2, 3, 4, 2 };` `    ``// Function Call``    ``areaToPaint(N, W, L, Heights);``}`

## Java

 `// Java code of above approach``import` `java.util.*;``class` `GFG {` `  ``// Function to find the total area``  ``// of walls painted in N row-houses``  ``static` `void` `areaToPaint(``int` `N, ``int` `W, ``int` `L,``                          ``int` `Heights[])``  ``{` `    ``// Stores total area of N row-houses``    ``// that needs to be painted``    ``int` `total = ``0``;` `    ``// Traverse the array of wall heights``    ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``// Update total area painted``      ``total += ``2` `* Heights[i] * W;``    ``}` `    ``// Update total``    ``total += L * (Heights[``0``]``                  ``+ Heights[N - ``1``]);` `    ``// Traverse all the houses and``    ``// print the shared walls``    ``for` `(``int` `i = ``1``; i < N; i++) {` `      ``// Update total``      ``total += L * Math.abs(Heights[i]``                            ``- Heights[i - ``1``]);``    ``}` `    ``// Print total area needs to paint``    ``System.out.print(total);``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``// Given N, W & L``    ``int` `N = ``7``, W = ``1``, L = ``1``;` `    ``// Given heights of houses``    ``int` `Heights[]``      ``= { ``4``, ``3``, ``1``, ``2``, ``3``, ``4``, ``2` `};` `    ``// Function Call``    ``areaToPaint(N, W, L, Heights);``  ``}``}` `// This code is contributed by offbeat`

## Python3

 `# Python 3 program for the above approach` `# Function to find the total area``# of walls painted in N row-houses``def` `areaToPaint(N, W, L, Heights):` `    ``# Stores total area of N row-houses``    ``# that needs to be painted``    ``total ``=` `0` `    ``# Traverse the array of wall heights``    ``for` `i ``in` `range``(N):` `        ``# Update total area painted``        ``total ``+``=` `2` `*` `Heights[i] ``*` `W` `    ``# Update total``    ``total ``+``=` `L ``*` `(Heights[``0``]``                  ``+` `Heights[N ``-` `1``])` `    ``# Traverse all the houses and``    ``# print the shared walls``    ``for` `i ``in` `range``(``1``, N):` `        ``# Update total``        ``total ``+``=` `L ``*` `abs``(Heights[i]``                         ``-` `Heights[i ``-` `1``])` `    ``# Print total area needs to paint``    ``print``(total)`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Given N, W & L``    ``N ``=` `7``    ``W ``=` `1``    ``L ``=` `1` `    ``# Given heights of houses``    ``Heights ``=` `[``4``, ``3``, ``1``, ``2``, ``3``, ``4``, ``2``]` `    ``# Function Call``    ``areaToPaint(N, W, L, Heights)` `    ``# This code is contributed by chitranayal.`

## C#

 `// C# program for the above approach``using` `System;`` ` `class` `GFG{`` ` `// Function to find the total area``// of walls painted in N row-houses``static` `void` `areaToPaint(``int` `N, ``int` `W, ``int` `L, ``int``[] Heights)``{``    ``// Stores total area of N row-houses``    ``// that needs to be painted``    ``int` `total = 0;` `    ``// Traverse the array of wall heights``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Update total area painted``        ``total += 2 * Heights[i] * W;``    ``}` `    ``// Update total``    ``total += L * (Heights[0] + Heights[N - 1]);` `    ``// Traverse all the houses and``    ``// print the shared walls``    ``for` `(``int` `i = 1; i < N; i++) {` `        ``// Update total``        ``total += L * Math.Abs(Heights[i] - Heights[i - 1]);``    ``}` `    ``// Print total area needs to paint``    ``Console.Write(total);``}`` ` `// Driver Code``public` `static` `void` `Main(String[] args)``{`` ` `    ``// Given N, W & L``    ``int` `N = 7, W = 1, L = 1;` `    ``// Given heights of houses``    ``int``[] Heights = { 4, 3, 1, 2, 3, 4, 2 };` `    ``// Function Call``    ``areaToPaint(N, W, L, Heights);``}``}`` ` `// This code is contributed by aditya942003patil`

## Javascript

 ``

Output:

`52`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up