Given two binary arrays **X[]** and **Y[]** of size **N**, the task is to convert array **X[]** into array **Y[]** by minimum number of operations of selecting any subarray of odd length and flipping all odd-indexed elements from the subarray.

**Examples:**

Input:X[] = {1, 0, 0, 0, 0, 1}, Y[] = {1, 1, 0, 1, 1, 1}Output:2Explanation:

Initially, X[] is {1, 0, 0, 0, 0, 1}.Operation 1:Choose the sub-array {0, 0, 0} from array X[] and change the 2^{nd}and 4^{th}character and convert it to {1, 0, 1}.

Now X becomes {1, 1, 0, 1, 0, 1}.Operation 2:Choose the sub-array {0} containing only the 5^{th}character and convert it to 1.

Finally, X becomes {1, 1, 0, 1, 1, 1}, which is equal to Y.

Therefore, the count of operations is 2.

Input:X[] = {0, 1, 0}, Y[] = {0, 1, 0}Output:0Explanation:

Since both the arrays X and Y are equal thus the minimum operations would be 0.

**Approach:** The idea is to count the operations for both even and odd positions individually. Follow the steps below to solve the problem:

- Initialize a variable
**C**as**0**to store the count of operations. - Traverse the array
**X[]**elements over odd positions and take a counter**count**= 0.- Check for consecutive unequal elements between
**X[i]**and**Y[i]**and increase the counter**count**by**1**every time. - When
**X[i]**and**Y[i]**are equal increase a global**C**to increase operation by 1 since in that one operation all odd positions can be made equal to as in**Y**.

- Check for consecutive unequal elements between
- Similarly, Traverse the array
**X[]**elements over even positions and again take a counter**count**= 0.- Check for consecutive unequal elements between
**X[i]**and**Y[i]**and increase the counter**count**by**1**every time. - When
**X[i]**and**Y[i]**are equal increase a global counter**C**to increase operation by**1**.

- Check for consecutive unequal elements between
- After the above steps, print the value of
**C**as the resultant count of operations required.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum flip` `// of subarrays required at alternate` `// index to make binary arrays equals` `void` `minOperation(` `int` `X[], ` `int` `Y[],` ` ` `int` `n)` `{` ` ` `// Stores count of total operations` ` ` `int` `C = 0;` ` ` `// Stores count of consecutive` ` ` `// unequal elements` ` ` `int` `count = 0;` ` ` `// Loop to run on odd positions` ` ` `for` `(` `int` `i = 1; i < n; i = i + 2) {` ` ` `if` `(X[i] != Y[i]) {` ` ` `count++;` ` ` `}` ` ` `else` `{` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != 0)` ` ` `C++;` ` ` `// Change count to 0` ` ` `count = 0;` ` ` `}` ` ` `}` ` ` `// If all last elements are equal` ` ` `if` `(count != 0)` ` ` `C++;` ` ` `count = 0;` ` ` `// Loop to run on even positions` ` ` `for` `(` `int` `i = 0; i < n; i = i + 2) {` ` ` `if` `(X[i] != Y[i]) {` ` ` `count++;` ` ` `}` ` ` `else` `{` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != 0)` ` ` `C++;` ` ` `// Change count to 0` ` ` `count = 0;` ` ` `}` ` ` `}` ` ` `if` `(count != 0)` ` ` `C++;` ` ` `// Print the minimum operations` ` ` `cout << C;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `X[] = { 1, 0, 0, 0, 0, 1 };` ` ` `int` `Y[] = { 1, 1, 0, 1, 1, 1 };` ` ` `int` `N = ` `sizeof` `(X) / ` `sizeof` `(X[0]);` ` ` `// Function Call` ` ` `minOperation(X, Y, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*;` ` ` `class` `GFG{` ` ` `// Function to find the minimum flip` `// of subarrays required at alternate` `// index to make binary arrays equals` `static` `void` `minOperation(` `int` `X[], ` `int` `Y[],` ` ` `int` `n)` `{` ` ` ` ` `// Stores count of total operations` ` ` `int` `C = ` `0` `;` ` ` ` ` `// Stores count of consecutive` ` ` `// unequal elements` ` ` `int` `count = ` `0` `;` ` ` ` ` `// Loop to run on odd positions` ` ` `for` `(` `int` `i = ` `1` `; i < n; i = i + ` `2` `)` ` ` `{` ` ` ` ` `if` `(X[i] != Y[i]) ` ` ` `{` ` ` `count++;` ` ` `}` ` ` `else` ` ` `{` ` ` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != ` `0` `)` ` ` `C++;` ` ` ` ` `// Change count to 0` ` ` `count = ` `0` `;` ` ` `}` ` ` `}` ` ` ` ` `// If all last elements are equal` ` ` `if` `(count != ` `0` `)` ` ` `C++;` ` ` ` ` `count = ` `0` `;` ` ` ` ` `// Loop to run on even positions` ` ` `for` `(` `int` `i = ` `0` `; i < n; i = i + ` `2` `) ` ` ` `{` ` ` `if` `(X[i] != Y[i]) ` ` ` `{` ` ` `count++;` ` ` `}` ` ` `else` ` ` `{` ` ` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != ` `0` `)` ` ` `C++;` ` ` ` ` `// Change count to 0` ` ` `count = ` `0` `;` ` ` `}` ` ` `}` ` ` ` ` `if` `(count != ` `0` `)` ` ` `C++;` ` ` ` ` `// Print the minimum operations` ` ` `System.out.print(C);` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `X[] = { ` `1` `, ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `1` `};` ` ` `int` `Y[] = { ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `};` ` ` `int` `N = X.length;` ` ` ` ` `// Function Call` ` ` `minOperation(X, Y, N);` `}` `}` `// This code is contributed by susmitakundugoaldanga` |

