Open In App
Related Articles

Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 

  1. 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.
  2. If K = 0: The count remains C(N+M-2, N-1).
  3. 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++

// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the value of
// Binomial Coefficient C(n, k)
int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
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 {
 
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        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;
}
 
// Driver Code
int main()
{
    int N = 3;
    int M = 3;
    int K = 1;
 
    cout << countPath(N, M, K);
 
    return 0;
}

                    

Java

// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to return the value of
// Binomial Coefficient C(n, k)
static int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
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
    {
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        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;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int M = 3;
    int K = 1;
    System.out.print(countPath(N, M, K));
}
}
 
// This code is contributed by shikhasingrajput

                    

Python3

#Python3 Program to implement
#the above approach
#Function to return the value of
#Binomial Coefficient C(n, k)
def ncr(n, k):
    res = 1
 
    #Since C(n, k) = C(n, n-k)
    if (k > n - k):
        k = n - k
 
    #Calculate the value of
    #[n * (n-1) *---* (n-k+1)] /
    #[k * (k-1) *----* 1]
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
#Function to find the minimum
#count of paths from top
#left to bottom right by
#placing K 1s in the matrix
def countPath(N, M, K):
    answer = 0
    if (K >= 2):
        answer = 0
    elif (K == 0):
        answer = ncr(N + M - 2, N - 1)
    else:
 
        #Count of ways without 1s
        answer = ncr(N + M - 2, N - 1)
 
        #Count of paths from starting
        #point to mid point
        X = (N - 1) // 2 + (M - 1) // 2
        Y = (N - 1) // 2
        midCount = ncr(X, Y)
 
        #Count of paths from mid
        #point to end point
        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
 
#Driver Code
if __name__ == '__main__':
    N = 3
    M = 3
    K = 1
    print(countPath(N, M, K))
 
# This code is contributed by Mohit Kumar 29

                    

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to return the value of
// Binomial Coefficient C(n, k)
static int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
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
    {
         
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        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;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    int M = 3;
    int K = 1;
     
    Console.Write(countPath(N, M, K));
}
}
 
// This code is contributed by Amit Katiyar

                    

Javascript

<script>
 
// Javascript Program to implement
// the above approach
 
// Function to return the value of
// Binomial Coefficient C(n, k)
function ncr(n, k)
{
    var res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for (var i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
function countPath(N, M, K)
{
    var answer;
    if (K >= 2)
        answer = 0;
    else if (K == 0)
        answer = ncr(N + M - 2, N - 1);
    else {
 
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        var X = (N - 1) / 2 + (M - 1) / 2;
        var Y = (N - 1) / 2;
        var midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        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;
}
 
// Driver Code
var N = 3;
var M = 3;
var K = 1;
document.write( countPath(N, M, K));
 
</script>

                    

Output: 
2

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
Previous
Next
Share your thoughts in the comments
Similar Reads