dhyuti-n/xlcost-variable-roles huggingface dataset:
Java program:
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 ) ; } }

Index/key variables: i, p, v
Accumulator variables: i, j, zero1
Iterator variables: i, j
Class/struct names: UnsortedIntersectionUnion
Java program:
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 ) ) ; } }

Accumulator variables: i, sum
Iterator variables: i
Class/struct names: GFG
Java program:
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 ) ; } }

Index/key variables: hi, i, lo, mid
Accumulator variables: hi, i, lo, mid
Iterator variables: i
Class/struct names: countzot
Java program:
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 ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
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 ) ; } }

Index/key variables: i
Accumulator variables: cnt0, cnt1, cnt2, i
Iterator variables: i
Class/struct names: GFG
Java program:
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 ) ) ; } }

Index/key variables: i, j, n, sum
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int row = 6 ; static int col = 8 ; static int cal ( int hist [ ] , int n ) { Stack < Integer > s = new Stack < > ( ) ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . empty ( ) || hist [ s . peek ( ) ] <= hist [ i ] ) s . push ( i ++ ) ; else { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . empty ( ) == false ) { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } static int largestMatrix ( int a [ ] [ ] ) { int [ ] [ ] dp = new int [ row ] [ col ] ; for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 1 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } int ans = - 1 ; for ( int i = 0 ; i < row ; i ++ ) { int [ ] hist = new int [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = Math . max ( ans , cal ( hist , col ) ) ; } for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 0 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } for ( int i = 0 ; i < row ; i ++ ) { int [ ] hist = new int [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = Math . max ( ans , cal ( hist , col ) ) ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 , 1 , 1 , 0 , 0 } , { 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 } } ; System . out . println ( largestMatrix ( a ) ) ; } }

Index/key variables: col, i, j, row, tp
Accumulator variables: cnt, i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
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 ) ; } }

Index/key variables: e, i, s
Accumulator variables: e, i, s
Iterator variables: e, i, s
Class/struct names: Main
Java program:
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 ) ) ; } }

Index/key variables: i, j, k
Accumulator variables: count, i, j, k
Iterator variables: i, j, k
Class/struct names: GFG
Java program:
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 ) ) ; } }

Index/key variables: i, j, k
Accumulator variables: count
Iterator variables: i, j
Class/struct names: CountTriangles
Java program:
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 ) ; } }

Index/key variables: i, l, r
Accumulator variables: count, i, l, r
Iterator variables: i
Class/struct names: GFG
Java program:
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 ) ) ; } }

Accumulator variables: sum
Class/struct names: FindSumOfNotAdjacentNodes, Node
Java program:
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 ) ) ; } }

Index/key variables: flag, i, j
Accumulator variables: i, j, sum
Iterator variables: i, j
Class/struct names: GFG
Java program:
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 ; }

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Java program:
class AllPermutation { private final int Arr [ ] ; private int Indexes [ ] ; private int Increase ; public AllPermutation ( int arr [ ] ) { this . Arr = arr ; this . Increase = - 1 ; this . Indexes = new int [ this . Arr . length ] ; } public void GetFirst ( ) { this . Indexes = new int [ this . Arr . length ] ; for ( int i = 0 ; i < Indexes . length ; ++ i ) { this . Indexes [ i ] = i ; } this . Increase = 0 ; this . Output ( ) ; } public boolean HasNext ( ) { return this . Increase != ( this . Indexes . length - 1 ) ; } public void GetNext ( ) { if ( this . Increase == 0 ) { this . Swap ( this . Increase , this . Increase + 1 ) ; this . Increase += 1 ; while ( this . Increase < this . Indexes . length - 1 && this . Indexes [ this . Increase ] > this . Indexes [ this . Increase + 1 ] ) { ++ this . Increase ; } } else { if ( this . Indexes [ this . Increase + 1 ] > this . Indexes [ 0 ] ) { this . Swap ( this . Increase + 1 , 0 ) ; } else { int start = 0 ; int end = this . Increase ; int mid = ( start + end ) / 2 ; int tVal = this . Indexes [ this . Increase + 1 ] ; while ( ! ( this . Indexes [ mid ] < tVal && this . Indexes [ mid - 1 ] > tVal ) ) { if ( this . Indexes [ mid ] < tVal ) { end = mid - 1 ; } else { start = mid + 1 ; } mid = ( start + end ) / 2 ; } this . Swap ( this . Increase + 1 , mid ) ; } for ( int i = 0 ; i <= this . Increase / 2 ; ++ i ) { this . Swap ( i , this . Increase - i ) ; } this . Increase = 0 ; } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 ; i < this . Indexes . length ; ++ i ) { System . out . print ( this . Arr [ this . Indexes [ i ] ] ) ; System . out . print ( " ▁ " ) ; } System . out . println ( ) ; } private void Swap ( int p , int q ) { int tmp = this . Indexes [ p ] ; this . Indexes [ p ] = this . Indexes [ q ] ; this . Indexes [ q ] = tmp ; } } class AppDriver { public static void main ( String args [ ] ) { int [ ] arr = { 0 , 1 , 2 } ; AllPermutation perm = new AllPermutation ( arr ) ; perm . GetFirst ( ) ; while ( perm . HasNext ( ) ) { perm . GetNext ( ) ; } } }

Index/key variables: i, mid, p, q
Accumulator variables: Increase
Iterator variables: i
Class/struct names: AllPermutation, AppDriver
Java program:
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 ) ) ; } }

Index/key variables: i
Accumulator variables: R, i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
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 ) ) ; } }

Index/key variables: i, idx
Accumulator variables: ans, i, idx, total_pairs
Iterator variables: i
Class/struct names: Test
Java program:
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 ) ) ; } }

Index/key variables: i, j
Accumulator variables: count, i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
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 ) ) ; } }

Index/key variables: i, mid
Accumulator variables: count, i
Iterator variables: i
Class/struct names: GFG
Java program:
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 ; }

Index/key variables: MAX, i, x
Accumulator variables: count, i
Iterator variables: i
Java program:
import java . util . * ; class GFG { static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; Arrays . sort ( arr ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } 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 ) ) ; } }

Index/key variables: l, r
Accumulator variables: count, l, r
Class/struct names: GFG
Java program:
import java . io . * ; class GFG { static int BS ( int [ ] arr , int X , int low ) { int high = arr . length - 1 ; int ans = arr . length ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] >= X ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } static int countPairsWithDiffK ( int [ ] arr , int N , int k ) { int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; ++ i ) { int X = BS ( arr , arr [ i ] + k , i + 1 ) ; if ( X != N ) { int Y = BS ( arr , arr [ i ] + k + 1 , i + 1 ) ; count += Y - X ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 8 , 6 , 4 , 6 } ; int n = arr . length ; int k = 2 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }

Index/key variables: i, mid
Accumulator variables: count
Iterator variables: i
Class/struct names: GFG