Finding whether Clockwise or Anti - clockwise path take minimum flip .
if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) { System . out . println ( " Clockwise ▁ " + valClockwise ) ; } else { System . out . println ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } } else if ( ! isOpposite ) { System . out . println ( " Anti - clockwise ▁ " + valAnticlockwise ) ; } else { System . out . println ( " Clockwise ▁ " + valClockwise ) ; } } static void swap ( int a , int b ) { int c = a ; a = b ; b = c ; }