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++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function to calculate minimum operations 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 minimum operations
return (ans + 1) / 2;
} // Driver code int main()
{ vector< int > arr = { 1, 1, 0, 1, 1, 0, 1 };
int N = arr.size();
// Function call
cout << minOp(N, arr) << endl;
return 0;
} |
// Java code to implement the approach 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 minimum operations
return (ans + 1 ) / 2 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 1 , 0 , 1 , 1 , 0 , 1 };
int N = arr.length;
// Function call
System.out.println(minOp(N, arr));
}
} // This code is contributed by lokesh. |
# Python program for the above approach import math
# Function to calculate minimum operations 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 minimum operations
return math.floor((ans + 1 ) / 2 )
# Driver code arr = [ 1 , 1 , 0 , 1 , 1 , 0 , 1 ]
N = len (arr)
# Function call print (minOp(N, arr))
# This code is contributed by Potta Lokesh |
// C# code to implement the approach 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 minimum operations
return (ans + 1) / 2;
}
static public void Main()
{
// Code
int [] arr = { 1, 1, 0, 1, 1, 0, 1 };
int N = arr.Length;
// Function call
Console.WriteLine(minOp(N, arr));
}
} // This code is contributed by lokeshmvs21. |
// JavaScript code to implement the approach // Function to calculate minimum operations function minOp( n, arr)
{ let ans = 0;
for (let i = 0; i < n / 2; i++)
if (arr[i] != arr[n - 1 - i])
ans++;
// Return minimum operations
return Math.floor((ans + 1) / 2);
} // Driver code const arr = [ 1, 1, 0, 1, 1, 0, 1 ];
let N = arr.length;
// Function call
console.log(minOp(N, arr));
// This code is contributed by poojaagarwal2. |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
- Introduction to Array – Data Structures and Algorithms
- Introduction to Greedy Algorithm – Data Structures and Algorithms