Open In App

Minimum operations to set given coordinates as 1 by choosing a set bit index and changing whole row to 1

Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary matrix arr[][] having N rows and M columns, the task is to calculate the minimum number of operations required to set the value of the coordinate (x, y) as 1 wherein each operation, select any index such that its value is 1 and set all its row elements or columns elements to 1.

Example:

Input: arr[][] = {{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 1, 1}}, X = 1, Y = 3
Output: 1
Explanation: In the 1st operation, select the coordinate (2, 3) as arr[2][3] = 1, and set all the value in that column as 1. Hence, making the value of arr[1][3] = 1, in 1 operation which is the minimum possible.

 Input: arr[][] = {{0, 0}, {0, 0}}, X = 1, Y = 1
Output: -1
Explanation: It is not possible to set the value of the coordinate arr[1][1] as 1 using any number of operations.

 

Approach: The given problem is an implementation based problem and it can be divided into the following cases:

  • Case 1 where arr[x][y] is already 1. In such cases, 0 operations will be required.
  • Case 2 where any of either the xth row or the yth column contains an index with value 1. In such cases, 1 operation will be required.
  • Case 3 where at least one block exists such that arr[i][j] = 1. In such cases, 2 moves will be required.
  • Case 4 where no block exists with value 1. In such cases, it is impossible to perform the given task.

Therefore, check for each of the mentioned cases in the respective order using which will provide the required answer.

Below is the implementation of the above approach:

C++




// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum operations
// required to set the value of the given
// coordinates as 1 in matrix arr[][]
int minOperations(
    vector<vector<int> > arr,
    int x, int y)
{
    // Case 1 where arr[x][y] = 1
    if (arr[x][y]) {
        // Return Answer
        return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr[0].size(); i++) {
 
        // Case 2 for Xth row
        if (arr[x][i])
            return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.size(); j++) {
 
        // Case 2 for Yth column
        if (arr[j][y])
            return 1;
    }
 
    // Loop to traverse arr[][]
    for (int i = 0; i < arr.size(); i++) {
        for (int j = 0; j < arr[0].size(); j++) {
 
            // Case 3 where any
            // arr[i][j] = 1
            if (arr[i][j])
                return 1;
        }
    }
 
    // Case 4
    return -1;
}
 
// Driver Code
int main()
{
    vector<vector<int> > arr{ { 0, 1, 0, 0 },
                              { 1, 1, 1, 0 },
                              { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    cout << minOperations(arr, x, y);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find minimum operations
  // required to set the value of the given
  // coordinates as 1 in matrix arr[][]
  static int minOperations(
    int[][] arr,
    int x, int y)
  {
    // Case 1 where arr[x][y] = 1
    if (arr[x][y] > 0) {
      // Return Answer
      return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr[0].length; i++) {
 
      // Case 2 for Xth row
      if (arr[x][i] > 0)
        return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.length; j++) {
 
      // Case 2 for Yth column
      if (arr[j][y] > 0)
        return 1;
    }
 
    // Loop to traverse arr[][]
    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[0].length; j++) {
 
        // Case 3 where any
        // arr[i][j] = 1
        if (arr[i][j] > 0)
          return 1;
      }
    }
 
    // Case 4
    return -1;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int[][] arr = { { 0, 1, 0, 0 },
                   { 1, 1, 1, 0 },
                   { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    System.out.println(minOperations(arr, x, y));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python program of the above approach
 
# Function to find minimum operations
# required to set the value of the given
# coordinates as 1 in matrix arr[][]
def minOperations(arr, x,  y):
 
    # Case 1 where arr[x][y] = 1
    if (arr[x][y]):
        # Return Answer
        return 0
 
    # Loop to iterate a row
    for i in range(0, len(arr[0])):
 
        # Case 2 for Xth row
        if (arr[x][i]):
            return 1
 
    # Loop to iterate a column
    for j in range(0, len(arr)):
 
        # Case 2 for Yth column
        if (arr[j][y]):
            return 1
 
    # Loop to traverse arr[][]
    for i in range(0, len(arr)):
        for j in range(0, len(arr[0])):
 
            # Case 3 where any
            # arr[i][j] = 1
            if (arr[i][j]):
                return 1
 
    # Case 4
    return -1
 
# Driver Code
 
arr = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]]
x = 1
y = 3
 
print(minOperations(arr, x, y))
 
# This code is contributed by Taranpreet


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
 
  // Function to find minimum operations
  // required to set the value of the given
  // coordinates as 1 in matrix [,]arr
  static int minOperations(
    int[,] arr,
    int x, int y)
  {
 
    // Case 1 where arr[x,y] = 1
    if (arr[x, y] > 0)
    {
 
      // Return Answer
      return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr.GetLength(0); i++) {
 
      // Case 2 for Xth row
      if (arr[x,i] > 0)
        return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.Length; j++) {
 
      // Case 2 for Yth column
      if (arr[j,y] > 0)
        return 1;
    }
 
    // Loop to traverse [,]arr
    for (int i = 0; i < arr.GetLength(0); i++) {
      for (int j = 0; j < arr.GetLength(1); j++) {
 
        // Case 3 where any
        // arr[i,j] = 1
        if (arr[i,j] > 0)
          return 1;
      }
    }
 
    // Case 4
    return -1;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    int[,] arr = { { 0, 1, 0, 0 },
                  { 1, 1, 1, 0 },
                  { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    Console.WriteLine(minOperations(arr, x, y));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
     // JavaScript code for the above approach
 
     // Function to find minimum operations
     // required to set the value of the given
     // coordinates as 1 in matrix arr[][]
     function minOperations(arr, x, y)
     {
      
         // Case 1 where arr[x][y] = 1
         if (arr[x][y])
         {
          
             // Return Answer
             return 0;
         }
 
         // Loop to iterate a row
         for (let i = 0; i < arr[0].length; i++) {
 
             // Case 2 for Xth row
             if (arr[x][i])
                 return 1;
         }
 
         // Loop to iterate a column
         for (let j = 0; j < arr.length; j++) {
 
             // Case 2 for Yth column
             if (arr[j][y])
                 return 1;
         }
 
         // Loop to traverse arr[][]
         for (let i = 0; i < arr.length; i++) {
             for (let j = 0; j < arr[0].length; j++) {
 
                 // Case 3 where any
                 // arr[i][j] = 1
                 if (arr[i][j])
                     return 1;
             }
         }
 
         // Case 4
         return -1;
     }
 
     // Driver Code
     let arr = [[0, 1, 0, 0],
     [1, 1, 1, 0],
     [0, 0, 1, 1]];
     let x = 1;
     let y = 3;
 
     document.write(minOperations(arr, x, y));
 
    // This code is contributed by Potta Lokesh
 </script>


 
 

Output

1

 

Time Complexity: O(N*M)
Auxiliary Space: O(1)

 



Last Updated : 22 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads