codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to remove objects such that exactly M equidistant objects remain | Java program for the above approach ; Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If len > n ; Update the number of ways ; Print the result ; Driver Code
import java . io . * ; class GFG { static void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { System . out . println ( n ) ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; } }
Count unique stairs that can be reached by moving given number of steps forward or backward | Java program for the above approach ; Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; / Push the starting position ; Mark the starting position S as visited ; Iterate until queue is not empty ; Store the current stair number ; Pop it from the queue ; Check for all possible moves from the current stair ; Store the new stair number ; If it is valid and unvisited ; Push it into queue ; Mark the stair as visited ; Store the result ; Count the all visited stairs ; Print the result ; Driver Code
import java . util . LinkedList ; import java . util . Queue ; class GFG { static void countStairs ( int n , int x , int a , int b ) { int [ ] vis = new int [ n + 1 ] ; int [ ] moves = { + a , - a , + b , - b } ; Queue < Integer > q = new LinkedList < Integer > ( ) ; q . add ( x ) ; vis [ x ] = 1 ; while ( ! q . isEmpty ( ) ) { int currentStair = q . peek ( ) ; q . remove ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && vis [ newStair ] == 0 ) { q . add ( newStair ) ; vis [ newStair ] = 1 ; } } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; System . out . print ( cnt ) ; } public static void main ( String args [ ] ) { int N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ; } }
Count smaller elements present in the array for each array element | Java program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void smallerNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { count ++ ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . length ; smallerNumbers ( arr , N ) ; } }
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | Java program for the above approach ; Function to calculate GCD of two numbers ; Base Case ; Calculate GCD recursively ; Function to count the minimum number of divisions required to make X and Y equal ; Calculate GCD of X and Y ; Divide X and Y by their GCD ; Stores the number of divisions ; Iterate until X != Y ; Maintain the order X <= Y ; If X is divisible by 2 , then divide X by 2 ; If X is divisible by 3 , then divide X by 3 ; If X is divisible by 5 , then divide X by 5 ; If X is not divisible by 2 , 3 , or 5 , then print - 1 ; Increment count by 1 ; Print the value of count as the minimum number of operations ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void minimumOperations ( int X , int Y ) { int GCD = gcd ( X , Y ) ; X = X / GCD ; Y = Y / GCD ; int count = 0 ; while ( X != Y ) { if ( Y > X ) { int t = X ; X = Y ; Y = t ; } if ( X % 2 == 0 ) { X = X / 2 ; } else if ( X % 3 == 0 ) { X = X / 3 ; } else if ( X % 5 == 0 ) { X = X / 5 ; } else { System . out . print ( " - 1" ) ; return ; } count += 1 ; } System . out . println ( count ) ; } static public void main ( String args [ ] ) { int X = 15 , Y = 20 ; minimumOperations ( X , Y ) ; } }
Minimum increments or decrements required to convert a sorted array into a power sequence | Java program for the above approach ; Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; Calculate F ( x ) ; Check if X ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; Update ans to store the minimum of ans and F ( x ) ; Return the minimum number of operations required ; Driver Code
class GFG { static int minOperations ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += a [ i ] ; } ans -= n ; for ( int x = 1 ; ; x ++ ) { int curPow = 1 , curCost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curCost += Math . abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = Math . min ( ans , curCost ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 } ; int N = arr . length ; System . out . print ( minOperations ( arr , N ) ) ; } }
Modulo Operations in Programming With Negative Results | Java program to illustrate modulo operations using the above equation ; Function to calculate and return the remainder of a % n ; Type casting is necessary as ( int ) / ( int ) will give int result , i . e . - 3 / 2 will give - 1 and not - 1.5 ; Return the resultant remainder ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
public class GFG { static int floorMod ( int a , int n ) { int q = ( int ) Math . floor ( ( double ) a / n ) ; return a - n * q ; } public static void main ( String [ ] args ) { int a , b ; a = 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; } }
Find the absolute difference between the nearest powers of two given integers for every array element | Java program for the above approach ; Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ] ; Find the log a of arr [ i ] ; Find the power of a less than and greater than a ; Find the log b of arr [ i ] ; Find the power of b less than and greater than b ; Update arr [ i ] with absolute difference of log_a & log _b ; Print the modified array ; Driver Code
import java . io . * ; class GFG { static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void nearestPowerDiff ( int [ ] arr , int N , int a , int b ) { for ( int i = 0 ; i < N ; i ++ ) { int log_a = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( a ) ) ; int A = ( int ) ( Math . pow ( a , log_a ) ) ; int B = ( int ) ( Math . pow ( a , log_a + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_a = A ; else log_a = B ; int log_b = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( b ) ) ; A = ( int ) ( Math . pow ( b , log_b ) ) ; B = ( int ) ( Math . pow ( b , log_b + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_b = A ; else log_b = B ; arr [ i ] = Math . abs ( log_a - log_b ) ; } printArray ( arr , N ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 12 , 25 } ; int A = 2 , B = 3 ; int N = arr . length ; nearestPowerDiff ( arr , N , A , B ) ; } }
Count subarrays having even Bitwise OR | Java program for the above approach ; Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver Code ; Function Call
import java . util . * ; class GFG { static int bitOr ( int arr [ ] , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 2 , 6 } ; int N = arr . length ; System . out . print ( bitOr ( arr , N ) ) ; } }
Find all pairs raised to power K differs by exactly N | Java program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
import java . util . * ; class GFG { static void ValidPairs ( int X , int K ) { int count = 0 ; for ( int A = - 1000 ; A <= 1000 ; A ++ ) { for ( int B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . pow ( A , K ) - Math . pow ( B , K ) == X ) { count ++ ; System . out . println ( A + " ▁ " + B ) ; } } } if ( count == 0 ) { System . out . println ( " - 1" ) ; } } public static void main ( String args [ ] ) { int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; } }
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | Java program for the above approach ; Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
import java . util . * ; class GFG { static int maximumAND ( int arr [ ] , int n , int m ) { int tot = 1 << n ; int mx = 0 ; for ( int bm = 0 ; bm < tot ; bm ++ ) { int andans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . max ( mx , andans ) ; } } return mx ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; int M = 2 ; System . out . println ( maximumAND ( arr , N , M ) ) ; } }
Calculate sum of the array generated by given operations | Java program for the above approach ; Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the stack ; If the character is D , then push 2 * top element into stack ; If the character is + , add sum of top two elements from the stack ; Otherwise , push x and add it to ans ; Print the resultant sum ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findTotalSum ( String ops [ ] ) { if ( ops . length == 0 ) { System . out . println ( 0 ) ; return ; } Stack < Integer > pts = new Stack < > ( ) ; int ans = 0 ; for ( int i = 0 ; i < ops . length ; i ++ ) { if ( ops [ i ] == " C " ) { ans -= pts . pop ( ) ; } else if ( ops [ i ] == " D " ) { pts . push ( pts . peek ( ) * 2 ) ; ans += pts . peek ( ) ; } else if ( ops [ i ] == " + " ) { int a = pts . pop ( ) ; int b = pts . peek ( ) ; pts . push ( a ) ; ans += ( a + b ) ; pts . push ( a + b ) ; } else { int n = Integer . parseInt ( ops [ i ] ) ; ans += n ; pts . push ( n ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String arr [ ] = { "5" , " - 2" , " C " , " D " , " + " } ; findTotalSum ( arr ) ; } }
XOR of major diagonal elements of a 3D Matrix | Java program for the above approach ; Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findXOR ( int mat [ ] [ ] [ ] , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) { if ( ( i == j && j == k ) ) { XOR ^= mat [ i ] [ j ] [ k ] ; XOR ^= mat [ i ] [ j ] [ N - k - 1 ] ; } } } } System . out . println ( XOR ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] [ ] = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . length ; findXOR ( mat , N ) ; } }
XOR of major diagonal elements of a 3D Matrix | Java program for the above approach ; Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findXOR ( int mat [ ] [ ] [ ] , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XOR ^= mat [ i ] [ i ] [ i ] ; XOR ^= mat [ i ] [ i ] [ N - i - 1 ] ; } System . out . println ( XOR ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] [ ] = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . length ; findXOR ( mat , N ) ; } }
Count of subtrees possible from an N | Java program of above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the root ; Traverse the adjacency list ; Iterate over every ancestor ; Calculate product of the number of subtrees for each child node ; Update the value of ans ; Return the resultant count ; Function to count the number of subtrees in the given tree ; Initialize an adjacency matrix ; Add the edges ; Function Call to count the number of subtrees possible ; Print count of subtrees ; Driver code
import java . util . * ; class GFG { static int MAX = 300004 ; static ArrayList < ArrayList < Integer > > graph ; static long mod = ( long ) 1e9 + 7 ; static int ans = 0 ; static int countSubtreesUtil ( int cur , int par ) { int res = 1 ; for ( int i = 0 ; i < graph . get ( cur ) . size ( ) ; i ++ ) { int v = graph . get ( cur ) . get ( i ) ; if ( v == par ) continue ; res = ( int ) ( ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod ) ; } ans = ( int ) ( ( ans + res ) % mod ) ; return res ; } static void countSubtrees ( int N , int [ ] [ ] adj ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = adj [ i ] [ 0 ] ; int b = adj [ i ] [ 1 ] ; graph . get ( a ) . add ( b ) ; graph . get ( b ) . add ( a ) ; } countSubtreesUtil ( 1 , 1 ) ; System . out . println ( ans + 1 ) ; } public static void main ( String [ ] args ) { int N = 3 ; int [ ] [ ] adj = { { 0 , 1 } , { 1 , 2 } } ; graph = new ArrayList < > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) graph . add ( new ArrayList < > ( ) ) ; countSubtrees ( N , adj ) ; } }
Find the player who wins the game of placing alternate + and | Java program for the above approach ; Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
import java . util . * ; class GFG { static void checkWinner ( int arr [ ] , int N ) { int diff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { diff -= arr [ i ] ; } if ( diff % 2 == 0 ) { System . out . println ( " A " ) ; } else { System . out . println ( " B " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int N = arr . length ; checkWinner ( arr , N ) ; } }
Generate an array having sum of Euler Totient Function of all elements equal to N | Java program for the above approach ; Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; Push the current divisor ; If N is not a perfect square ; Push the second divisor ; Print the resultant array ; Driver Code ; Function Call
import java . util . * ; class GFG { static void constructArray ( int N ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { ans . add ( i ) ; if ( N != ( i * i ) ) { ans . add ( N / i ) ; } } } for ( int it : ans ) { System . out . print ( it + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 12 ; constructArray ( N ) ; } }
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | Java Program to implement the above approach ; Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumRowValue ( int n , int m ) { return gcd ( n , m ) ; } public static void main ( String args [ ] ) { int N = 4 ; int M = 2 ; System . out . print ( maximumRowValue ( N , M ) ) ; } }
Program to generate an array having convolution of two given arrays | Java program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c [ ] ; Driver Code
import java . util . * ; class GFG { static int MOD = 998244353 ; static void findConvolution ( int [ ] a , int [ ] b ) { int n = a . length , m = b . length ; int [ ] c = new int [ ( n + m - 1 ) ] ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD ; } } for ( int k = 0 ; k < c . length ; ++ k ) { c [ k ] %= MOD ; System . out . print ( c [ k ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] B = { 5 , 6 , 7 , 8 , 9 } ; findConvolution ( A , B ) ; } }
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | Java program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Driver code ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A
class GFG { static int count1s ( int start_i , int start_j , int end_i , int end_j , int [ ] [ ] mat ) { int count = 0 ; for ( int x = start_i ; x < end_i ; x ++ ) { for ( int y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } static int findMinimumCount ( int N , int M , int A , int B , int [ ] [ ] mat ) { int minimum = ( int ) 1e9 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + A <= N && j + B <= M ) { int count = count1s ( i , j , i + A , j + B , mat ) ; minimum = Math . min ( count , minimum ) ; } if ( i + B <= N && j + A <= M ) { int count = count1s ( i , j , i + B , j + A , mat ) ; minimum = Math . min ( count , minimum ) ; } } } return minimum ; } public static void main ( String [ ] args ) { int A = 2 , B = 2 ; int N = 3 , M = 4 ; int [ ] [ ] mat = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; System . out . println ( findMinimumCount ( N , M , A , B , mat ) ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | Java program for the above approach ; Function to check if N can be expressed as an even power of 2 ; Iterate till x is N ; if x is even then return true ; Increment x ; If N is not a power of 2 ; Driver Code
import java . io . * ; class GFG { static boolean checkEvenPower ( int n ) { int x = 0 ; while ( x < n ) { int value = ( int ) Math . pow ( 2 , x ) ; if ( value == n ) { if ( x % 2 == 0 ) return true ; else return false ; } x ++ ; } return false ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( ( checkEvenPower ( N ) ? " Yes " : " No " ) ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | Java program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver code
import java . util . * ; class GFG { static String checkEvenPower ( int n ) { int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int value = ( int ) Math . pow ( 2 , mid ) ; if ( value == n ) { if ( mid % 2 == 1 ) return " No " ; else return " Yes " ; } else if ( value < n ) low = mid + 1 ; else high = mid - 1 ; } return " No " ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( checkEvenPower ( N ) ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | Java program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; If N is not a power of 2 ; Bitwise AND operation ; Driver Code
import java . io . * ; class GFG { static boolean checkEvenPower ( long N ) { if ( ( N & ( N - 1 ) ) != 0 ) return false ; N = N & 0x55555555 ; return ( N > 0 ) ; } public static void main ( String [ ] args ) { long N = 4 ; System . out . println ( checkEvenPower ( N ) ? 1 : 0 ) ; } }