Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps
Last Updated :
07 May, 2023
Given a matrix mat[][] of dimensions N*M and a positive integer K, the task is to find the number of cells in a grid from which maximum cells can be reached by K jumps in the vertical or horizontal direction.
Examples:
Input: N = 3, M = 3, K = 2
Output: 4
Explanation:
The cells represented as X are the cells from which maximum cells (= 2) can be reached by K (= 2) jumps:
X O X
O O O
X O X
Input: N = 5, M = 5, K = 2
Output: 9
Approach: The given problem can be solved by counting the number of possible cells for the rows and columns separately based on the following observations:
- Consider any row, say i such that i <= K, then the number of rows from i that can be reached using jumps of K is (N – i) / K + 1.
- If the rows say i is i > K, then these cells are connected to some row X where X <= K, hence they are already considered.
Therefore, from the above observations, the idea is to find the count of rows that are connected to the maximum number of rows in a variable, say count_R. Similarly, for the columns, find the count of columns that are connected to maximum columns in a variable, say count_C.
Now, the number of cells in the grid such that the maximum cell is reachable from that cell with a jump of K in the vertical or horizontal direction is given by (count_R)*(count_C).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long countCells( int n, int m, int s)
{
int mx1 = -1;
int cont1 = 0;
for ( int i = 0; i < s && i < n; ++i) {
int aux = (n - (i + 1)) / s + 1;
if (aux > mx1) {
mx1 = cont1 = aux;
}
else if (aux == mx1)
cont1 += aux;
}
int mx2 = -1;
int cont2 = 0;
for ( int i = 0; i < s && i < m; ++i) {
int aux = (m - (i + 1)) / s + 1;
if (aux > mx2)
mx2 = cont2 = aux;
else if (aux == mx2)
cont2 += aux;
}
return ( long long )(cont1 * cont2);
}
int main()
{
int N = 5, M = 5, K = 2;
cout << countCells(N, M, K);
return 0;
}
|
Java
class GFG
{
public static long countCells( int n, int m, int s)
{
int mx1 = - 1 ;
int cont1 = 0 ;
for ( int i = 0 ; i < s && i < n; ++i) {
int aux = (n - (i + 1 )) / s + 1 ;
if (aux > mx1) {
mx1 = cont1 = aux;
}
else if (aux == mx1)
cont1 += aux;
}
int mx2 = - 1 ;
int cont2 = 0 ;
for ( int i = 0 ; i < s && i < m; ++i) {
int aux = (m - (i + 1 )) / s + 1 ;
if (aux > mx2)
mx2 = cont2 = aux;
else if (aux == mx2)
cont2 += aux;
}
return ( long ) (cont1 * cont2);
}
public static void main(String args[]) {
int N = 5 , M = 5 , K = 2 ;
System.out.println(countCells(N, M, K));
}
}
|
Python3
def countCells(n, m, s):
mx1 = - 1
cont1 = 0
i = 0
while (i < s and i < n):
aux = (n - (i + 1 )) / / s + 1
if (aux > mx1):
mx1 = cont1 = aux
elif (aux = = mx1):
cont1 + = aux
i + = 1
mx2 = - 1
cont2 = 0
i = 0
while (i < s and i < m):
aux = (m - (i + 1 )) / / s + 1
if (aux > mx2):
mx2 = cont2 = aux
elif (aux = = mx2):
cont2 + = aux
i + = 1
return cont1 * cont2
if __name__ = = '__main__' :
N = 5
M = 5
K = 2
print (countCells(N, M, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countCells( int n, int m, int s)
{
int mx1 = -1;
int cont1 = 0;
for ( int i = 0; i < s && i < n; ++i) {
int aux = (n - (i + 1)) / s + 1;
if (aux > mx1) {
mx1 = cont1 = aux;
}
else if (aux == mx1)
cont1 += aux;
}
int mx2 = -1;
int cont2 = 0;
for ( int i = 0; i < s && i < m; ++i) {
int aux = (m - (i + 1)) / s + 1;
if (aux > mx2)
mx2 = cont2 = aux;
else if (aux == mx2)
cont2 += aux;
}
return (cont1 * cont2);
}
public static void Main()
{
int N = 5, M = 5, K = 2;
Console.Write(countCells(N, M, K));
}
}
|
Javascript
<script>
function countCells(n, m, s)
{
let mx1 = -1;
let cont1 = 0;
for (let i = 0; i < s && i < n; ++i) {
let aux = Math.floor((n - (i + 1)) / s + 1);
if (aux > mx1) {
mx1 = cont1 = aux;
}
else if (aux == mx1) cont1 += aux;
}
let mx2 = -1;
let cont2 = 0;
for (let i = 0; i < s && i < m; ++i) {
let aux = Math.floor((m - (i + 1)) / s + 1);
if (aux > mx2) mx2 = cont2 = aux;
else if (aux == mx2) cont2 += aux;
}
return cont1 * cont2;
}
let N = 5,
M = 5,
K = 2;
document.write(countCells(N, M, K));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...