codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of array elements removed by performing the given operations | Java program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
import java . util . * ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { ArrayList < int [ ] > A = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . add ( new int [ ] { arr [ i ] , min [ i ] } ) ; } Collections . sort ( A , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . max ( A . get ( i ) [ 0 ] - K , A . get ( i ) [ 1 ] ) ; K += k ; } } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 2 , 1 } ; int [ ] min = { 3 , 2 , 1 , 3 } ; int N = arr . length ; int K = 3 ; S = 0 ; findMaxSum ( arr , N , min , K ) ; System . out . println ( S ) ; } }
Length of smallest substring of a given string which contains another string as subsequence | Java program to implement the above approach ; Function to find the length of smallest substring of a having string b as a subsequence ; Stores the characters present in string b ; Find index of characters of a that are also present in string b ; If character is present in string b ; Store the index of character ; Flag is used to check if substring is possible ; Assume that substring is possible ; Stores first and last indices of the substring respectively ; For first character of string b ; If the first character of b is not present in a ; If the first character of b is present in a ; Remove the index from map ; Update indices of the substring ; For the remaining characters of b ; If index possible for current character ; If no index is possible ; If no more substring is possible ; Update the minimum length of substring ; Return the result ; Driver code ; Given two string
import java . util . ArrayList ; import java . util . HashMap ; class GFG { static int minLength ( String a , String b ) { HashMap < Character , Integer > Char = new HashMap < > ( ) ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { Char . put ( b . charAt ( i ) , Char . getOrDefault ( b . charAt ( i ) , 0 ) + 1 ) ; } HashMap < Character , ArrayList < Integer > > CharacterIndex = new HashMap < > ( ) ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { char x = a . charAt ( i ) ; if ( Char . containsKey ( x ) ) { if ( CharacterIndex . get ( x ) == null ) { CharacterIndex . put ( x , new ArrayList < Integer > ( ) ) ; } CharacterIndex . get ( x ) . add ( i ) ; } } int len = Integer . MAX_VALUE ; int flag ; while ( true ) { flag = 1 ; int firstVar = 0 , lastVar = 0 ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { if ( i == 0 ) { if ( CharacterIndex . containsKey ( i ) ) { flag = 0 ; break ; } else { int x = CharacterIndex . get ( b . charAt ( i ) ) . get ( 0 ) ; CharacterIndex . get ( b . charAt ( i ) ) . remove ( CharacterIndex . get ( b . charAt ( i ) ) . get ( 0 ) ) ; firstVar = x ; lastVar = x ; } } else { int elementFound = 0 ; for ( var e : CharacterIndex . get ( b . charAt ( i ) ) ) { if ( e > lastVar ) { elementFound = 1 ; lastVar = e ; break ; } } if ( elementFound == 0 ) { flag = 0 ; break ; } } } if ( flag == 0 ) { break ; } len = Math . min ( len , Math . abs ( lastVar - firstVar ) + 1 ) ; } return len ; } public static void main ( String [ ] args ) { String a = " abcdefababaef " ; String b = " abf " ; int len = minLength ( a , b ) ; if ( len != Integer . MAX_VALUE ) { System . out . println ( len ) ; } else { System . out . println ( " Impossible " ) ; } } }
Count smaller primes on the right of each array element | Java program for the above approach ; Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
class GFG { static boolean is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countSmallerPrimes ( int ar [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 43 , 3 , 5 , 7 , 2 , 41 } ; int N = ar . length ; countSmallerPrimes ( ar , N ) ; } }
Check if a string is concatenation of another given string | Java program to implement the above approach ; Function to check if a String is concatenation of another String ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the Strings ; If str1 is not concatenation of str2 ; Driver Code
import java . util . * ; class GFG { static boolean checkConcat ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i % M ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str1 = " abcabcabc " ; String str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Minimize length of string by replacing K pairs of distinct adjacent characters | Java program to implement the above approach ; Function to minimize the length of the String by replacing distinct pairs of adjacent characters ; Stores the length of the given String ; Stores the index of the given String ; Traverse the given String ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
class GFG { static int MinLen ( String str , int K ) { int N = str . length ( ) ; int i = 0 ; while ( i < N - 1 ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . max ( 1 , N - K ) ; } public static void main ( String [ ] args ) { String str = " aabc " ; int K = 1 ; System . out . print ( MinLen ( str , K ) ) ; } }
Length of smallest sequence having sum X and product Y | Java program for the above approach ; Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
import java . util . * ; class GFG { static double temp ( int n , int x ) { return Math . pow ( x * 1.0 / n , n ) ; } static boolean check ( int n , int y , int x ) { double v = temp ( n , x ) ; return ( v >= y ) ; } static void find ( int x , int y ) { int high = ( int ) Math . floor ( x / Math . exp ( 1.0 ) ) ; int low = 1 ; if ( x == y ) System . out . print ( 1 + "NEW_LINE"); else if ( ! check ( high , y , x ) ) System . out . print ( - 1 + "NEW_LINE"); else { while ( high - low > 1 ) { int mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } System . out . print ( high + "NEW_LINE"); } } public static void main ( String [ ] args ) { int x = 9 , y = 8 ; find ( x , y ) ; } }
Print the middle nodes of each level of a Binary Tree | Java program for the above approach ; Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Driver Code ; Given Tree ; Function Call
import java . util . * ; class GFG { static Map < Integer , Vector < Integer > > M ; static class node { int data ; node left ; node right ; public node ( ) { } public node ( int data , node left , node right ) { super ( ) ; this . data = data ; this . left = left ; this . right = right ; } } ; static node newnode ( int d ) { node temp = new node ( ) ; temp . data = d ; temp . left = null ; temp . right = null ; return temp ; } static void dfs ( node root , int l ) { if ( root == null ) return ; if ( ! M . containsKey ( l ) ) { Vector < Integer > temp = new Vector < Integer > ( ) ; temp . add ( root . data ) ; M . put ( l , temp ) ; } else M . get ( l ) . add ( root . data ) ; dfs ( root . left , l + 1 ) ; dfs ( root . right , l + 1 ) ; } static void printMidNodes ( node root ) { M = new HashMap < Integer , Vector < Integer > > ( ) ; dfs ( root , 0 ) ; for ( Map . Entry < Integer , Vector < Integer > > it : M . entrySet ( ) ) { int size = it . getValue ( ) . size ( ) ; if ( size % 2 == 1 ) { System . out . print ( it . getValue ( ) . get ( ( size - 1 ) / 2 ) + "NEW_LINE"); } else { System . out . print ( it . getValue ( ) . get ( ( size - 1 ) / 2 ) + " ▁ " + it . getValue ( ) . get ( ( ( size - 1 ) / 2 ) + 1 ) + "NEW_LINE"); } } } 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 . left . right . left = newnode ( 11 ) ; root . left . right . right = newnode ( 6 ) ; root . left . right . right . left = newnode ( 7 ) ; root . left . right . right . right = newnode ( 9 ) ; root . right . left = newnode ( 10 ) ; root . right . right = newnode ( 8 ) ; printMidNodes ( root ) ; } }
Smallest array that can be obtained by replacing adjacent pairs with their products | Java implementation of the above approach ; Function to minimize the size of array by performing given operations ; If all array elements are not same ; If all array elements are same ; Driver Code ; Function call
import java . io . * ; class GFG { static int minLength ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return 1 ; } } return N ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 1 } ; int N = arr . length ; System . out . print ( minLength ( arr , N ) ) ; } }
Maximum XOR of Two Numbers in an Array | Set 2 | Java program for the above approach ; Structure of Trie ; Function to insert binary representation of element x in the Trie ; Store the head ; Find the i - th bit ; If curr . left is null ; Update curr to curr . left ; If curr . right is null ; Update curr to curr . right ; Function that finds the maximum Bitwise XOR value for all such pairs ; head Node of Trie ; Insert each element in trie ; Stores the maximum XOR value ; Traverse the given array ; Stores the XOR with current value arr [ i ] ; Finding ith bit ; Check if the bit is 0 ; If right node exists ; Update the currentXOR ; Check if left node exists ; Update the currentXOR ; Update M to M / 2 for next set bit ; Update the maximum XOR ; Return the maximum XOR found ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static class node { node left ; node right ; } ; static void insert ( int x , node head ) { node curr = head ; for ( int i = 30 ; i >= 0 ; i -- ) { int val = ( x >> i ) & 1 ; if ( val == 0 ) { if ( curr . left == null ) curr . left = new node ( ) ; curr = curr . left ; } else { if ( curr . right == null ) curr . right = new node ( ) ; curr = curr . right ; } } } static int findMaximumXOR ( int arr [ ] , int n ) { node head = new node ( ) ; for ( int i = 0 ; i < n ; i ++ ) { insert ( arr [ i ] , head ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; int M = ( int ) Math . pow ( 2 , 30 ) ; node curr = head ; for ( int j = 30 ; j >= 0 ; j -- ) { int val = ( arr [ i ] >> j ) & 1 ; if ( val == 0 ) { if ( curr . right != null ) { curr_xor += M ; curr = curr . right ; } else { curr = curr . left ; } } else { if ( curr . left != null ) { curr_xor += M ; curr = curr . left ; } else { curr = curr . right ; } } M /= 2 ; } ans = Math . max ( ans , curr_xor ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . print ( findMaximumXOR ( arr , N ) ) ; } }
Subarrays whose sum is a perfect square | Java program to implement the above approach ; Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver code
import java . io . * ; class GFG { static void PrintIndexes ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int currSubSum = 0 ; for ( int j = i ; j < N ; j ++ ) { currSubSum += arr [ j ] ; int sq = ( int ) Math . sqrt ( currSubSum ) ; if ( sq * sq == currSubSum ) { System . out . print ( " ( " + i + " , " + j + " ) " + " ▁ " ) ; } } } } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 65 , 79 , 81 } ; PrintIndexes ( arr , arr . length ) ; } }
Count positions in Binary Matrix having equal count of set bits in corresponding row and column | Java Program to implement above approach ; Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static int countPosition ( int [ ] [ ] mat ) { int n = mat . length ; int m = mat [ 0 ] . length ; int [ ] row = new int [ n ] ; int [ ] col = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { col [ j ] ++ ; row [ i ] ++ ; } } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( row [ i ] == col [ j ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 } , { 1 , 1 } } ; System . out . println ( countPosition ( mat ) ) ; } }
Maximum value at each level in an N | Java program for the above approach ; Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Driver Code ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call
import java . util . * ; class GFG { static void maxAtLevel ( int N , int M , int [ ] Value , int Edges [ ] [ ] ) { Vector < Integer > [ ] adj = new Vector [ N ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . add ( v ) ; } Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; while ( ! q . isEmpty ( ) ) { int count = q . size ( ) ; int maxVal = 0 ; while ( count -- > 0 ) { int temp = q . peek ( ) ; q . remove ( ) ; maxVal = Math . max ( maxVal , Value [ temp ] ) ; for ( int i = 0 ; i < adj [ temp ] . size ( ) ; i ++ ) { q . add ( adj [ temp ] . get ( i ) ) ; } } System . out . print ( maxVal + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 10 ; int Edges [ ] [ ] = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; int [ ] Value = { 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAtLevel ( N , N - 1 , Value , Edges ) ; } }
Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | Java program to implement the above approach ; Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
import java . io . * ; import java . util . Arrays ; class GFG { static void ctTriplets ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int [ ] temp = new int [ N ] ; int index = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } for ( int i = 0 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } int ct = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && i + 1 < N ) { if ( temp [ i ] < temp [ i + 1 ] && temp [ i ] < temp [ i - 1 ] ) { ct ++ ; } } } System . out . println ( " Count ▁ of ▁ triplets : " + ct ) ; System . out . print ( " Array : " ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( temp [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; ctTriplets ( arr , N ) ; } }
Rearrange array to make it non | Java program for above approach ; Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Print the answer
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { public static boolean check ( int [ ] a , int n ) { int [ ] b = new int [ n ] ; int minElement = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = Math . min ( minElement , a [ i ] ) ; } Arrays . sort ( b ) ; int k = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } public static void main ( String [ ] args ) { int [ ] a = { 4 , 3 , 6 , 6 , 2 , 9 } ; int n = a . length ; if ( check ( a , n ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | Java program for the above approach ; Function to count required 1 s from the given matrix ; Stores the dimensions of the mat [ ] [ ] ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the final count ; Driver Code ; Given matrix ; Function call
class GFG { static int numSpecial ( int [ ] [ ] mat ) { int m = mat . length ; int n = mat [ 0 ] . length ; int [ ] rows = new int [ m ] ; int [ ] cols = new int [ n ] ; int i , j ; for ( i = 0 ; i < m ; i ++ ) { rows [ i ] = 0 ; for ( j = 0 ; j < n ; j ++ ) rows [ i ] += mat [ i ] [ j ] ; } for ( i = 0 ; i < n ; i ++ ) { cols [ i ] = 0 ; for ( j = 0 ; j < m ; j ++ ) cols [ i ] += mat [ j ] [ i ] ; } int cnt = 0 ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 1 && rows [ i ] == 1 && cols [ j ] == 1 ) cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 0 , 0 , 0 } } ; System . out . print ( numSpecial ( mat ) + "NEW_LINE"); } }
Find all matrix elements which are minimum in their row and maximum in their column | Java program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver code ; Function call ; If no such matrix element is found
import java . util . * ; class GFG { public static Vector < Integer > minmaxNumbers ( int [ ] [ ] matrix , Vector < Integer > res ) { Set < Integer > set = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < matrix . length ; i ++ ) { int minr = Integer . MAX_VALUE ; for ( int j = 0 ; j < matrix [ i ] . length ; j ++ ) { minr = Math . min ( minr , matrix [ i ] [ j ] ) ; } set . add ( minr ) ; } for ( int j = 0 ; j < matrix [ 0 ] . length ; j ++ ) { int maxc = Integer . MIN_VALUE ; for ( int i = 0 ; i < matrix . length ; i ++ ) { maxc = Math . max ( maxc , matrix [ i ] [ j ] ) ; } if ( set . contains ( maxc ) ) { res . add ( maxc ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; Vector < Integer > ans = new Vector < Integer > ( ) ; ans = minmaxNumbers ( mat , ans ) ; if ( ans . size ( ) == 0 ) System . out . println ( " - 1" ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . println ( ans . get ( i ) ) ; } }
Node whose removal minimizes the maximum size forest from an N | Java program to implement the above approach ; Function to create the graph ; Function to traverse the graph and find the minimum of maximum size forest after removing a node ; Traversing every child subtree except the parent node ; Traverse all subtrees ; Update the maximum size of forests ; Update the minimum of maximum size of forests obtained ; Condition to find the minimum of maximum size forest ; Update and store the corresponding node ; Driver Code
import java . util . * ; class GFG { static int mini = 105 , ans , n ; static Vector < Integer > [ ] g = new Vector [ 100 ] ; static int [ ] size = new int [ 100 ] ; static void create_graph ( ) { g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; g [ 1 ] . add ( 3 ) ; g [ 3 ] . add ( 1 ) ; g [ 1 ] . add ( 4 ) ; g [ 4 ] . add ( 1 ) ; g [ 2 ] . add ( 5 ) ; g [ 5 ] . add ( 2 ) ; g [ 2 ] . add ( 6 ) ; g [ 6 ] . add ( 2 ) ; } static void dfs ( int node , int parent ) { size [ node ] = 1 ; int mx = 0 ; for ( int y : g [ node ] ) { if ( y == parent ) continue ; dfs ( y , node ) ; size [ node ] += size [ y ] ; mx = Math . max ( mx , size [ y ] ) ; } mx = Math . max ( mx , n - size [ node ] ) ; if ( mx < mini ) { mini = mx ; ans = node ; } } public static void main ( String [ ] args ) { n = 6 ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; create_graph ( ) ; dfs ( 1 , - 1 ) ; System . out . print ( ans + "NEW_LINE"); } }