 Open in App
Not now

# Check if Binary Array can be made Palindrome by flipping two bits at a time

• Last Updated : 09 Nov, 2022

Given a binary array A[] of size N, the task is to check whether the array can be converted into a palindrome by flipping two bits in each operation.

Note: The operation can be performed any number of times.

Examples:

Input: A[] = {1, 0, 1, 0, 1, 1}
Output: Yes
Explanation: We can perform the following operation:
Select i = 2 and j = 4. Then {1, 0, 1, 0, 1, 1}→{1, 0, 0, 0, 0, 1} which is a palindrome.

Input: A[] = {0, 1}
Output: No

Approach: The problem can be solved based on the following observation:

Count number of 0s and 1s.

• If the value of count of 0s and 1s are odd then no palindrome can be made by performing operation on A[].
• Otherwise, array A[] can converted into a palindrome after performing operation on array.

Follow the below steps to solve the problem:

• Set zeros = 0 and ones = 0 to store the count of number of 0s and 1s in the array.
• After that iterate a loop from 0 to N-1 such as:
• If A[i] = 0 increment the value of zeros otherwise increment the value of ones.
• If the value of zeros and ones is odd then print “No” i.e. it is not possible to convert A to a palindrome by applying the above operation any number of times.
• Otherwise, print “Yes”.

Below is the implementation of the above approach.

## C++

```// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;

// Function to find check whether
// array can converted into palindrome
string check(int arr[], int n)
{
int one = 0, zero = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == '0') {
zero++;
}
else {
one++;
}
}
if (one % 2 != 0 && zero % 2 != 0)
return "No";
return "Yes";
}

// Driver Code
int main()
{
int A[] = { 1, 0, 1, 0, 1, 1 };
int N = sizeof(A) / sizeof(A);

// Function call
cout << check(A, N);

return 0;
}

// This code is contributed by aarohirai2616.```

## Java

```// Java code to implement the approach

import java.io.*;
import java.util.*;

public class GFG {

// Function to find check whether
// array can converted into palindrome
public static String check(int arr[], int n)
{
int one = 0, zero = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == '0') {
zero++;
}
else {
one++;
}
}
if (one % 2 != 0 && zero % 2 != 0)
return "No";
return "Yes";
}

// Driver Code
public static void main(String[] args)
{
int[] A = { 1, 0, 1, 0, 1, 1 };
int N = A.length;

// Function Call
System.out.println(check(A, N));
}
}```

## Python3

```# Python code to implement the approach

# Function to find check whether
# array can converted into palindrome
def check(arr, n):
one = 0
zero = 0
for i in range(0, n):
if (arr[i] == '0'):
zero = zero + 1

else:
one = one + 1

if(one % 2 != 0 & zero % 2 != 0):
return "No"
return "Yes"

# Driver Code
if __name__ == '__main__':
A = [1, 0, 1, 0, 1, 1 ]
N = len(A)

# Function call
print(check(A,N))

# This code is contributed by aarohirai2616.```

## C#

```using System;
class GFG {
static string check(int[] arr, int n)
{
int one = 0, zero = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == '0') {
zero++;
}
else {
one++;
}
}
if (one % 2 != 0 && zero % 2 != 0)
return "No";
return "Yes";
}
static void Main()
{
int[] A = { 1, 0, 1, 0, 1, 1 };
int N = 6;

// Function Call
Console.Write(check(A, N));
}
}

// This code is contributed by garg28harsh.```

## Javascript

```        // JavaScript code to implement the approach

// Function to find check whether
// array can converted into palindrome
const check = (arr, n) => {
let one = 0, zero = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == '0') {
zero++;
}
else {
one++;
}
}
if (one % 2 != 0 && zero % 2 != 0)
return "No";
return "Yes";
}

// Driver Code
let A = [1, 0, 1, 0, 1, 1];
let N = A.length;

// Function call
console.log(check(A, N));

// This code is contributed by rakeshsahni
```
Output

`Yes`

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

My Personal Notes arrow_drop_up