# Minimum bit changes in Binary Circular array to reach a index

Given a Binary Circular Array of size N elements and two positive integers x and y indicating the indices in the circular array. The task is check which path, clockwise or anti-clockwise, from index x to index y, we face the minimum number bit flips. Output “Clockwise” or “Anti-clockwise” and the value of minimum bit flip, in case of equal count output “Clockwise”.

Examples:

```Input : arr[] = { 0, 0, 0, 1, 1, 0 }
x = 0, y = 5
Output : Anti-clockwise 0
The path 0 -> 1 -> 2 -> 3 -> 4 -> 5, we have only 1 value change i.e from index 2 to 3.
The path 0 -> 5 have 0 value change.
So, the answer is Anti-clockwise 0.

Input : s = { 1, 1, 0, 1, 1 }
x = 2, y = 0
Output : Clockwise 1
```

The idea is to check by going once Clockwise and store the count1 and then going anti-clockwise and store the count2. Then output by comparing count1 and count2.

How to travel clockwise or anticlockwise?
It will be hard to travel clockwise in the array where x > y and same in case of anticlockwise where y > x. So, we will store the given binary array in the string “S”. And to make it circular, we will append S to S i.e S = S + S. We will make the adjustment in x and y to travel clockwise or anticlockwise.
Now, if y > x and to go clockwise, it will be easy to iterate from x to y and calculate the number of flip bits.
If y > x and to go anti-clockwise, we will add |S| to x then iterate from y to x and calculate the number of flip bits.

Now, if x > y, we will swap x and y and calculate the answer using above approach. Then output the opposite of the result .

To calculate the number of flip bits, just store the current bit of index and check if next index have the same bit as current. If yes then do nothing else change the current bit to the bit of the next index and increment minimum bit by 1.

Below is the implementation of this approach:

## C++

 `// CPP program to find direction with minimum flips ` `#include ` `using` `namespace` `std; ` ` `  `// finding which path have minimum flip bit and ` `// the minimum flip bits ` `void` `minimumFlip(string s, ``int` `x, ``int` `y) ` `{ ` `    ``// concatenating given strin to itself, ` `    ``// to make it circular ` `    ``s = s + s; ` ` `  `    ``// check x is greater than y. ` `    ``// marking if output need to ` `    ``// be opposite. ` `    ``bool` `isOpposite = ``false``;     ` `    ``if` `(x > y) { ` `        ``swap(x, y); ` `        ``isOpposite = ``true``; ` `    ``} ` ` `  `    ``// iterate Clockwise ` `    ``int` `valClockwise = 0; ` `    ``char` `cur = s[x]; ` `    ``for` `(``int` `i = x; i <= y; i++) { ` `         `  `        ``// if current bit is not equal  ` `        ``// to next index bit. ` `        ``if` `(s[i] != cur) { ` `            ``cur = s[i]; ` `            ``valClockwise++; ` `        ``} ` `    ``} ` ` `  `    ``// iterate Anti-Clockwise ` `    ``int` `valAnticlockwise = 0; ` `    ``cur = s[y]; ` `    ``x += s.length(); ` `    ``for` `(``int` `i = y; i <= x; i++) { ` `         `  `        ``// if current bit is not equal ` `        ``// to next index bit. ` `        ``if` `(s[i] != cur) { ` `            ``cur = s[i]; ` `            ``valAnticlockwise++; ` `        ``} ` `    ``} ` ` `  `    ``// Finding whether Clockwise or Anti-clockwise  ` `    ``// path take minimum flip. ` `    ``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; ` `    ``} ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `x = 0, y = 8; ` `    ``string s = ``"000110"``; ` `    ``minimumFlip(s, x, y); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find direction ` `// with minimum flips  ` `class` `GFG ` `{ ` ` `  `    ``// finding which path have ` `    ``// minimum flip bit and  ` `    ``// the minimum flip bits  ` `    ``static` `void` `minimumFlip(String s, ` `                            ``int` `x, ``int` `y) ` `    ``{ ` `        ``// concatenating given strin to   ` `        ``// itself, to make it circular  ` `        ``s = s + s; ` ` `  `        ``// check x is greater than y.  ` `        ``// marking if output need to  ` `        ``// be opposite.  ` `        ``boolean` `isOpposite = ``false``; ` `        ``if` `(x > y)  ` `        ``{ ` `            ``swap(x, y); ` `            ``isOpposite = ``true``; ` `        ``} ` ` `  `        ``// iterate Clockwise  ` `        ``int` `valClockwise = ``0``; ` `        ``char` `cur = s.charAt(x); ` `        ``for` `(``int` `i = x; i <= y; i++)  ` `        ``{ ` ` `  `            ``// if current bit is not equal  ` `            ``// to next index bit.  ` `            ``if` `(s.charAt(i) != cur) ` `            ``{ ` `                ``cur = s.charAt(i); ` `                ``valClockwise++; ` `            ``} ` `        ``} ` ` `  `        ``// iterate Anti-Clockwise  ` `        ``int` `valAnticlockwise = ``0``; ` `        ``cur = s.charAt(y); ` `        ``x += s.length(); ` `        ``for` `(``int` `i = y; i < x; i++) ` `        ``{ ` ` `  `            ``// if current bit is not equal  ` `            ``// to next index bit.  ` `            ``if` `(s.charAt(i) != cur)  ` `            ``{ ` `                ``cur = s.charAt(i); ` `                ``valAnticlockwise++; ` `            ``} ` `        ``} ` ` `  `        ``// 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; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `x = ``0``, y = ``8``; ` `        ``String s = ``"000110"``; ` `        ``minimumFlip(s, x, y); ` `    ``} ` `}  ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python 3 program to find direction ` `# with minimum flips ` ` `  `# finding which path have minimum flip bit  ` `# and the minimum flip bits ` `def` `minimumFlip(s, x, y): ` `     `  `    ``# concatenating given string to itself, ` `    ``# to make it circular ` `    ``s ``=` `s ``+` `s ` `     `  `    ``# check x is greater than y. ` `    ``# marking if output need to ` `    ``# be opposite. ` `    ``isOpposite ``=` `False` `    ``if` `(x > y): ` `        ``temp ``=` `y ` `        ``y ``=` `x; ` `        ``x ``=` `temp ` `        ``isOpposite ``=` `True` ` `  `    ``# iterate Clockwise ` `    ``valClockwise ``=` `0` `    ``cur ``=` `s[x] ` `    ``for` `i ``in` `range``(x, y ``+` `1``, ``1``): ` `         `  `        ``# if current bit is not equal  ` `        ``# to next index bit. ` `        ``if` `(s[i] !``=` `cur): ` `            ``cur ``=` `s[i] ` `            ``valClockwise ``+``=` `1` ` `  `    ``# iterate Anti-Clockwise ` `    ``valAnticlockwise ``=` `0` `    ``cur ``=` `s[y] ` `    ``x ``+``=` `len``(s) ``-` `1` `    ``for` `i ``in` `range``(y, x ``+` `1``, ``1``): ` `         `  `        ``# if current bit is not equal ` `        ``# to next index bit. ` `        ``if` `(s[i] !``=` `cur): ` `            ``cur ``=` `s[i] ` `            ``valAnticlockwise ``+``=` `1` ` `  `    ``# Finding whether Clockwise or Anti-clockwise  ` `    ``# path take minimum flip. ` `    ``if` `(valClockwise <``=` `valAnticlockwise): ` `        ``if` `(isOpposite ``=``=` `False``): ` `            ``print``(``"Clockwise"``, valClockwise) ` `        ``else``: ` `            ``print``(``"Anti-clockwise"``,  ` `                   ``valAnticlockwise) ` ` `  `    ``else``: ` `        ``if` `(isOpposite ``=``=` `False``): ` `            ``print``(``"Anti-clockwise"``,  ` `                   ``valAnticlockwise) ` ` `  `        ``else``: ` `            ``print``(``"Clockwise"``, valClockwise) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``x ``=` `0` `    ``y ``=` `8` `    ``s ``=` `"000110"` `    ``minimumFlip(s, x, y) ` `     `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to find direction ` `// with minimum flips  ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// finding which path have ` `    ``// minimum flip bit and  ` `    ``// the minimum flip bits  ` `    ``static` `void` `minimumFlip(String s, ` `                            ``int` `x, ``int` `y) ` `    ``{ ` `        ``// concatenating given strin to  ` `        ``// itself, to make it circular  ` `        ``s = s + s; ` ` `  `        ``// check x is greater than y.  ` `        ``// marking if output need to  ` `        ``// be opposite.  ` `        ``bool` `isOpposite = ``false``; ` `        ``if` `(x > y)  ` `        ``{ ` `            ``swap(x, y); ` `            ``isOpposite = ``true``; ` `        ``} ` ` `  `        ``// iterate Clockwise  ` `        ``int` `valClockwise = 0; ` `        ``char` `cur = s[x]; ` `        ``for` `(``int` `i = x; i <= y; i++)  ` `        ``{ ` ` `  `            ``// if current bit is not equal  ` `            ``// to next index bit.  ` `            ``if` `(s[i] != cur) ` `            ``{ ` `                ``cur = s[i]; ` `                ``valClockwise++; ` `            ``} ` `        ``} ` ` `  `        ``// iterate Anti-Clockwise  ` `        ``int` `valAnticlockwise = 0; ` `        ``cur = s[y]; ` `        ``x += s.Length; ` `        ``for` `(``int` `i = y; i < x; i++) ` `        ``{ ` ` `  `            ``// if current bit is not equal  ` `            ``// to next index bit.  ` `            ``if` `(s[i] != cur)  ` `            ``{ ` `                ``cur = s[i]; ` `                ``valAnticlockwise++; ` `            ``} ` `        ``} ` ` `  `        ``// Finding whether Clockwise ` `        ``// or Anti-clockwise path  ` `        ``// take minimum flip.  ` `        ``if` `(valClockwise <= valAnticlockwise) ` `        ``{ ` `            ``if` `(!isOpposite)  ` `            ``{ ` `                ``Console.WriteLine(``"Clockwise "` `+ ` `                                    ``valClockwise); ` `            ``}  ` `            ``else` `            ``{ ` `                ``Console.WriteLine(``"Anti-clockwise "` `+ ` `                                    ``valAnticlockwise); ` `            ``} ` ` `  `        ``}  ` `        ``else` `if` `(!isOpposite)  ` `        ``{ ` `            ``Console.WriteLine(``"Anti-clockwise "` `+  ` `                                ``valAnticlockwise); ` `        ``}  ` `        ``else` `        ``{ ` `            ``Console.WriteLine(``"Clockwise "` `+ ` `                                ``valClockwise); ` `        ``} ` `    ``} ` ` `  `    ``static` `void` `swap(``int` `a, ``int` `b)  ` `    ``{ ` `        ``int` `c = a; ` `        ``a = b; ` `        ``b = c; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `x = 0, y = 8; ` `        ``String s = ``"000110"``; ` `        ``minimumFlip(s, x, y); ` `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

Output:

`Clockwise 2`

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.