codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of sheets possible by repeatedly reducing its area to half | Java program for the above approach ; Function to calculate the maximum number of sheets possible by given operations ; Initial count of sheets ; Keep dividing the sheets into half ; Reduce area by half ; Increase count by twice ; Driver Code
import java . util . * ; class GFG { static int maxSheets ( int A , int B ) { int area = A * B ; int count = 1 ; while ( area % 2 == 0 ) { area /= 2 ; count *= 2 ; } return count ; } public static void main ( String args [ ] ) { int A = 5 , B = 10 ; System . out . println ( maxSheets ( A , B ) ) ; } }
Minimum number of steps required to reach origin from a given point | Java program for the above approach ; Function to find the minimum moves required to reach origin from ( a , b ) ; Stores the minimum number of moves ; Check if the absolute difference is 1 or 0 ; Store the minimum of a , b ; Store the maximum of a , b ; Print the answer ; Driver Code ; Given co - ordinates ; Function Call
import java . io . * ; class GFG { static void findMinMoves ( int a , int b ) { int ans = 0 ; if ( a == b || Math . abs ( a - b ) == 1 ) { ans = a + b ; } else { int k = Math . min ( a , b ) ; int j = Math . max ( a , b ) ; ans = 2 * k + 2 * ( j - k ) - 1 ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int a = 3 , b = 5 ; findMinMoves ( a , b ) ; } }
Count even sum pairs possible by selecting two integers from two given ranges respectively | Java program to implement the above approach ; Function to count maximum even sum pairs in the given range ; Stores the count of even numbers between 1 to X ; Stores the count of odd numbers between 1 to X ; Stores the count of even numbers between 1 to Y ; Stores the count of odd numbers between 1 to Y ; Stores the count of pairs having even sum ; Retuens the count of pairs having even sum ; Driver Code
import java . io . * ; class GFG { static long cntEvenSumPairs ( long X , long Y ) { long cntXEvenNums = X / 2 ; long cntXOddNums = ( X + 1 ) / 2 ; long cntYEvenNums = Y / 2 ; long cntYOddNums = ( Y + 1 ) / 2 ; long cntPairs = ( cntXEvenNums * cntYEvenNums ) + ( cntXOddNums * cntYOddNums ) ; return cntPairs ; } public static void main ( String [ ] args ) { long X = 2 ; long Y = 3 ; System . out . println ( cntEvenSumPairs ( X , Y ) ) ; } }
Minimize array elements required to be incremented or decremented to convert given array into a Fibonacci Series | Java program for the above approach ; Function to calculate minimum number of moves to make the sequence a Fibonacci series ; If number of elements is less than 3 ; Initialize the value of the result ; Try all permutations of the first two elements ; Value of first element after operation ; Value of second element after operation ; Calculate number of moves for rest of the elements of the array ; Element at idx index ; If it is not possible to change the element in atmost one move ; Otherwise ; Update the answer ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int minMoves ( int [ ] arr ) { int N = arr . length ; if ( N <= 2 ) return 0 ; int ans = Integer . MAX_VALUE ; for ( int i = - 1 ; i <= 1 ; i ++ ) { for ( int j = - 1 ; j <= 1 ; j ++ ) { int num1 = arr [ 0 ] + i ; int num2 = arr [ 1 ] + j ; int flag = 1 ; int moves = Math . abs ( i ) + Math . abs ( j ) ; for ( int idx = 2 ; idx < N ; idx ++ ) { int num = num1 + num2 ; if ( Math . abs ( arr [ idx ] - num ) > 1 ) flag = 0 ; else moves += Math . abs ( arr [ idx ] - num ) ; num1 = num2 ; num2 = num ; } if ( flag > 0 ) ans = Math . min ( ans , moves ) ; } } if ( ans == Integer . MAX_VALUE ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 9 , 17 , 27 } ; System . out . print ( minMoves ( arr ) ) ; } }
Queries to calculate sum of array elements present at every Yth index starting from the index X | Java program for the above approach ; Function to Find the sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... for all queries ; Iterate over each query ; Stores the sum of arr [ x ] + arr [ x + y ] + arr [ x + 2 * y ] + ... ; Traverse the array and calculate the sum of the expression ; Update sum ; Update x ; Driver Code
import java . util . * ; class GFG { static void querySum ( int arr [ ] , int N , int Q [ ] [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int x = Q [ i ] [ 0 ] ; int y = Q [ i ] [ 1 ] ; int sum = 0 ; while ( x < N ) { sum += arr [ x ] ; x += y ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 7 , 5 , 4 } ; int Q [ ] [ ] = { { 2 , 1 } , { 3 , 2 } } ; int N = arr . length ; int M = Q . length ; querySum ( arr , N , Q , M ) ; } }
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values | Java program to implement the above approach ; Function to calculate Bitwise OR from given bitwise XOR and bitwise AND values ; Driver Code
class GFG { static int findBitwiseORGivenXORAND ( int X , int Y ) { return X + Y ; } public static void main ( String [ ] args ) { int X = 5 , Y = 2 ; System . out . print ( findBitwiseORGivenXORAND ( X , Y ) ) ; } }
Check if a given value can be reached from another value in a Circular Queue by K | Java program for the above approach ; Function to return GCD of two numbers a and b ; Base Case ; Recursively Find the GCD ; Function to check of B can be reaced from A with a jump of K elements in the circular queue ; Find GCD of N and K ; If A - B is divisible by gcd then print Yes ; Otherwise ; Driver Code ; Function Call
import java . util . * ; class solution { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void canReach ( int N , int A , int B , int K ) { int gcd = GCD ( N , K ) ; if ( Math . abs ( A - B ) % gcd == 0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 5 , A = 2 , B = 1 , K = 2 ; canReach ( N , A , B , K ) ; } }
Count of subarrays having sum equal to its length | Set 2 | Java program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Store count of elements upto current element with length i ; Stores the final count of subarray ; Stores the prefix sum ; If size of subarray is 1 ; Iterate the array ; Find the sum ; Update frequency in map ; Print the total count ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static void countOfSubarray ( int arr [ ] , int N ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int answer = 0 ; int sum = 0 ; if ( mp . get ( 1 ) != null ) mp . put ( 1 , mp . get ( 1 ) + 1 ) ; else mp . put ( 1 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( mp . get ( sum - i ) != null ) answer += mp . get ( sum - i ) ; if ( mp . get ( sum - i ) != null ) mp . put ( sum - i , mp . get ( sum - i ) + 1 ) ; else mp . put ( sum - i , 1 ) ; } System . out . print ( answer ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 2 , 1 , 2 , - 2 , 2 , 4 } ; int N = arr . length ; countOfSubarray ( arr , N ) ; } }
Split first N natural numbers into two sets with minimum absolute difference of their sums | Java program to implement the above approach ; Function to split the first N natural numbers into two sets having minimum absolute difference of their sums ; Stores the sum of elements of set1 ; Stores the sum of elements of set2 ; Traverse first N natural numbers ; Check if sum of elements of set1 is less than or equal to sum of elements of set2 ; Driver code
import java . io . * ; class GFG { static int minAbsDiff ( int N ) { int sumSet1 = 0 ; int sumSet2 = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( sumSet1 <= sumSet2 ) { sumSet1 += i ; } else { sumSet2 += i ; } } return Math . abs ( sumSet1 - sumSet2 ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minAbsDiff ( N ) ) ; } }
Check if a number is prime in Flipped Upside Down , Mirror Flipped and Mirror Flipped Upside Down | Java program to implement the above approach ; Function to check if N contains digits 0 , 1 , 2 , 5 , 8 only ; Extract digits of N ; Return false if any of these digits are present ; Function to check if N is prime or not ; Check for all factors ; Function to check if n is prime in all the desired forms ; Driver Code
import java . util . * ; class GFG { static boolean checkDigits ( int n ) { do { int r = n % 10 ; if ( r == 3 r == 4 r == 6 r == 7 r == 9 ) return false ; n /= 10 ; } while ( n != 0 ) ; return true ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isAllPrime ( int n ) { return isPrime ( n ) && checkDigits ( n ) ; } public static void main ( String [ ] args ) { int N = 101 ; if ( isAllPrime ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimum Cost required to generate a balanced Bracket Sequence | Java program to implement the above approach ; Function to calculate the minimum cost required to generate a balanced bracket sequence ; Stores the count of unbalanced open brackets ; Stores the count of unbalanced closed brackets ; Stores the count of open brackets ; Stores the count of closed brackets ; If open brace is encountered ; Otherwise ; If no unbalanced open brackets are present ; Increase count of unbalanced closed brackets ; Otherwise ; Reduce count of unbalanced open brackets ; Increase count of closed brackets ; Calculate lower bound of minimum cost ; Reduce excess open or closed brackets to prevent counting them twice ; Update answer by adding minimum of removing both unbalanced open and closed brackets or inserting closed unbalanced brackets to end of String ; Print the result ; Driver Code
import java . util . * ; class GFG { static void minCost ( String str , int a , int b ) { int openUnbalanced = 0 ; int closedUnbalanced = 0 ; int openCount = 0 ; int closedCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { openUnbalanced ++ ; openCount ++ ; } else { if ( openUnbalanced == 0 ) closedUnbalanced ++ ; else openUnbalanced -- ; closedCount ++ ; } } int result = a * ( Math . abs ( openCount - closedCount ) ) ; if ( closedCount > openCount ) closedUnbalanced -= ( closedCount - openCount ) ; if ( openCount > closedCount ) openUnbalanced -= ( openCount - closedCount ) ; result += Math . min ( a * ( openUnbalanced + closedUnbalanced ) , b * closedUnbalanced ) ; System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " ) ) ( ) ( ( ) ( ) ( " ; int A = 1 , B = 3 ; minCost ( str , A , B ) ; } }
Count of permutations such that sum of K numbers from given range is even | Java program for the above approach ; Function to return the number of all permutations such that sum of K numbers in range is even ; Find total count of even and odd number in given range ; Iterate loop k times and update even_sum & odd_sum using previous values ; Update the prev_even and odd_sum ; Even sum ; Odd sum ; Return even_sum ; Driver Code ; Given ranges ; Length of permutation ; Function call
import java . util . * ; class GFG { public static void countEvenSum ( int low , int high , int k ) { int even_count = high / 2 - ( low - 1 ) / 2 ; int odd_count = ( high + 1 ) / 2 - low / 2 ; long even_sum = 1 ; long odd_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { long prev_even = even_sum ; long prev_odd = odd_sum ; even_sum = ( prev_even * even_count ) + ( prev_odd * odd_count ) ; odd_sum = ( prev_even * odd_count ) + ( prev_odd * even_count ) ; } System . out . println ( even_sum ) ; } public static void main ( String [ ] args ) { int low = 4 ; int high = 5 ; int K = 3 ; countEvenSum ( low , high , K ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2 | Java Program to implement the above approach ; Function to count the number of N - digit numbers such that sum of every K consecutive digits are equal ; Print the answer ; Driver Code
class GFG { public static void count ( int n , int k ) { long count = ( long ) ( Math . pow ( 10 , k ) - Math . pow ( 10 , k - 1 ) ) ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 2 , k = 1 ; count ( n , k ) ; } }
Sum of largest divisor of numbers upto N not divisible by given prime number P | Java program for the above approach ; Function to find the sum of largest divisors of numbers in range 1 to N not divisible by prime number P ; Total sum upto N ; If no multiple of P exist up to N ; If only P itself is in the range from 1 to N ; Sum of those that are divisible by P ; Recursively function call to find the sum for N / P ; Driver Code ; Given N and P ; Function call
class GFG { static int func ( int N , int P ) { int sumUptoN = ( N * ( N + 1 ) / 2 ) ; int sumOfMultiplesOfP ; if ( N < P ) { return sumUptoN ; } else if ( ( N / P ) == 1 ) { return sumUptoN - P + 1 ; } sumOfMultiplesOfP = ( ( N / P ) * ( 2 * P + ( N / P - 1 ) * P ) ) / 2 ; return ( sumUptoN + func ( N / P , P ) - sumOfMultiplesOfP ) ; } public static void main ( String [ ] args ) { int N = 10 , P = 5 ; System . out . println ( func ( N , P ) ) ; } }
Count of right shifts for each array element to be in its sorted position | Java program to implement the approach ; Function to find the right shifts required for each element to reach its sorted array position in A [ ] ; Stores required number of shifts for each element ; If the element is at sorted position ; Otherwise ; Calculate right shift ; Print the respective shifts ; Driver code
class GFG { public static void findShifts ( int [ ] A , int N ) { int [ ] shift = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == A [ i ] - 1 ) shift [ i ] = 0 ; else shift [ i ] = ( A [ i ] - 1 - i + N ) % N ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( shift [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 2 , 5 } ; int N = arr . length ; findShifts ( arr , N ) ; } }
Construct a matrix with sum equal to the sum of diagonal elements | Java program to implement the above approach ; Function to construct matrix with diagonal sum equal to matrix sum ; If diagonal position ; Positive element ; Negative element ; Driver Code
public class Main { public static void constructmatrix ( int N ) { boolean check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { System . out . print ( "1 ▁ " ) ; } else if ( check ) { System . out . print ( "2 ▁ " ) ; check = false ; } else { System . out . print ( " - 2 ▁ " ) ; check = true ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 5 ; constructmatrix ( 5 ) ; } }
Minimum count of numbers required with unit digit X that sums up to N | Java Program to implement the above approach ; Function to calculate and return the minimum number of times a number with unit digit X needs to be added to get a sum N ; Calculate the number of additions required to get unit digit of N ; If unit digit of N cannot be obtained ; Function to return the minimum number required to represent N ; Stores unit digit of N ; Stores minimum addition of X required to obtain unit digit of N ; If unit digit of N cannot be obtained ; Otherwise ; If N is greater than or equal to ( X * times ) ; Minimum count of numbers that needed to represent N ; Representation not possible ; Driver Code
class GFG { static int check ( int unit_digit , int X ) { int times , digit ; for ( times = 1 ; times <= 10 ; times ++ ) { digit = ( X * times ) % 10 ; if ( digit == unit_digit ) return times ; } return - 1 ; } static int getNum ( int N , int X ) { int unit_digit ; unit_digit = N % 10 ; int times = check ( unit_digit , X ) ; if ( times == - 1 ) return times ; else { if ( N >= ( times * X ) ) return times ; else return - 1 ; } } public static void main ( String [ ] args ) { int N = 58 , X = 7 ; System . out . println ( getNum ( N , X ) ) ; } }
Minimum number of points required to cover all blocks of a 2 | Java program for the above approach ; Function to find the minimum number of Points required to cover a grid ; If number of block is even ; Return the minimum points ; Driver Code ; Given size of grid ; Function Call
class GFG { static int minPoints ( int n , int m ) { int ans = 0 ; if ( ( n % 2 != 0 ) && ( m % 2 != 0 ) ) { ans = ( ( n * m ) / 2 ) + 1 ; } else { ans = ( n * m ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 , M = 7 ; System . out . print ( minPoints ( N , M ) ) ; } }
Largest lexicographical string with at most K consecutive elements | Java code for the above approach ; Function to find the largest lexicographical String with given constraints . ; Vector containing frequency of each character . ; Assigning frequency ; Empty String of String class type ; Loop to iterate over maximum priority first . ; If frequency is greater than or equal to k . ; Temporary variable to operate in - place of k . ; Concatenating with the resultant String ans . ; Handling k case by adjusting with just smaller priority element . ; Condition to verify if index j does have frequency greater than 0 ; ; If no such element is found than String can not be processed further . ; If frequency is greater than 0 and less than k . ; Here we don 't need to fix K  consecutive element criteria. ; Otherwise check for next possible element . ; Driver code
import java . util . * ; class GFG { static String getLargestString ( String s , int k ) { int [ ] frequency_array = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { frequency_array [ s . charAt ( i ) - ' a ' ] ++ ; } String ans = " " ; for ( int i = 25 ; i >= 0 { if ( frequency_array [ i ] > k ) { int temp = k ; String st = String . valueOf ( ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } frequency_array [ i ] -= k ; int j = i - 1 ; while ( frequency_array [ j ] <= 0 && j >= 0 ) { j -- ; } if ( frequency_array [ j ] > 0 && j >= 0 ) { String str = String . valueOf ( ( char ) ( j + ' a ' ) ) ; ans += str ; frequency_array [ j ] -= 1 ; } else { break ; } } else if ( frequency_array [ i ] > 0 ) { int temp = frequency_array [ i ] ; frequency_array [ i ] -= temp ; String st = String . valueOf ( ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } } else { i -- ; } } return ans ; } public static void main ( String [ ] args ) { String S = " xxxxzza " ; int k = 3 ; System . out . print ( getLargestString ( S , k ) ) ; } }
Minimum operations to make all elements equal using the second array | Java implementation to find the minimum operations make all elements equal using the second array ; Function to find the minimum operations required to make all elements of the array equal ; Minimum element of A [ ] ; Traverse through all final values ; Variable indicating whether all elements can be converted to x or not ; Total operations ; Traverse through all array elements ; All elements can 't  be converted to x ; Driver Code
import java . util . * ; class GFG { static int minOperations ( int a [ ] , int b [ ] , int n ) { int minA = Arrays . stream ( a ) . min ( ) . getAsInt ( ) ; for ( int x = minA ; x >= 0 ; x -- ) { boolean check = true ; int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( x % b [ i ] == a [ i ] % b [ i ] ) { operations += ( a [ i ] - x ) / b [ i ] ; } else { check = false ; break ; } } if ( check ) return operations ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 5 ; int A [ ] = { 5 , 7 , 10 , 5 , 15 } ; int B [ ] = { 2 , 2 , 1 , 3 , 5 } ; System . out . print ( minOperations ( A , B , N ) ) ; } }
Find the maximum sum ( a + b ) for a given input integer N satisfying the given condition | Java implementation to find the largest value of a + b satisfying the given condition ; Function to return the maximum sum of a + b satisfying the given condition ; Initialize max_sum ; Consider all the possible pairs ; Check if the product is divisible by the sum ; Storing the maximum sum in the max_sum variable ; Return the max_sum value ; Driver code
import java . util . * ; class GFG { static int getLargestSum ( int N ) { int max_sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { if ( i * j % ( i + j ) == 0 ) max_sum = Math . max ( max_sum , i + j ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int N = 25 ; int max_sum = getLargestSum ( N ) ; System . out . print ( max_sum ) ; } }
Maximize the sum of array after multiplying a prefix and suffix by | Java implementation to find the maximum sum of the array by multiplying the prefix and suffix of the array by - 1 ; Kadane 's algorithm to find  the maximum subarray sum ; Loop to find the maximum subarray array sum in the given array ; Function to find the maximum sum of the array by multiplying the prefix and suffix by - 1 ; Total intital sum ; Loop to find the maximum sum of the array ; Maximum value ; Driver Code
class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } static int maxSum ( int a [ ] , int n ) { int S = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) S += a [ i ] ; int X = maxSubArraySum ( a , n ) ; return 2 * X - S ; } public static void main ( String [ ] args ) { int a [ ] = { - 1 , - 2 , - 3 } ; int n = a . length ; int max_sum = maxSum ( a , n ) ; System . out . print ( max_sum ) ; } }
Count of interesting primes upto N | Java program to find the number of interesting primes up to N ; Function to check if a number is prime or not ; If n is divisible by any number between 2 and Math . sqrt ( n ) , it is not prime ; Function to check if a number is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find the number of interesting primes less than equal to N . ; Check whether the number is prime or not ; Iterate for values of b ; Check condition for a ; Return the required answer ; Driver code
class GFG { static boolean isPrime ( int n ) { int flag = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { flag = 0 ; break ; } } return ( flag == 1 ? true : false ) ; } static boolean isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static int countInterestingPrimes ( int n ) { int answer = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) { for ( int j = 1 ; j * j * j * j <= i ; j ++ ) { if ( isPerfectSquare ( i - j * j * j * j ) ) { answer ++ ; break ; } } } } return answer ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( countInterestingPrimes ( N ) ) ; } }
Maximize the number by flipping at most K bits | Java implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array arr [ ] ; Function to convert the number represented as a binary array arr [ ] into its decimal equivalent ; Function to return the maximized number by flipping atmost k bits ; Number of bits in n ; Find the binary representation of n ; To count the number of 0 s flipped ; Return the decimal equivalent of the maximized number ; Driver code
class GFG { static void decBinary ( int arr [ ] , int n ) { int k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } static int binaryDec ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } static int maxNum ( int n , int k ) { int l = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; int a [ ] = new int [ l ] ; decBinary ( a , n ) ; int cn = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] == 0 && cn < k ) { a [ i ] = 1 ; cn ++ ; } } return binaryDec ( a , l ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 1 ; System . out . println ( maxNum ( n , k ) ) ; } }