codeparrot/xlcost-text-to-code huggingface dataset:
Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | Java program to minimize number of bits to be fipped in X or Y such that their bitwise or is equal to minimize ; This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
import java . util . * ; class GFG { static int minimumFlips ( int X , int Y , int Z ) { int res = 0 ; while ( X > 0 Y > 0 Z > 0 ) { if ( ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) && ( Z % 2 == 1 ) ) { X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; continue ; } else if ( ! ( X % 2 == 1 ) && ! ( Y % 2 == 1 ) && ( Z % 2 == 1 ) ) { res ++ ; } else if ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) { if ( ( X % 2 == 1 ) && ( Y % 2 == 1 ) && ! ( Z % 2 == 1 ) ) { res += 2 ; } else if ( ( ( X % 2 == 1 ) || ( Y % 2 == 1 ) ) && ! ( Z % 2 == 1 ) ) { res ++ ; } } X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; } return res ; } public static void main ( String [ ] args ) { int X = 5 , Y = 8 , Z = 6 ; System . out . print ( minimumFlips ( X , Y , Z ) ) ; } }
Count subsequences with same values of Bitwise AND , OR and XOR | function for finding count of possible subsequence ; creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; driver function
static int countSubseq ( int arr [ ] , int n ) { int count = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; else mp . put ( arr [ i ] , 1 ) ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { if ( entry . getKey ( ) == 0 ) count += Math . pow ( 2 , entry . getValue ( ) ) - 1 ; else count += Math . pow ( 2 , entry . getValue ( ) - 1 ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 5 , 6 } ; int n = arr . length ; System . out . println ( countSubseq ( arr , n ) ) ; } }
Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | Java implementation to find Minimum possible value of the maximum xor in an array by choosing some integer ; Function to calculate Minimum possible value of the Maximum XOR in an array ; Base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; Explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
import java . util . * ; class GFG { static int calculate ( Vector < Integer > section , int pos ) { if ( pos < 0 ) return 0 ; Vector < Integer > on_section = new Vector < Integer > ( ) , off_section = new Vector < Integer > ( ) ; for ( int el : section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . add ( el ) ; else on_section . add ( el ) ; } if ( off_section . size ( ) == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . size ( ) == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } static int minXorValue ( int a [ ] , int n ) { Vector < Integer > section = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) section . add ( a [ i ] ) ; return calculate ( section , 30 ) ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 3 , 2 , 5 , 6 } ; System . out . print ( minXorValue ( A , N ) ) ; } }
Program to find the Nth natural number with exactly two bits set | Java Code to find the Nth number with exactly two bits set ; Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
class GFG { static void findNthNum ( int N ) { int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; System . out . print ( ( 1 << bit_L ) + ( 1 << bit_R ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 13 ; findNthNum ( N ) ; } }
XOR of all Prime numbers in an Array at positions divisible by K | Java program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
class GFG { static int MAX = 1000005 ; static boolean prime [ ] = new boolean [ MAX ] ; static void SieveOfEratosthenes ( boolean [ ] prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static void prime_xor ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 7 , 11 , 8 } ; int n = arr . length ; int K = 2 ; prime_xor ( arr , n , K ) ; } }
Find XOR of all elements in an Array | Java program to find the XOR of all elements in the array ; Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call
class GFG { static int xorOfArray ( int arr [ ] , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; System . out . println ( xorOfArray ( arr , n ) ) ; } }
Count of even set bits between XOR of two arrays | Java program for the above approach ; Function that count the XOR of B [ ] with all the element in A [ ] having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for B [ ] such that XOR with all the element in A [ ] having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
import java . util . * ; class GFG { static void countEvenBit ( int A [ ] , int B [ ] , int n , int m ) { int i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = Integer . bitCount ( A [ i ] ) ; if ( x % 2 == 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int [ ] CountB = new int [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = Integer . bitCount ( B [ i ] ) ; if ( x % 2 == 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { System . out . print ( CountB [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int B [ ] = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; } }
Check if a Number is Odd or Even using Bitwise Operators | Java program to check for even or odd using Bitwise XOR operator ; Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code
class GFG { static boolean isEven ( int n ) { if ( ( n ^ 1 ) == n + 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . print ( isEven ( n ) == true ? " Even " : " Odd " ) ; } }
Bitwise OR ( | ) of all even number from 1 to N | Java implementation of the above approach ; Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code
class GFG { static int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } static public void main ( String args [ ] ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }
Bitwise OR ( | ) of all even number from 1 to N | Java implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
class GFG { static int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; return ( int ) Math . pow ( 2 , bitCount ) - 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }
Generating N | Java program Generating N - bit Gray Code starting from K ; Function to Generating N - bit Gray Code starting from K ; Generate gray code of corresponding binary number of integer i . ; Driver code
class GFG { static void genSequence ( int n , int val ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int x = i ^ ( i >> 1 ) ^ val ; System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; genSequence ( n , k ) ; } }
Winner in the Rock | Java implementation of the approach ; Function to return the winner of the game ; Both the players chose to play the same move ; Player A wins the game ; Function to perform the queries ; Driver code
import java . util . * ; class GFG { static String winner ( String moves ) { HashMap < Character , Integer > data = new HashMap < Character , Integer > ( ) ; data . put ( ' R ' , 0 ) ; data . put ( ' P ' , 1 ) ; data . put ( ' S ' , 2 ) ; if ( moves . charAt ( 0 ) == moves . charAt ( 1 ) ) { return " Draw " ; } if ( ( ( data . get ( moves . charAt ( 0 ) ) | 1 << ( 2 ) ) - ( data . get ( moves . charAt ( 1 ) ) | 0 << ( 2 ) ) ) % 3 != 0 ) { return " A " ; } return " B " ; } static void performQueries ( String arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( winner ( arr [ i ] ) + "NEW_LINE"); } public static void main ( String [ ] args ) { String arr [ ] = { " RS " , " SR " , " SP " , " PP " } ; int n = arr . length ; performQueries ( arr , n ) ; } }
Maximum number of consecutive 1 's in binary representation of all the array elements | Java implementation of the approach ; Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr [ ] ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code
class GFG { static int maxConsecutiveOnes ( int x ) { int count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } static int maxOnes ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . max ( ans , currMax ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( maxOnes ( arr , n ) ) ; } }
Maximum Bitwise OR pair from a range | Java implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; Check for every possible pair ; Maximum among all ( i , j ) pairs ; Driver code
class GFG { static int maxOR ( int L , int R ) { int maximum = Integer . MIN_VALUE ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }
Maximum Bitwise OR pair from a range | Java implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; Loop through each bit from MSB to LSB ; MSBs where the bits differ , all bits from that bit are set ; If MSBs are same , then ans bit is same as that of bit of right or left limit ; Driver code
class GFG { static int MAX = 64 ; static int maxOR ( int L , int R ) { if ( L == R ) { return L ; } int ans = 0 ; for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }
Longest subsequence with a given AND value | O ( N ) | Java implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the AND of all the filtered elements ; Check if the AND is equal to m ; Driver code
import java . util . * ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { Vector < Integer > filter = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . add ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_and = filter . get ( 0 ) ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_and &= filter . get ( i ) ; if ( c_and == m ) return filter . size ( ) ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . length ; int m = 3 ; System . out . println ( findLen ( arr , n , m ) ) ; } }
Longest sub | Java implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the OR of all the filtered elements ; Check if the OR is equal to m ; Driver code
import java . util . * ; class GFG { static int findLen ( int arr [ ] , int n , int m ) { Vector < Integer > filter = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] m ) == m ) filter . add ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_or = filter . get ( 0 ) ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_or |= filter . get ( i ) ; if ( c_or == m ) return filter . size ( ) ; return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . length ; int m = 3 ; System . out . print ( findLen ( arr , n , m ) ) ; } }
Program to toggle K | Java program to toggle K - th bit of a number N ; Function to toggle the kth bit of n ; Driver code
class GFG { static int toggleBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . printf ( "%dNEW_LINE", toggleBit(n, k)); } }
Program to clear K | Java program to clear K - th bit of a number N ; Function to clear the kth bit of n ; Driver code
class GFG { static int clearBit ( int n , int k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 1 ; System . out . println ( clearBit ( n , k ) ) ; } }
Maximize the Expression | Bit Manipulation | Java implementation of the approach ; Function to return the value of the maximized expression ; int can have 32 bits ; Consider the ith bit of D to be 1 ; Calculate the value of ( B AND bitOfD ) ; Check if bitOfD satisfies ( B AND D = D ) ; Check if bitOfD can maximize ( A XOR D ) ; Note that we do not need to consider ith bit of D to be 0 because if above condition are not satisfied then value of result will not change which is similar to considering bitOfD = 0 as result XOR 0 = result ; Driver code
class GFG { final static int MAX = 32 ; static int maximizeExpression ( int a , int b ) { int result = a ; for ( int bit = MAX - 1 ; bit >= 0 ; bit -- ) { int bitOfD = 1 << bit ; int x = b & bitOfD ; if ( x == bitOfD ) { int y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } public static void main ( String [ ] args ) { int a = 11 , b = 14 ; System . out . println ( maximizeExpression ( a , b ) ) ; } }
Find number of subarrays with XOR value a power of 2 | Java Program to count number of subarrays with Bitwise - XOR as power of 2 ; Function to find number of subarrays ; Hash Map to store prefix XOR values ; When no element is selected ; Check for all the powers of 2 , till a MAX value ; Insert Current prefixxor in Hash Map ; Driver Code
import java . util . * ; class GFG { static int MAX = 10 ; static int findSubarray ( int array [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , 1 ) ; int answer = 0 ; int preXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int value = 1 ; preXor ^= array [ i ] ; for ( int j = 1 ; j <= MAX ; j ++ ) { int Y = value ^ preXor ; if ( mp . containsKey ( Y ) ) { answer += mp . get ( Y ) ; } value *= 2 ; } if ( mp . containsKey ( preXor ) ) { mp . put ( preXor , mp . get ( preXor ) + 1 ) ; } else { mp . put ( preXor , 1 ) ; } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 2 , 6 , 7 , 5 , 8 } ; int n = array . length ; System . out . println ( findSubarray ( array , n ) ) ; } }
Maximum XOR of Two Numbers in an Array | Java implementation of the approach ; Function to return the maximum xor ; Calculating xor of each pair ; Driver Code
class GFG { static int max_xor ( int arr [ ] , int n ) { int maxXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { maxXor = Math . max ( maxXor , arr [ i ] ^ arr [ j ] ) ; } } return maxXor ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . length ; System . out . println ( max_xor ( arr , n ) ) ; } }
Maximum XOR of Two Numbers in an Array | Java implementation of the above approach ; Function to return the maximum xor ; set the i 'th bit in mask  like 100000, 110000, 111000.. ; Just keep the prefix till i ' th ▁ bit ▁ neglecting ▁ all ▁ ▁ the ▁ bit ' s after i 'th bit ; find two pair in set such that a ^ b = newMaxx which is the highest possible bit can be obtained ; clear the set for next iteration ; Driver Code
import java . util . * ; class GFG { static int max_xor ( int arr [ ] , int n ) { int maxx = 0 , mask = 0 ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 30 ; i >= 0 ; i -- ) { mask |= ( 1 << i ) ; for ( int j = 0 ; j < n ; ++ j ) { se . add ( arr [ j ] & mask ) ; } int newMaxx = maxx | ( 1 << i ) ; for ( int prefix : se ) { if ( se . contains ( newMaxx ^ prefix ) ) { maxx = newMaxx ; break ; } } se . clear ( ) ; } return maxx ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . length ; System . out . println ( max_xor ( arr , n ) ) ; } }
Count of triplets that satisfy the given equation | Java implementation of the approach ; Function to return the count of required triplets ; First element of the current sub - array ; XOR every element of the current sub - array ; If the XOR becomes 0 then update the count of triplets ; Driver code
class GFG { static int CountTriplets ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int first = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { first ^= arr [ j ] ; if ( first == 0 ) ans += ( j - i ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 6 , 4 , 2 } ; int n = arr . length ; System . out . println ( CountTriplets ( arr , n ) ) ; } }
Find the Majority Element | Set 3 ( Bit Magic ) |  ; Number of bits in the integer ; Variable to calculate majority element ; Loop to iterate through all the bits of number ; Loop to iterate through all elements in array to count the total set bit at position i from right ; If the total set bits exceeds n / 2 , this bit should be present in majority Element . ; iterate through array get the count of candidate majority element ; Verify if the count exceeds n / 2 ; Driver Code
class GFG { static void findMajority ( int arr [ ] , int n ) { int len = 32 ; int number = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) number += ( 1 << i ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > ( n / 2 ) ) System . out . println ( number ) ; else System . out . println ( " Majority ▁ Element ▁ Not ▁ Present " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 } ; int n = arr . length ; findMajority ( arr , n ) ; } }
Count number of bits to be flipped to convert A to B | Set | Java implementation of the approach ; Function to return the count of bits to be flipped to convert a to b ; To store the required count ; Loop until both of them become zero ; Store the last bits in a as well as b ; If the current bit is not same in both the integers ; Right shift both the integers by 1 ; Return the count ; Driver code
import java . util . * ; class GFG { static int countBits ( int a , int b ) { int count = 0 ; while ( a > 0 b > 0 ) { int last_bit_a = a & 1 ; int last_bit_b = b & 1 ; if ( last_bit_a != last_bit_b ) count ++ ; a = a >> 1 ; b = b >> 1 ; } return count ; } public static void main ( String [ ] args ) { int a = 10 , b = 7 ; System . out . println ( countBits ( a , b ) ) ; } }
Count Set | Java program to find number of set bist in a number ; Recursive function to find number of set bist in a number ; Base condition ; If Least significant bit is set ; If Least significant bit is not set ; Driver code ; Function call
class GFG { static int CountSetBits ( int n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( CountSetBits ( n ) ) ; } }
Sum of Bitwise OR of all pairs in a given array | A Simple Java program to compute sum of bitwise OR of all pairs ; Returns value of " arr [ 0 ] ▁ | ▁ arr [ 1 ] ▁ + ▁ ▁ arr [ 0 ] ▁ | ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ | ▁ arr [ j ] ▁ + ▁ ▁ . . . . . ▁ arr [ n - 2 ] ▁ | ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
import java . io . * ; class GFG { static int pairORSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] | arr [ j ] ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( pairORSum ( arr , n ) ) ; } }
Number of 0 s and 1 s at prime positions in the given array | Java implementation of the approach ; Function that returns true if n is prime ; Check from 2 to n ; Function to find the count of 0 s and 1 s at prime indices ; To store the count of 0 s and 1 s ; If current 0 is at prime position ; If current 1 is at prime position ; Driver code
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countPrimePosition ( int arr [ ] ) { int c0 = 0 , c1 = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } System . out . println ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; System . out . println ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }