Maximize occupied cells in given Matrix satisfying the conditions
Last Updated :
12 Jul, 2022
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.
Input: N = 3 ,M = 3
Output: 4
Explanation: There are three rows with three seats each.
Maximum occupied cells can be 4.
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++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, int M)
{
int ans = 0;
while (1) {
for ( int i = 1; i <= M; i += 2) {
ans++;
}
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break ;
}
}
return ans;
}
int main()
{
int N = 1;
int M = 5;
cout << solve(N, M);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int solve( int N, int M)
{
int ans = 0 ;
while ( true )
{
for ( int i = 1 ; i <= M; i += 2 ) {
ans++;
}
if (N >= 2 ) {
N--;
N--;
}
else if (N == 1 ) {
N--;
}
if (N == 0 ) {
break ;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 1 ;
int M = 5 ;
System.out.print(solve(N, M));
}
}
|
Python3
def solve(N, M):
ans = 0 ;
while ( 1 ):
for i in range ( 1 , M + 1 , 2 ):
ans + = 1
if (N > = 2 ):
N - = 1
N - = 1
elif (N = = 1 ):
N - = 1
if (N = = 0 ):
break
return ans;
N = 1 ;
M = 5 ;
print (solve(N, M));
|
C#
using System;
class GFG {
static int solve( int N, int M)
{
int ans = 0;
while ( true )
{
for ( int i = 1; i <= M; i += 2) {
ans++;
}
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break ;
}
}
return ans;
}
public static void Main()
{
int N = 1;
int M = 5;
Console.Write(solve(N, M));
}
}
|
Javascript
<script>
function solve(N, M)
{
let ans = 0;
while (1)
{
for (let i = 1; i <= M; i += 2) {
ans++;
}
if (N >= 2) {
N--;
N--;
}
else if (N == 1) {
N--;
}
if (N == 0) {
break ;
}
}
return ans;
}
let N = 1;
let M = 5;
document.write(solve(N, M));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
return ans;
}
int main()
{
int N = 1;
int M = 5;
cout << solve(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int solve( int N, int M)
{
int x = (M + 1 ) / 2 ;
int y = (N + 1 ) / 2 ;
int ans = x * y;
return ans;
}
public static void main (String[] args)
{
int N = 1 ;
int M = 5 ;
System.out.print(solve(N, M));
}
}
|
Python3
def solve (N, M):
x = (M + 1 ) / / 2
y = (N + 1 ) / / 2
ans = x * y
return ans
N = 1
M = 5
print (solve(N, M));
|
C#
using System;
public class GFG
{
public static int solve( int N, int M)
{
int x = (M + 1) / 2;
int y = (N + 1) / 2;
int ans = x * y;
return ans;
}
public static void Main(String[] args)
{
int N = 1;
int M = 5;
Console.Write(solve(N, M));
}
}
|
Javascript
<script>
const solve = (N, M) => {
let x = parseInt((M + 1) / 2);
let y = parseInt((N + 1) / 2);
let ans = x * y;
return ans;
}
let N = 1;
let M = 5;
document.write(solve(N, M));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...