Maximize the largest number K such that bitwise and of K till N is 0 | Java program for the above approach ; Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code
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 ) ) ; } }