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++
// CPP program to find direction with minimum flips
#include <bits/stdc++.h>
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 string 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 string 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 string 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
JavaScript
<script>
// Javascript program to find direction with minimum flips
// finding which path have minimum flip bit and
// the minimum flip bits
function 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.
var isOpposite = false;
if (x > y) {
swap(x, y);
isOpposite = true;
}
// iterate Clockwise
var valClockwise = 0;
var cur = s[x];
for (var 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
var valAnticlockwise = 0;
cur = s[y];
x += s.length;
for (var 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)
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>");
}
}
// Driven Program
var x = 0, y = 8;
var s = "000110";
minimumFlip(s, x, y);
// This code is contributed by rrrtnx.
</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
Similar Reads
Minimum swaps to group all 0s together in Binary Circular Array Given a binary circular array arr[] of size N, the task is to find the minimum swaps to group all 0s together in the array. Examples: Input: arr[] = {1, 0, 1, 0, 0, 1, 1}Output: 1Explanation: Here are a few of the ways to group all the 0's together: {1, 1, 0, 0, 0, 1, 1} using 1 swap.{1, 0, 0, 0, 1,
6 min read
Minimum cost to convert all characters of given binary array equal Given a binary array arr[] of size N. Task for this problem is to find minimum cost required to make all elements of array equal. You can perform one of the following operations (considering 0 indexed array) Choose i and flip all characters from 0 to i with cost i + 1Choose i and flip all characters
15+ min read
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Maximum consecutive oneâs (or zeros) in a binary circular array Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1âs present in the circular array.Examples: Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1} Output: 6 The last 4 and first 2 positions have 6 consecutive ones. Input: a[] = {0, 1, 0, 1, 0, 1, 0,
8 min read
Minimum number of 1's to be replaced in a binary array Given a binary array arr[] of zero's and one's only. The task is to find the minimum number of one's to be changed to zero such if there exist any index i in the array such that arr[i] = 0 then arr[i-1] and arr[i+1] both should not be equals to 1 at the same time. That is, for any index i the below
5 min read