Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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

HackerEarth A XOR challenge problem solution

YASH PAL, 31 July 2024
In this HackerEearth Natural XOR challenge problem solution You are given an integer C such that the XOR of two integers (A, B) is C. In short A ⊕ B = C (⊕ denotes the bitwise XOR operation).
Out of all possible pairs of A and B, you must find two integers such that their product is maximum. 
Let us define L(A) as the length of A in its binary representation. Then, L(A) <= L(C) and L(B) <= L(C).
HackerEarth A XOR challenge problem solution

HackerEarth A XOR challenge problem solution.

#include<bits/stdc++.h>
#include<climits>
using namespace std;

#define debug(x,y) cout<<(#x)<<" " <<(#y)<<" is " << (x) <<" "<< (y) << endl
#define watch(x) cout<<(#x)<<" is " << (x) << endl
#define fast ios_base::sync_with_stdio(false)
#define fie(i,a,b) for(i=a;i<b;i++)
#define MOD 1000000007
#define mod 998244353
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ll long long
#define lld long long int
#define ALL(x) (x).begin(),(x).end()

typedef vector<lld> vi;
typedef vector<vector<lld>> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<pair<lld, lld>> vpi;
typedef long long LL;

string convert(lld n) {
string s = "";
while (n > 0) {
if (n % 2 == 1) s = "1" + s;
else s = "0" + s;
n /= 2;
}
return s;
}


lld convertBack(string s) {
lld n = 0 , p = 1;
for (lld i = s.length() - 1; i >= 0; i--) {
n += ((s[i] - '0') * p);
p *= 2;
}
return n;
}

int main() {
fast;
cin.tie(0);
lld n, i;
cin >> n;
string s = convert(n);
string a = "" , b = "";
bool first = false;
for (i = 0; i < s.length(); i++) {
if (s[i] == '0') {
a += "1";
b += "1";
}
else {
if (first) {
a += "0";
b += "1";
}
else {
a += "1";
b += "0";
first = true;
}
}
}
lld n1 = convertBack(a);
lld n2 = convertBack(b);
cout << n1*n2 << endl;
}

Second solution

c = int(input())
ans = 0
for a in range(c + 1):
ans = max(ans, a * (a ^ c))
print(ans)
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes