Open In App

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: 
 

Below is the implementation of the above approach. 
 




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




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


Article Tags :