dhyuti-n/xlcost-variable-roles huggingface dataset:
Java program:
import java . util . * ; class GFG { static int maxdiff ( int arr [ ] , int n ) { Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) freq . put ( arr [ i ] , freq . get ( arr [ i ] ) == null ? 1 : freq . get ( arr [ i ] ) + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( freq . get ( arr [ i ] ) > freq . get ( arr [ j ] ) && arr [ i ] > arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ i ] ) - freq . get ( arr [ j ] ) ) ; else if ( freq . get ( arr [ i ] ) < freq . get ( arr [ j ] ) && arr [ i ] < arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ j ] ) - freq . get ( arr [ i ] ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = arr . length ; System . out . println ( maxdiff ( arr , n ) ) ; } }

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int maxdiff ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; int [ ] dist = new int [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dist [ j ++ ] = arr [ i ] ; if ( ! freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , 1 ) ; else freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } Arrays . sort ( dist ) ; int min_freq = n + 1 ; int ans = 0 ; for ( int i = 0 ; i < j ; i ++ ) { int cur_freq = freq . get ( dist [ i ] ) ; ans = Math . max ( ans , cur_freq - min_freq ) ; min_freq = Math . min ( min_freq , cur_freq ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = arr . length ; System . out . print ( maxdiff ( arr , n ) + "NEW_LINE"); } }

Index/key variables: i, n
Accumulator variables: i, j
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; import java . io . * ; class GFG { static int n , m , X ; public static int maxSum ( int [ ] [ ] grid ) { int prefsum [ ] [ ] = new int [ n ] [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int x = 0 ; x < m ; x ++ ) { if ( x == 0 ) prefsum [ i ] [ x ] = grid [ i ] [ x ] ; else prefsum [ i ] [ x ] = prefsum [ i ] [ x - 1 ] + grid [ i ] [ x ] ; } } int dp [ ] [ ] = new int [ n ] [ X + 1 ] ; for ( int dpp [ ] : dp ) Arrays . fill ( dpp , Integer . MIN_VALUE ) ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= Math . min ( m , X ) ; ++ i ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + grid [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 1 ; j <= X ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; for ( int x = 1 ; x <= Math . min ( j , m ) ; x ++ ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - x ] + prefsum [ i ] [ x - 1 ] ) ; } } } return dp [ n - 1 ] [ X ] ; } public static void main ( String [ ] args ) { n = 4 ; m = 4 ; X = 6 ; int grid [ ] [ ] = { { 3 , 2 , 6 , 1 } , { 1 , 9 , 2 , 4 } , { 4 , 1 , 3 , 9 } , { 3 , 8 , 2 , 1 } } ; int ans = maxSum ( grid ) ; System . out . println ( ans ) ; } }

Index/key variables: X, i, j, m, n, x
Accumulator variables: i, x
Iterator variables: i, j, x
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int findDiff ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = Math . max ( max_count , count ) ; min_count = Math . min ( min_count , count ) ; count = 0 ; } } return ( max_count - min_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = arr . length ; System . out . println ( findDiff ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: count, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; class GFG { static int min_operations ( int A [ ] , int n ) { if ( n == 0 ) return 0 ; int ans = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) { ans += A [ i ] - A [ i - 1 ] ; } } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int A [ ] = { 1 , 2 , 3 , 2 , 1 } ; System . out . println ( min_operations ( A , n ) ) ; } }

