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.
C++
// C++ program to find the// minimum number sprinklers// required 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 codeint main(){Â Â Â Â int N, M;Â Â Â Â N = 5;Â Â Â Â M = 3;Â Â Â Â solve(N, M);} |
Java
// Java program to find minimum// number sprinklers required// to cover the parkclass GFG{     // Function to find the minimum// number sprinklers required// 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 codepublic static void main(String args[]){Â Â Â Â int N = 5;Â Â Â Â int M = 3;Â Â Â Â Â Â Â Â Â System.out.println(solve(N, M));}}Â
// This code is contributed by grand_master |
Python3
# Python3 program to find the# minimum number sprinklers# required to water the park.Â
# Function to find the# minimum number sprinklers# required 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 codeN = 5M = 3solve(N, M)Â
# This code is contributed by yatinagg |
C#
// C# program to find minimum// number sprinklers required// to cover the parkusing System;Â
class GFG{     // Function to find the minimum// number sprinklers required// 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 codepublic static void Main(String []args){Â Â Â Â int N = 5;Â Â Â Â int M = 3;Â Â Â Â Â Â Â Â Â Console.WriteLine(solve(N, M));}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// javascript program to find the// minimum number sprinklers// required to water the park.Â
// Function to find the// minimum number sprinklers// required to water the park.function solve(N, M){Â
    // General requirements of    // sprinklers    var ans = (N) * parseInt((M / 2));Â
    // if M is odd then add    // one additional sprinklers    if (M % 2 == 1) {        ans += parseInt((N + 1) / 2);    }Â
    document.write(ans);}Â
// Driver code    var N, M;    N = 5;    M = 3;    solve(N, M);Â
// This code is contributed by SURENDRA_GANGWAR.</script> |
Output:
8
Time complexity: O(1)
Auxiliary space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Here you will find 40474 more Information on that Topic: geeksforgeeks.org/minimum-sprinklers-required-to-water-a-rectangular-park/ […]
… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/minimum-sprinklers-required-to-water-a-rectangular-park/ […]
… [Trackback]
[…] There you will find 51651 more Information to that Topic: geeksforgeeks.org/minimum-sprinklers-required-to-water-a-rectangular-park/ […]