Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
Last Updated :
15 Jun, 2021
Given a binary matrix mat[][] containing 0’s and 1’s. Each row of the matrix is sorted in the non-decreasing order, the task is to find the left-most column of the matrix with at least one 1 in it.
Note: If no such column exists return -1.
Examples:
Input:
mat[][] = {{0, 0, 0, 1}
{0, 1, 1, 1}
{0, 0, 1, 1}}
Output: 2
Explanation:
The 2nd column of the
matrix contains atleast a 1.
Input:
mat[][] = {{0, 0, 0}
{0, 1, 1}
{1, 1, 1}}
Output: 1
Explanation:
The 1st column of the
matrix contains atleast a 1.
Input:
mat[][] = {{0, 0}
{0, 0}}
Output: -1
Explanation:
There is no such column which
contains atleast one 1.
Approach:
- Here we start our traversal from the last element of the first row. This includes two steps.
- If the current iterating element is 1, we decrement the column index. As we find the leftmost column index with value 1, so we don’t have to check elements with greater column index.
- If the current iterating element is 0, we increment row index. As that element is 0, we don’t need to check previous elements of that row.
- We continue until one of the row or column index become invalid.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 3
#define M 4
int FindColumn( int mat[N][M])
{
int row = 0, col = M - 1;
int flag = 0;
while (row < N && col >= 0)
{
if (mat[row][col] == 1)
{
col--;
flag = 1;
}
else
{
row++;
}
}
col++;
if (flag)
return col + 1;
else
return -1;
}
int main()
{
int mat[N][M] = { { 0, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 0, 0, 1, 1 } };
cout << FindColumn(mat);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 3 ;
static final int M = 4 ;
static int FindColumn( int mat[][])
{
int row = 0 , col = M - 1 ;
int flag = 0 ;
while (row < N && col >= 0 )
{
if (mat[row][col] == 1 )
{
col--;
flag = 1 ;
}
else
{
row++;
}
}
col++;
if (flag!= 0 )
return col + 1 ;
else
return - 1 ;
}
public static void main(String[] args)
{
int [][] mat = { { 0 , 0 , 0 , 1 },
{ 0 , 1 , 1 , 1 },
{ 0 , 0 , 1 , 1 } };
System.out.print(FindColumn(mat));
}
}
|
Python3
N = 3
M = 4
def findColumn(mat: list ) - > int :
row = 0
col = M - 1
while row < N and col > = 0 :
if mat[row][col] = = 1 :
col - = 1
flag = 1
else :
row + = 1
col + = 1
if flag:
return col + 1
else :
return - 1
if __name__ = = "__main__" :
mat = [ [ 0 , 0 , 0 , 1 ],
[ 0 , 1 , 1 , 1 ],
[ 0 , 0 , 1 , 1 ] ]
print (findColumn(mat))
|
C#
using System;
class GFG{
static readonly int N = 3;
static readonly int M = 4;
static int FindColumn( int [,]mat)
{
int row = 0, col = M - 1;
int flag = 0;
while (row < N && col >= 0)
{
if (mat[row, col] == 1)
{
col--;
flag = 1;
}
else
{
row++;
}
}
col++;
if (flag != 0)
return col + 1;
else
return -1;
}
public static void Main(String[] args)
{
int [,] mat = { { 0, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 0, 0, 1, 1 } };
Console.Write(FindColumn(mat));
}
}
|
Javascript
<script>
let N = 3;
let M = 4;
function FindColumn(mat)
{
let row = 0, col = M - 1;
let flag = 0;
while (row < N && col >= 0)
{
if (mat[row][col] == 1)
{
col--;
flag = 1;
}
else
{
row++;
}
}
col++;
if (flag != 0)
return col + 1;
else
return -1;
}
let mat = [ [ 0, 0, 0, 1 ],
[ 0, 1, 1, 1 ],
[ 0, 0, 1, 1 ] ];
document.write(FindColumn(mat));
</script>
|
Time Complexity: O(N + M). where N is number of row and M is number of column.
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...