If given sequence is in descending order then we need to swap last two elements again
if ( res [ N - 1 ] == seq [ N - 1 ] ) : NEW_LINE INDENT res [ N - 1 ] = res [ N - 2 ] NEW_LINE res [ N - 2 ] = seq [ N - 1 ] NEW_LINE DEDENT print ( " Largest ▁ Derangement " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT