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


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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.