dhyuti-n/xlcost-variable-roles huggingface dataset:
Java program:
import java . io . * ; class GFG { public static int calculateSum ( String arr [ ] , int n ) { if ( n == 0 ) return 0 ; String s = arr [ 0 ] ; int value = Integer . parseInt ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = Integer . parseInt ( s ) ; char operation = arr [ i - 1 ] . charAt ( 0 ) ; if ( operation == ' + ' ) sum += value ; else sum -= value ; } return sum ; } public static void main ( String [ ] args ) { String arr [ ] = { "3" , " + " , "4" , " - " , "7" , " + " , "13" } ; int n = arr . length ; System . out . println ( calculateSum ( arr , n ) ) ; } }

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . Arrays ; class GFG { static int MAX_CHAR = 26 ; static boolean twoStrings ( String s1 , String s2 ) { boolean v [ ] = new boolean [ MAX_CHAR ] ; Arrays . fill ( v , false ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) v [ s1 . charAt ( i ) - ' a ' ] = true ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) if ( v [ s2 . charAt ( i ) - ' a ' ] ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = " hello " ; String str2 = " world " ; if ( twoStrings ( str1 , str2 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

Index/key variables: MAX_CHAR
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . lang . * ; import java . io . * ; import java . util . Arrays ; class Geek { public static void LargestString ( String na [ ] ) { int N = na . length ; int c [ ] = new int [ N ] ; int m = 0 ; for ( int j = 0 ; j < N ; j ++ ) { boolean character [ ] = new boolean [ 26 ] ; for ( int k = 0 ; k < na [ j ] . length ( ) ; k ++ ) { int x = na [ j ] . charAt ( k ) - ' A ' ; if ( ( na [ j ] . charAt ( k ) != ' ▁ ' ) && ( character [ x ] == false ) ) { c [ j ] ++ ; character [ x ] = true ; } } if ( c [ j ] > c [ m ] ) m = j ; } System . out . println ( na [ m ] ) ; } public static void main ( String [ ] args ) { String na [ ] = { " BOB " , " A ▁ AB ▁ C ▁ JOHNSON " , " ANJALI " , " ASKRIT " , " ARMAN ▁ MALLIK " } ; LargestString ( na ) ; } }

Index/key variables: N, j, m, x
Accumulator variables: j, k
Iterator variables: j, k
Class/struct names: Geek
Java program:
class GFG { static int LongestWordLength ( String str ) { String [ ] words = str . split ( " ▁ " ) ; int length = 0 ; for ( String word : words ) { if ( length < word . length ( ) ) { length = word . length ( ) ; } } return length ; } public static void main ( String args [ ] ) { String str = " I ▁ am ▁ an ▁ intern ▁ at ▁ geeksforgeeks " ; System . out . println ( LongestWordLength ( str ) ) ; } }

Iterator variables: word
Class/struct names: GFG
Java program:
class GFG { static String morseEncode ( char x ) { switch ( x ) { case ' a ' : return " . - " ; case ' b ' : return " - . . . " ; case ' c ' : return " - . - . " ; case ' d ' : return " - . . " ; case ' e ' : return " . " ; case ' f ' : return " . . - . " ; case ' g ' : return " - - . " ; case ' h ' : return " . . . . " ; case ' i ' : return " . . " ; case ' j ' : return " . - - - " ; case ' k ' : return " - . - " ; case ' l ' : return " . - . . " ; case ' m ' : return " - - " ; case ' n ' : return " - . " ; case ' o ' : return " - - - " ; case ' p ' : return " . - - . " ; case ' q ' : return " - - . - " ; case ' r ' : return " . - . " ; case ' s ' : return " . . . " ; case ' t ' : return " - " ; case ' u ' : return " . . - " ; case ' v ' : return " . . . - " ; case ' w ' : return " . - - " ; case ' x ' : return " - . . - " ; case ' y ' : return " - . - - " ; case ' z ' : return " - - . . " ; case '1' : return " . - - - - " ; case '2' : return " . . - - - " ; case '3' : return " . . . - - " ; case '4' : return " . . . . - " ; case '5' : return " . . . . . " ; case '6' : return " - . . . . " ; case '7' : return " - - . . . " ; case '8' : return " - - - . . " ; case '9' : return " - - - - . " ; case '0' : return " - - - - - " ; } return " " ; } static void morseCode ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) System . out . print ( morseEncode ( s . charAt ( i ) ) ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; morseCode ( s ) ; } }

Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
class GFG { static void polybiusCipher ( String s ) { int row , col ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { row = ( int ) Math . ceil ( ( s . charAt ( i ) - ' a ' ) / 5 ) + 1 ; col = ( ( s . charAt ( i ) - ' a ' ) % 5 ) + 1 ; if ( s . charAt ( i ) == ' k ' ) { row = row - 1 ; col = 5 - col + 1 ; } else if ( s . charAt ( i ) >= ' j ' ) { if ( col == 1 ) { col = 6 ; row = row - 1 ; } col = col - 1 ; } System . out . print ( row + " " + col ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; polybiusCipher ( s ) ; } }

Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . Arrays ; class GFG { static final int MAX_CHAR = 26 ; static int minRemoval ( String str ) { int hash [ ] = new int [ MAX_CHAR ] ; Arrays . fill ( hash , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) hash [ str . charAt ( i ) - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( hash [ i ] % 2 == 1 ) count ++ ; return ( count == 0 ) ? 0 : count - 1 ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( minRemoval ( str ) ) ; } }

Index/key variables: MAX_CHAR, i
Accumulator variables: count, i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int MAX_VERTEX = 101 ; static int [ ] Arr = new int [ MAX_VERTEX ] ; static int [ ] size = new int [ MAX_VERTEX ] ; static void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { Arr [ i ] = i ; size [ i ] = 1 ; } } static int find ( int i ) { while ( Arr [ i ] != i ) { Arr [ i ] = Arr [ Arr [ i ] ] ; i = Arr [ i ] ; } return i ; } static void _union ( int xr , int yr ) { if ( size [ xr ] < size [ yr ] ) { Arr [ xr ] = Arr [ yr ] ; size [ yr ] += size [ xr ] ; } else { Arr [ yr ] = Arr [ xr ] ; size [ xr ] += size [ yr ] ; } } static int isCycle ( Vector < Integer > adj [ ] , int V ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < adj [ i ] . size ( ) ; j ++ ) { int x = find ( i ) ; int y = find ( adj [ i ] . get ( j ) ) ; if ( x == y ) return 1 ; _union ( x , y ) ; } } return 0 ; } public static void main ( String [ ] args ) { int V = 3 ; initialize ( V ) ; Vector < Integer > [ ] adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 0 ] . add ( 1 ) ; adj [ 0 ] . add ( 2 ) ; adj [ 1 ] . add ( 2 ) ; if ( isCycle ( adj , V ) == 1 ) System . out . print ( "Graph contains Cycle.NEW_LINE"); else System . out . print ( "Graph does not contain Cycle.NEW_LINE"); } }

Index/key variables: MAX_VERTEX, V, i, xr, yr
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int MAX_LEN = 1005 ; static int MAX_CHAR = 26 ; static int [ ] [ ] cnt = new int [ MAX_LEN ] [ MAX_CHAR ] ; static void preProcess ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ i ] [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) cnt [ i ] [ j ] += cnt [ i - 1 ] [ j ] ; } } static int findCharFreq ( int l , int r , char c ) { return ( cnt [ r ] [ ( c ) - 97 ] - cnt [ l ] [ ( c ) - 97 ] ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int Q = 4 ; preProcess ( s ) ; System . out . println ( findCharFreq ( 0 , 5 , ' e ' ) ) ; System . out . println ( findCharFreq ( 2 , 6 , ' f ' ) ) ; System . out . println ( findCharFreq ( 4 , 7 , ' m ' ) ) ; System . out . println ( findCharFreq ( 0 , 12 , ' e ' ) ) ; } }

