Possible arrangement of persons waiting to sit in a hall | Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Given N ; Given arr [ ] ; Given string ; Function Call
function findTheOrder ( arr , s , N ) { let ans = [ ] ; let A = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let p = [ arr [ i ] , i + 1 ] ; A . push ( p ) ; } A . sort ( function ( p1 , p2 ) { return p1 [ 0 ] - p2 [ 0 ] ; } ) ; let q = [ ] ; let index = 0 ; for ( let i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == ' ' ) { ans . push ( A [ index ] [ 1 ] ) ; q . push ( A [ index ] ) ; index ++ ; } else { ans . push ( q [ 0 ] [ 1 ] ) ; q . shift ( ) ; } q . sort ( function ( a , b ) { return b [ 0 ] - a [ 0 ] ; } ) ; } for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let N = 3 ; let arr = [ 2 , 1 , 3 ] ; let s = " " ; findTheOrder ( arr , s , N ) ;