Index/key variables: i
Accumulator variables: ans, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int findDiff ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int max_count = 0 , min_count = n ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { max_count = Math . max ( max_count , x . getValue ( ) ) ; min_count = Math . min ( min_count , x . getValue ( ) ) ; } return ( max_count - min_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = arr . length ; System . out . println ( findDiff ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static boolean areIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new LinkedList < Node > ( ) ; Queue < Node > q2 = new LinkedList < Node > ( ) ; q1 . add ( root1 ) ; q2 . add ( root2 ) ; while ( ! q1 . isEmpty ( ) && ! q2 . isEmpty ( ) ) { Node n1 = q1 . peek ( ) ; Node n2 = q2 . peek ( ) ; if ( n1 . data != n2 . data ) return false ; q1 . remove ( ) ; q2 . remove ( ) ; if ( n1 . left != null && n2 . left != null ) { q1 . add ( n1 . left ) ; q2 . add ( n2 . left ) ; } else if ( n1 . left != null n2 . left != null ) return false ; if ( n1 . right != null && n2 . right != null ) { q1 . add ( n1 . right ) ; q2 . add ( n2 . right ) ; } else if ( n1 . right != null n2 . right != null ) return false ; } return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; if ( areIdentical ( root1 , root2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

Class/struct names: GfG, Node
Java program:
import java . util . * ; class GFG { static final int MAX_NODES = 100005 ; static int [ ] parent = new int [ MAX_NODES ] ; static int [ ] size_set = new int [ MAX_NODES ] ; static void make_set ( int v ) { parent [ v ] = v ; size_set [ v ] = 1 ; } static int find_set ( int v ) { if ( v == parent [ v ] ) return v ; return parent [ v ] = find_set ( parent [ v ] ) ; } static void union_set ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( size_set [ a ] < size_set [ b ] ) { a = a + b ; b = a - b ; a = a - b ; } parent [ b ] = a ; size_set [ a ] += size_set [ b ] ; } } static String check ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; return ( a == b ) ? " Yes " : " No " ; } public static void main ( String [ ] args ) { int n = 5 , m = 3 ; make_set ( 1 ) ; make_set ( 2 ) ; make_set ( 3 ) ; make_set ( 4 ) ; make_set ( 5 ) ; union_set ( 1 , 3 ) ; union_set ( 3 , 4 ) ; union_set ( 3 , 5 ) ; int q = 3 ; System . out . print ( check ( 1 , 5 ) + "NEW_LINE"); System . out . print ( check ( 3 , 2 ) + "NEW_LINE"); System . out . print ( check ( 5 , 2 ) + "NEW_LINE"); } }

Index/key variables: MAX_NODES, a, b, v
Class/struct names: GFG
Java program:
class GFG { public static long count ( int N , int A , int B ) { if ( N == 1 ) { return 9 / B + 1 ; } int max_sum = 9 * N ; int odd_count = N / 2 + N % 2 ; int even_count = N - odd_count ; long dp [ ] [ ] = new long [ even_count ] [ max_sum + 1 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) dp [ 0 ] [ i % A ] ++ ; for ( int i = 1 ; i < even_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 ] [ k ] > 0 ) dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] ; } } } long dp1 [ ] [ ] = new long [ odd_count ] [ max_sum + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp1 [ 0 ] [ i % B ] ++ ; for ( int i = 1 ; i < odd_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp1 [ i - 1 ] [ k ] > 0 ) dp1 [ i ] [ ( j + k ) % B ] += dp1 [ i - 1 ] [ k ] ; } } } return dp [ even_count - 1 ] [ 0 ] * dp1 [ odd_count - 1 ] [ 0 ] ; } public static void main ( String [ ] args ) { int N = 2 , A = 2 , B = 5 ; System . out . println ( count ( N , A , B ) ) ; } }

Index/key variables: even_count, i, k, odd_count
Accumulator variables: i, j, k
Iterator variables: i, j, k
Class/struct names: GFG
Java program:
import java . io . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }

