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++
#include <bits/stdc++.h>
using namespace std;
int minOperations(
vector<vector< int > > arr,
int x, int y)
{
if (arr[x][y]) {
return 0;
}
for ( int i = 0; i < arr[0].size(); i++) {
if (arr[x][i])
return 1;
}
for ( int j = 0; j < arr.size(); j++) {
if (arr[j][y])
return 1;
}
for ( int i = 0; i < arr.size(); i++) {
for ( int j = 0; j < arr[0].size(); j++) {
if (arr[i][j])
return 1;
}
}
return -1;
}
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
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int minOperations(
int [][] arr,
int x, int y)
{
if (arr[x][y] > 0 ) {
return 0 ;
}
for ( int i = 0 ; i < arr[ 0 ].length; i++) {
if (arr[x][i] > 0 )
return 1 ;
}
for ( int j = 0 ; j < arr.length; j++) {
if (arr[j][y] > 0 )
return 1 ;
}
for ( int i = 0 ; i < arr.length; i++) {
for ( int j = 0 ; j < arr[ 0 ].length; j++) {
if (arr[i][j] > 0 )
return 1 ;
}
}
return - 1 ;
}
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));
}
}
|
Python3
def minOperations(arr, x, y):
if (arr[x][y]):
return 0
for i in range ( 0 , len (arr[ 0 ])):
if (arr[x][i]):
return 1
for j in range ( 0 , len (arr)):
if (arr[j][y]):
return 1
for i in range ( 0 , len (arr)):
for j in range ( 0 , len (arr[ 0 ])):
if (arr[i][j]):
return 1
return - 1
arr = [[ 0 , 1 , 0 , 0 ], [ 1 , 1 , 1 , 0 ], [ 0 , 0 , 1 , 1 ]]
x = 1
y = 3
print (minOperations(arr, x, y))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int minOperations(
int [,] arr,
int x, int y)
{
if (arr[x, y] > 0)
{
return 0;
}
for ( int i = 0; i < arr.GetLength(0); i++) {
if (arr[x,i] > 0)
return 1;
}
for ( int j = 0; j < arr.Length; j++) {
if (arr[j,y] > 0)
return 1;
}
for ( int i = 0; i < arr.GetLength(0); i++) {
for ( int j = 0; j < arr.GetLength(1); j++) {
if (arr[i,j] > 0)
return 1;
}
}
return -1;
}
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));
}
}
|
Javascript
<script>
function minOperations(arr, x, y)
{
if (arr[x][y])
{
return 0;
}
for (let i = 0; i < arr[0].length; i++) {
if (arr[x][i])
return 1;
}
for (let j = 0; j < arr.length; j++) {
if (arr[j][y])
return 1;
}
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[0].length; j++) {
if (arr[i][j])
return 1;
}
}
return -1;
}
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));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)