- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find a number which give minimum sum when XOR with every number of array of integer in Python

Suppose we have an array A, we have to find a number X such that (A[0] XOR X) + (A[1] XOR X) + … + A[n – 1] XOR X is as minimum as possible.

So, if the input is like [3, 4, 5, 6, 7], then the output will be X = 7 , Sum = 10

To solve this, we will follow these steps −

- Define a function search_res() . This will take arr, n
- element := arr[0]
- for i in range 0 to size of arr, do
- if arr[i] > element, then
- element := arr[i]

- if arr[i] > element, then
- p := integer of (log of element base 2) + 1
- X := 0
- for i in range 0 to p, do
- cnt := 0
- for j in range 0 to n, do
- if arr[j] AND (2^i) is non-zero, then
- cnt := cnt + 1

- if arr[j] AND (2^i) is non-zero, then
- if cnt > integer part of (n / 2) is non-zero, then
- X := X + (2^i)

- sum := 0
- for i in range 0 to n, do
- sum := sum +(X XOR arr[i])

- return X and sum

## Example

Let us see the following implementation to get better understanding −

from math import log2 def search_res(arr, n): element = arr[0] for i in range(len(arr)): if(arr[i] > element): element = arr[i] p = int(log2(element)) + 1 X = 0 for i in range(p): cnt = 0 for j in range(n): if (arr[j] & (1 << i)): cnt += 1 if (cnt > int(n / 2)): X += 1 << i sum = 0 for i in range(n): sum += (X ^ arr[i]) print("X =", X, ", Sum =", sum) arr = [3, 4, 5, 6, 7] n = len(arr) search_res(arr, n)

## Input

[3, 4, 5, 6, 7]

## Output

X = 7 , Sum = 10

- Related Articles
- Find a number which give minimum sum when XOR with every number of array of integer in C++
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Python Program for Find minimum sum of factors of number
- Program to find equal sum arrays with minimum number of operations in Python
- XOR of Sum of every possible pair of an array in C++
- XOR a given scalar value with every element of a masked array in Python
- Find minimum sum of factors of number using C++.
- Java Program to find minimum sum of factors of a number
- C Program to Find the minimum sum of factors of a number?
- XOR every element of a masked array by a given scalar value in Python
- Find XOR of two number without using XOR operator in C++
- Maximum possible XOR of every element in an array with another array in C++
- State whether the following statements are true or false. Give reasons for your answers.(i) Every natural number is a whole number.(ii) Every integer is a whole number.(iii) Every rational number is a whole number.
- Find number of times every day occurs in a Year in Python

Advertisements