Check if it is possible to make all elements into 1 except obstacles
Last Updated : 30 Mar, 2023
Given a matrix M[][] of size N * M containing characters 0 and X. You have to choose a sub-matrix of 2*2 size, which doesn't contains X in it, and convert all the 0 under that sub-matrix into 1. Update M[][] after each operation. Return "YES", otherwise "NO" If it is possible to make all the zeros into ones except the cell containing X by using the given operation.
Examples:
Input: N = 3, M = 3
M[][]:
0 0 X 0 0 0 0 0 0 Output: YES Explanation:
Graphical Explanation of input test case 1
It can be seen that from the initial matrix, at each operation a sub-matrix of size 2*2 is chose and convert all the zeros into ones under those sub-matrices using the given operation. It also noted that some sub-matrices contain 1 also, in this case remain the same. Any sub-matrix of size 2*2 doesn't contains 'X'. Therefore, it is possible to convert all the zeros into ones using given operation except X. Hence output is YES.
Input: N = 4, M = 2
M[][]:
0 0 0 X 0 0 0 0 Output: NO Explanation:
Graphical explanation of input test case 2
It can be verified that all zeros in initial matrix can't be converted into ones.
Approach: Implement the idea below to solve the problem:
The problem is observation based and can be solved by using those observations.
Steps were taken to solve the problem:
Create a StringBuilder Array let's say X[] of size N.
Initialize X[] with input matrix M[][].
Run two nested loops for i = 0 to i < N - 1 and j = 0 to j < M - 1 and follow the below-mentioned steps under the scope of the loop:
If ( X[ i ].charAt( j ) == '0' || X[ i ].charAt( j ) == '1' )
Now, just traverse the X[] and check if there exists 0 or not. If 0 exists then output NO or else YES.
Below is the code to implement the approach:
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// Function for checking is it possible// to make all zeros into onesvoidIs_Possible(intN,intM,vector<string>&x){// Implementing the approach on original matrixfor(inti=0;i<N-1;i++){for(intj=0;j<M-1;j++){if(x[i][j]=='0'||x[i][j]=='1'){if(x[i][j+1]!='X'&&x[i+1][j+1]!='X'&&x[i+1][j]!='X'){x[i][j]='1';x[i][j+1]='1';x[i+1][j]='1';x[i+1][j+1]='1';}}}}// Flag initialized and set to falseboolflag=false;// Traversing vector of string x[]// to check if there is an element// exists any 0 Which is still not// converted into 1for(inti=0;i<N;i++){for(intj=0;j<M;j++){if(x[i][j]=='0'){cout<<"NO"<<endl;flag=true;break;}}if(flag)break;}if(!flag)cout<<"YES"<<endl;}intmain(){// InputsintN=4;intM=2;vector<string>arr={"00","00","00","0X"};// Function callIs_Possible(N,M,arr);}
Java
// Java code to implement the approahcimportjava.util.*;classGFG{// Driver Functionpublicstaticvoidmain(Stringaz[]){// InputsintN=4;intM=2;String[]arr={"00","00","00","0X"};// Function callIs_Possible(N,M,arr);}// Function for checking is it possible// to make all zeros into onesstaticvoidIs_Possible(intN,intM,String[]arr){// StringBuilder array object is// initialized to hold matrixStringBuilderx[]=newStringBuilder[N];// Loop for initializing// StringBuilder arrayfor(inti=0;i<N;i++){x[i]=newStringBuilder(arr[i]);}// Implementing the approachfor(inti=0;i<N-1;i++){for(intj=0;j<M-1;j++){if(x[i].charAt(j)=='0'||x[i].charAt(j)=='1'){if(x[i].charAt(j+1)!=88&&x[i+1].charAt(j+1)!=88&&x[i+1].charAt(j)!=88){x[i].setCharAt(j,'1');x[i].setCharAt(j+1,'1');x[i+1].setCharAt(j,'1');x[i+1].setCharAt(j+1,'1');}}}}// Below lines will print// StringBuilder object(Should be// use only for debugging)// for(StringBuilder X : x)// System.out.println(X);// Flag initialized and set to falsebooleanflag=false;// Traversing StringBuilder array// to check if there is an element// exists any 0 Which is still not// converted into 1for(inti=0;i<N;i++){for(intj=0;j<M;j++){if(x[i].charAt(j)=='0'){System.out.println("NO");flag=true;break;}}if(flag)break;}if(!flag)System.out.println("YES");}}
C#
// C# code to implement the approahcusingSystem;usingSystem.Text;publicclassGFG{// Function for checking is it possible// to make all zeros into onesstaticvoidIs_Possible(intN,intM,string[]arr){// StringBuilder array object is// initialized to hold matrixStringBuilder[]x=newStringBuilder[N];// Loop for initializing// StringBuilder arrayfor(inti=0;i<N;i++){x[i]=newStringBuilder(arr[i]);}for(inti=0;i<N-1;i++){for(intj=0;j<M-1;j++){if(x[i][j]=='0'||x[i][j]=='1'){if(x[i][j+1]!='X'&&x[i+1][j+1]!='X'&&x[i+1][j]!='X'){x[i][j]='1';x[i][j+1]='1';x[i+1][j]='1';x[i+1][j+1]='1';}}}}// Flag initialized and set to falseboolflag=false;// Traversing StringBuilder array// to check if there is an element// exists any 0 Which is still not// converted into 1for(inti=0;i<N;i++){for(intj=0;j<M;j++){if(x[i][j]=='0'){Console.WriteLine("NO");flag=true;break;}}if(flag)break;}if(!flag)Console.WriteLine("YES");}staticpublicvoidMain(){// InputsintN=4;intM=2;string[]arr={"00","00","00","0X"};// Function callIs_Possible(N,M,arr);}}
Python
# Function for checking if it is possible to make all zeros into onesdefis_possible(n,m,arr):# Initialize a list of lists to hold the matrixx=[list(row)forrowinarr]# Implementing the approachforiinrange(n-1):forjinrange(m-1):ifx[i][j]=='0'orx[i][j]=='1':ifx[i][j+1]!='X'andx[i+1][j+1]!='X'andx[i+1][j]!='X':x[i][j]='1'x[i][j+1]='1'x[i+1][j]='1'x[i+1][j+1]='1'# Traversing list of lists to check if there is any 0 which is still not converted into 1foriinrange(n):forjinrange(m):ifx[i][j]=='0':return"NO"return"YES"# Driver codeif__name__=="__main__":# Inputsn=4m=2arr=["00","00","00","0X"]# Function callprint(is_possible(n,m,arr))
JavaScript
// JavaScript code to implement the approach// Function for checking is it possible// to make all zeros into onesfunctionIs_Possible(N,M,arr){// Implementing the approach on original matrixfor(leti=0;i<N-1;i++){for(letj=0;j<M-1;j++){if(arr[i][j]=='0'||arr[i][j]=='1'){if(arr[i][j+1]!='X'&&arr[i+1][j+1]!='X'&&arr[i+1][j]!='X'){arr[i]=arr[i].substr(0,j)+'11'+arr[i].substr(j+2,M);arr[i+1]=arr[i+1].substr(0,j)+'11'+arr[i+1].substr(j+2,M);}}}}// Flag initialized and set to falseletflag=false;// Traversing array of strings arr[]// to check if there is an element// exists any 0 Which is still not// converted into 1for(leti=0;i<N;i++){for(letj=0;j<M;j++){if(arr[i][j]=='0'){console.log("NO");flag=true;break;}}if(flag)break;}if(!flag)console.log("YES");}// InputsletN=4;letM=2;letarr=["00","00","00","0X"];// Function callIs_Possible(N,M,arr);
Output
NO
Time Complexity: O(N*M) Auxiliary Space: O(N*M), As StringBuilder is used of size N*M.