Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same

Given a range [L, R], the task is to find a pair (X, Y), not necessarily distinct. Find the maximum possible value of the bitwise AND of the chosen integers.
Examples:Â
Â
Input: L = 3, R = 7Â
Output: 7Â
Explanation:Â
In all the possible pairs, pair (7, 7) gives the maximum value for bitwise AND.
Input: L = 54, R = 55Â
Output: 55Â
Explanation:Â
In all the possible pairs, pair (55, 55) gives the maximum value for bitwise AND.Â
Â
Â
Naive Approach: To solve the problem mentioned above the naive method is to iterate from L to R and check the bitwise AND for every possible pair and print the maximum value in the end.
Time Complexity: O(N2)
Efficient Approach:
To optimize the above method we have to observe that here we have to integers L and R and we have to select two integers from the interval [L, R] so that their bitwise AND should be maximum. Bitwise AND of any two numbers between L and R will be always less than or equal to R only. So if we have to select two integers from the interval, we can choose the integers to be R and that’s the only way to maximize the bitwise AND.
Below is the implementation of above approach:
Â
C
// C implementation to find the// Maximum Bitwise AND pair (X, Y)// from given range such that// X and Y can be sameÂ
#include <stdio.h>Â
// Function to return the// maximum bitwise ANDint maximumAND(int L, int R){Â Â Â Â return R;}Â
// Driver codeint main(){Â Â Â Â int l = 3;Â Â Â Â int r = 7;Â
    printf("%d", maximumAND(l, r));Â
    return 0;} |
C++
// C++ implementation to find the// Maximum Bitwise AND pair (X, Y)// from given range such that// X and Y can be sameÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to return the// maximum bitwise ANDint maximumAND(int L, int R){Â Â Â Â return R;}Â
// Driver codeint main(){Â Â Â Â int l = 3;Â Â Â Â int r = 7;Â
    cout << maximumAND(l, r);Â
    return 0;} |
Java
// Java implementation to find the// Maximum Bitwise AND pair (X, Y)// from given range such that// X and Y can be sameÂ
class GFG {Â
    // Function to return the    // maximum bitwise AND    static int maximumAND(int L, int R)    {        return R;    }Â
    // Driver code    public static void main(String[] args)    {        int l = 3;        int r = 7;        System.out.print(maximumAND(l, r));    }} |
Python3
# Python3 implementation to find the # Maximum Bitwise AND pair (X, Y)# from given range such that# X and Y can be sameÂ
# Function to return the# maximum bitwise AND def maximumAND(L, R):Â Â Â Â return RÂ
# Driver code if __name__ == '__main__': Â Â Â Â l = 3Â Â Â Â r = 7Â Â Â Â print(maximumAND(l, r)) |
C#
// C# implementation to find the// maximum Bitwise AND pair (X, Y)// from given range such that// X and Y can be sameusing System;Â
class GFG{Â
// Function to return the// maximum bitwise ANDstatic int maximumAND(int L, int R){Â Â Â Â return R;}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int l = 3;Â Â Â Â int r = 7;Â Â Â Â Â Â Â Â Â Console.Write(maximumAND(l, r));}}Â
// This code is contributed by amal kumar choubey |
Javascript
<script>Â
// JavaScript implementation to find the// Maximum Bitwise AND pair (X, Y)// from given range such that// X and Y can be sameÂ
// Function to return the    // maximum bitwise AND    function maximumAND(L, R)    {        return R;    }Â
// Driver CodeÂ
        let l = 3;        let r = 7;        document.write(maximumAND(l, r));Â
</script> |
7
Â
Time Complexity: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



