Increasing permutation of first N natural numbers | Function that returns true if it is possible to make the permutation increasing by swapping any two numbers ; To count misplaced elements ; Count all misplaced elements ; If possible ; Driver code
function isPossible ( a , n ) { let k = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != i + 1 ) k ++ ; } if ( k <= 2 ) return true ; return false ; } let a = [ 5 , 2 , 3 , 4 , 1 ] ; let n = a . length ; if ( isPossible ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;