codeparrot/xlcost-text-to-code huggingface dataset:
Sum of quotients of division of N by powers of K not exceeding N | Java program for the above approach ; Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Print the result ; Driver Code ; Given N and K
import java . util . * ; class GFG { static void findSum ( int N , int K ) { int ans = 0 ; int i = 1 ; while ( i <= N ) { ans += N / i ; i = i * K ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 10 , K = 2 ; findSum ( N , K ) ; } }
Count Arithmetic Progressions having sum S and common difference equal to D | Java program for above approach package whatever do not write package name here ; Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver code
import java . io . * ; class GFG { static int countAPs ( int S , int D ) { S = S * 2 ; int answer = 0 ; for ( int i = 1 ; i <= Math . sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { int S = 12 , D = 1 ; System . out . println ( countAPs ( S , D ) ) ; } }
Minimize insertions to make sum of every pair of consecutive array elements at most K | Java program for the above approach ; Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K ; Stores if it is possible to make sum of each pair of adjacent elements at most K ; Stores the count of insertions ; Stores the previous value to index i ; Traverse the array ; If arr [ i ] is greater than or equal to K ; Mark possible 0 ; If last + arr [ i ] is greater than K ; Increment res by 1 ; Assign arr [ i ] to last ; If possible to make the sum of pairs of adjacent elements at most K ; Otherwise print " - 1" ; Driver Code
import java . util . * ; class GFG { static void minimumInsertions ( int arr [ ] , int N , int K ) { boolean possible = true ; int res = 0 ; int last = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= K ) { possible = false ; break ; } if ( last + arr [ i ] > K ) res ++ ; last = arr [ i ] ; } if ( possible ) { System . out . print ( res ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 6 ; int N = arr . length ; minimumInsertions ( arr , N , K ) ; } }
Maximum sum of K | Java program for the above approach ; Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver Code ; Stores the count of distinct elements
import java . util . * ; class GFG { static int distinct ( int arr [ ] , int n ) { Set < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { set . add ( arr [ i ] ) ; } return set . size ( ) ; } static int maxSubSum ( int arr [ ] , int n , int k , int totalDistinct ) { if ( k > n ) return 0 ; int max = 0 , sum = 0 ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { sum = 0 ; Set < Integer > set = new HashSet < > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { sum += arr [ j ] ; set . add ( arr [ j ] ) ; } if ( set . size ( ) == totalDistinct ) max = Math . max ( sum , max ) ; } return max ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; int N = arr . length ; int totalDistinct = distinct ( arr , N ) ; System . out . println ( maxSubSum ( arr , N , K , totalDistinct ) ) ; } }
Maximum sum of K | Java program for the above approach ; Function to count the number of distinct elements present in the array ; Insert array elements into Set ; Return the Set size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the map ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If map size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call
import java . util . * ; class GFG { static int distinct ( int arr [ ] , int N ) { Set < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { set . add ( arr [ i ] ) ; } return set . size ( ) ; } static int maxSubarraySumUtil ( int arr [ ] , int N , int K , int totalDistinct ) { if ( K > N ) return 0 ; int max = 0 ; int sum = 0 ; Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { map . put ( arr [ i ] , map . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; sum += arr [ i ] ; if ( i >= K ) { map . put ( arr [ i - K ] , map . get ( arr [ i - K ] ) - 1 ) ; sum -= arr [ i - K ] ; if ( map . get ( arr [ i - K ] ) == 0 ) map . remove ( arr [ i - K ] ) ; } if ( map . size ( ) == totalDistinct ) max = Math . max ( max , sum ) ; } return max ; } static void maxSubarraySum ( int arr [ ] , int K ) { int N = arr . length ; int totalDistinct = distinct ( arr , N ) ; System . out . println ( maxSubarraySumUtil ( arr , N , K , totalDistinct ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; maxSubarraySum ( arr , K ) ; } }
Number of Irreflexive Relations on a Set | Java program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int irreflexiveRelation ( int N ) { return power ( 2 , N * N - N ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( irreflexiveRelation ( N ) ) ; } }
Count Arithmetic Progressions having sum N and common difference equal to 1 | Java program for the above approach ; Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Print count - 1 ; Driver code ; Given value of N ; Function call to count required number of AP series
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countAPs ( long N ) { long count = 0 ; for ( long i = 1 ; i * i <= 2 * N ; i ++ ) { long res = 2 * N ; if ( res % i == 0 ) { long op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res && ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } System . out . println ( count - 1 ) ; } public static void main ( String [ ] args ) { long N = 963761198400L ; countAPs ( N ) ; } }
Check if concatenation of first and last digits forms a prime number or not for each array element | Java program for the above approach ; Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver Code ; Computes and stores primes using Sieve ; Function call to perform queries
import java . io . * ; import java . util . * ; class GFG { static int [ ] sieve = new int [ 105 ] ; static void buildSieve ( ) { for ( int i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( int i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( int j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } static boolean isAnyPrime ( int first , int last ) { int num1 = first * 10 + last ; int num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } static void performQueries ( int [ ] q ) { for ( int i = 0 ; i < q . length ; i ++ ) { int A = q [ i ] ; int last = A % 10 ; int first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) System . out . println ( "TrueNEW_LINE"); else System . out . print ( "FalseNEW_LINE"); } } public static void main ( String [ ] args ) { int [ ] q = { 30 , 66 } ; buildSieve ( ) ; performQueries ( q ) ; } }
Check if a given number N has at least one odd divisor not exceeding N | package whatever do not write package name here ; Function to check whether N has at least one odd divisor not exceeding N - 1 or not ; Stores the value of N ; Reduce the given number N by dividing it by 2 ; If N is divisible by an odd divisor i ; Check if N is an odd divisor after reducing N by dividing it by 2 ; Otherwise ; Driver Code ; Function Call
import java . io . * ; class GFG { public static String oddDivisor ( int N ) { int X = N ; while ( N % 2 == 0 ) { N /= 2 ; } for ( int i = 3 ; i * i <= X ; i += 2 ) { if ( N % i == 0 ) { return " Yes " ; } } if ( N != X ) { return " Yes " ; } return " No " ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( oddDivisor ( N ) ) ; } }
Count numbers from a range whose cube is a Palindrome | Java program of the above approach ; Function to check if n is a pallindrome number or not ; Temporarily store n ; Stores reverse of n ; Iterate until temp reduces to 0 ; Extract the last digit ; Add to the start ; Remove the last digit ; If the number and its reverse are equal ; Otherwise ; Function to precompute and store the count of numbers whose cube is a palindrome number ; Iterate upto 10 ^ 4 ; Check if i * i * i is a pallindrome or not ; Convert arr [ ] to prefix sum array ; Driver Code ; Given queries ; Using inclusion - exclusion principle , count required numbers
import java . io . * ; import java . util . * ; public class Pair { private final int key ; private final int value ; public Pair ( int aKey , int aValue ) { key = aKey ; value = aValue ; } public int key ( ) { return key ; } public int value ( ) { return value ; } } class GFG { static int [ ] arr = new int [ 10005 ] ; static int isPalindrome ( int n ) { int temp = n ; int res = 0 ; while ( temp != 0 ) { int rem = temp % 10 ; res = res * 10 + rem ; temp /= 10 ; } if ( res == n ) { return 1 ; } else return 0 ; } static void precompute ( ) { for ( int i = 1 ; i <= 10000 ; i ++ ) { if ( isPalindrome ( i * i * i ) != 0 ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 10000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static void main ( String [ ] args ) { ArrayList < Pair > Q = new ArrayList < Pair > ( ) ; Pair pair = new Pair ( 2 , 7 ) ; Q . add ( pair ) ; Pair pair2 = new Pair ( 10 , 25 ) ; Q . add ( pair2 ) ; precompute ( ) ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { System . out . println ( arr [ Q . get ( i ) . value ( ) ] - arr [ Q . get ( i ) . key ( ) - 1 ] ) ; } } }
Queries to count numbers from a given range which are divisible the sum of their digits | java program for the above approach ; Function to check if the number x is divisible by its sum of digits ; Stores sum of digits of x ; Temporarily store x ; Calculate sum of digits of x ; If x is not divisible by sum ; Otherwise ; Function to perform the precomputation ; Iterate from i equals 1 to 1e5 ; Check if i is divisible by sum of its digits ; Convert arr [ ] to prefix sum array ; Driver Code ; Given queries ; Using inclusion - exclusion principle , calculate the result
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int arr [ ] = new int [ 100005 ] ; static boolean isDivisible ( int x ) { int sum = 0 ; int temp = x ; while ( x != 0 ) { sum += x % 10 ; x /= 10 ; } if ( temp % sum != 0 ) return false ; else return true ; } static void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { if ( isDivisible ( i ) ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 100000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static void main ( String [ ] args ) { int Q [ ] [ ] = { { 5 , 9 } , { 5 , 20 } } ; precompute ( ) ; for ( int q [ ] : Q ) { System . out . println ( arr [ q [ 1 ] ] - arr [ q [ 0 ] - 1 ] ) ; } } }
Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M | Java program for the above approach ; Stores the prime factor of numbers ; Function to find GCD of a and b ; Base Case ; Otherwise , calculate GCD ; Function to precompute the prime numbers till 1000000 ; Initialize all the positions with their respective values ; Iterate over the range [ 2 , sqrt ( 10 ^ 6 ) ] ; If i is prime number ; Mark it as the factor ; Utility function to count the number of steps to make X and Y equal ; Initialise steps ; Iterate x is at most 1 ; Divide with the smallest prime factor ; If X and Y can 't be  made equal ; Return steps ; Function to find the minimum number of steps required to make X and Y equal ; Generate all the prime factors ; Calculate GCD of x and y ; Divide the numbers by their gcd ; If not possible , then return - 1 ; ; Return the resultant number of steps ; Driver Code
import java . util . * ; public class GFG { static int primes [ ] = new int [ 1000006 ] ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void preprocess ( ) { for ( int i = 1 ; i <= 1000000 ; i ++ ) primes [ i ] = i ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( primes [ i ] == i ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { if ( primes [ j ] == j ) primes [ j ] = i ; } } } } static int Steps ( int x , int m ) { int steps = 0 ; boolean flag = false ; while ( x > 1 ) { if ( primes [ x ] > m ) { flag = true ; break ; } x /= primes [ x ] ; steps ++ ; } if ( flag ) return - 1 ; return steps ; } static int minimumSteps ( int x , int y , int m ) { preprocess ( ) ; int g = gcd ( x , y ) ; x = x / g ; y = y / g ; int x_steps = Steps ( x , m ) ; int y_steps = Steps ( y , m ) ; if ( x_steps == - 1 y_steps == - 1 ) return - 1 ; return x_steps + y_steps ; } public static void main ( String args [ ] ) { int X = 160 ; int Y = 180 ; int M = 10 ; System . out . println ( minimumSteps ( X , Y , M ) ) ; } }
Length of longest subsequence consisting of Non | Java program for the above approach ; Function to check if n is a deficient number or not ; Stores sum of divisors ; Iterate over the range [ 1 , sqrt ( N ) ] ; If n is divisible by i ; If divisors are equal , add only one of them ; Otherwise add both ; Function to print the longest subsequence which does not contain any deficient numbers ; Stores the count of array elements which are non - deficient ; Traverse the array ; If element is non - deficient ; Return the answer ; Driver Code
import java . util . * ; class GFG { static boolean isNonDeficient ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum >= 2 * n ; } static int LongestNonDeficientSubsequence ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNonDeficient ( arr [ i ] ) ) { res += 1 ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 55 , 240 , 32 , 24 , 27 , 56 , 80 , 100 , 330 , 89 } ; int N = arr . length ; System . out . print ( LongestNonDeficientSubsequence ( arr , N ) ) ; } }
Number of subarrays consisting only of Pronic Numbers | Java program for the approach ; Function to check if a number is pronic number or not ; Iterate over the range [ 1 , sqrt ( N ) ] ; Return true if N is pronic ; Otherwise , return false ; Function to count the number of subarrays consisting of pronic numbers ; Stores the count of subarrays ; Stores the number of consecutive array elements which are pronic ; Traverse the array ; If i is pronic ; Return the total count ; Driver code
import java . lang . * ; class GFG { static boolean isPronic ( int n ) { int range = ( int ) Math . sqrt ( n ) ; for ( int i = 0 ; i < range + 1 ; i ++ ) { if ( i * ( i + 1 ) == n ) return true ; } return false ; } static int countSub ( int [ ] arr , int n ) { int ans = 0 ; int ispro = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPronic ( arr [ i ] ) ) ispro += 1 ; else ispro = 0 ; ans += ispro ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 12 , 3 , 4 } ; int n = arr . length ; System . out . print ( countSub ( arr , n ) ) ; } }
Efficient method to store a Lower Triangular Matrix using row | Java program for the above approach ; Dimensions of a matrix ; Structure of the efficient matrix ; Function to set the values in the Matrix ; Function to store the values in the Matrix ; Function to display the elements of the matrix ; Traverse the matrix ; Function to generate an efficient matrix ; Declare efficient Matrix ; Initialize the Matrix ; Set the values in matrix ; Return the matrix ; Driver Code ; Stores the efficient matrix ; Print the Matrix
class GFG { static int N = 5 ; static class Matrix { int [ ] A ; int size ; } ; static void Set ( Matrix mat , int i , int j , int x ) { if ( i >= j ) mat . A [ i * ( i - 1 ) / 2 + j - 1 ] = x ; } static int Get ( Matrix mat , int i , int j ) { if ( i >= j ) { return mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ; } else { return 0 ; } } static void Display ( Matrix mat ) { int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { if ( i >= j ) { System . out . printf ( " % d ▁ " , mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ) ; } else { System . out . printf ( "0 ▁ " ) ; } } System . out . printf ( "NEW_LINE"); } } static Matrix createMat ( int Mat [ ] [ ] ) { Matrix mat = new Matrix ( ) ; mat . size = N ; mat . A = new int [ ( mat . size * ( mat . size + 1 ) ) / 2 ] ; int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { Set ( mat , i , j , Mat [ i - 1 ] [ j - 1 ] ) ; } } return mat ; } public static void main ( String [ ] args ) { int Mat [ ] [ ] = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 2 , 0 , 0 , 0 } , { 1 , 2 , 3 , 0 , 0 } , { 1 , 2 , 3 , 4 , 0 } , { 1 , 2 , 3 , 4 , 5 } } ; Matrix mat = createMat ( Mat ) ; Display ( mat ) ; } }
Replace all array elements with the nearest power of its previous element | Java program for the above approach ; Function to calculate log x for given base ; Function to replace all array elements with the nearest power of previous adjacent nelement ; For the first element , set the last array element to its previous element ; Traverse the array ; Find K for which x ^ k is nearest to arr [ i ] ; Find the power to x nearest to arr [ i ] ; Update x ; Driver Code ; Function Call ; Display the array
import java . util . * ; class GFG { static int LOG ( int a , int b ) { return ( int ) ( Math . log ( a ) / Math . log ( b ) ) ; } static void repbyNP ( int [ ] arr , int n ) { int x = arr [ n - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = LOG ( arr [ i ] , x ) ; int temp = arr [ i ] ; if ( Math . abs ( Math . pow ( x , k ) - arr [ i ] ) < Math . abs ( Math . pow ( x , k + 1 ) - arr [ i ] ) ) arr [ i ] = ( int ) Math . pow ( x , k ) ; else arr [ i ] = ( int ) Math . pow ( x , k + 1 ) ; x = temp ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 3 , 11 } ; int n = arr . length ; repbyNP ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | package whatever do not write package name here ; Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver Code
import java . io . * ; class GFG { public static int lastElement ( int arr [ ] , int n ) { int sum = 0 ; int multiplier = n % 2 == 0 ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * multiplier ; multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 1 } ; int N = 4 ; System . out . println ( lastElement ( arr , N ) ) ; } }
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR | Java program to implement the above approach ; Function to maximize the count of pairs with even XOR possible in an array by given operations ; Stores count of odd array elements ; Traverse the array ; If arr [ i ] is odd ; Stores the total number of even pairs ; Driver Code ; Input ; Function call to count the number of pairs whose XOR is even
public class GFG { static int countPairs ( int [ ] arr , int N ) { int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } int ans = ( N - odd + odd / 2 - 1 ) + odd / 2 ; return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 6 , 1 , 3 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND | C # program for the above approach ; Function to count the number of pairs whose Bitwise AND is greater than the Bitwise XOR ; Stores the frequency of MSB of array elements ; Traverse the array ; Increment count of numbers having MSB at log ( arr [ i ] ) ; Stores total number of pairs ; Traverse the Map ; Return total count of pairs ; Driver Code
import java . util . * ; class GFG { static int countPairs ( int [ ] arr , int N ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( ( int ) ( Math . log ( arr [ i ] ) ) ) ) freq . put ( ( int ) ( Math . log ( arr [ i ] ) ) , ( int ) ( Math . log ( arr [ i ] ) ) + 1 ) ; else freq . put ( ( int ) ( Math . log ( arr [ i ] ) ) , 1 ) ; } int pairs = 0 ; for ( Map . Entry < Integer , Integer > item : freq . entrySet ( ) ) { pairs += item . getValue ( ) - 1 ; } return pairs ; } public static void main ( String [ ] args ) { int [ ] arr = { 12 , 9 , 15 , 7 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }