Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Maximizing expressions problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Maximizing expressions problem solution You are given three arrays A, B, and C. All the three arrays consist of N integers.
 
You must perform the following operation on each index exactly one time:
 
Convert(i): Select a positive value D such that Ci & D = D, and update Bi = Bi xor D.
 
Here, xor and & denotes the bitwise XOR operation and bitwise AND operation.
 
You are required to maximize the following expression:
 
S = sigma(n, i = 1) Ai xor Bi
 
 
HackerEarth Maximizing expressions problem solution

 

 

HackerEarth Maximizing expressions problem solution.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100009;
ll n, m, x, y, z;

ll ara1[maxn], ara2[maxn], ara3[maxn];

bool getBit(ll val, ll pos)
{
return (bool)(val & (1LL << pos));
}

int main()
{

cin >> n;
for(ll i = 1; i <= n; i++) scanf("%lld", &ara1[i]);
for(ll i = 1; i <= n; i++) scanf("%lld", &ara2[i]);
for(ll i = 1; i <= n; i++) scanf("%lld", &ara3[i]);
ll anss = 0;

for(ll i = 1; i <= n; i++){

ll tmp = 0;
bool flg = false;

for(ll j = 0; j < 30; j++){
ll mx1 = getBit(ara1[i], j) ^ getBit(ara2[i], j);
ll mx2 = getBit(ara1[i], j) ^ getBit(ara2[i], j) ^ getBit(ara3[i], j);
tmp += (1LL << j) * max(mx1, mx2);
if(mx2 && getBit(ara3[i], j)) flg = true;
}

if(!flg){
ll lstSetbit = 0;
while(getBit(ara3[i], lstSetbit) == 0) lstSetbit++;
if(getBit(tmp, lstSetbit) == 0) assert(false);

tmp -= (1LL << lstSetbit);
}

anss += tmp;

}

cout << anss << endl;

return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes