- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to find out the number of border cells in a grid

Suppose, we are given a grid of dimensions h * w. We have to color the border cells of the grid (i.e. the outermost cells of the grid) with a special color. We have to find out how many border cells we have to color.

## Problem Category

Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.

So, if the input of our problem is like h = 4, w = 4, then the output will be 12.

## Steps

To solve this, we will follow these steps −

return (2 * ((h - 2) + 2 * (w - 2)))

## Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h> using namespace std; void solve(int h, int w) { cout<< 2 * (h - 2) + 2 * (w - 2) + 4 ; } int main() { int h = 4, w = 4; solve(h, w); return 0; }

## Input

4, 4

## Output

12

- Related Articles
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of cells to block in a grid to create a path
- C++ Program to find out the number of operations to maximize the number of even-numbered cells in a grid
- C++ program to find out the maximum number of cells a cleaning robot can clean in a grid
- Program to Find Out the Number of Squares in a Grid in Python
- C++ Program to find out the number of sides that a polygon has inside a grid
- C++ program to find out the number of ways a grid with boards can be colored
- C++ program to find out the maximum number of cells that can be illuminated
- C++ program to find out the number of iterations needed to convert all cells to black
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- C++ Program to find out if there is a pattern in a grid
- C++ program to find out number of changes required to get from one end to other end in a grid
- Python Program to find out the sum of values in hyperrectangle cells
- C++ Program to find out the total cost required for a robot to make a trip in a grid