dhyuti-n/xlcost-variable-roles huggingface dataset:
Java program:
import java . util . * ; class geeks { public static int sumoflength ( int [ ] arr , int n ) { Set < Integer > s = new HashSet < > ( ) ; int j = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n && ! s . contains ( arr [ j ] ) ) { s . add ( arr [ i ] ) ; j ++ ; } ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ; s . remove ( arr [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( sumoflength ( arr , n ) ) ; } }

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i
Class/struct names: geeks
Java program:
import java . util . HashMap ; class Test { static int countDistictSubarray ( int arr [ ] , int n ) { HashMap < Integer , Integer > vis = new HashMap < Integer , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( ! containsKey ( key ) ) return 0 ; return super . get ( key ) ; } } ; for ( int i = 0 ; i < n ; ++ i ) vis . put ( arr [ i ] , 1 ) ; int k = vis . size ( ) ; vis . clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { vis . put ( arr [ right ] , vis . get ( arr [ right ] ) + 1 ) ; if ( vis . get ( arr [ right ] ) == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; vis . put ( arr [ left ] , vis . get ( arr [ left ] ) - 1 ) ; if ( vis . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 2 , 3 } ; System . out . println ( countDistictSubarray ( arr , arr . length ) ) ; } }

Index/key variables: i, left, right
Accumulator variables: ans
Iterator variables: i, left
Class/struct names: Test
Java program:
class GFG { static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; } else { difference -- ; } if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 } ; int n = arr . length ; System . out . println ( " Total ▁ Number ▁ of ▁ Even - Odd " + " ▁ subarrays ▁ are ▁ " + countSubarrays ( arr , n ) ) ; } }

Index/key variables: difference, i
Accumulator variables: ans, difference, i
Iterator variables: i
Class/struct names: GFG
Java program:
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printPaths ( Node node ) { int path [ ] = new int [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } void printPathsRecur ( Node node , int path [ ] , int pathLen ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) printArray ( path , pathLen ) ; else { printPathsRecur ( node . left , path , pathLen ) ; printPathsRecur ( node . right , path , pathLen ) ; } } void printArray ( int ints [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) System . out . print ( ints [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . printPaths ( tree . root ) ; } }

Index/key variables: i, pathLen
Accumulator variables: i, pathLen
Iterator variables: i
Class/struct names: BinaryTree, Node
Java program:
class GFG { static final int MAX = 100 ; static boolean search ( int mat [ ] [ ] , int m , int n , int x ) { int i = m - 1 , j = 0 ; while ( i >= 0 && j < n ) { if ( mat [ i ] [ j ] == x ) return true ; if ( mat [ i ] [ j ] > x ) i -- ; else j ++ ; } return false ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } , { 50 , 60 , 70 , 80 } } ; if ( search ( mat , 5 , 4 , 29 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

Index/key variables: i, j
Accumulator variables: i, j
Class/struct names: GFG
Java program:
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; public class DistinctIds { static int distinctIds ( int arr [ ] , int n , int mi ) { Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int count = 0 ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) == false ) { m . put ( arr [ i ] , 1 ) ; size ++ ; } else m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } for ( Entry < Integer , Integer > mp : m . entrySet ( ) ) { if ( mp . getKey ( ) <= mi ) { mi -= mp . getKey ( ) ; count ++ ; } else return size - count ; } return size - count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 , 3 , 3 } ; int m = 3 ; System . out . println ( distinctIds ( arr , arr . length , m ) ) ; } }

