## Friday, 24 November 2017

### HackerRank Solution For Day 29: Bitwise AND

Problem:- hacker rank-30-Days-of-Code/Day-29-Bitwise-AND or Day 29: Bitwise AND Discussion | 30 Days of Code or Day 29: Bitwise AND or Day 29 Bitwise AND! hacker rank or Hackerrank Day 29: Bitwise AND or Hackerrank Day 29: Bitwise AND solution or Hacker rank, 30 Days of Code Challenges or bitwise and hacker rank or day 29 hacker rank or bitwise and hacker rank solution or 30 days of code Java or how to code in hacker rank or bitwise and hacker rank solution.

Given set S = {1, 2, 3, . . . . , N}. Find two integers, A and B (where A<B), from set S such that the value of A&B is the maximum possible and also less than a given integer, K. In this case, & represents the bitwise AND operator.

Basic Operators

Here are some commonly used Java operators you should familiarize yourself with & Bitwise AND (^). This binary operation evaluates to 1 (true) if both operands are true, otherwise 0 (false). In other words:

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

| Bitwise Inclusive OR (V). This binary operation evaluates to 1 if either operand is true, otherwise 0 (false) if both operands are false. In other words:

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

^ Bitwise Exclusive OR or XOR (⊕). This binary operation evaluates to 1 (true) if and only if exactly one of the two operands is 1; if both operands are 1 or 0, it evaluates to 0 (false). In other words:

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

~ The unary Bitwise Complement operator flips every bit; for example, the bitwise-inverted 8-bit binary number 01111001 becomes 10000110, and the bitwise-inverted signed decimal integer 8 becomes -9.

Explanation:- As we can see above in Bitwise AND if  1 and 1 then the only condition is true. In this problem, we are taking two input from the user first one in number N and second in K. Now we have to find the all set of number S = {1, 2, 3, . . . . , N}. Let's take an example and try to understand the problem "Bitwise AND" easily. suppose N=5 and K=2 then set S={1, 2, 3, 4, 5}. then the combination is below.

1. A = 1, B = 2; A & B = 0
2. A = 1, B = 3; A & B = 1
3. A = 1, B = 4; A & B = 0
4. A = 1, B = 5; A & B = 1
5. A = 2, B = 3; A & B = 2
6. A = 2, B = 4; A & B = 0
7. A = 2, B = 5; A & B = 0
8. A = 3, B = 4; A & B = 0
9. A = 4, B = 5; A & B = 1
10. A = 4, B = 5; A & B = 4

The maximum possible value of A & B (LINE NUMBER 5) that is also < (K = 2) is 1, so we print 1 on a new line. We continue to check A & B value is maximum and A & B value is less than or equal to the K.

### Solution:- Day 29: Bitwise AND

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int T, n, k, i, j, val;
cin>>T;

while(T>0)
{
int maximum = 0;
cin>>n>>k;

for (i=1;i<n-1;i++)
{
for (j=i+1;j<=n;j++)
{
val=i&j;

if (val>maximum&&val<k)
maximum = val;
}
}
cout<<maximum<<endl;
T--;
}
return 0;
}

2. You have to just Scroll a page until End.

Output:-

### You May Also See

1. Day 24: More Linked Lists

2. Day 25: Running Time and Complexity

3. Day 26: Nested Logic

4. Day 27: Testing

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media