*chevron_right*

*filter_none*

## Python3

`# Python program for the above approach` `# Function to find the minimum flip` `# of subarrays required at alternate` `# index to make binary arrays equals` `def` `minOperation(X, Y, n):` ` ` ` ` `# Stores count of total operations` ` ` `C ` `=` `0` `;` ` ` `# Stores count of consecutive` ` ` `# unequal elements` ` ` `count ` `=` `0` `;` ` ` `# Loop to run on odd positions` ` ` `for` `i ` `in` `range` `(` `1` `, n, ` `2` `):` ` ` `if` `(X[i] !` `=` `Y[i]):` ` ` `count ` `+` `=` `1` `;` ` ` `else` `:` ` ` `# Incrementing the` ` ` `# global counter` ` ` `if` `(count !` `=` `0` `):` ` ` `C ` `+` `=` `1` `;` ` ` `# Change count to 0` ` ` `count ` `=` `0` `;` ` ` `# If all last elements are equal` ` ` `if` `(count !` `=` `0` `):` ` ` `C ` `+` `=` `1` `;` ` ` `count ` `=` `0` `;` ` ` `# Loop to run on even positions` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `2` `):` ` ` `if` `(X[i] !` `=` `Y[i]):` ` ` `count ` `+` `=` `1` `;` ` ` `else` `:` ` ` `# Incrementing the` ` ` `# global counter` ` ` `if` `(count !` `=` `0` `):` ` ` `C ` `+` `=` `1` `;` ` ` `# Change count to 0` ` ` `count ` `=` `0` `;` ` ` `if` `(count !` `=` `0` `):` ` ` `C ` `+` `=` `1` `;` ` ` `# Prthe minimum operations` ` ` `print` `(C);` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `X ` `=` `[` `1` `, ` `0` `, ` `0` `, ` `0` `, ` `0` `, ` `1` `];` ` ` `Y ` `=` `[` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `];` ` ` `N ` `=` `len` `(X);` ` ` `# Function Call` ` ` `minOperation(X, Y, N);` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum flip` `// of subarrays required at alternate` `// index to make binary arrays equals` `static` `void` `minOperation(` `int` `[]X, ` `int` `[]Y,` ` ` `int` `n)` `{` ` ` ` ` `// Stores count of total operations` ` ` `int` `C = 0;` ` ` ` ` `// Stores count of consecutive` ` ` `// unequal elements` ` ` `int` `count = 0;` ` ` ` ` `// Loop to run on odd positions` ` ` `for` `(` `int` `i = 1; i < n; i = i + 2)` ` ` `{` ` ` `if` `(X[i] != Y[i]) ` ` ` `{` ` ` `count++;` ` ` `}` ` ` `else` ` ` `{` ` ` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != 0)` ` ` `C++;` ` ` ` ` `// Change count to 0` ` ` `count = 0;` ` ` `}` ` ` `}` ` ` ` ` `// If all last elements are equal` ` ` `if` `(count != 0)` ` ` `C++;` ` ` ` ` `count = 0;` ` ` ` ` `// Loop to run on even positions` ` ` `for` `(` `int` `i = 0; i < n; i = i + 2) ` ` ` `{` ` ` `if` `(X[i] != Y[i]) ` ` ` `{` ` ` `count++;` ` ` `}` ` ` `else` ` ` `{` ` ` ` ` `// Incrementing the` ` ` `// global counter` ` ` `if` `(count != 0)` ` ` `C++;` ` ` ` ` `// Change count to 0` ` ` `count = 0;` ` ` `}` ` ` `}` ` ` ` ` `if` `(count != 0)` ` ` `C++;` ` ` ` ` `// Print the minimum operations` ` ` `Console.Write(C);` `}` ` ` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[]X = { 1, 0, 0, 0, 0, 1 };` ` ` `int` `[]Y = { 1, 1, 0, 1, 1, 1 };` ` ` `int` `N = X.Length;` ` ` ` ` `// Function Call` ` ` `minOperation(X, Y, N);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.