Index/key variables: i
Accumulator variables: count, i, mi, size
Iterator variables: i, mp
Class/struct names: DistinctIds
Java program:
class GFG { static void printGreaterCount ( String str ) { int len = str . length ( ) , right [ ] = new int [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( str . charAt ( i ) < str . charAt ( j ) ) { right [ i ] ++ ; } } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( right [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }

Index/key variables: i, len
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int findLongestConseqSubseq ( int arr [ ] , int n ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) S . add ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . contains ( arr [ i ] ) ) { int j = arr [ i ] ; while ( S . contains ( j ) ) j ++ ; ans = Math . max ( ans , j - arr [ i ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 } ; int n = arr . length ; System . out . println ( findLongestConseqSubseq ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i
Class/struct names: GFG
Java program:
class GFG { static int appearsNBy3 ( int arr [ ] , int n ) { int count1 = 0 , count2 = 0 ; int first = Integer . MIN_VALUE ; ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) return first ; if ( count2 > n / 3 ) return second ; return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 } ; int n = arr . length ; System . out . println ( appearsNBy3 ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: count1, count2, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class Count { public static int count4Divisibiles ( int arr [ ] , int n ) { int freq [ ] = { 0 , 0 , 0 , 0 } ; int i = 0 ; int ans ; for ( i = 0 ; i < n ; i ++ ) ++ freq [ arr [ i ] % 4 ] ; ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 ; ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 ; ans += freq [ 1 ] * freq [ 3 ] ; return ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 7 , 5 } ; int n = 5 ; System . out . print ( count4Divisibiles ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Class/struct names: Count
Java program:
import java . util . * ; class GFG { static int findSubarraySum ( int arr [ ] , int n ) { int i , j ; int cArray [ ] = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) cArray [ i + 1 ] = cArray [ i ] + arr [ i ] ; Vector < Integer > subArrSum = new Vector < Integer > ( ) ; for ( i = 1 ; i <= n ; i ++ ) for ( j = i ; j <= n ; j ++ ) subArrSum . add ( cArray [ j ] - cArray [ i - 1 ] ) ; Collections . sort ( subArrSum ) ; int totalSum = 0 ; for ( i = 0 ; i < subArrSum . size ( ) - 1 ; i ++ ) { if ( subArrSum . get ( i ) == subArrSum . get ( i + 1 ) ) { j = i + 1 ; while ( subArrSum . get ( j ) == subArrSum . get ( i ) && j < subArrSum . size ( ) ) { subArrSum . set ( j , 0 ) ; j ++ ; } subArrSum . set ( i , 0 ) ; } } for ( i = 0 ; i < subArrSum . size ( ) ; i ++ ) totalSum += subArrSum . get ( i ) ; return totalSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . length ; System . out . print ( findSubarraySum ( arr , n ) ) ; } }

Index/key variables: i, j
Accumulator variables: i, j, totalSum
Iterator variables: i, j
Class/struct names: GFG
Java program:
class gfg { static int countMaxSetBits ( int left , int right ) { int max_count = - 1 , num = 0 ; for ( int i = left ; i <= right ; ++ i ) { int temp = i , cnt = 0 ; while ( temp > 0 ) { if ( temp % 2 == 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } public static void main ( String [ ] args ) { int l = 1 , r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . print ( countMaxSetBits ( l , r ) ) ; } }

Iterator variables: i
Class/struct names: gfg
Java program:
import java . util . * ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int res = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( m . containsKey ( sum ) ) { m . put ( sum , m . get ( sum ) + 1 ) ; } else { m . put ( sum , 1 ) ; } } } for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) if ( x . getValue ( ) == 1 ) res += x . getKey ( ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . length ; System . out . println ( findSubarraySum ( arr , n ) ) ; } }

Index/key variables: j
Accumulator variables: i, j, res, sum
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . Arrays ; class GFG { public static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ high ] , pivotloc = low ; for ( int i = low ; i <= high ; i ++ ) { if ( arr [ i ] < pivot ) { int temp = arr [ i ] ; arr [ i ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; pivotloc ++ ; } } int temp = arr [ high ] ; arr [ high ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; return pivotloc ; } public static int kthSmallest ( int [ ] arr , int low , int high , int k ) { int partition = partition ( arr , low , high ) ; if ( partition == k - 1 ) return arr [ partition ] ; else if ( partition < k - 1 ) return kthSmallest ( arr , partition + 1 , high , k ) ; else return kthSmallest ( arr , low , partition - 1 , k ) ; } public static void main ( String [ ] args ) { int [ ] array = new int [ ] { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int [ ] arraycopy = new int [ ] { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int kPosition = 3 ; int length = array . length ; if ( kPosition > length ) { System . out . println ( " Index ▁ out ▁ of ▁ bound " ) ; } else { System . out . println ( " K - th ▁ smallest ▁ element ▁ in ▁ array ▁ : ▁ " + kthSmallest ( arraycopy , 0 , length - 1 , kPosition ) ) ; } } }

Index/key variables: high, i, partition, pivotloc
Accumulator variables: i, pivotloc
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; class GFG { static void recaman ( int n ) { int arr [ ] = new int [ n ] ; arr [ 0 ] = 0 ; System . out . print ( arr [ 0 ] + " ▁ , " ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }

Index/key variables: i, j, n
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . io . * ; class Node { int data ; Node left , right ; Node ( int val ) { data = val ; left = right = null ; } } class GFG { static int ans , lh , rh , f ; static Node k ; public static Node Root ; static int height ( Node root ) { if ( root == null ) return 0 ; int left_height = height ( root . left ) ; int right_height = height ( root . right ) ; if ( ans < 1 + left_height + right_height ) { ans = 1 + left_height + right_height ; k = root ; lh = left_height ; rh = right_height ; } return 1 + Math . max ( left_height , right_height ) ; } static void printArray ( int [ ] ints , int len ) { int i ; if ( f == 0 ) { for ( i = len - 1 ; i >= 0 ; i -- ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } else if ( f == 1 ) { for ( i = 0 ; i < len ; i ++ ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } } static void printPathsRecur ( Node node , int [ ] path , int pathLen , int max ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) { if ( pathLen == max && ( f == 0 f == 1 ) ) { printArray ( path , pathLen ) ; f = 2 ; } } else { printPathsRecur ( node . left , path , pathLen , max ) ; printPathsRecur ( node . right , path , pathLen , max ) ; } } static void diameter ( Node root ) { if ( root == null ) return ; ans = Integer . MIN_VALUE ; lh = 0 ; rh = 0 ; f = 0 ; int height_of_tree = height ( root ) ; int [ ] lPath = new int [ 100 ] ; int pathlen = 0 ; printPathsRecur ( k . left , lPath , pathlen , lh ) ; System . out . print ( k . data + " ▁ " ) ; int [ ] rPath = new int [ 100 ] ; f = 1 ; printPathsRecur ( k . right , rPath , pathlen , rh ) ; } public static void main ( String [ ] args ) { GFG . Root = new Node ( 1 ) ; GFG . Root . left = new Node ( 2 ) ; GFG . Root . right = new Node ( 3 ) ; GFG . Root . left . left = new Node ( 4 ) ; GFG . Root . left . right = new Node ( 5 ) ; GFG . Root . left . right . left = new Node ( 6 ) ; GFG . Root . left . right . right = new Node ( 7 ) ; GFG . Root . left . left . right = new Node ( 8 ) ; GFG . Root . left . left . right . left = new Node ( 9 ) ; diameter ( Root ) ; } }

Index/key variables: i, pathLen
Accumulator variables: i, pathLen
Iterator variables: i
Class/struct names: GFG, Node
Java program:
import java . util . * ; class GFG { static void recaman ( int n ) { if ( n <= 0 ) return ; System . out . printf ( " % d , ▁ " , 0 ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ; s . add ( curr ) ; System . out . printf ( " % d , ▁ " , curr ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }

Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
class GFG { static int find_extra_element_index ( int [ ] arrA , int [ ] arrB ) { int extra_element = sum ( arrA ) - sum ( arrB ) ; return indexOf ( arrA , extra_element ) ; } static int sum ( int [ ] arr ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int indexOf ( int [ ] arr , int element ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arrA = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arrB = { 2 , 4 , 6 , 8 , 10 , 12 } ; System . out . println ( find_extra_element_index ( arrA , arrB ) ) ; } }

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { public static void findFibSubset ( Integer [ ] x ) { List < Integer > fib = new ArrayList < Integer > ( ) ; List < Integer > result = new ArrayList < Integer > ( ) ; Integer max = Collections . max ( Arrays . asList ( x ) ) ; Integer a = 0 ; Integer b = 1 ; while ( b < max ) { Integer c = a + b ; a = b ; b = c ; fib . add ( c ) ; } for ( Integer i = 0 ; i < x . length ; i ++ ) { if ( fib . contains ( x [ i ] ) ) { result . add ( x [ i ] ) ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { Integer [ ] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; findFibSubset ( a ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int sumOfDiv ( int x ) { int sum = 1 ; for ( int i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } static boolean isAmicable ( int a , int b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } static int countPairs ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( sumOfDiv ( arr [ i ] ) ) ) { int sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return count / 2 ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 220 , 284 , 1184 , 1210 , 2 , 5 } ; int n1 = arr1 . length ; System . out . println ( countPairs ( arr1 , n1 ) ) ; int arr2 [ ] = { 2620 , 2924 , 5020 , 5564 , 6232 , 6368 } ; int n2 = arr2 . length ; System . out . println ( countPairs ( arr2 , n2 ) ) ; } }

Index/key variables: i
Accumulator variables: count, i, sum
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minimumDeletion ( String s , int n ) { HashMap < Character , Integer > countMap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; countMap . put ( ch , countMap . getOrDefault ( ch , 0 ) + 1 ) ; } ArrayList < Integer > countMultiset = new ArrayList < > ( countMap . values ( ) ) ; Collections . sort ( countMultiset ) ; int ans = Integer . MAX_VALUE ; int i = 0 ; int m = countMultiset . size ( ) ; for ( int j : countMultiset ) { ans = Math . min ( ans , n - ( m - i ) * j ) ; i ++ ; } return ans ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . length ( ) ; System . out . println ( minimumDeletion ( S , N ) ) ; } }

Accumulator variables: i
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void findMinimumDistinct ( int A [ ] , int N , int K ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) mp . put ( A [ i ] , mp . getOrDefault ( A [ i ] , 0 ) + 1 ) ; int count = 0 ; int len = 0 ; ArrayList < Integer > counts = new ArrayList < > ( ) ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) counts . add ( i . getValue ( ) ) ; Collections . sort ( counts , ( a , b ) -> b - a ) ; for ( int i = 0 ; i < counts . size ( ) ; i ++ ) { if ( len >= K ) break ; len += counts . get ( i ) ; count ++ ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 1 , 3 , 2 , 3 , 4 , 5 , 4 } ; int K = 4 ; int N = A . length ; findMinimumDistinct ( A , N , K ) ; } }

Index/key variables: i
Accumulator variables: count, i, len
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int calculateDistance ( int arr [ ] , int N ) { Pair v [ ] = new Pair [ N ] ; for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Pair ( arr [ i ] , i ) ; Arrays . sort ( v , ( p1 , p2 ) -> { if ( p1 . first != p2 . first ) return p1 . first - p2 . first ; return p1 . second - p2 . second ; } ) ; int ans = 0 ; int last = 0 ; for ( Pair j : v ) { ans += Math . abs ( j . second - last ) ; last = j . second ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 5 , 1 } ; int N = arr . length ; System . out . println ( calculateDistance ( arr , N ) ) ; } }

Index/key variables: N, i
Accumulator variables: ans, i
Iterator variables: i, j
Class/struct names: GFG, Pair
Java program:
import java . util . * ; class GFG { static void reverse ( char [ ] a ) { int i , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static void sortArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { String s = Integer . toString ( arr [ i ] ) ; char [ ] str = s . toCharArray ( ) ; Arrays . sort ( str ) ; reverse ( str ) ; String string = new String ( str ) ; arr [ i ] = Integer . parseInt ( string ) ; int sr = ( int ) Math . sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) arr [ i ] = a ; else arr [ i ] = b ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 29 , 43 , 28 , 12 } ; int N = arr . length ; sortArr ( arr , N ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . Arrays ; public class GFG { static void minCost ( int arr [ ] , int N , int X ) { Arrays . sort ( arr ) ; int sum = 0 ; int cost = 0 ; int INT_MAX = Integer . MAX_VALUE ; int min_cost = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cost = Math . abs ( arr [ i ] ) * X + ( sum - Math . abs ( arr [ i ] ) * i ) ; sum += Math . abs ( arr [ i ] ) ; min_cost = Math . min ( min_cost , cost ) ; } } System . out . print ( min_cost ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 3 , - 2 , 4 , - 1 } ; int N = arr . length ; int X = 2 ; minCost ( arr , N , X ) ; } }

Index/key variables: i
Accumulator variables: i, sum
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . ArrayList ; class GFG { static int ans = 0 ; public static void countArraysUtil ( ArrayList < Integer > arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr . get ( i ) > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr . get ( 0 ) ; arr . remove ( 0 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( 0 , temp ) ; temp = arr . get ( arr . size ( ) - 1 ) ; arr . remove ( arr . size ( ) - 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( temp ) ; arr . set ( 0 , arr . get ( 0 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( 0 , arr . get ( 0 ) - 1 ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) - 1 ) ; } public static void countArrays ( ArrayList < Integer > arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) xorVal = xorVal ^ arr . get ( i ) ; countArraysUtil ( arr , X , K , xorVal ) ; System . out . println ( ans ) ; } public static void main ( String arg [ ] ) { int [ ] input = { 10 , 2 , 10 , 5 } ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; for ( int i : input ) { arr . add ( i ) ; } int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; } }

Accumulator variables: ans, cnt, i
Iterator variables: i
Class/struct names: GFG