
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum operations to make XOR of array zero in C++
Problem statement
We are given an array of n elements. The task is to make XOR of whole array 0. We can do following to achieve this.
We can select any one of the element −
- After selecting element, we can either increment or decrement it by 1.
- We need to find the minimum number of increment/decrement operation required for the selected element to make the XOR sum of whole array zero
Example
If arr[] = {2, 4, 7} then 1 operation is required −
- Select element 2
- Decrement it by 1
- Now array becomes {3, 4, 7} and its XOR is 0
Algorithm
- Find the XOR of whole array
- Now, suppose we have selected element arr[i], so cost required for that element will be absolute(arr[i]-(XORsum^arr[i]))
- Calculating minimum of these absolute values for each of element will be our minimum required operation
Example
#include <iostream> #include <climits> #include <cmath> using namespace std; void getMinCost(int *arr, int n) { int operations = INT_MAX; int elem; int xorValue = 0; for (int i = 0; i < n; ++i) { xorValue = xorValue ^ arr[i]; } for (int i = 0; i < n; ++i) { if (operations > abs((xorValue ^ arr[i]) - arr[i])) { operations = abs((xorValue ^ arr[i]) - arr[i]); elem = arr[i]; } } cout << "Element= " << elem << endl; cout << "Minimum required operations = " << abs(operations) << endl; } int main() { int arr[] = {2, 4, 7}; int n = sizeof(arr) / sizeof(arr[0]); getMinCost(arr, n); return 0; }
Output
When you compile and execute above program. It generates following output:
Element = 2 Minimum required operations = 1
- Related Articles
- Program to find minimum one bit operations to make integers zero in Python
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations to make GCD of array a multiple of k in C++
- Find minimum operations needed to make an Array beautiful in C++
- Find minimum number of merge operations to make an array palindrome in C++
- Program to find minimum operations to make array equal using Python
- Minimum operations required to make all the array elements equal in C++
- Program to find minimum operations to make the array increasing using Python
- Program to make the XOR of all segments equal to zero in Python
- Minimum number of operations on an array to make all elements 0 using C++.
- Program to find minimum operations to reduce X to zero in Python
- Minimum number of elements to be removed to make XOR maximum using C++.
- Program to find minimum number of operations to make string sorted in Python
- Minimum operations required to remove an array in C++
- Minimum move to end operations to make all strings equal in C++

Advertisements