Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
Last Updated :
30 Aug, 2022
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[2][2] = { {0, 0},
{1, 1} }
Output: 1
Explanation:
The 1st column of the
matrix contains atleast a 1.
Input:
mat[2][2] = {{0, 0},
{0, 0}}
Output: -1
Explanation:
There is no such column which
contains atleast one 1.
Approach: The idea is to iterate over all the rows of the matrix and Binary Search over them for the first occurrence of the 1 in that row. The minimum of all the occurrence of the first 1 in the rows of the matrix will be the desired solution for the problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
int search( int mat[][], int n, int m)
{
int i, a = INT_MAX;
for (i = 0; i < n; i++) {
int low = 0;
int high = m - 1;
int mid;
int ans = INT_MAX;
while (low <= high) {
mid = (low + high) / 2;
if (mat[i][mid] == 1) {
if (mid == 0) {
ans = 0;
break ;
}
else if (mat[i][mid - 1] == 0) {
ans = mid;
break ;
}
}
if (mat[i][mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
if (ans < a)
a = ans;
}
if (a == INT_MAX)
return -1;
return a+1;
}
int main()
{
int mat[3][3] = { 0, 0, 0,
0, 0, 1,
0, 1, 1 };
cout << search(mat, 3, 3);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 3 ;
static int search( int mat[][], int n, int m)
{
int i, a = Integer.MAX_VALUE;
for (i = 0 ; i < n; i++)
{
int low = 0 ;
int high = m - 1 ;
int mid;
int ans = Integer.MAX_VALUE;
while (low <= high)
{
mid = (low + high) / 2 ;
if (mat[i][mid] == 1 )
{
if (mid == 0 )
{
ans = 0 ;
break ;
}
else if (mat[i][mid - 1 ] == 0 )
{
ans = mid;
break ;
}
}
if (mat[i][mid] == 1 )
high = mid - 1 ;
else
low = mid + 1 ;
}
if (ans < a)
a = ans;
}
if (a == Integer.MAX_VALUE)
return - 1 ;
return a + 1 ;
}
public static void main(String[] args)
{
int mat[][] = { { 0 , 0 , 0 },
{ 0 , 0 , 1 },
{ 0 , 1 , 1 } };
System.out.print(search(mat, 3 , 3 ));
}
}
|
Python3
import sys
N = 3
def search(mat, n, m):
a = sys.maxsize
for i in range (n):
low = 0
high = m - 1
ans = sys.maxsize
while (low < = high):
mid = (low + high) / / 2
if (mat[i][mid] = = 1 ):
if (mid = = 0 ):
ans = 0
break
elif (mat[i][mid - 1 ] = = 0 ):
ans = mid
break
if (mat[i][mid] = = 1 ):
high = mid - 1
else :
low = mid + 1
if (ans < a):
a = ans
if (a = = sys.maxsize):
return - 1
return a + 1
if __name__ = = "__main__" :
mat = [[ 0 , 0 , 0 ],
[ 0 , 0 , 1 ],
[ 0 , 1 , 1 ]]
print (search(mat, 3 , 3 ))
|
C#
using System;
class GFG{
static int search( int [,]mat, int n, int m)
{
int i, a = int .MaxValue;
for (i = 0; i < n; i++)
{
int low = 0;
int high = m - 1;
int mid;
int ans = int .MaxValue;
while (low <= high)
{
mid = (low + high) / 2;
if (mat[i, mid] == 1)
{
if (mid == 0)
{
ans = 0;
break ;
}
else if (mat[i, mid - 1] == 0)
{
ans = mid;
break ;
}
}
if (mat[i, mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
if (ans < a)
a = ans;
}
if (a == int .MaxValue)
return -1;
return a + 1;
}
public static void Main(String[] args)
{
int [,]mat = { { 0, 0, 0 },
{ 0, 0, 1 },
{ 0, 1, 1 } };
Console.Write(search(mat, 3, 3));
}
}
|
Javascript
<script>
var N = 3;
function search(mat, n, m)
{
var i, a = 1000000000;
for (i = 0; i < n; i++) {
var low = 0;
var high = m - 1;
var mid;
var ans = 1000000000;
while (low <= high) {
mid = parseInt((low + high) / 2);
if (mat[i][mid] == 1) {
if (mid == 0) {
ans = 0;
break ;
}
else if (mat[i][mid - 1] == 0) {
ans = mid;
break ;
}
}
if (mat[i][mid] == 1)
high = mid - 1;
else
low = mid + 1;
}
if (ans < a)
a = ans;
}
if (a == 1000000000)
return -1;
return a+1;
}
var mat = [[0, 0, 0],
[0, 0, 1],
[0, 1, 1]];
document.write( search(mat, 3, 3));
</script>
|
Performance Analysis:
- Time Complexity: O(N*logN)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...