Minimum bit changes in Binary Circular array to reach a index | finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driven Program
function minimumFlip ( s , x , y ) { s = s + s ; var isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } var valClockwise = 0 ; var cur = s [ x ] ; for ( var i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } var valAnticlockwise = 0 ; cur = s [ y ] ; x += s . length ; for ( var i = y ; i <= x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) document . write ( " " + valClockwise + " " ) ; else document . write ( " " + valAnticlockwise + " " ) ; } else { if ( ! isOpposite ) document . write ( " " + valAnticlockwise + " " ) ; else document . write ( " " + valClockwise + " " ) ; } } var x = 0 , y = 8 ; var s = " " ; minimumFlip ( s , x , y ) ;