# Minimum sprinklers required to water a rectangular park

Given **N * M** rectangular park having **N** rows and **M** columns, each cell of the park is a square of unit area and boundaries between the cells is called hex and a sprinkler can be placed in the middle of the hex. The task is to find the minimum number of sprinklers required to water the entire park.

**Examples:**

Input:N = 3 M = 3

Output:5

Explanation:

For the first two columns 3 sprinklers are required and for last column we are bound to use 2 sprinklers to water the last column.

Input:N = 5 M = 3

Output:8

Explanation:

For the first two columns 5 sprinklers are required and for last column we are bound to use 3 sprinklers to water the last column.

**Approach:**

- After making some observation one thing can be point out i.e for every
**two**column,**N**sprinkler are required because we can placed them in between of two columns. - If
**M**is even, then clearly**N* (M / 2)**sprinklers are required. - But if
**M**is odd then solution for**M – 1**column can be computed using even column formula, and for last column add**( N + 1) / 2**sprinkler to water the last column irrespective of N is odd or even. - Minimum number of square tiles required to fill the rectangular floor
- Count of minimum reductions required to get the required sum K
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Minimum Players required to win the game
- Minimum time required to rot all oranges
- Minimum steps required to reach the end of a matrix | Set 2
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number of changes required to make the given array an AP
- Minimum number of given operation required to convert n to m
- Minimum number operations required to convert n to m | Set-2
- Minimum number of primes required such that their sum is equal to N
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of operations required to reduce N to 1
- Minimum concatenation required to get strictly LIS for the given array
- Minimum inversions required so that no two adjacent elements are same
- Minimum deletions required to make GCD of the array equal to 1
- Minimum time required to cover a Binary Array
- Minimum time required to rot all oranges | Dynamic Programming

Here is implementation of above approach:

## C++

`// C++ program to find the ` `// minimum number sprinklers ` `// reqired to water the park. ` ` ` `#include <iostream> ` `using` `namespace` `std; ` `typedef` `long` `long` `int` `ll; ` ` ` `// Function to find the ` `// minimum number sprinklers ` `// required to water the park. ` `void` `solve(` `int` `N, ` `int` `M) ` `{ ` ` ` ` ` `// General requirements of ` ` ` `// sprinklers ` ` ` `ll ans = (N) * (M / 2); ` ` ` ` ` `// if M is odd then add ` ` ` `// one additional sprinklers ` ` ` `if` `(M % 2 == 1) { ` ` ` `ans += (N + 1) / 2; ` ` ` `} ` ` ` ` ` `cout << ans << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N, M; ` ` ` `N = 5; ` ` ` `M = 3; ` ` ` `solve(N, M); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// number sprinklers required ` `// to cover the park ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `// number sprinklers reqired ` `// to water the park. ` `public` `static` `int` `solve(` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// General requirements of sprinklers ` ` ` `int` `ans = n * (m / ` `2` `); ` ` ` ` ` `// If M is odd then add one ` ` ` `// additional sprinklers ` ` ` `if` `(m % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `ans += (n + ` `1` `) / ` `2` `; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `int` `M = ` `3` `; ` ` ` ` ` `System.out.println(solve(N, M)); ` `} ` `} ` ` ` `// This code is contributed by grand_master ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the ` `# minimum number sprinklers ` `# required to water the park. ` ` ` `# Function to find the ` `# minimum number sprinklers ` `# reqired to water the park. ` `def` `solve(N, M) : ` ` ` ` ` `# General requirements of ` ` ` `# sprinklers ` ` ` `ans ` `=` `int` `((N) ` `*` `int` `(M ` `/` `2` `)) ` ` ` ` ` `# if M is odd then add ` ` ` `# one additional sprinklers ` ` ` `if` `(M ` `%` `2` `=` `=` `1` `): ` ` ` `ans ` `+` `=` `int` `((N ` `+` `1` `) ` `/` `2` `) ` ` ` ` ` `print` `(ans) ` ` ` `# Driver code ` `N ` `=` `5` `M ` `=` `3` `solve(N, M) ` ` ` `# This code is contributed by yatinagg ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// number sprinklers required ` `// to cover the park ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `// number sprinklers reqired ` `// to water the park. ` `public` `static` `int` `solve(` `int` `n, ` `int` `m) ` `{ ` ` ` ` ` `// General requirements of sprinklers ` ` ` `int` `ans = n * (m / 2); ` ` ` ` ` `// If M is odd then add one ` ` ` `// additional sprinklers ` ` ` `if` `(m % 2 == 1) ` ` ` `{ ` ` ` `ans += (n + 1) / 2; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `M = 3; ` ` ` ` ` `Console.WriteLine(solve(N, M)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity:** O(1)

**Auxiliary Space:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.