C++ code to find the lamps needed to light up a floor

C++Server Side ProgrammingProgramming

Suppose, there is a floor divided into a grid that has n rows and m columns. Now the floor has to be lit using lamps. A lamp, if placed at the border of two cells can light up two cells. If the lamp is placed in the vertical border, it lights up the cells to its left and right and if it is placed in the horizontal border, it lights up cells to its front and back. Given n and m, we have to find out the minimum number of lamps needed to light up the whole floor.

So, if the input is like n = 5, m = 3, then the output will be 8.

Steps

To solve this, we will follow these steps −

res := (n * m + 1) / 2
return res

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
#define N 100
int solve(int n, int m) {
   int res = (n * m + 1) / 2;
   return res;
}
int main() {
   int n = 5, m = 3;
   cout<< solve(n, m);
   return 0;
}

Input

5, 3

Output

8
raja
Updated on 11-Mar-2022 06:38:37

Advertisements