Skip to content
Related Articles

Related Articles

Improve Article

Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2

  • Difficulty Level : Expert
  • Last Updated : 15 Jun, 2021
Geek Week

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. 
    1. 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. 
    2. 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>
using namespace std;
 
#define N 3
#define M 4
 
// Function return leftmost
// column having at least a 1
int FindColumn(int mat[N][M])
{
    int row = 0, col = M - 1;
    int 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)
        return col + 1;
    else
        return -1;
}
 
// Driver code
int main()
{
    int mat[N][M] = { { 0, 0, 0, 1 },
                      { 0, 1, 1, 1 },
                      { 0, 0, 1, 1 } };
 
    cout << FindColumn(mat);
 
    return 0;
}

Java




// Java program to calculate leftmost
// column having at least a 1
import java.util.*;
 
class GFG{
 
static final int N = 3;
static final int M = 4;
 
// Function return leftmost
// column having at least a 1
static int FindColumn(int mat[][])
{
    int row = 0, col = M - 1;
    int 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)
        return col + 1;
    else
        return -1;
}
 
// Driver code
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));
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to calculate leftmost
# column having at least a 1
N = 3
M = 4
 
# Function return leftmost
# column having at least a 1
def findColumn(mat: list) -> int:
    row = 0
    col = M - 1
 
    while row < N and col >= 0:
 
        # If current element is
        # 1 decrement column by 1
        if mat[row][col] == 1:
            col -= 1
            flag = 1
 
        # If current element is
        # 0 increment row by 1
        else:
            row += 1
             
    col += 1
 
    if flag:
        return col + 1
    else:
        return -1
 
# Driver Code
if __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 1
using System;
 
class GFG{
 
static readonly int N = 3;
static readonly int M = 4;
 
// Function return leftmost
// column having at least a 1
static int FindColumn(int [,]mat)
{
    int row = 0, col = M - 1;
    int 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)
        return col + 1;
    else
        return -1;
}
 
// Driver code
public static void Main(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
    function FindColumn(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)
            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>
Output: 



2

 

Time Complexity: O(N + M). where N is number of row and M is number of column. 
Space Complexity: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :