Minimum bit changes in Binary Circular array to reach a index
Last Updated :
17 Aug, 2022
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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG
{
static void minimumFlip(String s,
int x, int y)
{
s = s + s;
boolean isOpposite = false ;
if (x > y)
{
swap(x, y);
isOpposite = true ;
}
int valClockwise = 0 ;
char cur = s.charAt(x);
for ( int i = x; i <= y; i++)
{
if (s.charAt(i) != cur)
{
cur = s.charAt(i);
valClockwise++;
}
}
int valAnticlockwise = 0 ;
cur = s.charAt(y);
x += s.length();
for ( int i = y; i < x; i++)
{
if (s.charAt(i) != cur)
{
cur = s.charAt(i);
valAnticlockwise++;
}
}
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;
}
public static void main(String[] args)
{
int x = 0 , y = 8 ;
String s = "000110" ;
minimumFlip(s, x, y);
}
}
|
Python3
def minimumFlip(s, x, y):
s = s + s
isOpposite = False
if (x > y):
temp = y
y = x;
x = temp
isOpposite = True
valClockwise = 0
cur = s[x]
for i in range (x, y + 1 , 1 ):
if (s[i] ! = cur):
cur = s[i]
valClockwise + = 1
valAnticlockwise = 0
cur = s[y]
x + = len (s) - 1
for i in range (y, x + 1 , 1 ):
if (s[i] ! = cur):
cur = s[i]
valAnticlockwise + = 1
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)
if __name__ = = '__main__' :
x = 0
y = 8
s = "000110"
minimumFlip(s, x, y)
|
C#
using System;
class GFG
{
static 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)
{
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;
}
public static void Main(String[] args)
{
int x = 0, y = 8;
String s = "000110" ;
minimumFlip(s, x, y);
}
}
|
Javascript
<script>
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( "Clockwise "
+ valClockwise + "<br>" );
else
document.write( "Anti-clockwise "
+ valAnticlockwise + "<br>" );
}
else {
if (!isOpposite)
document.write( "Anti-clockwise "
+ valAnticlockwise + "<br>" );
else
document.write( "Clockwise "
+ valClockwise + "<br>" );
}
}
var x = 0, y = 8;
var s = "000110" ;
minimumFlip(s, x, y);
</script>
|
Complexity Analysis:
- Time Complexity: O(y-x) + O(x-y), where x and y are given by user
- Auxiliary Space: O(1), as no extra space was used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...