**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.

**Task**

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.

**Submit Your Solution Here:-**Click Here

### 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;

}

✱

**Now You Can Download a Full Code Click Here**

__New Feature:-__**How to Download**

1. After Clicking

**Click here**it will jump to the new page.

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

3. Now click Download Button your File will be Download.

**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

Sir bahut hi informative website hai Apki, aur apke dwara bataye Gaye hackerrank ke sabhi solutions work Kar rahe hai , mai asha karta hu ki aap yese hi madad kare, dhanyabad

ReplyDeleteHi, Vikram

DeleteDhanyabad Aapko Meri Post Bahut Achhi lagi. Visit Karte Rahiye Aur Like, Share, Subscribe Bhi Kar Lijiye.

Looking For RRB Mumbai Group D Result? Look no more. Here, we are, providing the all RRB Result, may it be Group D, ALP, and RPF right, at one place at railwayresult.in. So, keep yourself updated with RRB Result.

ReplyDeleteHere is the efficient solution:

ReplyDeletevoid find_pair_with_max_val(int n, int k)

{

int max_val = 0;

int a = 0, b = k - 1;

for (a = n; a > 2; a--)

{

if (a == b) continue;

if ((a & b) > max_val)

{

max_val = a & b;

}

}

cout << max_val << endl;

}