Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
Given two integers N and M where M and N denote a matrix of dimensions N * M consisting of 0‘s only. The task is to minimize the count of unique paths from the top left (0, 0) to bottom right (N – 1, M – 1) of the matrix across cells consisting of 0’s only by placing exactly K 1s in the matrix.
Note: Neither the bottom right nor the top-left cell can be modified to a 0.
Examples:
Input: N = 3, M = 3, K = 1
Output: 2
Explanation:
Placing K(= 1) 1s in the matrix to generate the matrix [[0, 0, 0], [0, 1, 0], [0, 0, 0]] leaves only two possible paths from top-left to bottom-right cells.
The paths are[(0, 0) ? (0, 1) ? (0, 2) ? (1, 2) ? (2, 2)] and [(0, 0) ? (1, 0) ? (2, 0) ? (2, 1) ? (2, 2)]
Input: N = 3, M = 3, K = 3
Output: 0
Explanation:
Placing K(= 3) 1s to generate a matrix [[0, 1, 1], [1, 0, 0], [0, 0, 0]] leaves no possible path from top-left to bottom-right.
Approach: The problem can be solved by considering the following possible cases.
- If K ? 2: The count of possible paths can be reduced to 0 by placing two 1s in (0, 1) and (1, 0) cells of the matrix.
- If K = 0: The count remains C(N+M-2, N-1).
- If K = 1: Place a 1 at the Centre of the matrix, ((N-1)/2, (M-1)/2) to minimize the path count. Therefore, the count of possible paths for this case is as follows:
Result = Total number of ways to reach the bottom right from the top left – ( Number of paths to midpoint from the top left * Number of ways to reach the endpoint from the midpoint)
where
- Total number of ways to reach the bottom right from the top left = C(N + M – 2, N – 1)
- Number of paths to midpoint from the top left = C((N – 1) / 2 + (M – 1) / 2, (N – 1) / 2)
- Number of ways to reach the endpoint from the midpoint=C(((N – 1) – (N – 1 ) / 2) + ((M – 1) – (M – 1) / 2), ((N – 1) – (N – 1) / 2))
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ncr( int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
int countPath( int N, int M, int K)
{
int answer;
if (K >= 2)
answer = 0;
else if (K == 0)
answer = ncr(N + M - 2, N - 1);
else {
answer = ncr(N + M - 2, N - 1);
int X = (N - 1) / 2 + (M - 1) / 2;
int Y = (N - 1) / 2;
int midCount = ncr(X, Y);
X = ((N - 1) - (N - 1) / 2)
+ ((M - 1) - (M - 1) / 2);
Y = ((N - 1) - (N - 1) / 2);
midCount *= ncr(X, Y);
answer -= midCount;
}
return answer;
}
int main()
{
int N = 3;
int M = 3;
int K = 1;
cout << countPath(N, M, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int ncr( int n, int k)
{
int res = 1 ;
if (k > n - k)
k = n - k;
for ( int i = 0 ; i < k; ++i)
{
res *= (n - i);
res /= (i + 1 );
}
return res;
}
static int countPath( int N, int M, int K)
{
int answer;
if (K >= 2 )
answer = 0 ;
else if (K == 0 )
answer = ncr(N + M - 2 , N - 1 );
else
{
answer = ncr(N + M - 2 , N - 1 );
int X = (N - 1 ) / 2 + (M - 1 ) / 2 ;
int Y = (N - 1 ) / 2 ;
int midCount = ncr(X, Y);
X = ((N - 1 ) - (N - 1 ) / 2 ) +
((M - 1 ) - (M - 1 ) / 2 );
Y = ((N - 1 ) - (N - 1 ) / 2 );
midCount *= ncr(X, Y);
answer -= midCount;
}
return answer;
}
public static void main(String[] args)
{
int N = 3 ;
int M = 3 ;
int K = 1 ;
System.out.print(countPath(N, M, K));
}
}
|
Python3
def ncr(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range (k):
res * = (n - i)
res / / = (i + 1 )
return res
def countPath(N, M, K):
answer = 0
if (K > = 2 ):
answer = 0
elif (K = = 0 ):
answer = ncr(N + M - 2 , N - 1 )
else :
answer = ncr(N + M - 2 , N - 1 )
X = (N - 1 ) / / 2 + (M - 1 ) / / 2
Y = (N - 1 ) / / 2
midCount = ncr(X, Y)
X = ((N - 1 ) - (N - 1 ) / / 2 ) +
((M - 1 ) - (M - 1 ) / / 2 )
Y = ((N - 1 ) - (N - 1 ) / / 2 )
midCount * = ncr(X, Y)
answer - = midCount
return answer
if __name__ = = '__main__' :
N = 3
M = 3
K = 1
print (countPath(N, M, K))
|
C#
using System;
class GFG{
static int ncr( int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static int countPath( int N, int M, int K)
{
int answer;
if (K >= 2)
answer = 0;
else if (K == 0)
answer = ncr(N + M - 2, N - 1);
else
{
answer = ncr(N + M - 2, N - 1);
int X = (N - 1) / 2 + (M - 1) / 2;
int Y = (N - 1) / 2;
int midCount = ncr(X, Y);
X = ((N - 1) - (N - 1) / 2) +
((M - 1) - (M - 1) / 2);
Y = ((N - 1) - (N - 1) / 2);
midCount *= ncr(X, Y);
answer -= midCount;
}
return answer;
}
public static void Main(String[] args)
{
int N = 3;
int M = 3;
int K = 1;
Console.Write(countPath(N, M, K));
}
}
|
Javascript
<script>
function ncr(n, k)
{
var res = 1;
if (k > n - k)
k = n - k;
for ( var i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
function countPath(N, M, K)
{
var answer;
if (K >= 2)
answer = 0;
else if (K == 0)
answer = ncr(N + M - 2, N - 1);
else {
answer = ncr(N + M - 2, N - 1);
var X = (N - 1) / 2 + (M - 1) / 2;
var Y = (N - 1) / 2;
var midCount = ncr(X, Y);
X = ((N - 1) - (N - 1) / 2)
+ ((M - 1) - (M - 1) / 2);
Y = ((N - 1) - (N - 1) / 2);
midCount *= ncr(X, Y);
answer -= midCount;
}
return answer;
}
var N = 3;
var M = 3;
var K = 1;
document.write( countPath(N, M, K));
</script>
|
Time Complexity: O(N+M), The time complexity of the given program is O(N+M) because it only involves simple mathematical operations and a loop that runs k times, where k <= min(N,M). The loop calculates the binomial coefficient, which takes O(k) time in each iteration. Since k can be at most min(N,M), the time complexity of the loop is O(min(N,M)). Therefore, the overall time complexity of the program is O(N+M).
Auxiliary Space: O(1), The space complexity of the program is constant because it only uses a fixed number of integer variables to store the values of N, M, K, res, i, X, Y, and answer. Since the size of these variables does not depend on the input size, the space complexity is constant or O(1).
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...