Computer Science, asked by sushmapatnaik0912, 7 months ago



Given two numbers X and Y. Find the value of pair (P,Q) such
that X <= P <Q<= Y and a logical conjunction value of P,Q is
maximum.
Input:
First line of input contains number of test cases T. Each test case
contains two numbers X and Y.
Output:
For each test case print the value of maximum AND.
Constraints:
1<=G=1000​

Answers

Answered by dreamrob
5

Program in C++:

#include<iostream>

#include<math.h>

using namespace std;

int main()

{

int G;

cout<<"Enter total number of test cases : ";

cin>>G;

if(G < 1 || G > 1000)

{

 cout<<"Invalid Input.";

 return 0;

}

for(int i = 1 ; i <= G ; i++)

{

 int X, Y;

 cout<<"\nEnter first number : ";

 cin>>X;

 cout<<"Enter second number : ";

 cin>>Y;

 if(X<1 || Y>(int)pow(10,18) || X>=Y)

 {

  cout<<"Invalid Input";

  return 0;

 }

 int Max = 0;

 for(int i = X ; i <= Y ; i++)

 {

  for(int j = i+1 ; j <= Y ; j++)

  {

   if((i&j)>Max && i!=j)

   {

    Max = i&j;

   }

  }

 }

 cout<<"Value of maximum AND = "<<Max<<endl;

}

return 0;

}

Output:

Enter total number of test cases : 2

Enter first number : 2

Enter second number : 3

Value of maximum AND = 2

Enter first number : 4

Enter second number : 8

Value of maximum AND = 6

Similar questions