Index/key variables: i, j
Accumulator variables: SubsetSum_1, SubsetSum_2, i, j
Iterator variables: i, j
Boolean variables: isSingleOccurance
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int NN = ( int ) 3e5 ; static Vector < Integer > [ ] G = new Vector [ NN + 1 ] ; static int dfs ( int node , int A , int [ ] subtree_size , int [ ] visited , int [ ] check_subtree ) { visited [ node ] = 1 ; subtree_size [ node ] = 1 ; if ( node == A ) { check_subtree [ node ] = 1 ; } else check_subtree [ node ] = 0 ; for ( int v : G [ node ] ) { if ( visited [ v ] == 0 ) { subtree_size [ node ] += dfs ( v , A , subtree_size , visited , check_subtree ) ; check_subtree [ node ] = check_subtree [ node ] | check_subtree [ v ] ; } } return subtree_size [ node ] ; } static void addedge ( int node1 , int node2 ) { G [ node1 ] . add ( node2 ) ; G [ node2 ] . add ( node1 ) ; } static int numberOfPairs ( int N , int B , int A ) { int [ ] subtree_size = new int [ N + 1 ] ; int [ ] visited = new int [ N + 1 ] ; int [ ] check_subtree = new int [ N + 1 ] ; dfs ( B , A , subtree_size , visited , check_subtree ) ; int difference = 0 ; for ( int v : G [ B ] ) { if ( check_subtree [ v ] > 0 ) { difference = N - subtree_size [ v ] ; break ; } } return ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ; } public static void main ( String [ ] args ) { int N = 9 ; int X = 5 , Y = 3 ; for ( int i = 0 ; i < G . length ; i ++ ) G [ i ] = new Vector < Integer > ( ) ; addedge ( 0 , 2 ) ; addedge ( 1 , 2 ) ; addedge ( 2 , 3 ) ; addedge ( 3 , 4 ) ; addedge ( 4 , 6 ) ; addedge ( 4 , 5 ) ; addedge ( 5 , 7 ) ; addedge ( 5 , 8 ) ; System . out . print ( numberOfPairs ( N , Y , X ) ) ; } }

Index/key variables: A, B, i, node, node1, node2, v
Accumulator variables: i
Iterator variables: i, v
Class/struct names: GFG
Java program:
class GFG { static int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) Math . log ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ( n & ( 1 << i ) ) <= 0 ) { m += ( 1 << i ) ; } } return m ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( equalXORandOR ( n ) ) ; } }

Accumulator variables: i, m
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; import java . util . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: i, result
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static long findNewString ( String s ) { long ans = 0 ; int [ ] freq = new int [ 26 ] ; int n = s . length ( ) ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; ++ i ) { ans += freq [ s . charAt ( i ) - ' a ' ] ; freq [ s . charAt ( i ) - ' a ' ] ++ ; } return ans ; } public static void main ( String [ ] args ) { String s = " aaabaaa " ; System . out . print ( findNewString ( s ) ) ; } }

Accumulator variables: ans
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { public static int maxDiff ( int arr [ ] , int n ) { HashMap < Integer , Integer > hashPositive = new HashMap < > ( ) ; HashMap < Integer , Integer > hashNegative = new HashMap < > ( ) ; int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . containsKey ( arr [ i ] ) ) { hashPositive . replace ( arr [ i ] , hashPositive . get ( arr [ i ] ) + 1 ) ; } else { hashPositive . put ( arr [ i ] , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . containsKey ( arr [ i ] ) ) { if ( hashPositive . get ( arr [ i ] ) == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { hashNegative . replace ( Math . abs ( arr [ i ] ) , hashNegative . get ( Math . abs ( arr [ i ] ) ) + 1 ) ; } else { hashNegative . put ( Math . abs ( arr [ i ] ) , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { if ( hashNegative . get ( Math . abs ( arr [ i ] ) ) == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . print ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: SubsetSum_1, SubsetSum_2, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { public static String proofreadSpell ( String str ) { Vector < Character > result = new Vector < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { result . add ( str . charAt ( i ) ) ; int n = result . size ( ) ; if ( n >= 3 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 1 ) == result . get ( n - 3 ) ) { result . remove ( result . size ( ) - 1 ) ; } } n = result . size ( ) ; if ( n >= 4 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 3 ) == result . get ( n - 4 ) ) { result . remove ( result . size ( ) - 1 ) ; } } } String resultStr = " " ; for ( Character c : result ) { resultStr += c ; } return resultStr ; } public static void main ( String [ ] args ) { String str = " hello " ; System . out . println ( proofreadSpell ( str ) ) ; } }

Accumulator variables: i, resultStr
Iterator variables: c, i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }

Index/key variables: j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size ( ) < k ) hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . size ( ) == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size ( ) < k ) break ; while ( hm . size ( ) == k ) { if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; i ++ ; if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; public class GfG { public static int sum ( int a [ ] , int n ) { Map < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; int ans = 0 , pre_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . containsKey ( a [ i ] - 1 ) ) ans -= cnt . get ( a [ i ] - 1 ) ; if ( cnt . containsKey ( a [ i ] + 1 ) ) ans += cnt . get ( a [ i ] + 1 ) ; if ( cnt . containsKey ( a [ i ] ) ) { cnt . put ( a [ i ] , cnt . get ( a [ i ] ) + 1 ) ; } else { cnt . put ( a [ i ] , 1 ) ; } } return ans ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = a . length ; System . out . println ( sum ( a , n ) ) ; } }

