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++
| // C++ program to calculate leftmost // column having at least a 1#include <bits/stdc++.h>usingnamespacestd;#define N 3#define M 4// Function return leftmost // column having at least a 1intFindColumn(intmat[N][M]){    introw = 0, col = M - 1;    intflag = 0;    while(row < N && col >= 0)    {        // If current element is        // 1 decrement column by 1        if(mat[row][col] == 1)        {            col--;            flag = 1;        }        // If current element is        // 0 increment row by 1        else        {            row++;        }    }    col++;        if(flag)        returncol + 1;    else        return-1;}// Driver codeintmain(){    intmat[N][M] = { { 0, 0, 0, 1 },                      { 0, 1, 1, 1 },                      { 0, 0, 1, 1 } };    cout << FindColumn(mat);    return0;} | 
Java
| // Java program to calculate leftmost // column having at least a 1importjava.util.*;classGFG{staticfinalintN = 3;staticfinalintM = 4;// Function return leftmost // column having at least a 1staticintFindColumn(intmat[][]){    introw = 0, col = M - 1;    intflag = 0;    while(row < N && col >= 0)    {        // If current element is        // 1 decrement column by 1        if(mat[row][col] == 1)        {           col--;           flag = 1;        }        // If current element is        // 0 increment row by 1        else        {            row++;        }    }    col++;    if(flag!=0)        returncol + 1;    else        return-1;}// Driver codepublicstaticvoidmain(String[] args){    int[][] mat = { { 0, 0, 0, 1},                    { 0, 1, 1, 1},                    { 0, 0, 1, 1} };                        System.out.print(FindColumn(mat));}}// This code is contributed by 29AjayKumar | 
Python3
| # Python3 program to calculate leftmost# column having at least a 1N =3M =4# Function return leftmost# column having at least a 1deffindColumn(mat: list) -> int:    row =0    col =M -1    whilerow < N andcol >=0:        # If current element is        # 1 decrement column by 1        ifmat[row][col] ==1:            col -=1            flag =1        # If current element is        # 0 increment row by 1        else:            row +=1                col +=1    ifflag:        returncol +1    else:        return-1# Driver Codeif__name__ =="__main__":        mat =[ [0, 0, 0, 1],            [0, 1, 1, 1],            [0, 0, 1, 1] ]                  print(findColumn(mat))# This code is contributed by sanjeev2552 | 
C#
| // C# program to calculate leftmost // column having at least 1usingSystem;classGFG{staticreadonlyintN = 3;staticreadonlyintM = 4;// Function return leftmost // column having at least a 1staticintFindColumn(int[,]mat){    introw = 0, col = M - 1;    intflag = 0;    while(row < N && col >= 0)    {                // If current element is        // 1 decrement column by 1        if(mat[row, col] == 1)        {            col--;            flag = 1;        }                // If current element is        // 0 increment row by 1        else        {            row++;        }    }    col++;    if(flag != 0)        returncol + 1;    else        return-1;}// Driver codepublicstaticvoidMain(String[] args){    int[,] mat = { { 0, 0, 0, 1 },                   { 0, 1, 1, 1 },                   { 0, 0, 1, 1 } };                        Console.Write(FindColumn(mat));}}// This code is contributed by Rohit_ranjan | 
Javascript
| <script>    // JavaScript program to calculate leftmost     // column having at least 1        let N = 3;    let M = 4;    // Function return leftmost     // column having at least a 1    functionFindColumn(mat)    {        let row = 0, col = M - 1;        let flag = 0;        while(row < N && col >= 0)        {            // If current element is            // 1 decrement column by 1            if(mat[row][col] == 1)            {                col--;                flag = 1;            }            // If current element is            // 0 increment row by 1            else            {                row++;            }        }        col++;        if(flag != 0)            returncol + 1;        else            return-1;    }        let mat = [ [ 0, 0, 0, 1 ],                 [ 0, 1, 1, 1 ],                 [ 0, 0, 1, 1 ] ];                          document.write(FindColumn(mat));</script> | 
2
Time Complexity: O(N + M). where N is number of row and M is number of column. 
Space Complexity: O(1)
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    






