Given an array a[] of size N and an integer K, the task is to find a value X in the range [0, K] which can maximize the value of the given function
- Xor-sum(X) = (X XOR A[0]) + (X Xor A[1]) + (X Xor A[2]) + __________+ (X Xor A[N-1]).
Examples:
Input: a[] = {1, 2, 3, 4, 5, 6}, N=6, K=10
Output: 8
Explanation: The value of X is 1 for which the required sum becomes maximum.
Input: a[] = {1, 6}, N=2, K=7
Output: 0
Approach: The idea is to consider each and every value of K and then find the value of X which satisfies the above condition. Follow the steps below to solve the problem:
- Initialize the variables max and X as 0 and -1 to store the maximum sum defined and the value of X for which it is happening.
- Iterate over the range [0, K] using the variable j and perform the following tasks:
- Initialize the variable sum as 0 to store the defined sum.
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- Set the value of sum as sum + j^a[i].
- If sum is greater than max, then set the value of max as sum and X as j.
- After performing the above steps, print the value of X as the answer.
Below is the implementation of the above approach
C++14
#include <bits/stdc++.h>
using namespace std;
void findValue(vector< int >& a, int N, int K)
{
long long int max = 0, X = -1;
for ( int j = 0; j <= K; j++) {
long long int sum = 0;
for ( int i = 0; i < N; i++) {
(sum = sum + (j ^ a[i]));
}
if (sum > max) {
max = sum;
X = j;
}
}
cout << X;
}
int main()
{
vector< int > a = { 1, 2, 3, 4, 5, 6 };
int N = 6, K = 10;
findValue(a, N, K);
return 0;
}
|
C
#include <stdio.h>
void findValue( int *a, int N, int K)
{
long long int max = 0, X = -1;
for ( int j = 0; j <= K; j++) {
long long int sum = 0;
for ( int i = 0; i < N; i++) {
(sum = sum + (j ^ a[i]));
}
if (sum > max) {
max = sum;
X = j;
}
}
printf ( "%lli" , X);
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int N = 6, K = 10;
findValue(a, N, K);
return 0;
}
|
Java
class GFG {
public static void findValue( int [] a, int N, int K) {
int max = 0 , X = - 1 ;
for ( int j = 0 ; j <= K; j++) {
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum = sum + (j ^ a[i]);
}
if (sum > max) {
max = sum;
X = j;
}
}
System.out.println(X);
}
public static void main(String args[])
{
int [] a = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = 6 , K = 10 ;
findValue(a, N, K);
}
}
|
Python3
def findValue(a, N, K) :
max = 0 ; X = - 1 ;
for j in range ( K + 1 ) :
sum = 0 ;
for i in range (N) :
sum = sum + (j ^ a[i]);
if ( sum > max ) :
max = sum ;
X = j;
print (X);
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 , 4 , 5 , 6 ];
N = 6 ; K = 10 ;
findValue(a, N, K);
|
C#
using System;
public class GFG {
public static void findValue( int [] a, int N, int K) {
int max = 0, X = -1;
for ( int j = 0; j <= K; j++) {
int sum = 0;
for ( int i = 0; i < N; i++) {
sum = sum + (j ^ a[i]);
}
if (sum > max) {
max = sum;
X = j;
}
}
Console.WriteLine(X);
}
public static void Main( string []args)
{
int [] a = { 1, 2, 3, 4, 5, 6 };
int N = 6, K = 10;
findValue(a, N, K);
}
}
|
Javascript
<script>
function findValue(a, N, K) {
let max = 0, X = -1;
for (let j = 0; j <= K; j++) {
let sum = 0;
for (let i = 0; i < N; i++) {
(sum = sum + (j ^ a[i]));
}
if (sum > max) {
max = sum;
X = j;
}
}
document.write(X);
}
let a = [1, 2, 3, 4, 5, 6];
let N = 6, K = 10;
findValue(a, N, K);
</script>
|
Time Complexity: O(K*N)
Auxiliary Space: O(1)