Index/key variables: MAX_CHAR, MAX_LEN, i, j, l, r
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . io . * ; import java . util . * ; class GfG { static int findLUSlength ( String a , String b ) { if ( a . equals ( b ) == true ) return 0 ; return Math . max ( a . length ( ) , b . length ( ) ) ; } public static void main ( String [ ] args ) { String a = " abcdabcd " , b = " abcabc " ; System . out . println ( findLUSlength ( a , b ) ) ; } }

Class/struct names: GfG
Java program:
import java . io . * ; class ns { public static void swapTwoHalves ( String a , String b ) { int la = a . length ( ) ; int lb = b . length ( ) ; String c = a . substring ( 0 , la / 2 ) + b . substring ( lb / 2 , lb ) ; String d = b . substring ( 0 , lb / 2 ) + a . substring ( la / 2 , la ) ; System . out . println ( c + "NEW_LINE" + d); } public static void main ( String args [ ] ) { String a = " remuneration " ; String b = " day " ; swapTwoHalves ( a , b ) ; } }

Class/struct names: ns
Java program:
import java . io . * ; class msc { public static void solve ( String a , String b , int k ) { String s = " " ; int la = a . length ( ) ; int lb = b . length ( ) ; int l = la + lb ; int indexa = 0 , indexb = 0 ; while ( l > 0 ) { int pa = 0 , pb = 0 ; if ( la - indexa >= k ) { s = s + a . substring ( indexa , indexa + k ) ; indexa = indexa + k ; pa = k ; } ''		Please refer below link for details of library function https : www . geeksforgeeks . org / stdsubstr - in - ccpp / ''' else if ( la - indexa < k && la - indexa > 0 ) { s = s + a . substring ( indexa , la ) ; pa = la - indexa ; indexa = la ; } else if ( indexa >= la ) pa = 0 ; if ( lb - indexb >= k ) { s = s + b . substring ( indexb , indexb + k ) ; pb = k ; indexb = indexb + k ; } else if ( lb - indexb < k && lb - indexb > 0 ) { s = s + b . substring ( indexb , lb ) ; pb = lb - indexb ; indexb = lb ; } else if ( indexb >= lb ) pb = 0 ; l = l - pa - pb ; } System . out . println ( s ) ; } public static void main ( String args [ ] ) throws IOException { String a = " determination " , b = " stance " ; int k = 3 ; solve ( a , b , k ) ; } }

Class/struct names: msc
Java program:
import java . util . * ; class GFG { static class Node { Node left ; int data ; Node right ; } ; static void modifiedLevelOrder ( Node node ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { System . out . print ( node . data ) ; return ; } Queue < Node > myQueue = new LinkedList < > ( ) ; Stack < Node > myStack = new Stack < > ( ) ; Node temp = null ; int sz ; int ct = 0 ; boolean rightToLeft = false ; myQueue . add ( node ) ; while ( ! myQueue . isEmpty ( ) ) { ct ++ ; sz = myQueue . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { temp = myQueue . peek ( ) ; myQueue . remove ( ) ; if ( rightToLeft == false ) System . out . print ( temp . data + " ▁ " ) ; else myStack . push ( temp ) ; if ( temp . left != null ) myQueue . add ( temp . left ) ; if ( temp . right != null ) myQueue . add ( temp . right ) ; } if ( rightToLeft == true ) { while ( ! myStack . isEmpty ( ) ) { temp = myStack . peek ( ) ; myStack . pop ( ) ; System . out . print ( temp . data + " ▁ " ) ; } } if ( ct == 2 ) { rightToLeft = ! rightToLeft ; ct = 0 ; } System . out . print ( "NEW_LINE"); } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }

Accumulator variables: ct, i
Iterator variables: i
Boolean variables: rightToLeft
Class/struct names: GFG, Node
Java program:
import java . util . * ; class GFG { static int minnode ( int n , int keyval [ ] , boolean mstset [ ] ) { int mini = Integer . MAX_VALUE ; int mini_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mstset [ i ] == false && keyval [ i ] < mini ) { mini = keyval [ i ] ; mini_index = i ; } } return mini_index ; } static void findcost ( int n , int city [ ] [ ] ) { int parent [ ] = new int [ n ] ; int keyval [ ] = new int [ n ] ; boolean mstset [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { keyval [ i ] = Integer . MAX_VALUE ; mstset [ i ] = false ; } parent [ 0 ] = - 1 ; keyval [ 0 ] = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = minnode ( n , keyval , mstset ) ; mstset [ u ] = true ; for ( int v = 0 ; v < n ; v ++ ) { if ( city [ u ] [ v ] > 0 && mstset [ v ] == false && city [ u ] [ v ] < keyval [ v ] ) { keyval [ v ] = city [ u ] [ v ] ; parent [ v ] = u ; } } } int cost = 0 ; for ( int i = 1 ; i < n ; i ++ ) cost += city [ parent [ i ] ] [ i ] ; System . out . println ( cost ) ; } public static void main ( String args [ ] ) { int n1 = 5 ; int city1 [ ] [ ] = { { 0 , 1 , 2 , 3 , 4 } , { 1 , 0 , 5 , 0 , 7 } , { 2 , 5 , 0 , 6 , 0 } , { 3 , 0 , 6 , 0 , 0 } , { 4 , 7 , 0 , 0 , 0 } } ; findcost ( n1 , city1 ) ; int n2 = 6 ; int city2 [ ] [ ] = { { 0 , 1 , 1 , 100 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 100 , 0 , 0 , 0 , 2 , 2 } , { 0 , 0 , 0 , 2 , 0 , 2 } , { 0 , 0 , 0 , 2 , 2 , 0 } } ; findcost ( n2 , city2 ) ; } }

Index/key variables: i, n, u, v
Accumulator variables: cost, i, v
Iterator variables: i, v
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int V = 5 ; static int minKey ( int key [ ] , boolean [ ] mstSet ) { int min = Integer . MAX_VALUE , min_index = 0 ; for ( int v = 0 ; v < V ; v ++ ) { if ( mstSet [ v ] == false && key [ v ] < min ) { min = key [ v ] ; min_index = v ; } } return min_index ; } static void printMST ( int parent [ ] , int n , int graph [ ] [ ] ) { System . out . printf ( "Edge WeightNEW_LINE"); int minProduct = 1 ; for ( int i = 1 ; i < V ; i ++ ) { System . out . printf ( "%d - %d %d NEW_LINE", parent[i], i, graph[i][parent[i]]); minProduct *= graph [ i ] [ parent [ i ] ] ; } System . out . printf ( "Minimum Obtainable product is %dNEW_LINE", minProduct); } static void primMST ( int inputGraph [ ] [ ] , double logGraph [ ] [ ] ) { int [ ] parent = new int [ V ] ; int [ ] key = new int [ V ] ; boolean [ ] mstSet = new boolean [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { key [ i ] = Integer . MAX_VALUE ; mstSet [ i ] = false ; } key [ 0 ] = 0 ; parent [ 0 ] = - 1 ; for ( int count = 0 ; count < V - 1 ; count ++ ) { int u = minKey ( key , mstSet ) ; mstSet [ u ] = true ; for ( int v = 0 ; v < V ; v ++ ) { if ( logGraph [ u ] [ v ] > 0 && mstSet [ v ] == false && logGraph [ u ] [ v ] < key [ v ] ) { parent [ v ] = u ; key [ v ] = ( int ) logGraph [ u ] [ v ] ; } } } printMST ( parent , V , inputGraph ) ; } static void minimumProductMST ( int graph [ ] [ ] ) { double [ ] [ ] logGraph = new double [ V ] [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( graph [ i ] [ j ] > 0 ) { logGraph [ i ] [ j ] = Math . log ( graph [ i ] [ j ] ) ; } else { logGraph [ i ] [ j ] = 0 ; } } } primMST ( graph , logGraph ) ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 2 , 0 , 6 , 0 } , { 2 , 0 , 3 , 8 , 5 } , { 0 , 3 , 0 , 0 , 7 } , { 6 , 8 , 0 , 0 , 9 } , { 0 , 5 , 7 , 9 , 0 } , } ; minimumProductMST ( graph ) ; } }

Index/key variables: V, i, j, u, v
Accumulator variables: count, i, j, minProduct, v
Iterator variables: count, i, j, v
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static final int LEFT = 0 ; static final int RIGHT = 1 ; static int ChangeDirection ( int Dir ) { Dir = 1 - Dir ; return Dir ; } static class node { int data ; node left , right ; } ; static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void modifiedLevelOrder ( node root ) { if ( root == null ) return ; int dir = LEFT ; node temp ; Queue < node > Q = new LinkedList < > ( ) ; Stack < node > S = new Stack < > ( ) ; S . add ( root ) ; while ( ! Q . isEmpty ( ) || ! S . isEmpty ( ) ) { while ( ! S . isEmpty ( ) ) { temp = S . peek ( ) ; S . pop ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) Q . add ( temp . left ) ; if ( temp . right != null ) Q . add ( temp . right ) ; } else { if ( temp . right != null ) Q . add ( temp . right ) ; if ( temp . left != null ) Q . add ( temp . left ) ; } } System . out . println ( ) ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) S . add ( temp . left ) ; if ( temp . right != null ) S . add ( temp . right ) ; } else { if ( temp . right != null ) S . add ( temp . right ) ; if ( temp . left != null ) S . add ( temp . left ) ; } } System . out . println ( ) ; dir = ChangeDirection ( dir ) ; } } public static void main ( String [ ] args ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }

Class/struct names: GFG, node
Java program:
import java . util . LinkedList ; import java . util . Queue ; public class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static Node temp = root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void insert ( Node temp , int key ) { if ( temp == null ) { root = new Node ( key ) ; return ; } Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( temp ) ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null ) { temp . left = new Node ( key ) ; break ; } else q . add ( temp . left ) ; if ( temp . right == null ) { temp . right = new Node ( key ) ; break ; } else q . add ( temp . right ) ; } } public static void main ( String args [ ] ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ insertion : " ) ; inorder ( root ) ; int key = 12 ; insert ( root , key ) ; System . out . print ( " Inorder traversal after insertion : "); inorder ( root ) ; } }

Class/struct names: GFG, Node
Java program:
import java . util . * ; import java . lang . * ; import java . io . * ; class TugOfWar { public int min_diff ; void TOWUtil ( int arr [ ] , int n , boolean curr_elements [ ] , int no_of_selected_elements , boolean soln [ ] , int sum , int curr_sum , int curr_position ) { if ( curr_position == n ) return ; if ( ( n / 2 - no_of_selected_elements ) > ( n - curr_position ) ) return ; TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; no_of_selected_elements ++ ; curr_sum = curr_sum + arr [ curr_position ] ; curr_elements [ curr_position ] = true ; if ( no_of_selected_elements == n / 2 ) { if ( Math . abs ( sum / 2 - curr_sum ) < min_diff ) { min_diff = Math . abs ( sum / 2 - curr_sum ) ; for ( int i = 0 ; i < n ; i ++ ) soln [ i ] = curr_elements [ i ] ; } } else { TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; } curr_elements [ curr_position ] = false ; } void tugOfWar ( int arr [ ] ) { int n = arr . length ; boolean [ ] curr_elements = new boolean [ n ] ; boolean [ ] soln = new boolean [ n ] ; min_diff = Integer . MAX_VALUE ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; curr_elements [ i ] = soln [ i ] = false ; } TOWUtil ( arr , n , curr_elements , 0 , soln , sum , 0 , 0 ) ; System . out . print ( " The ▁ first ▁ subset ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == true ) System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( " The second subset is : "); for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == false ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 45 , - 34 , 12 , 0 , 98 , - 99 , 4 , 189 , - 1 , 4 } ; TugOfWar a = new TugOfWar ( ) ; a . tugOfWar ( arr ) ; } }

Index/key variables: curr_position, i, n
Accumulator variables: i, no_of_selected_elements, sum
Iterator variables: i
Class/struct names: TugOfWar
Java program:
class KnightTour { static int N = 8 ; static boolean isSafe ( int x , int y , int sol [ ] [ ] ) { return ( x >= 0 && x < N && y >= 0 && y < N && sol [ x ] [ y ] == - 1 ) ; } static void printSolution ( int sol [ ] [ ] ) { for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < N ; y ++ ) System . out . print ( sol [ x ] [ y ] + " ▁ " ) ; System . out . println ( ) ; } } static boolean solveKT ( ) { int sol [ ] [ ] = new int [ 8 ] [ 8 ] ; for ( int x = 0 ; x < N ; x ++ ) for ( int y = 0 ; y < N ; y ++ ) sol [ x ] [ y ] = - 1 ; int xMove [ ] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int yMove [ ] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; sol [ 0 ] [ 0 ] = 0 ; if ( ! solveKTUtil ( 0 , 0 , 1 , sol , xMove , yMove ) ) { System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } else printSolution ( sol ) ; return true ; } static boolean solveKTUtil ( int x , int y , int movei , int sol [ ] [ ] , int xMove [ ] , int yMove [ ] ) { int k , next_x , next_y ; if ( movei == N * N ) return true ; for ( k = 0 ; k < 8 ; k ++ ) { next_x = x + xMove [ k ] ; next_y = y + yMove [ k ] ; if ( isSafe ( next_x , next_y , sol ) ) { sol [ next_x ] [ next_y ] = movei ; if ( solveKTUtil ( next_x , next_y , movei + 1 , sol , xMove , yMove ) ) return true ; else sol [ next_x ] [ next_y ] = - 1 ; } } return false ; } public static void main ( String args [ ] ) { solveKT ( ) ; } }

Index/key variables: k, next_x, next_y, x, y
Accumulator variables: k, x, y
Iterator variables: k, x, y
Class/struct names: KnightTour