Increasing permutation of first N natural numbers | Java implementation of the approach ; 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
class GFG { static boolean isPossible ( int a [ ] , int n ) { int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != i + 1 ) k ++ ; } if ( k <= 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 2 , 3 , 4 , 1 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }