Minimum bit changes in Binary Circular array to reach a index | CPP program to find direction with minimum flips ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumFlip ( string s , int x , int y ) { s = s + s ; bool isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } int valClockwise = 0 ; char cur = s [ x ] ; for ( int i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } int valAnticlockwise = 0 ; cur = s [ y ] ; x += s . length ( ) ; for ( int i = y ; i <= x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) cout << " Clockwise ▁ " << valClockwise << endl ; else cout << " Anti - clockwise ▁ " << valAnticlockwise << endl ; } else { if ( ! isOpposite ) cout << " Anti - clockwise ▁ " << valAnticlockwise << endl ; else cout << " Clockwise ▁ " << valClockwise << endl ; } } int main ( ) { int x = 0 , y = 8 ; string s = "000110" ; minimumFlip ( s , x , y ) ; return 0 ; }