codeparrot/xlcost-text-to-code huggingface dataset:
Find Union and Intersection of two unsorted arrays | Java program to find union and intersection using similar Hashing Technique without using any predefined Java Collections Time Complexity best case & avg case = O ( m + n ) Worst case = O ( nlogn ) package com . arrays . math ; ; Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Iterate first array ; Iterate second array ; Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; placeZeroes function ; Function to itreate array ; placeValue function ; Hashing collision happened increment position and do recursive call ; Driver code
public class UnsortedIntersectionUnion { public void findPosition ( int a [ ] , int b [ ] ) { int v = ( a . length + b . length ) ; int ans [ ] = new int [ v ] ; int zero1 = 0 ; int zero2 = 0 ; System . out . print ( " Intersection ▁ : ▁ " ) ; for ( int i = 0 ; i < a . length ; i ++ ) zero1 = iterateArray ( a , v , ans , i ) ; for ( int j = 0 ; j < b . length ; j ++ ) zero2 = iterateArray ( b , v , ans , j ) ; int zero = zero1 + zero2 ; placeZeros ( v , ans , zero ) ; printUnion ( v , ans , zero ) ; } private void printUnion ( int v , int [ ] ans , int zero ) { int zero1 = 0 ; System . out . print ( " Union : "); for ( int i = 0 ; i < v ; i ++ ) { if ( ( zero == 0 && ans [ i ] == 0 ) || ( ans [ i ] == 0 && zero1 > 0 ) ) continue ; if ( ans [ i ] == 0 ) zero1 ++ ; System . out . print ( ans [ i ] + " , " ) ; } } private void placeZeros ( int v , int [ ] ans , int zero ) { if ( zero == 2 ) { System . out . println ( "0" ) ; int d [ ] = { 0 } ; placeValue ( d , ans , 0 , 0 , v ) ; } if ( zero == 1 ) { int d [ ] = { 0 } ; placeValue ( d , ans , 0 , 0 , v ) ; } } private int iterateArray ( int [ ] a , int v , int [ ] ans , int i ) { if ( a [ i ] != 0 ) { int p = a [ i ] % v ; placeValue ( a , ans , i , p , v ) ; } else return 1 ; return 0 ; } private void placeValue ( int [ ] a , int [ ] ans , int i , int p , int v ) { p = p % v ; if ( ans [ p ] == 0 ) ans [ p ] = a [ i ] ; else { if ( ans [ p ] == a [ i ] ) System . out . print ( a [ i ] + " , " ) ; else { p = p + 1 ; placeValue ( a , ans , i , p , v ) ; } } } public static void main ( String args [ ] ) { int a [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int b [ ] = { 3 , 8 , 6 , 20 , 7 } ; UnsortedIntersectionUnion uiu = new UnsortedIntersectionUnion ( ) ; uiu . findPosition ( a , b ) ; } }
Minimum number of coins that can generate all the values in the given range | Java implementation of the approach ; Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver Code
import java . util . * ; class GFG { static int findCount ( int N ) { Vector list = new Vector ( ) ; int sum = 0 ; int i ; for ( i = 0 ; i < 20 ; i ++ ) { sum += Math . pow ( 2 , i ) ; list . add ( sum ) ; } for ( i = 0 ; i < 20 ; i ++ ) { if ( ( int ) list . get ( i ) >= N ) return ( list . indexOf ( list . get ( i ) ) + 1 ) ; } return 0 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( findCount ( N ) ) ; } }
Sort an array of 0 s , 1 s and 2 s | Java program to sort an array of 0 , 1 and 2 ; Sort the input array , the array is assumed to have values in { 0 , 1 , 2 } ; Utility function to print array arr [ ] ; Driver function to check for above functions
import java . io . * ; class countzot { static void sort012 ( int a [ ] , int arr_size ) { int lo = 0 ; int hi = arr_size - 1 ; int mid = 0 , temp = 0 ; while ( mid <= hi ) { switch ( a [ mid ] ) { case 0 : { temp = a [ lo ] ; a [ lo ] = a [ mid ] ; a [ mid ] = temp ; lo ++ ; mid ++ ; break ; } case 1 : mid ++ ; break ; case 2 : { temp = a [ mid ] ; a [ mid ] = a [ hi ] ; a [ hi ] = temp ; hi -- ; break ; } } } } static void printArray ( int arr [ ] , int arr_size ) { int i ; for ( i = 0 ; i < arr_size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int arr_size = arr . length ; sort012 ( arr , arr_size ) ; System . out . println ( " Array ▁ after ▁ seggregation ▁ " ) ; printArray ( arr , arr_size ) ; } }
Calculate the number of set bits for every number from 0 to N | Java implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; dp [ i ] will store the count of set bits in i ; Count of set bits in 0 is 0 ; For every number starting from 1 ; If current number is even ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) ; If current element is odd ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) + 1 ; Print the count of set bits in i ; Driver code
class GFG { static void findSetBits ( int n ) { int [ ] dp = new int [ n + 1 ] ; System . out . print ( dp [ 0 ] + " ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { dp [ i ] = dp [ i / 2 ] ; } else { dp [ i ] = dp [ i / 2 ] + 1 ; } System . out . print ( dp [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; findSetBits ( n ) ; } }
Sort an array of 0 s , 1 s and 2 s | Java implementation of the approach ; Utility function to print the contents of an array ; Function to sort the array of 0 s , 1 s and 2 s ; Count the number of 0 s , 1 s and 2 s in the array ; Update the array ; Store all the 0 s in the beginning ; Then all the 1 s ; Finally all the 2 s ; Print the sorted array ; Driver code
import java . io . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortArr ( int arr [ ] , int n ) { int i , cnt0 = 0 , cnt1 = 0 , cnt2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { switch ( arr [ i ] ) { case 0 : cnt0 ++ ; break ; case 1 : cnt1 ++ ; break ; case 2 : cnt2 ++ ; break ; } } i = 0 ; while ( cnt0 > 0 ) { arr [ i ++ ] = 0 ; cnt0 -- ; } while ( cnt1 > 0 ) { arr [ i ++ ] = 1 ; cnt1 -- ; } while ( cnt2 > 0 ) { arr [ i ++ ] = 2 ; cnt2 -- ; } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
Count of subsets with sum equal to X |  ; Initializing the matrix ; Initializing the first value of matrix ; If the value is greater than the sum ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int subsetSum ( int a [ ] , int n , int sum ) { int tab [ ] [ ] = new int [ n + 1 ] [ sum + 1 ] ; tab [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= sum ; i ++ ) tab [ 0 ] [ i ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) tab [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( a [ i - 1 ] > j ) tab [ i ] [ j ] = tab [ i - 1 ] [ j ] ; else { tab [ i ] [ j ] = tab [ i - 1 ] [ j ] + tab [ i - 1 ] [ j - a [ i - 1 ] ] ; } } } return tab [ n ] [ sum ] ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] = { 3 , 3 , 3 , 3 } ; int sum = 6 ; System . out . print ( subsetSum ( a , n , sum ) ) ; } }
Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | Java program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo
class Main { static void printUnsorted ( int arr [ ] , int n ) { int s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { System . out . println ( " The ▁ complete ▁ array ▁ is ▁ sorted " ) ; return ; } for ( e = n - 1 ; e > 0 ; e -- ) { if ( arr [ e ] < arr [ e - 1 ] ) break ; } max = arr [ s ] ; min = arr [ s ] ; for ( i = s + 1 ; i <= e ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; if ( arr [ i ] < min ) min = arr [ i ] ; } for ( i = 0 ; i < s ; i ++ ) { if ( arr [ i ] > min ) { s = i ; break ; } } for ( i = n - 1 ; i >= e + 1 ; i -- ) { if ( arr [ i ] < max ) { e = i ; break ; } } System . out . println ( " ▁ The ▁ unsorted ▁ subarray ▁ which " + " ▁ makes ▁ the ▁ given ▁ array ▁ sorted ▁ lies " + " ▁ between ▁ the ▁ indices ▁ " + s + " ▁ and ▁ " + e ) ; return ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 } ; int arr_size = arr . length ; printUnsorted ( arr , arr_size ) ; } }
Count the number of possible triangles | Java code to count the number of possible triangles using brute force approach ; Function to count all possible triangles with arr [ ] elements ; Count of triangles ; The three loops select three different values from array ; The innermost loop checks for the triangle property ; Sum of two sides is greater than the third ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int findNumberOfTriangles ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] > arr [ k ] && arr [ i ] + arr [ k ] > arr [ j ] && arr [ k ] + arr [ j ] > arr [ i ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 } ; int size = arr . length ; System . out . println ( " Total ▁ number ▁ of ▁ triangles ▁ possible ▁ is ▁ " + findNumberOfTriangles ( arr , size ) ) ; } }
Count the number of possible triangles | Java program to count number of triangles that can be formed from given array ; Function to count all possible triangles with arr [ ] elements ; Sort the array elements in non - decreasing order ; Initialize count of triangles ; Fix the first element . We need to run till n - 3 as the other two elements are selected from arr [ i + 1. . . n - 1 ] ; Initialize index of the rightmost third element ; Fix the second element ; Find the rightmost element which is smaller than the sum of two fixed elements The important thing to note here is , we use the previous value of k . If value of arr [ i ] + arr [ j - 1 ] was greater than arr [ k ] , then arr [ i ] + arr [ j ] must be greater than k , because the array is sorted . ; Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr [ i ] and arr [ j ] . All elements between arr [ j + 1 ] to arr [ k - 1 ] can form a triangle with arr [ i ] and arr [ j ] . One is subtracted from k because k is incremented one extra in above while loop . k will always be greater than j . If j becomes equal to k , then above loop will increment k , because arr [ k ] + arr [ i ] is always / greater than arr [ k ] ; Driver code
import java . io . * ; import java . util . * ; class CountTriangles { static int findNumberOfTriangles ( int arr [ ] ) { int n = arr . length ; Arrays . sort ( arr ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j ) count += k - j - 1 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 } ; System . out . println ( " Total ▁ number ▁ of ▁ triangles ▁ is ▁ " + findNumberOfTriangles ( arr ) ) ; } }
Count the number of possible triangles | Java implementation of the above approach ; CountTriangles function ; If it is possible with a [ l ] , a [ r ] and a [ i ] then it is also possible with a [ l + 1 ] . . a [ r - 1 ] , a [ r ] and a [ i ] ; checking for more possible solutions ; if not possible check for higher values of arr [ l ] ; Driver Code
import java . util . * ; class GFG { static void CountTriangles ( int [ ] A ) { int n = A . length ; Arrays . sort ( A ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else { l ++ ; } } } System . out . print ( " No ▁ of ▁ possible ▁ solutions : ▁ " + count ) ; } public static void main ( String [ ] args ) { int [ ] A = { 4 , 3 , 5 , 7 , 6 } ; CountTriangles ( A ) ; } }
Maximum sum of nodes in Binary tree such that no two are adjacent | Java program to find maximum sum from a subset of nodes of binary tree ; A binary tree node structure ; method returns maximum sum possible from subtrees rooted at grandChildrens of node ' node ' ; call for children of left child only if it is not NULL ; call for children of right child only if it is not NULL ; Utility method to return maximum sum rooted at node ' node ' ; If node is already processed then return calculated value from map ; take current node value and call for all grand children ; don 't take current node value and call for all children ; choose maximum from both above calls and store that in map ; Returns maximum sum from subset of nodes of binary tree under given constraints ; Driver code to test above methods
import java . util . HashMap ; public class FindSumOfNotAdjacentNodes { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; public static int sumOfGrandChildren ( Node node , HashMap < Node , Integer > mp ) { int sum = 0 ; if ( node . left != null ) sum += getMaxSumUtil ( node . left . left , mp ) + getMaxSumUtil ( node . left . right , mp ) ; if ( node . right != null ) sum += getMaxSumUtil ( node . right . left , mp ) + getMaxSumUtil ( node . right . right , mp ) ; return sum ; } public static int getMaxSumUtil ( Node node , HashMap < Node , Integer > mp ) { if ( node == null ) return 0 ; if ( mp . containsKey ( node ) ) return mp . get ( node ) ; int incl = node . data + sumOfGrandChildren ( node , mp ) ; int excl = getMaxSumUtil ( node . left , mp ) + getMaxSumUtil ( node . right , mp ) ; mp . put ( node , Math . max ( incl , excl ) ) ; return mp . get ( node ) ; } public static int getMaxSum ( Node node ) { if ( node == null ) return 0 ; HashMap < Node , Integer > mp = new HashMap < > ( ) ; return getMaxSumUtil ( node , mp ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . left . left = new Node ( 1 ) ; System . out . print ( getMaxSum ( root ) ) ; } }
Flip minimum signs of array elements to get minimum sum of positive elements possible | Java implementation of the above approach : ; Function to return the minimum number of elements whose sign must be flipped to get the positive sum of array elements as close to 0 as possible ; boolean variable used for toggling between maps ; Calculate the sum of all elements of the array ; Initializing first map ( dp [ 0 ] ) with INT_MAX because for i = 0 , there are no elements in the array to flip ; Base Case ; For toggling ; Required sum is minimum non - negative So , we iterate from i = 0 to sum and find the first i where dp [ flag ^ 1 ] [ i ] != INT_MAX ; In worst case we will flip max n - 1 elements ; Driver code
class GFG { public static int solve ( int [ ] A , int n ) { int [ ] [ ] dp = new int [ 2000 ] [ 2000 ] ; int flag = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += A [ i ] ; for ( int i = - sum ; i <= sum ; i ++ ) { try { dp [ 0 ] [ i ] = Integer . MAX_VALUE ; } catch ( Exception e ) { } } dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) { try { dp [ flag ] [ j ] = Integer . MAX_VALUE ; if ( j - A [ i - 1 ] <= sum && j - A [ i - 1 ] >= - sum ) dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] ; if ( j + A [ i - 1 ] <= sum && j + A [ i - 1 ] >= - sum && dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != Integer . MAX_VALUE ) dp [ flag ] [ j ] = Math . min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) ; } catch ( Exception e ) { } } flag = flag ^ 1 ; } for ( int i = 0 ; i <= sum ; i ++ ) { if ( dp [ flag ^ 1 ] [ i ] != Integer . MAX_VALUE ) return dp [ flag ^ 1 ] [ i ] ; } return n - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( solve ( arr , n ) ) ; } }
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x |
public static long countPairsBruteForce ( long X [ ] , long Y [ ] , int m , int n ) { long ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( Math . pow ( X [ i ] , Y [ j ] ) > Math . pow ( Y [ j ] , X [ i ] ) ) ans ++ ; return ans ; }
Maximum items that can be filled in K Knapsacks of given Capacity | package whatever do not write package name here ; 2 - d array to store if a state has been solved ; 2 - d array to store states of DP ; Vector to store power of variable ' C ' . ; function to compute the states ; Base case ; Checking if a state has been solved ; Setting a state as solved ; Recurrence relation ; Returning the solved state ; Function to initialize global variables and find the initial value of ' R ' ; Resizing the variables ; Variable to store the initial value of R ; Driver Code ; Input array ; number of knapsacks and capacity ; Performing required pre - computation ; finding the required answer
import java . io . * ; import java . util . * ; class GFG { static ArrayList < ArrayList < Boolean > > v = new ArrayList < ArrayList < Boolean > > ( ) ; static ArrayList < ArrayList < Integer > > dp = new ArrayList < ArrayList < Integer > > ( ) ; static ArrayList < Integer > exp_c = new ArrayList < Integer > ( ) ; static int FindMax ( int i , int r , int w [ ] , int n , int c , int k ) { if ( i >= n ) { return 0 ; } if ( v . get ( i ) . get ( r ) ) { return dp . get ( i ) . get ( r ) ; } v . get ( i ) . set ( r , true ) ; dp . get ( i ) . set ( r , FindMax ( i + 1 , r , w , n , c , k ) ) ; for ( int j = 0 ; j < k ; j ++ ) { int x = ( r / exp_c . get ( j ) ) % ( c + 1 ) ; if ( x - w [ i ] >= 0 ) { dp . get ( i ) . set ( r , Math . max ( dp . get ( i ) . get ( r ) , w [ i ] + FindMax ( i + 1 , r - w [ i ] * exp_c . get ( j ) , w , n , c , k ) ) ) ; } } return dp . get ( i ) . get ( r ) ; } static int PreCompute ( int n , int c , int k ) { for ( int i = 0 ; i < k ; i ++ ) { exp_c . add ( 0 ) ; } exp_c . set ( 0 , 1 ) ; for ( int i = 1 ; i < k ; i ++ ) { exp_c . set ( i , ( exp_c . get ( i - 1 ) * ( c + 1 ) ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { dp . add ( new ArrayList < Integer > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < ( exp_c . get ( k - 1 ) * ( c + 1 ) ) ; j ++ ) { dp . get ( i ) . add ( 0 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { v . add ( new ArrayList < Boolean > ( Arrays . asList ( new Boolean [ ( exp_c . get ( k - 1 ) * ( c + 1 ) ) ] ) ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { Collections . fill ( v . get ( i ) , Boolean . FALSE ) ; } int R = 0 ; for ( int i = 0 ; i < k ; i ++ ) { R += exp_c . get ( i ) * c ; } return R ; } public static void main ( String [ ] args ) { int w [ ] = { 3 , 8 , 9 } ; int k = 1 , c = 11 ; int n = w . length ; int r = PreCompute ( n , c , k ) ; System . out . println ( FindMax ( 0 , r , w , n , c , k ) ) ; } }
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x | Java program to finds number of pairs ( x , y ) in an array such that x ^ y > y ^ x ; Function to return count of pairs with x as one element of the pair . It mainly looks for all values in Y [ ] where x ^ Y [ i ] > Y [ i ] ^ x ; If x is 0 , then there cannot be any value in Y such that x ^ Y [ i ] > Y [ i ] ^ x ; If x is 1 , then the number of pais is equal to number of zeroes in Y [ ] ; Find number of elements in Y [ ] with values greater than x getting upperbound of x with binary search ; If we have reached here , then x must be greater than 1 , increase number of pairs for y = 0 and y = 1 ; Decrease number of pairs for x = 2 and ( y = 4 or y = 3 ) ; Increase number of pairs for x = 3 and y = 2 ; Function to returns count of pairs ( x , y ) such that x belongs to X [ ] , y belongs to Y [ ] and x ^ y > y ^ x ; To store counts of 0 , 1 , 2 , 3 and 4 in array Y ; Sort Y [ ] so that we can do binary search in it ; Initialize result ; Take every element of X and count pairs with it ; Driver method
import java . util . Arrays ; class Test { static int count ( int x , int Y [ ] , int n , int NoOfY [ ] ) { if ( x == 0 ) return 0 ; if ( x == 1 ) return NoOfY [ 0 ] ; int idx = Arrays . binarySearch ( Y , x ) ; int ans ; if ( idx < 0 ) { idx = Math . abs ( idx + 1 ) ; ans = Y . length - idx ; } else { while ( idx < n && Y [ idx ] == x ) { idx ++ ; } ans = Y . length - idx ; } ans += ( NoOfY [ 0 ] + NoOfY [ 1 ] ) ; if ( x == 2 ) ans -= ( NoOfY [ 3 ] + NoOfY [ 4 ] ) ; if ( x == 3 ) ans += NoOfY [ 2 ] ; return ans ; } static long countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int NoOfY [ ] = new int [ 5 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( Y [ i ] < 5 ) NoOfY [ Y [ i ] ] ++ ; Arrays . sort ( Y ) ; long total_pairs = 0 ; for ( int i = 0 ; i < m ; i ++ ) total_pairs += count ( X [ i ] , Y , n , NoOfY ) ; return total_pairs ; } public static void main ( String args [ ] ) { int X [ ] = { 2 , 1 , 6 } ; int Y [ ] = { 1 , 5 } ; System . out . println ( " Total ▁ pairs ▁ = ▁ " + countPairs ( X , Y , X . length , Y . length ) ) ; } }
Count all distinct pairs with difference equal to k | A simple Java program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver code
import java . util . * ; import java . io . * ; class GFG { static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
Count all distinct pairs with difference equal to k | A sorting base java program to count pairs with difference k ; Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver code
import java . util . * ; import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 , i ; Arrays . sort ( arr ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
Count all distinct pairs with difference equal to k | An efficient program to count pairs with difference k when the range numbers is small ; Initialize count ; Initialize empty hashmap . ; Insert array elements to hashmap
static int MAX = 100000 ; public static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; boolean hashmap [ MAX ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; if ( x - k >= 0 && hashmap [ x - k ] ) count ++ ; if ( x + k < MAX && hashmap [ x + k ] ) count ++ ; hashmap [ x ] = false ; } return count ; }