Maximize the largest number K such that bitwise and of K till N is 0
Last Updated :
14 Sep, 2021
Given an integer N, the task is to find the maximum value of K such that N & (N-1) & (N-2) & … & (K) = 0. Here & represents bitwise AND operator.
Example:
Input: N = 5
Output: 3
Explanation: The value of the expression 5 & 4 & 3 = 0. any value greater than 3 (example 4) will not satisfy
our given condition
Input: N =17
Output: 15
Naive approach: The brute force approach is to start from N-1 and perform bitwise AND operation till we get 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxK( int N)
{
int K = N;
for ( int i = N - 1; i >= 0; i--) {
K &= i;
if (K == 0) {
return i;
}
}
return 0;
}
int main()
{
int N = 5;
cout << findMaxK(N);
}
|
Java
import java.io.*;
class GFG {
static int findMaxK( int N)
{
int K = N;
for ( int i = N - 1 ; i >= 0 ; i--) {
K &= i;
if (K == 0 ) {
return i;
}
}
return 0 ;
}
public static void main (String[] args) {
int N = 5 ;
System.out.println(findMaxK(N));
}
}
|
Python3
def findMaxK(N):
K = N
i = N - 1
while (i > = 0 ):
K & = i
if (K = = 0 ):
return i
i - = 1
return 0
if __name__ = = '__main__' :
N = 5
print (findMaxK(N))
|
C#
using System;
class GFG
{
static int findMaxK( int N)
{
int K = N;
for ( int i = N - 1; i >= 0; i--) {
K &= i;
if (K == 0) {
return i;
}
}
return 0;
}
public static void Main (String[] args)
{
int N = 5;
Console.Write(findMaxK(N));
}
}
|
Javascript
<script>
function findMaxK(N)
{
let K = N;
for (let i = N - 1; i >= 0; i--) {
K &= i;
if (K == 0) {
return i;
}
}
return 0;
}
let N = 5;
document.write(findMaxK(N));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Efficient approach: By some observation, it can be seen that the answer is always equal to the highest power of 2, which is less than or equal to (N-1). So finally, the answer is always equal to 2^K -1, where K is some value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxK( int N)
{
int p = log2(N);
return pow (2, p);
}
int main()
{
int N = 5;
cout << findMaxK(N) - 1 << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMaxK( int N)
{
int p = ( int )(Math.log(N) / Math.log( 2 ));
return ( int )Math.pow( 2 , p);
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(findMaxK(N) - 1 );
}
}
|
Python3
import math
def findMaxK(N):
p = math.log(N) / / math.log( 2 );
return int ( pow ( 2 , p));
N = 5 ;
print (findMaxK(N) - 1 );
|
C#
using System;
class GFG
{
static int findMaxK( int N)
{
int p = ( int )(Math.Log(N) / Math.Log(2));
return ( int )Math.Pow(2, p);
}
public static void Main(String[] args)
{
int N = 5;
Console.Write(findMaxK(N) - 1);
}
}
|
Javascript
<script>
function findMaxK(N)
{
var p = Math.log(N) / Math.log(2);
return parseInt(Math.pow(2, p));
}
var N = 5;
document.write(findMaxK(N) - 1);
</script>
|
Time complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...