Minimize XOR of pairs to make the Array Palindrome
Given an array arr[] of size N consisting only of 0s and 1s, the task is to find the minimum number of operations required to make the array a palindrome where in each operation you can choose any two elements at positions i and j and replace both arr[i] and arr[j] with arr[i]^arr[j] (where ^ represents bitwise XOR operation.)
Examples:
Input: N = 4, arr[] = { 1, 0, 0, 0 }
Output: 1
Explanation: We can choose elements at 0 and 3. Therefore the arr[] become = {1, 0, 0, 1}
Input: N = 3, arr[] = {1, 0, 1}
Output: 0
Approach: This problem can be solved using the Greedy Algorithm.
The idea is to count the number of pairs such that arr[i] is not equal to arr[N – 1 – i]. So we have two options to make arr[i] = arr[N – 1 – i]:
- Choosing two different elements from the same pair (i, N – 1 – i) and performing the operation on them to make them equal.
- Choosing two same elements from different pairs and performing the operation on them to make arr[i] equal to arr[N – 1 – i].
Since we need to minimize the number of operations we will try choosing 2nd option as using this we are getting two pairs done in one operation. So we will choose 1st option only when a single pair is left.
Follow the below steps to implement the idea:
- Initialize the ans = 0, to count the state where inequality is present.
- Run a loop and count inequality.
- Return (ans + 1) / 2 as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOp( int n, vector< int >& arr)
{
int ans = 0;
for ( int i = 0; i < n / 2; i++)
if (arr[i] != arr[n - 1 - i])
ans++;
return (ans + 1) / 2;
}
int main()
{
vector< int > arr = { 1, 1, 0, 1, 1, 0, 1 };
int N = arr.size();
cout << minOp(N, arr) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minOp( int n, int [] arr)
{
int ans = 0 ;
for ( int i = 0 ; i < n / 2 ; i++)
if (arr[i] != arr[n - 1 - i])
ans++;
return (ans + 1 ) / 2 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 1 , 0 , 1 , 1 , 0 , 1 };
int N = arr.length;
System.out.println(minOp(N, arr));
}
}
|
Python3
import math
def minOp(n, arr):
ans = 0
for i in range (math.floor(n / 2 )):
if arr[i] ! = arr[n - 1 - i]:
ans = ans + 1
return math.floor((ans + 1 ) / 2 )
arr = [ 1 , 1 , 0 , 1 , 1 , 0 , 1 ]
N = len (arr)
print (minOp(N, arr))
|
C#
using System;
public class GFG {
static int minOp( int n, int [] arr)
{
int ans = 0;
for ( int i = 0; i < n / 2; i++)
if (arr[i] != arr[n - 1 - i])
ans++;
return (ans + 1) / 2;
}
static public void Main()
{
int [] arr = { 1, 1, 0, 1, 1, 0, 1 };
int N = arr.Length;
Console.WriteLine(minOp(N, arr));
}
}
|
Javascript
function minOp( n, arr)
{
let ans = 0;
for (let i = 0; i < n / 2; i++)
if (arr[i] != arr[n - 1 - i])
ans++;
return Math.floor((ans + 1) / 2);
}
const arr = [ 1, 1, 0, 1, 1, 0, 1 ];
let N = arr.length;
console.log(minOp(N, arr));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
30 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...