Function to return true if the elements can be arranged in the desired order
function canReorder ( A , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( A [ i ] ) ) { m . set ( A [ i ] , m . get ( A [ i ] ) + 1 ) } else { m . set ( A [ i ] , 1 ) } } A . sort ( ( a , b ) => a - b ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . get ( A [ i ] ) == 0 ) continue ;