Index/key variables: i
Accumulator variables: ans, i, pre_sum
Iterator variables: i
Class/struct names: GfG
Java program:
import java . util . * ; class GFG { public static int minDistance ( int a [ ] , int n ) { int max = - 1 , min = Integer . MAX_VALUE ; int min_index = - 1 , max_index = - 1 ; int min_dist = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . min ( min_dist , Math . abs ( min_index - max_index ) ) ; } return min_dist ; } public static void main ( String [ ] args ) { int n = 12 ; int a [ ] = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 } ; System . out . println ( minDistance ( a , n ) ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void printDiagonal ( int K , int N , int [ ] [ ] M ) { int startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { System . out . print ( M [ startrow ] [ startcol ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 4 ; int [ ] [ ] M = { { 4 , 7 , 8 } , { 9 , 2 , 3 } , { 0 , 4 , 1 } } ; printDiagonal ( K , N , M ) ; } }

Index/key variables: startcol, startrow
Accumulator variables: startcol, startrow
Class/struct names: GFG
Java program:
class GFG { public static void longestSubstring ( String str , char X , char Y ) { int N = str . length ( ) ; int start = 0 ; int end = N - 1 ; int xPos = 0 ; int yPos = 0 ; while ( true ) { if ( str . charAt ( start ) == X ) { xPos = start ; break ; } start ++ ; } while ( true ) { if ( str . charAt ( end ) == Y ) { yPos = end ; break ; } end -- ; } int length = ( yPos - xPos ) + 1 ; System . out . print ( length ) ; } public static void main ( String [ ] args ) { String str = " HASFJGHOGAKZZFEGA " ; char X = ' A ' , Y = ' Z ' ; longestSubstring ( str , X , Y ) ; } }

Accumulator variables: end, start
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int findParent ( int height , int node ) { int start = 1 ; int end = ( int ) Math . pow ( 2 , height ) - 1 ; if ( end == node ) return - 1 ; while ( node >= 1 ) { end = end - 1 ; int mid = start + ( end - start ) / 2 ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } return - 1 ; } public static void main ( String [ ] args ) { int height = 4 ; int node = 6 ; int k = findParent ( height , node ) ; System . out . print ( k ) ; } }

Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { if ( n < 2 ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } int firstSmallest = Integer . MAX_VALUE ; int secondSmallest = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < firstSmallest ) { secondSmallest = firstSmallest ; firstSmallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest && arr [ i ] != firstSmallest ) secondSmallest = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != firstSmallest ) arr [ i ] = firstSmallest ; else arr [ i ] = secondSmallest ; } for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int countSubarrWithEqualZeroAndOne ( int arr [ ] , int n ) { Map < Integer , Integer > um = new HashMap < > ( ) ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; um . put ( curr_sum , um . get ( curr_sum ) == null ? 1 : um . get ( curr_sum ) + 1 ) ; } int count = 0 ; for ( Map . Entry < Integer , Integer > itr : um . entrySet ( ) ) { if ( itr . getValue ( ) > 1 ) count += ( ( itr . getValue ( ) * ( itr . getValue ( ) - 1 ) ) / 2 ) ; } if ( um . containsKey ( 0 ) ) count += um . get ( 0 ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ = ▁ " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: count, curr_sum, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int lenOfLongSubarr ( int arr [ ] , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Length ▁ = ▁ " + lenOfLongSubarr ( arr , n ) ) ; } }

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