Open In App

Maximize occupied cells in given Matrix satisfying the conditions

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a matrix of dimension N*M, the task is to maximize the total number of occupied cells in the given matrix such that they follow the given condition:

  • If two cells are occupied in the same row, there must be at least one empty cell between them.
  • If two cells are occupied in different rows, there must be at least one completely empty row between them 
    i.e. if cells in ith and jth row are occupied such that i < j then there must be a kth row completely empty such that i < k < j.

Examples:

Input: N = 1 ,M = 5
Output: 3
Explanation: There is only one row with five seats. 
Maximum three cells can be occupied. 
See the table below where 1 denotes occupied cell.

1   1   1

Input: N = 3 ,M = 3
Output: 4
Explanation:  There are three rows with three seats each.
Maximum occupied cells can be 4.

1   1
     
1   1
 

Naive Approach: The problem can be solved using greedy approach. Start filling from the first row and first column and maintain a gap of 1 between any two occupied cells of a row and a gap of one row between two occupied cells of different rows.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// maximum number of occupied cells
int solve(int N, int M)
{
    int ans = 0;
    while (1) {
        // Count number of occupied cells
        // in one row
        for (int i = 1; i <= M; i += 2) {
            ans++;
        }
        // If row numbers to be filled
        // are greater than or equal to 2
        // decrease by 2 otherwise decrease by 1
        // and if number of rows to be filled
        // are 0 return ans
        if (N >= 2) {
            N--;
            N--;
        }
        else if (N == 1) {
            N--;
        }
        if (N == 0) {
            break;
        }
    }
   
    // Return number of occupied cells
    return ans;
}
 
// Driver code
int main()
{
    int N = 1;
    int M = 5;
    cout << solve(N, M);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
public class GFG
{
 
  // Function to find
  // maximum number of occupied cells
  static int solve(int N, int M)
  {
    int ans = 0;
    while (true)
    {
 
      // Count number of occupied cells
      // in one row
      for (int i = 1; i <= M; i += 2) {
        ans++;
      }
 
      // If row numbers to be filled
      // are greater than or equal to 2
      // decrease by 2 otherwise decrease by 1
      // and if number of rows to be filled
      // are 0 return ans
      if (N >= 2) {
        N--;
        N--;
      }
      else if (N == 1) {
        N--;
      }
      if (N == 0) {
        break;
      }
    }
 
    // Return number of occupied cells
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 1;
    int M = 5;
    System.out.print(solve(N, M));
  }
}
 
// This code is contributed by Samim Hossain Mondal


Python3




# Python program for the above approach
 
# Function to find
# maximum number of occupied cells
def solve(N, M):
    ans = 0;
    while (1):
     
        # Count number of occupied cells
        # in one row
        for i in range(1, M + 1, 2):
            ans += 1
         
        # If row numbers to be filled
        # are greater than or equal to 2
        # decrease by 2 otherwise decrease by 1
        # and if number of rows to be filled
        # are 0 return ans
        if (N >= 2):
            N -= 1
            N -= 1
        elif (N == 1):
            N -= 1
        if (N == 0):
            break
   
    # Return number of occupied cells
    return ans;
 
# Driver code
N = 1;
M = 5;
print(solve(N, M));
 
# This code is contributed by saurabh_jaiswal.


C#




// C# program for the above approach
using System;
class GFG {
 
  // Function to find
  // maximum number of occupied cells
  static int solve(int N, int M)
  {
    int ans = 0;
    while (true)
    {
 
      // Count number of occupied cells
      // in one row
      for (int i = 1; i <= M; i += 2) {
        ans++;
      }
 
      // If row numbers to be filled
      // are greater than or equal to 2
      // decrease by 2 otherwise decrease by 1
      // and if number of rows to be filled
      // are 0 return ans
      if (N >= 2) {
        N--;
        N--;
      }
      else if (N == 1) {
        N--;
      }
      if (N == 0) {
        break;
      }
    }
 
    // Return number of occupied cells
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 1;
    int M = 5;
    Console.Write(solve(N, M));
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
// Javascript program for the above approach
 
// Function to find
// maximum number of occupied cells
function solve(N, M)
{
    let ans = 0;
    while (1)
    {
     
        // Count number of occupied cells
        // in one row
        for (let i = 1; i <= M; i += 2) {
            ans++;
        }
         
        // If row numbers to be filled
        // are greater than or equal to 2
        // decrease by 2 otherwise decrease by 1
        // and if number of rows to be filled
        // are 0 return ans
        if (N >= 2) {
            N--;
            N--;
        }
        else if (N == 1) {
            N--;
        }
        if (N == 0) {
            break;
        }
    }
   
    // Return number of occupied cells
    return ans;
}
 
// Driver code
let N = 1;
let M = 5;
document.write(solve(N, M));
 
// This code is contributed by saurabh_jaiswal.
</script>


 
 

Output

3

Time Complexity: O(N*M)
Auxiliary Space: O(1), since no extra space has been taken.

Efficient Approach: To maximize the total number of occupied cells they need to be occupied in the above mentioned manner. The total number can be obtained from the following observation:

So, the maximum number of cells that can be occupied for each row is ceil(M/2).
And as there is a gap of one row between any two occupied cells of different rows, 
the maximum number of rows that can be occupied is ceil(N/2).
Therefore maximum number of cells that can be occupied is ceil(M/2) * ceil(N/2).

Below is the implementation of the above approach.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find
// maximum number of occupied cells
int solve(int N, int M)
{
    int x = (M + 1) / 2;
    int y = (N + 1) / 2;
    int ans = x * y;
 
    // Return number of occupied cells
    return ans;
}
 
// Driver code
int main()
{
    int N = 1;
    int M = 5;
    cout << solve(N, M);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to find
  // maximum number of occupied cells
  public static int solve(int N, int M)
  {
    int x = (M + 1) / 2;
    int y = (N + 1) / 2;
    int ans = x * y;
 
    // Return number of occupied cells
    return ans;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int N = 1;
    int M = 5;
    System.out.print(solve(N, M));
  }
}
 
// This code is contributed by Shubham Singh


Python3




# Python program for the above approach
 
# Function to find
# maximum number of occupied cells
def solve (N, M):
    x = (M + 1) // 2
    y = (N + 1) // 2
    ans = x * y
     
    # Return number of occupied cells
    return ans
     
# Driver code
N = 1
M = 5
print(solve(N, M));
 
# This code is contributed by Shubham Singh


C#




// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find
  // maximum number of occupied cells
  public static int solve(int N, int M)
  {
    int x = (M + 1) / 2;
    int y = (N + 1) / 2;
    int ans = x * y;
 
    // Return number of occupied cells
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 1;
    int M = 5;
    Console.Write(solve(N, M));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
        // JavaScript program for the above approach
 
        // Function to find
        // maximum number of occupied cells
        const solve = (N, M) => {
            let x = parseInt((M + 1) / 2);
            let y = parseInt((N + 1) / 2);
            let ans = x * y;
 
            // Return number of occupied cells
            return ans;
        }
 
        // Driver code
 
        let N = 1;
        let M = 5;
        document.write(solve(N, M));
 
    // This code is contributed by rakeshsahni
 
    </script>


 
 

Output

3

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads