Maximum non | Function to prlet the maximum rooks and their positions ; Initialize row and col array ; Marking the location of already placed rooks ; Prlet number of non - attacking rooks that can be placed ; To store the placed rook location ; Prlet lexographically smallest order ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call
function countRooks ( n , k , pos ) { let row = new Array ( n ) . fill ( 0 ) ; let col = new Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { row [ i ] = 0 ; col [ i ] = 0 ; } for ( let i = 0 ; i < k ; i ++ ) { row [ pos [ i ] [ 0 ] - 1 ] = 1 ; col [ pos [ i ] [ 1 ] - 1 ] = 1 ; } let res = n - k ; document . write ( res + " " + " " ) ; let ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( row [ ri ] == 1 ) { ri ++ ; } while ( col [ ci ] == 1 ) { ci ++ ; } document . write ( ( ri + 1 ) + " " + ( ci + 1 ) + " " + " " ) ; ri ++ ; ci ++ ; } } let N = 4 ; let K = 2 ; let pos = [ [ 1 , 4 ] , [ 2 , 2 ] ] ; countRooks ( N , K , pos ) ;