codeparrot/xlcost-text-to-code huggingface dataset:
Count levels in a Binary Tree consisting of node values having set bits at different positions | Java program for the above approach ; Structure of a node in the binary tree ; Function to find total unique levels ; Stores count of levels , where the set bits of all the nodes are at different positions ; Store nodes at each level of the tree using BFS ; Performing level order traversal ; Stores count of nodes at current level ; Stores prefix XOR of all the nodes at current level ; Stores prefix OR of all the nodes at current level ; Check if set bit of all the nodes at current level is at different positions or not ; Traverse nodes at current level ; Stores front element of the que ; Update prefix_OR ; Update prefix_XOR ; If left subtree not null ; If right subtree not null ; If bitwise AND is zero ; Driver Code ; Function Call
import java . util . * ; class GFG { static class TreeNode { int val = 0 ; TreeNode left , right ; TreeNode ( int x ) { val = x ; left = null ; right = null ; } } ; static void uniqueLevels ( TreeNode root ) { int uniqueLevels = 0 ; Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( que . size ( ) > 0 ) { int length = que . size ( ) ; int prefix_XOR = 0 ; int prefix_OR = 0 ; boolean flag = true ; for ( int i = 0 ; i < length ; i ++ ) { TreeNode temp = que . peek ( ) ; que . remove ( ) ; prefix_OR |= temp . val ; prefix_XOR ^= temp . val ; if ( prefix_XOR != prefix_OR ) flag = false ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; } if ( flag ) uniqueLevels += 1 ; } System . out . print ( uniqueLevels ) ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 6 ) ; root . right = new TreeNode ( 9 ) ; root . left . left = new TreeNode ( 1 ) ; root . left . right = new TreeNode ( 4 ) ; root . right . right = new TreeNode ( 7 ) ; uniqueLevels ( root ) ; } }
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Java implementation of above approach ; Driver code ; decreasing value by 1 because we want min heapifying k times and it starts from 0 so we have to decrease it 1 time ; Step 1 : Do extract minimum k1 times ( This step takes O ( K1 Log n ) time ) ; Step 2 : Do extract minimum k2 k1 1 times and sum all extracted elements . ( This step takes O ( ( K2 k1 ) * Log n ) time )
class GFG { static int n = 7 ; static void minheapify ( int [ ] a , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { int t = a [ small ] ; a [ small ] = a [ index ] ; a [ index ] = t ; minheapify ( a , small ) ; } } public static void main ( String [ ] args ) { int i = 0 ; int k1 = 3 ; int k2 = 6 ; int [ ] a = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int ans = 0 ; for ( i = ( n / 2 ) - 1 ; i >= 0 ; i -- ) { minheapify ( a , i ) ; } k1 -- ; k2 -- ; for ( i = 0 ; i <= k1 ; i ++ ) { a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } for ( i = k1 + 1 ; i < k2 ; i ++ ) { ans += a [ 0 ] ; a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } System . out . println ( ans ) ; } }
Construct a List using the given Q XOR queries | Java program for the above approach ; Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize final list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return final list ; Driver Code ; Given Queries ; Function Call
import java . util . * ; class GFG { static List < Integer > ConstructList ( int [ ] [ ] Q ) { int xor = 0 ; List < Integer > ans = new ArrayList < > ( ) ; for ( int i = Q . length - 1 ; i >= 0 ; i -- ) { if ( Q [ i ] [ 0 ] == 0 ) ans . add ( Q [ i ] [ 1 ] ^ xor ) ; else xor ^= Q [ i ] [ 1 ] ; } ans . add ( xor ) ; Collections . sort ( ans ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] Q = { { 0 , 6 } , { 0 , 3 } , { 0 , 2 } , { 1 , 4 } , { 1 , 5 } } ; System . out . println ( ConstructList ( Q ) ) ; } }
Bitwise operations on Subarrays of size K | Java program for maximum values of each bitwise OR operation on element of subarray of size K ; Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function Call
import java . util . * ; class GFG { static int build_num ( int bit [ ] ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } static int maximumOR ( int arr [ ] , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int max_or = build_num ( bit ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } max_or = Math . max ( build_num ( bit ) , max_or ) ; } return max_or ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . length ; System . out . print ( maximumOR ( arr , n , k ) ) ; } }
XOR of every element of an Array with a given number K | Java program to find XOR of every element of an array with a given number K ; Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
import java . util . * ; class GFG { static void constructXORArray ( int A [ ] , int n , int K ) { int [ ] B = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int K = 5 ; int n = A . length ; constructXORArray ( A , n , K ) ; int B [ ] = { 4 , 75 , 45 , 42 } ; K = 2 ; n = B . length ; constructXORArray ( B , n , K ) ; } }
Bitwise AND of all the elements of array | Java program to find bitwise AND of all the elements in the array ; Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND
import java . util . * ; class GFG { static int find_and ( int arr [ ] ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 5 , 9 , 11 } ; System . out . println ( find_and ( arr ) ) ; } }
Construct XOR tree by Given leaf nodes of Perfect Binary Tree | Java implementation of the above approach ; Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; System . out . print ( xor_tree [ x ] + " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to conXOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; Driver Code ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node
class GFG { static int maxsize = 100005 ; static int [ ] xor_tree = new int [ maxsize ] ; static void construct_Xor_Tree_Util ( int [ ] current , int start , int end , int x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } int left = x * 2 + 1 ; int right = x * 2 + 2 ; int mid = start + ( end - start ) / 2 ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } static void construct_Xor_Tree ( int [ ] arr , int n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } public static void main ( String [ ] args ) { int [ ] leaf_nodes = { 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 } ; int n = leaf_nodes . length ; construct_Xor_Tree ( leaf_nodes , n ) ; int x = ( int ) ( Math . ceil ( Math . log ( n ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; System . out . print ( "Nodes of the XOR Tree:NEW_LINE"); for ( int i = 0 ; i < max_size ; i ++ ) { System . out . print ( xor_tree [ i ] + " ▁ " ) ; } int root = 0 ; System . out . print ( " Root : " + xor_tree[root]); } }
Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | Java program to Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s ; Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code
class GFG { static boolean check ( int arr [ ] , int n , int m , int d ) { int i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } static int maximumDistance ( int arr [ ] , int n , int m ) { int low = 1 , high = n - 1 ; int ans = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; boolean flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int n = 5 , m = 3 ; int arr [ ] = new int [ n ] ; System . out . print ( maximumDistance ( arr , n , m ) ) ; } }
Maximum XOR value of maximum and second maximum element among all possible subarrays | Java implementation of the approach ; Function to return the maximum possible xor ; To store the final answer ; Borward traversal ; Backward traversal ; Driver code
import java . util . * ; class GFG { static int maximumXor ( int arr [ ] , int n ) { Stack < Integer > sForward = new Stack < Integer > ( ) , sBackward = new Stack < Integer > ( ) ; int ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! sForward . isEmpty ( ) && arr [ i ] < arr [ sForward . peek ( ) ] ) { ans = Math . max ( ans , arr [ i ] ^ arr [ sForward . peek ( ) ] ) ; sForward . pop ( ) ; } sForward . add ( i ) ; while ( ! sBackward . isEmpty ( ) && arr [ n - i - 1 ] < arr [ sBackward . peek ( ) ] ) { ans = Math . max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward . peek ( ) ] ) ; sBackward . pop ( ) ; } sBackward . add ( n - i - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 2 } ; int n = arr . length ; System . out . print ( maximumXor ( arr , n ) ) ; } }
Minimum count of Full Binary Trees such that the count of leaves is N | Java implementation of the approach ; Function to return the minimum count of trees required ; To store the count of set bits in n ; Driver code
class GFG { static int minTrees ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( minTrees ( n ) ) ; } }
Count number of steps to cover a distance if steps can be taken in powers of 2 | Java program to count minimum number of steps ; Function to count the minimum number of steps ; count the number of set bits in a number ; Driver Code
import java . io . * ; class GFG { static int getMinSteps ( int K ) { return Integer . bitCount ( K ) ; } public static void main ( String [ ] args ) { int n = 343 ; System . out . println ( getMinSteps ( n ) ) ; } }
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | Java implementation of the approach ; Function that returns true if the game cannot be won ; Driver code ; Common length
class GfG { static boolean isDefeat ( String s1 , String s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) || ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) ) continue ; else if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == ' Z ' ) || ( s1 . charAt ( i ) == ' Z ' && s2 . charAt ( i ) == '0' ) ) continue ; else { return true ; } } return false ; } public static void main ( String [ ] args ) { String s1 = ( "01001101ZZ " ) ; String s2 = ( "10Z1001000" ) ; int n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) System . out . println ( " Defeat " ) ; else System . out . println ( " Victory " ) ; } }
Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | Java implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; Driver Code ; First binary matrix ; Second binary matrix
class GFG { static final int N = 3 , M = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int b [ ] [ ] = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Position of the K | Java program to implement the above approach ; Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code
import java . util . * ; class GFG { static int FindIndexKthBit ( int n , int k ) { int cnt = 0 ; int ind = 0 ; while ( n > 0 ) { if ( ( n & 1 ) != 0 ) cnt ++ ; if ( cnt == k ) return ind ; ind ++ ; n = n >> 1 ; } return - 1 ; } public static void main ( String args [ ] ) { int n = 15 , k = 3 ; int ans = FindIndexKthBit ( n , k ) ; if ( ans != - 1 ) System . out . println ( ans ) ; else System . out . println ( " No ▁ k - th ▁ set ▁ bit " ) ; } }
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Java implementation of the above approach ; Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver Code
import java . util . * ; class GFG { static void hasEqualBlockFrequency ( int N ) { String S = Integer . toString ( N , 2 ) ; HashSet < Integer > p = new HashSet < Integer > ( ) ; int c = 1 ; for ( int i = 0 ; i < S . length ( ) - 1 ; i ++ ) { if ( S . charAt ( i ) == S . charAt ( i + 1 ) ) c += 1 ; else { p . add ( c ) ; c = 1 ; } p . add ( c ) ; } if ( p . size ( ) == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; } }
Distance between two nodes of binary tree with node values from 1 to N | Java program to find minimum distance between two nodes in binary tree ; Function to get minimum path distance ; count bit length of n1 and n2 ; find bit difference and maxBit ; calculate result by formula ; Driver program
import java . util . * ; class GFG { static int minDistance ( int n1 , int n2 ) { int bitCount1 = ( int ) Math . floor ( ( Math . log ( n1 ) / Math . log ( 2 ) ) ) + 1 ; int bitCount2 = ( int ) Math . floor ( ( Math . log ( n2 ) / Math . log ( 2 ) ) ) + 1 ; int bitDiff = Math . abs ( bitCount1 - bitCount2 ) ; int maxBitCount = Math . max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * ( int ) Math . pow ( 2 , bitDiff ) ; } else { n1 = n1 * ( int ) Math . pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue ; if ( xorValue == 0 ) bitCountXorValue = 1 ; else { bitCountXorValue = ( int ) Math . floor ( ( Math . log ( xorValue ) / Math . log ( 2 ) ) ) + 1 ; } int disSimilarBitPosition = maxBitCount - bitCountXorValue ; int result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition ; return result ; } public static void main ( String args [ ] ) { int n1 = 12 , n2 = 5 ; System . out . println ( minDistance ( n1 , n2 ) ) ; } }
Remove one bit from a binary number to get maximum value | Java program to find next maximum binary number with one bit removed ; Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Driver code ; Get the binary number ; Find the maximum binary number
import java . io . * ; class GFG { static int printMaxAfterRemoval ( String s ) { boolean flag = false ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '0' && flag == false ) { flag = true ; continue ; } else System . out . print ( s . charAt ( i ) ) ; } return 0 ; } public static void main ( String [ ] args ) { String s = "1001" ; printMaxAfterRemoval ( s ) ; } }
Find position of left most dis | Java program to find the leftmost position of first dis - similar bit ; Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver Code
import java . io . * ; class GFG { static int bitPos ( int n1 , int n2 ) { if ( n1 == n2 ) return 0 ; int bitCount1 = ( int ) Math . floor ( Math . log ( n1 ) / Math . log ( 2 ) ) + 1 ; int bitCount2 = ( int ) Math . floor ( Math . log ( n2 ) / Math . log ( 2 ) ) + 1 ; int bitDiff = Math . abs ( bitCount1 - bitCount2 ) ; int maxBitCount = Math . max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * ( int ) Math . pow ( 2 , bitDiff ) ; } else { n1 = n1 * ( int ) Math . pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue = ( int ) Math . floor ( Math . log ( xorValue ) / Math . log ( 2 ) ) + 1 ; int disSimilarBitPosition = maxBitCount - bitCountXorValue + 1 ; return disSimilarBitPosition ; } public static void main ( String [ ] args ) { int n1 = 53 , n2 = 55 ; System . out . println ( bitPos ( n1 , n2 ) ) ; } }
Number of pairs with Bitwise OR as Odd number | Java program to count pairs with odd OR ; Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main
public class GFG { static int countOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 != 1 ) ) { count ++ ; } } int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( countOddPair ( A , N ) ) ; } }
Replace every array element by Bitwise Xor of previous and next element | Java program to update every array element with sum of previous and next numbers in array ; Nothing to do when array size is 1 ; store current value of arr [ 0 ] and update it ; Update rest of the array elements ; Store current value of next interaction ; Update current value using previous value ; Update previous value ; Update last array element separately ; Driver Code ; Print the modified array
import java . io . * ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { if ( n <= 1 ) return ; int prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] ^ arr [ 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int curr = arr [ i ] ; arr [ i ] = prev ^ arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev ^ arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Lucky alive person in a circle | Set | Java implementation of the above approach ; To calculate the number of bits in the binary equivalent of n ; Finding complement by inverting the bits one by one from last ; XOR of n2 with ( 1 << i ) to flip the last ( i + 1 ) th bit of binary equivalent of n2 ; n2 will be the one 's complement of n ; Driver code
import java . io . * ; class GFG { static int luckiest_person ( int n ) { int len = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; int n2 = n ; for ( int i = 0 ; i < len ; i ++ ) { n2 = n2 ^ ( 1 << i ) ; } return Math . abs ( n - n2 ) ; } public static void main ( String [ ] args ) { int N = 3 ; int lucky_p = luckiest_person ( N ) ; System . out . println ( lucky_p ) ; } }
Lucky alive person in a circle | Set | package whatever do not write package name here ; Here is a O ( 1 ) solution for this problemit will work for 32 bit integers ] ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; to calculate the highest number which is power of 2 and less than n ; return the correct answer as per the approach in above article ; Driver Code
import java . io . * ; class GFG { static int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } static int luckiest_person ( int n ) { int nearestPower = setBitNumber ( n ) ; return 2 * ( n - nearestPower ) + 1 ; } public static void main ( String [ ] args ) { int N = 8 ; int lucky_p = luckiest_person ( N ) ; System . out . print ( lucky_p ) ; } }
Find triplets in an array whose AND is maximum | Java program to find triplet with maximum bitwise AND . ; Flag Array initially set to true for all numbers ; 2D array for bit representation of all the numbers . Initially all bits are set to 0. ; Finding bit representation of every number and storing it in bits array . ; Checking last bit of the number ; Dividing number by 2. ; maximum And number initially 0. ; Traversing the 2d binary representation . 0 th index represents 32 th bits while 32 th index represents 0 th bit . ; If cnt greater than 3 then ( 32 - i ) th bits of the number will be set . ; Setting flags of the numbers whose ith bit is not set . ; Counting the numbers whose flag are true . ; Driver code
import java . util . Arrays ; class GFG { static void maxTriplet ( int a [ ] , int n ) { boolean [ ] f = new boolean [ n ] ; Arrays . fill ( f , true ) ; int bits [ ] [ ] = new int [ n ] [ 33 ] ; for ( int i = 0 ; i < n ; ++ i ) { int num = a [ i ] ; int j = 32 ; while ( num > 0 ) { if ( num % 2 == 1 ) { bits [ i ] [ j ] = 1 ; } j -- ; num >>= 1 ; } } long ans = 0 ; for ( int i = 0 ; i <= 32 ; ++ i ) { int cnt = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( bits [ j ] [ i ] == 1 & f [ j ] ) { cnt ++ ; } } if ( cnt >= 3 ) { ans += Math . pow ( 2 , 32 - i ) ; for ( int j = 0 ; j < n ; ++ j ) { if ( bits [ j ] [ i ] != 1 ) { f [ j ] = false ; } } } } int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( f [ i ] ) { cnt ++ ; } } long NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) / 6 ; System . out . print ( NumberOfTriplets + " ▁ " + ans ) ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 11 , 10 , 15 , 26 } ; int n = a . length ; maxTriplet ( a , n ) ; } }
Find bitwise OR of all possible sub | Java program to find OR of all the sub - arrays ; function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays
class GFG { static int OR ( int a [ ] , int n ) { int ans = a [ 0 ] ; int i ; for ( i = 1 ; i < n ; i ++ ) { ans |= a [ i ] ; } return ans ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 4 , 6 } ; int n = a . length ; System . out . println ( OR ( a , n ) ) ; } }