
- 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 XOR Value Pair in C++
Problem statement
Given an array of integers. Find the pair in an array which has minimum XOR value
Example
If arr[] = {10, 20, 30, 40} then minimum value pair will be 20 and 30 as (20 ^ 30) = 10. (10 ^ 20) = 30 (10 ^ 30) = 20 (10 ^ 40) = 34 (20 ^ 30) = 10 (20 ^ 40) = 60 (30 ^ 40) = 54
Algorithm
- Generate all pairs of given array and compute XOR their values
- Return minimum XOR value
Example
#include <bits/stdc++.h> using namespace std; int getMinValue(int *arr, int n) { int minValue = INT_MAX; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { minValue = min(minValue, arr[i] ^ arr[j]); } } return minValue; } int main() { int arr[] = {10, 20, 30, 40}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum value = " << getMinValue(arr, n) << endl; return 0; }
When you compile and execute above program. It generates following output −
Output
Minimum value = 10
- Related Articles
- Maximum XOR value of a pair from a range in C++
- Maximum XOR value in matrix in C++
- Minimum operations to make XOR of array zero in C++
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- XOR of Sum of every possible pair of an array in C++
- Add key-value pair in C# Dictionary
- Count number of subsets having a particular XOR value in C++
- Minimum number of elements to be removed to make XOR maximum using C++.
- Count minimum bits to flip such that XOR of A and B equal to C in C++
- Maximum value of XOR among all triplets of an array in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Find a value whose XOR with given number is maximum in C++
- XOR Cipher in C++
- Get minimum difference in Tuple pair in Python
- Minimum flips in two binary arrays so that their XOR is equal to another array in C++.

Advertisements