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)

 



Previous Article
Next Article

Similar Reads

Number of K's such that the given array can be divided into two sets satisfying the given conditions
Given an array arr[] of size N. The task is to find the number of K's such that the array can be divided into two sets containing equal number of elements if elements less than K are in one set and the rest of them are in the other set. Note: N is always even. Examples: Input: arr[] = {9, 1, 4, 4, 6, 7} Output: 2 {1, 4, 4} and {6, 7, 9} are the two
4 min read
Count possible permutations of given array satisfying the given conditions
Given an array, arr[] consisting of N distinct elements, the task is to count possible permutations of the given array that can be generated which satisfies the following properties: The two halves must be sorted.arr[i] must be less than arr[N / 2 + i] Note: N is always even and indexing starts from 0. Examples: Input: arr[] = {10, 20, 30, 40} Outp
6 min read
Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
Given an M x N matrix mat[][]. The task is to count the number of good cells in the matrix. A cell will be good if the sum of the cell value and the number of the adjacent cells is a Fibonacci number.Examples: Input: mat[][] = { {1, 2}, {3, 4}} Output: 2 Only the cells mat[0][0] and mat[1][0] are good. i.e. (1 + 2) = 3 and (3 + 2) = 5 are both Fibo
9 min read
Count of cells in a matrix whose adjacent cells's sum is prime Number
Given a M x N matrix mat[][], the task is to count the number of cells which have the sum of its adjacent cells equal to a prime number. For a cell x[i][j], only x[i+1][j], x[i-1][j], x[i][j+1] and x[i][j-1] are the adjacent cells.Examples: Input : mat[][] = {{1, 3}, {2, 5}} Output :2 Explanation: Only the cells mat[0][0] and mat[1][1] satisfying t
10 min read
Count valid pairs in the array satisfying given conditions
Given an array of integers arr[], the task is to count the number of valid pairs of elements from arr. A pair (arr[x], arr[y]) is said to be invalid if arr[x] &lt; arr[y]abs(arr[x] - arr[y]) is odd Note: Pairs (arr[x], arr[y]) and (arr[y], arr[x]) are two different pairs when x != y and the value of arr[i] for all possible values of i is ? 120. Exa
5 min read
Generate N integers satisfying the given conditions
Given an integer N, the task is to generate an array of size N with the following properties: No two elements divide each other.Every odd subset has an odd sum and every even subset has an even sum. Examples: Input: N = 3 Output: 3 5 7 No two element divide each other and the sum of all the odd subsets {3}, {5}, {7} and {3, 5, 7} is odd. Sum of all
7 min read
Find the number of unique pairs satisfying given conditions
Given an array arr[] of distinct positive elements, the task is to find the number of unique pairs (a, b) such that a is the maximum and b is the second maximum element of some subarray of the given array.Examples: Input: arr[] = {1, 2, 3} Output: 2 {1, 2}, {2, 3}, {1, 2, 3} are the subarrays and the pairs satisfying given conditions are (2, 1) and
7 min read
Split N as the sum of K numbers satisfying the given conditions
Given an integer N, the task is to express the given number as the sum of K numbers where at least K - 1 numbers are distinct and are product of 2 primes. If no possible answer, exists, print -1. Examples: Input: N = 52, K = 5 Output: 6 10 14 15 7 Explanation: N = 52 can be expressed as 6 10 14 15 2, where 15 = 3 * 5, 14 = 2*7, 10 = 2*5, 6 = 2*3, i
9 min read
Count number of coordinates from an array satisfying the given conditions
Given an array arr[] consisting of N coordinates in the Cartesian Plane, the task is to find the number of coordinates, such as (X, Y), that satisfies all the following conditions: All possible arr[i][0] must be less than X and arr[i][1] must be equal to Y.All possible arr[i][0] must be greater than X and arr[i][1] must be equal to Y.All possible a
10 min read
Sort a permutation of first N Natural Numbers by swapping pairs satisfying given conditions
Given an array p[] of size N representing a permutation of first N natural numbers, where N is an even number, the task is to sort the array by taking a pair of indices a, b and swap p[a] and p[b] in each operation, where 2 * |a - b| ? N. Print the pairs of indices swapped in each operation. Examples: Input: p[] = {2, 5, 3, 1, 4, 6}Output: 31 52 51
13 min read