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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read