codeparrot/xlcost-text-to-code huggingface dataset:
Number of relations that are neither Reflexive nor Irreflexive on a Set | Java program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 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 res ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of relations that are neither reflexive nor irreflexive ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; 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 void countRelations ( int N ) { System . out . print ( ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; countRelations ( N ) ; } }
Minimum operations required to make all elements in an array of first N odd numbers equal | Java program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
class GFG { public static int minOperations ( int N ) { int [ ] arr = new int [ N ] ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } int mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } int ans = 0 ; for ( int i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minOperations ( N ) ) ; } }
Minimum operations required to make all elements in an array of first N odd numbers equal | Java program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver Code
class GFG { public static int minOperation ( int N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } int k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minOperation ( N ) ) ; } }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Java program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input
import java . util . * ; class GFG { static int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . print ( findXORS ( arr1 , arr2 , N , M ) ) ; } }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | Java program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int XORS1 = 0 ; int XORS2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return ( XORS1 & XORS2 ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . println ( findXORS ( arr1 , arr2 , N , M ) ) ; } }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Java program for the above approach ; Function to check if a number can be expressed as a power of 2 ; f N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of integers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver Code
import java . util . * ; class GFG { static boolean isPowerof2 ( int n ) { return ( ( n & ( n - 1 ) ) > 0 && n > 0 ) ; } static void countNum ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { boolean flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 100 ; countNum ( N ) ; } }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | Java program for the above approach ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void countNum ( int N ) { int ans = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 100 ; countNum ( N ) ; } }
Partition a Linked List into K continuous groups with difference in their sizes at most 1 | Java program for the above approach ; Link List Node ; Function to insert a node into the Linked List ; Allocate a new dynamic node ; Update new_node . val ; Stores the head_ref ; Update head_ref ; Function to split the linked list in K groups ; Stores the K groups ; If head is null ; Iterate until K is non - zero ; Stores the length of the linked list ; Stores the head node of the linked list ; Iterate over the linked list ; Update p ; Update N ; Iterate over the linked list ; Stores the length of the current group ; Stores the current node ; Stores the previous node ; If rem is greater than 0 ; Update p ; Decrement rem by 1 ; Iterate until x is non - zero ; If the last is equal to p ; Otherwise ; Join the link between last and the current element ; Update the last node ; Update p node ; Assign null to last . next ; Push the current linked list in ans ; Decrement K ; While K greater than 0 ; Update the value of ans ; Increment K ; Print the result ; Print the value ; Update ans [ i ] ; Driver Code
import java . util . * ; class GFG { static class ListNode { int val ; ListNode next ; } ; static ListNode push ( ListNode head_ref , int node_val ) { ListNode new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void splitListInParts ( ListNode head , int K ) { Vector < ListNode > ans = new Vector < ListNode > ( ) ; if ( head == null ) { while ( K -- > 0 ) ans . add ( null ) ; } int N = 0 ; ListNode p = head ; while ( p . next != null ) { p = p . next ; N ++ ; } int len = N / K ; int rem = N % K ; p = head ; while ( K > 0 && p . next != null ) { int x = len ; ListNode curr_head = p ; ListNode last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- > 0 ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . add ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . add ( null ) ; K -- ; } System . out . print ( " { " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( " { " ) ; while ( ans . get ( i ) != null ) { System . out . print ( ans . get ( i ) . val + " ▁ " ) ; ans . set ( i , ans . get ( i ) . next ) ; } System . out . print ( " } " ) ; if ( i != ans . size ( ) - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { ListNode root = new ListNode ( ) ; root = push ( root , 8 ) ; root = push ( root , 7 ) ; root = push ( root , 6 ) ; root = push ( root , 5 ) ; root = push ( root , 4 ) ; root = push ( root , 3 ) ; root = push ( root , 2 ) ; root = push ( root , 1 ) ; int K = 3 ; splitListInParts ( root , K ) ; } }
Replace array elements that contains K as a digit with the nearest power of K | Java program for the above approach ; Function to calculate the power of base nearest to x ; Stores logX to the base K ; Function to replace array elements with nearest power of K ; Traverse the array ; Convert integer into a string ; If K is found , then replace with the nearest power of K ; Print the array ; Driver Code ; Given array ; Given value of K ; Function call to replace array elements with nearest power of K
import java . io . * ; class GFG { static int nearestPow ( int x , int base1 ) { int k = ( int ) ( Math . log ( x ) / Math . log ( base1 ) ) ; if ( Math . abs ( Math . pow ( base1 , k ) - x ) < Math . abs ( Math . pow ( base1 , ( k + 1 ) ) - x ) ) return ( int ) Math . pow ( base1 , k ) ; else return ( int ) Math . pow ( base1 , ( k + 1 ) ) ; } static void replaceWithNearestPowerOfK ( int [ ] arr , int K , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = arr [ i ] ; String strEle = String . valueOf ( num ) ; for ( int c = 0 ; c < strEle . length ( ) ; c ++ ) { if ( ( strEle . charAt ( c ) - '0' ) == K ) { arr [ i ] = nearestPow ( arr [ i ] , K ) ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 432 , 953 , 232 , 333 } ; int n = arr . length ; int K = 3 ; replaceWithNearestPowerOfK ( arr , K , n ) ; } }
Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | Java approach for the above approach ; Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements divided by x ; Traverse the array ; Adding each array element ; Add the value ceil of arr [ i ] / x ; Find the ceil of the total sum divided by x ; Return absolute difference ; Driver Code
public class GFG { static int ceilDifference ( int arr [ ] , int n , int x ) { int totalSum = 0 ; int perElementSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; perElementSum += Math . ceil ( ( double ) ( arr [ i ] ) / ( double ) ( x ) ) ; } int totalCeilSum = ( int ) Math . ceil ( ( double ) ( totalSum ) / ( double ) ( x ) ) ; return Math . abs ( perElementSum - totalCeilSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 4 ; int N = arr . length ; System . out . println ( ceilDifference ( arr , N , K ) ) ; } }
Remove trailing zeros from the sum of two numbers ( Using Stack ) | Java program for the above approach ; Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0' ; Pop the top element ; Stores the resultant number without tailing 0 's ; While s is not empty ; Append top element of S in res ; Pop the top element of S ; Reverse the string res ; Driver Code ; Input ; Function Call
import java . io . * ; import java . util . * ; class GFG { static String removeTailing ( int A , int B ) { int N = A + B ; Stack < Character > s = new Stack < Character > ( ) ; String strsum = Integer . toString ( N ) ; for ( int i = 0 ; i < strsum . length ( ) ; i ++ ) { s . push ( strsum . charAt ( i ) ) ; } while ( s . peek ( ) == '0' ) { s . pop ( ) ; } String res = " " ; while ( s . empty ( ) == false ) { res = res + ( char ) s . peek ( ) ; s . pop ( ) ; } StringBuilder str = new StringBuilder ( ) ; str . append ( res ) ; str . reverse ( ) ; return str . toString ( ) ; } public static void main ( String [ ] args ) { int A = 130246 , B = 450164 ; System . out . println ( removeTailing ( A , B ) ) ; } }
Count number of triplets with product not exceeding a given number | Java program for the above approach ; Function to count the number of triplets whose product is at most N ; Stores the count of triplets ; Iterate over the range [ 0 , N ] ; Iterate over the range [ 0 , N ] ; If the product of pairs exceeds N ; Increment the count of possible triplets ; Return the total count ; Driver Code
import java . util . * ; class GFG { static int countTriplets ( int N ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i * j > N ) break ; ans += N / ( i * j ) ; } } return ans ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( countTriplets ( N ) ) ; } }
Sum of an Infinite Geometric Progression ( GP ) | Java program for the above approach ; Function to calculate the sum of an infinite Geometric Progression ; Case for Infinite Sum ; Store the sum of GP Series ; Print the value of sum ; Driver Code
import java . util . * ; class GFG { static void findSumOfGP ( double a , double r ) { if ( Math . abs ( r ) >= 1 ) { System . out . print ( " Infinite " ) ; return ; } double sum = a / ( 1 - r ) ; System . out . print ( sum ) ; } public static void main ( String [ ] args ) { double A = 1 , R = 0.5 ; findSumOfGP ( A , R ) ; } }
Number of Relations that are both Irreflexive and Antisymmetric on a Set | Java program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( long x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( int ) ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int numberOfRelations ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( numberOfRelations ( N ) ) ; } }
Count of numbers up to N having at least one prime factor common with N | Java program for the above approach ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the count of numbers having more than 1 factor with N ; Iterate over the range [ 1 , N ] ; If gcd is not 1 then increment the count ; Print the resultant count ; Driver Code
import java . util . * ; class GFG { static void countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( __gcd ( i , N ) != 1 ) count ++ ; } System . out . print ( count ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
Count of numbers up to N having at least one prime factor common with N | Java program for the above approach ; Function to calculate the value of Euler 's totient function ; Initialize result with N ; Find all prime factors of N and subtract their multiples ; Check if p is a prime factor ; If found to be true , then update N and result ; If N has a prime factor greater than sqrt ( N ) , then there can be at - most one such prime factor ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the resultant count ; Print the count ; Driver code
import java . util . * ; class GFG { static int phi ( int N ) { int result = N ; for ( int p = 2 ; p * p <= N ; ++ p ) { if ( N % p == 0 ) { while ( N % p == 0 ) N /= p ; result -= result / p ; } } if ( N > 1 ) result -= result / N ; return result ; } static void countNumbers ( int N ) { int count = N - phi ( N ) ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
Queries to update array by adding or multiplying array elements and print the element present at specified index | Java program for the above approach ; Function to modify the array by performing given queries ; Stores the multiplication of all integers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ; Query of type 0 ; Update the value of add ; Query of type 1 ; Update the value of mul ; Update the value of add ; Otherwise ; Store the element at index Q [ i ] [ 1 ] ; Print the result for the current query ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void Query ( int arr [ ] , int N , int Q [ ] [ ] ) { int mul = 1 ; int add = 0 ; for ( int i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { add = add + Q [ i ] [ 1 ] ; } else if ( Q [ i ] [ 0 ] == 1 ) { mul = mul * Q [ i ] [ 1 ] ; add = add * Q [ i ] [ 1 ] ; } else { int ans = arr [ Q [ i ] [ 1 ] ] * mul + add ; System . out . print ( ans + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 23 , 45 , 100 } ; int N = arr . length ; int Q [ ] [ ] = { { 1 , 2 } , { 0 , 10 } , { 2 , 3 } , { 1 , 5 } , { 2 , 4 } } ; Query ( arr , N , Q ) ; } }
Minimize cost of converting all array elements to Fibonacci Numbers | Java program for the above approach ; Function to find the N - th Fibonacci Number ; Find the value of a , b , and r ; Find the N - th Fibonacci ; Return the result ; Function to find the Fibonacci number which is nearest to X ; Calculate the value of n for X ; Return the nearest Fibonacci Number ; Function to find the minimum cost to convert all array elements to Fibonacci Numbers ; Stores the total minimum cost ; Traverse the given array arr [ ] ; Find the nearest Fibonacci Number ; Add the cost ; Return the final cost ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int nthFibo ( int n ) { double a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; double b = ( - 1 * ( Math . pow ( 5 , 0.5 ) ) + 1 ) / 2 ; double r = Math . pow ( 5 , 0.5 ) ; double ans = ( Math . pow ( a , n ) - Math . pow ( b , n ) ) / r ; return ( int ) ans ; } static int nearFibo ( int X ) { double a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; int n = ( int ) ( Math . log ( ( Math . pow ( 5 , 0.5 ) ) * X ) / Math . log ( a ) ) ; int nth = nthFibo ( n ) ; int nplus = nthFibo ( n + 1 ) ; if ( Math . abs ( X - nth ) < Math . abs ( X - nplus ) ) return nth ; else return nplus ; } static int getCost ( int arr [ ] , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int fibo = nearFibo ( arr [ i ] ) ; cost += Math . abs ( arr [ i ] - fibo ) ; } return cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 56 , 34 , 23 , 98 , 7 } ; int n = arr . length ; System . out . print ( getCost ( arr , n ) ) ; } }
Maximum sum of pairs that are at least K distance apart in an array | java program for the above approach ; Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int getMaxPairSum ( int [ ] arr , int N , int K ) { int [ ] preMax = new int [ N ] ; preMax [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . max ( preMax [ i - 1 ] , arr [ i ] ) ; } int res = Integer . MIN_VALUE ; for ( int i = K ; i < N ; i ++ ) { res = Math . max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 6 , 3 } ; int K = 3 ; int N = arr . length ; System . out . print ( getMaxPairSum ( arr , N , K ) ) ; } }
Program to calculate sum of an Infinite Arithmetic | Java program for the above approach ; Function to find the sum of the infinite AGP ; Stores the sum of infinite AGP ; Print the required sum ; Driver Code
class GFG { static void sumOfInfiniteAGP ( double a , double d , double r ) { double ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { double a = 0 , d = 1 , r = 0.5 ; sumOfInfiniteAGP ( a , d , r ) ; } }
Count pairs from an array having GCD equal to the minimum element in the pair | java program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; Increment count by 1 ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countPairs ( int arr [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 arr [ j ] % arr [ i ] == 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
Count pairs from an array having GCD equal to the minimum element in the pair | Java program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Stores the frequency of each array element ; Traverse the array arr [ ] ; Iterate over the Map mp ; Stores the array element ; Stores the count of array element x ; If x is 1 ; Increment res by N - 1 ; Increment res by yC2 ; Iterate over the range [ 2 , sqrt ( x ) ] ; If x is divisible by j ; Increment the value of res by mp [ j ] ; If j is not equal to x / j ; Increment res by mp [ x / j ] ; Return the resultant count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int CountPairs ( int arr [ ] , int N ) { int res = 0 ; Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Integer c = mp . get ( arr [ i ] ) ; mp . put ( arr [ i ] , ( c == null ) ? 1 : c + 1 ) ; } Iterator < Map . Entry < Integer , Integer > > itr = mp . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { Map . Entry < Integer , Integer > entry = itr . next ( ) ; int x = ( int ) entry . getKey ( ) ; int y = ( int ) entry . getValue ( ) ; if ( x == 1 ) { res += N - 1 ; continue ; } res += ( y * ( y - 1 ) ) / 2 ; for ( int j = 2 ; j <= Math . sqrt ( x ) ; j ++ ) { if ( x % j == 0 ) { res += mp . get ( j ) ; if ( j != x / j ) res += mp . get ( ( int ) x / j ) ; } } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 } ; int N = arr . length ; System . out . println ( CountPairs ( arr , N ) ) ; } }