Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Maximizing expressions problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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