Skip to content

Instantly share code, notes, and snippets.

@alinen
Created August 16, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alinen/ebbef8de8f38e419547ea56fa4eff1b8 to your computer and use it in GitHub Desktop.
Save alinen/ebbef8de8f38e419547ea56fa4eff1b8 to your computer and use it in GitHub Desktop.
// Question from hackerrank
// Solution by Isla, Joe, and Lyn
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N = 0;
cin >> N;
for (int i = 0; i < N; i++)
{
long a,b;
cin >> a;
cin >> b;
long delta = b - a;
long sequenceLen = log2(delta)+1;
long aShift = a >> sequenceLen;
long bShift = b >> sequenceLen;
long v = aShift;
for (long j = aShift+1; j <= bShift; j++)
{
v = v & j;
}
v = v << sequenceLen;
cout << v << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment