Naive Approach : Select an element and then find the XOR of rest of the array. If that element became equals to XOR obtained then our XOR of the whole array should become zero. Now, our cost for that will be the absolute difference of selected element and obtained XOR. This process of finding cost will be done for each element and thus resulting into Time Complexity of (n^2).

Efficient Approach : 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 also the element corresponding to minimum required operation will be our selected element.

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP to find min cost to make

// XOR of whole array zero

#include <bits/stdc++.h>

usingnamespacestd;

// function to find min cost

voidminCost(intarr[], intn)

{

intcost = INT_MAX;

intelement;

// calculate XOR sum of array

intXOR = 0;

for(inti = 0; i < n; i++)

XOR ^= arr[i];

// find the min cost and element corresponding

for(inti = 0; i < n; i++) {

if(cost > abs((XOR ^ arr[i]) - arr[i])) {

cost = abs((XOR ^ arr[i]) - arr[i]);

element = arr[i];

}

}

cout << "Element = "<< element << endl;

cout << "Operation required = "<< abs(cost);

}

// driver program

intmain()

{

intarr[] = { 2, 8, 4, 16 };

intn = sizeof(arr) / sizeof(arr[0]);

minCost(arr, n);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// JAVA program to find min cost to make

// XOR of whole array zero

importjava.lang.*;

classGFG

{

// function to find min cost

staticvoidminCost(int[] arr, intn)

{

intcost = Integer.MAX_VALUE;

intelement=0;

// calculate XOR sum of array

intXOR = 0;

for(inti = 0; i < n; i++)

XOR ^= arr[i];

// find the min cost and element

// corresponding

for(inti = 0; i < n; i++) {

if(cost > Math.abs((XOR ^ arr[i])

- arr[i])) {

cost = Math.abs((XOR ^ arr[i]) -

arr[i]);

element = arr[i];

}

}

System.out.println("Element = "+ element);

System.out.println("Operation required = "+

Math.abs(cost));

}

// driver program

publicstaticvoidmain (String[] args)

{

int[] arr = { 2, 8, 4, 16};

intn = arr.length;

minCost(arr, n);

}

}

/* This code is contributed by Kriti Shukla */

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# python to find min cost to make

# XOR of whole array zero

# function to find min cost

defminCost(arr,n):

cost =999999;

# calculate XOR sum of array

XOR =0;

fori inrange(0, n):

XOR ^=arr[i];

# find the min cost and element

# corresponding

fori inrange(0,n):

if(cost > abs((XOR ^ arr[i]) -arr[i])):

cost =abs((XOR ^ arr[i]) -arr[i])

element =arr[i]

print("Element = ", element)

print("Operation required = ", abs(cost))

# driver program

arr =[ 2, 8, 4, 16]

n =len(arr)

minCost(arr, n)

# This code is contributed by Sam007

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find min cost to

// make XOR of whole array zero

usingSystem;

classGFG

{

// function to find min cost

staticvoidminCost(int[]arr, intn)

{

intcost = int.MaxValue;

intelement=0;

// calculate XOR sum of array

intXOR = 0;

for(inti = 0; i < n; i++)

XOR ^= arr[i];

// find the min cost and

// element corresponding

for(inti = 0; i < n; i++)

{

if(cost > Math.Abs((XOR ^ arr[i]) - arr[i]))

{

cost = Math.Abs((XOR ^ arr[i]) - arr[i]);

element = arr[i];

}

}

Console.WriteLine("Element = "+ element);

Console.Write("Operation required = "+

Math.Abs(cost));

}

// Driver program

publicstaticvoidMain ()

{

int[]arr = {2, 8, 4, 16};

intn = arr.Length;

minCost(arr, n);

}

}

// This code is contributed by nitin mittal.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP to find min cost to make

// XOR of whole array zero

// function to find min cost

functionminCost($arr, $n)

{

$cost= PHP_INT_MAX;

$element;

// calculate XOR sum of array

$XOR= 0;

for($i= 0; $i< $n; $i++)

$XOR^= $arr[$i];

// find the min cost and

// element corresponding

for($i= 0; $i< $n; $i++)

{

if($cost> abs(($XOR^ $arr[$i]) -

$arr[$i]))

{

$cost= abs(($XOR^ $arr[$i]) -

$arr[$i]);

$element= $arr[$i];

}

}

echo"Element = ", $element,"\n";

echo"Operation required = ", abs($cost);

}

// Driver Code

$arr= array(2, 8, 4, 16) ;

$n= count($arr);

minCost($arr, $n);

// This code is contributed by vt_m.

?>

chevron_right

filter_none

Output:

Element = 16
Operation required = 2

Time Complexity : O(n)

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.