Given a binary array arr[] of size N and an integer K, the task is to check whether the binary array can be turned into a palindrome after K number of operations where in one operation, any random index can be chosen and the value stored in the index will be replaced by its bitwise XOR(^) with1.
Examples:
Input: arr[] = { 1, 0, 0, 1, 1}, K = 0
Output: No
Explanation: As the array is not a palindrome, we must need some non-zero number of operations
to make it palindrome.
Input: arr[] = { 1, 0, 1, 1, 0, 0}, K = 1
Output: Yes
Explanation: Closest arrays which are palindrome : {0, 0, 1, 1, 0, 0} and {1, 0, 1, 1, 0, 1}, which is possible
using 1 such operations only.
Approach: The approach to solve the problem is based on the following idea:
The minimum number of operations required is the number of unequal elements equidistant from the mid of the array and to make them equal it takes 1 operation (from 1 to 0 or from 0 to 1).
There is need of 2 operations to change two equal elements and make them have the same value again using bitwise XOR.
To implement this idea, follow the steps shown below :
- Initialize two pointers pointing at the two ends of the array.
- Find the number of unequal array elements while traversing the array from its two ends.
- If the number is greater than K, then it is impossible to reach the target with exactly K steps.
- If the number is exactly equal to K, then it is possible to make the array palindrome.
- Otherwise, if the number is lesser than K:
- If the array length is odd, then any number of positive K is fine as we can perform the operations at the mid index.
- If the array length is even, to keep it a palindrome, we must choose two indices and make the operations on those indices. So remaining K has to be even.
Here is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_possibility( int * arr, int K)
{
int arr_length = sizeof (arr) / sizeof (
arr[0]);
int i = 0;
int j = arr_length - 1;
while (i < j) {
if (arr[i] != arr[j]) {
K--;
}
i++;
j--;
}
if (K < 0) {
return false ;
}
else {
if ((arr_length % 2) != 0
|| (K % 2) == 0) {
return true ;
}
else {
return false ;
}
}
}
int main()
{
int arr[6] = { 1, 0, 1, 1, 0, 0 };
int K = 1;
if (check_possibility(arr, K) == true ) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static boolean check_possibility( int arr[],
int K)
{
int arr_length = arr.length;
int i = 0 ;
int j = arr_length - 1 ;
while (i < j) {
if (arr[i] != arr[j]) {
K--;
}
i++;
j--;
}
if (K < 0 ) {
return false ;
}
else {
if ((arr_length % 2 ) != 0 || (K % 2 ) == 0 ) {
return true ;
}
else {
return false ;
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 1 , 1 , 0 , 0 };
int K = 1 ;
if (check_possibility(arr, K) == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
C#
using System;
class GFG {
static bool check_possibility( int [] arr, int K)
{
int arr_length = arr.Length;
int i = 0;
int j = arr_length - 1;
while (i < j) {
if (arr[i] != arr[j]) {
K--;
}
i++;
j--;
}
if (K < 0) {
return false ;
}
else {
if ((arr_length % 2) != 0 || (K % 2) == 0) {
return true ;
}
else {
return false ;
}
}
}
public static int Main()
{
int [] arr = new int [] { 1, 0, 1, 1, 0, 0 };
int K = 1;
if (check_possibility(arr, K) == true ) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
return 0;
}
}
|
Python3
def check_possibility(arr, K):
arr_length = len (arr)
i = 0
j = arr_length - 1
while (i < j):
if (arr[i] ! = arr[j]):
K - = 1
i + = 1
j - = 1
if (K < 0 ):
return False
else :
if ( (arr_length % 2 )! = 0 or (K % 2 ) = = 0 ):
return True
else :
return False
if __name__ = = '__main__' :
arr = [ 1 , 0 , 1 , 1 , 0 , 0 ]
K = 1
if check_possibility(arr, K) = = True :
print ( "Yes" )
else :
print ( "No" )
|
Javascript
function check_possibility(arr, K)
{
var arr_length = arr.length;
var i = 0;
var j = arr_length - 1;
while (i < j) {
if (arr[i] != arr[j]) {
K--;
}
i++;
j--;
}
if (K < 0) {
return false ;
}
else {
if ((arr_length % 2) != 0 || (K % 2) == 0) {
return true ;
}
else {
return false ;
}
}
}
var arr = [ 1, 0, 1, 1, 0, 0 ];
var K = 1;
if (check_possibility(arr, K) == true ) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)