Leftmost Column with atleast one 1 in a row | JavaScript program to calculate leftmost column having at least 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver code
let N = 3 ; let M = 4 ; function FindColumn ( mat ) { let row = 0 , col = M - 1 ; let flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row ] [ col ] == 1 ) { col -- ; flag = 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 ) ) ;