codeparrot/xlcost-text-to-code huggingface dataset:
Randomized Binary Search Algorithm | Java program to implement recursive randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A recursive randomized binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code
public class RandomizedBinarySearch { public static int getRandom ( int x , int y ) { return ( x + ( int ) ( Math . random ( ) % ( y - x + 1 ) ) ) ; } public static int randomizedBinarySearch ( int arr [ ] , int low , int high , int key ) { if ( high >= low ) { int mid = getRandom ( low , high ) ; if ( arr [ mid ] == key ) return mid ; if ( arr [ mid ] > key ) return randomizedBinarySearch ( arr , low , mid - 1 , key ) ; return randomizedBinarySearch ( arr , mid + 1 , high , key ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int key = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , key ) ; System . out . println ( ( result == - 1 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
Program to remove vowels from a String | Java program to remove vowels from a String ; Driver method to test the above function
import java . util . Arrays ; import java . util . List ; class Test { static String remVowel ( String str ) { Character vowels [ ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' , ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; List < Character > al = Arrays . asList ( vowels ) ; StringBuffer sb = new StringBuffer ( str ) ; for ( int i = 0 ; i < sb . length ( ) ; i ++ ) { if ( al . contains ( sb . charAt ( i ) ) ) { sb . replace ( i , i + 1 , " " ) ; i -- ; } } return sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; System . out . println ( remVowel ( str ) ) ; } }
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int minCostToMakeElementEqual ( int a [ ] , int n ) { int y ; if ( n % 2 == 1 ) y = a [ n / 2 ] ; else y = ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) s += Math . abs ( a [ i ] - y ) ; return s ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 100 , 101 } ; int n = a . length ; System . out . println ( minCostToMakeElementEqual ( a , n ) ) ; } }
Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Driver Code ; Given array
public static boolean isSorted ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } public static boolean sortPoss ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int idx = - 1 ; int minVar = arr [ i ] ; int j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != - 1 ) { int t ; t = arr [ i ] ; arr [ i ] = arr [ idx ] ; arr [ idx ] = t ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 5 , 1 , 2 , 6 } ; int n = arr . length ; if ( sortPoss ( arr , n ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
Print all array elements appearing more than N / K times | Java program to implement the above approach ; Function to + find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to print all array elements whose frequency is greater than N / K ; Sort the array arr [ ] ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int upperBound ( int arr [ ] , int N , int K ) { int l = 0 ; int r = N ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( arr [ mid ] <= K ) { l = mid + 1 ; } else { r = mid ; } } return l ; } static void NDivKWithFreq ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < N ) { int X = upperBound ( arr , N , arr [ i ] ) ; if ( ( X - i ) > N / 4 ) { System . out . print ( arr [ i ] + " ▁ " ) ; } i = X ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = arr . length ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; } }
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | Java program for the above approach ; Function to find maximum possible sum after changing the array elements as per the given constraints ; Length of given array ; Stores the frequency of elements in given array ; Update frequency ; stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static long maxSum ( int [ ] a ) { int n = a . length ; int [ ] count = new int [ n + 1 ] ; for ( int x : a ) count [ Math . min ( x , n ) ] ++ ; int size = 0 ; long ans = 0 ; for ( int k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 1 } ; System . out . println ( maxSum ( arr ) ) ; } }
Find sum of product of every number and its frequency in given range | Java program to find sum of product of every number and square of its frequency in the given range ; Function to solve queries ; Calculating answer for every query ; The end points of the ith query ; Hashmap for storing frequency ; Incrementing the frequency ; Adding the contribution of ith number ; Print answer ; Driver code ; Calling function
import java . util . * ; class GFG { public static void answerQueries ( int [ ] arr , int n , int [ ] [ ] queries ) { for ( int i = 0 ; i < queries . length ; i ++ ) { int ans = 0 ; int l = queries [ i ] [ 0 ] - 1 ; int r = queries [ i ] [ 1 ] - 1 ; Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int j = l ; j < r + 1 ; j ++ ) { freq . put ( arr [ j ] , freq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; } for ( int k : freq . keySet ( ) ) { ans += k * freq . get ( k ) ; } System . out . println ( ans ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 1 } ; int n = arr . length ; int [ ] [ ] queries = { { 1 , 2 } , { 1 , 3 } } ; answerQueries ( arr , n , queries ) ; } }
Pandigital Product | Java code to check the number is Pandigital Product or not ; calculate the multiplicand , multiplier , and product eligible for pandigital ; To check the string formed from multiplicand multiplier and product is pandigital ; Driver function
import java . io . * ; import java . util . * ; class GFG { public static boolean PandigitalProduct_1_9 ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 && isPandigital ( " " + n + i + n / i ) ) return true ; return false ; } public static boolean isPandigital ( String str ) { if ( str . length ( ) != 9 ) return false ; char ch [ ] = str . toCharArray ( ) ; Arrays . sort ( ch ) ; return new String ( ch ) . equals ( "123456789" ) ; } public static void main ( String [ ] args ) { int n = 6952 ; if ( PandigitalProduct_1_9 ( n ) == true ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
Median and Mode using Counting Sort | Java Program for Mode and Median using Counting Sort technique ; function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Update count [ ] array with sum ; Sorted output array b [ ] to calculate median ; Median according to odd and even array size respectively . ; Output the result ; Driver program
import java . util . Arrays ; class GFG { static void printModeMedian ( int a [ ] , int n ) { int [ ] b = new int [ n ] ; int max = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int t = max + 1 ; int count [ ] = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { count [ a [ i ] ] ++ ; } int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } for ( int i = 1 ; i < t ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { b [ count [ a [ i ] ] - 1 ] = a [ i ] ; count [ a [ i ] ] -- ; } float median ; if ( n % 2 != 0 ) { median = b [ n / 2 ] ; } else { median = ( float ) ( ( b [ ( n - 1 ) / 2 ] + b [ ( n / 2 ) ] ) / 2.0 ) ; } System . out . println ( " median ▁ = ▁ " + median ) ; System . out . println ( " mode ▁ = ▁ " + mode ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . length ; printModeMedian ( a , n ) ; } }
Check if both halves of the string have at least one different character | Java implementation of the problem ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function
import java . util . * ; import java . lang . * ; class GeeksforGeeks { final static int MAX = 26 ; static boolean function ( String str ) { int l = str . length ( ) ; int counter1 [ ] = new int [ MAX ] ; int counter2 [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { counter1 [ i ] = 0 ; counter2 [ i ] = 0 ; } for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( counter2 [ i ] != counter1 [ i ] ) return true ; } return false ; } public static void main ( String args [ ] ) { String str = " abcasdsabcae " ; if ( function ( str ) ) System . out . print ( " Yes , ▁ both ▁ halves ▁ " + " differ ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else System . out . print ( " No , ▁ both ▁ halves ▁ " + " do ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Check if both halves of the string have at least one different character | Java implementation of the problem ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
import java . util . * ; import java . lang . * ; class GeeksforGeeks { final static int MAX = 26 ; static boolean function ( String str ) { int l = str . length ( ) ; int counter [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) counter [ i ] = 0 ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter [ str . charAt ( i ) - ' a ' ] -- ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter [ i ] != 0 ) return true ; return false ; } public static void main ( String args [ ] ) { String str = " abcasdsabcae " ; if ( function ( str ) ) System . out . print ( " Yes , ▁ both ▁ halves " + " ▁ differ ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else System . out . print ( " No , ▁ both ▁ halves " + " ▁ do ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Sort elements on the basis of number of factors | Java implementation to sort numbers on the basis of factors ; each element having its index in the input array and number of factors ; method to count factors for a given number n ; if the number is a perfect square ; count all other factors ; if i is a factor then n / i will be another factor . So increment by 2 ; function to print numbers after sorting them in decreasing order of number of factors ; for each element of input array create a structure element to store its index and factors count ; sort the array of structures as defined ; compare method for the elements of the structure ; if two elements have the same number of factors then sort them in increasing order of their index in the input array ; sort in decreasing order of number of factors ; access index from the structure element and corresponding to that index access the element from arr ; Driver program to test above
import java . util . Arrays ; import java . util . Comparator ; class Element { int index , no_of_fact ; public Element ( int i , int countFactors ) { index = i ; no_of_fact = countFactors ; } static int countFactors ( int n ) { int count = 0 ; int sq = ( int ) Math . sqrt ( n ) ; if ( sq * sq == n ) count ++ ; for ( int i = 1 ; i < Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) count += 2 ; } return count ; } static void printOnBasisOfFactors ( int arr [ ] , int n ) { Element num [ ] = new Element [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { num [ i ] = new Element ( i , countFactors ( arr [ i ] ) ) ; } Arrays . sort ( num , new Comparator < Element > ( ) { @ Override public int compare ( Element e1 , Element e2 ) { if ( e1 . no_of_fact == e2 . no_of_fact ) return e1 . index < e2 . index ? - 1 : 1 ; return e1 . no_of_fact > e2 . no_of_fact ? - 1 : 1 ; } } ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ num [ i ] . index ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 11 , 10 , 20 , 9 , 16 , 23 } ; printOnBasisOfFactors ( arr , arr . length ) ; } }
Minimum difference between max and min of all K | Java program to find minimum difference between max and min of all subset of K size ; returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver code
import java . util . Arrays ; class GFG { static int minDifferenceAmongMaxMin ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int res = 2147483647 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { int curSeqDiff = arr [ i + K - 1 ] - arr [ i ] ; res = Math . min ( res , curSeqDiff ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 30 , 100 , 101 , 102 } ; int N = arr . length ; int K = 3 ; System . out . print ( minDifferenceAmongMaxMin ( arr , N , K ) ) ; } }
Position of an element after stable sort | Java program to get index of array element in sorted array ; Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver code to test above methods
class ArrayIndex { static int getIndexInSortedArray ( int arr [ ] , int n , int idx ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ idx ] ) result ++ ; if ( arr [ i ] == arr [ idx ] && i < idx ) result ++ ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 } ; int n = arr . length ; int idxOfEle = 5 ; System . out . println ( getIndexInSortedArray ( arr , n , idxOfEle ) ) ; } }
Find permutation of numbers upto N with a specific sum in a specific range | Java program for the above approach ; Function to check if sum is possible with remaining numbers ; Stores the minimum sum possible with x numbers ; Stores the maximum sum possible with x numbers ; If S lies in the range [ minSum , maxSum ] ; Function to find the resultant permutation ; Stores the count of numbers in the given segment ; If the sum is not possible with numbers in the segment ; Output - 1 ; Stores the numbers present within the given segment ; Iterate over the numbers from 1 to N ; If ( S - i ) is a positive non - zero sum and if it is possible to obtain ( S - i ) remaining numbers ; Update sum S ; Update required numbers in the segement ; Push i in vector v ; If sum has been obtained ; Break from the loop ; If sum is not obtained ; Output - 1 ; Stores the numbers which are not present in given segment ; Loop to check the numbers not present in the segment ; Pointer to check if i is present in vector v or not ; If i is not present in v ; Push i in vector v1 ; Point to the first elements of v1 and v respectively ; Print the required permutation ; Driver Code
import java . util . ArrayList ; class GFG { public static boolean possible ( int x , int S , int N ) { int minSum = ( x * ( x + 1 ) ) / 2 ; int maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) / 2 ; if ( S < minSum S > maxSum ) { return false ; } return true ; } public static void findPermutation ( int N , int L , int R , int S ) { int x = R - L + 1 ; if ( ! possible ( x , S , N ) ) { System . out . print ( - 1 ) ; return ; } else { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = N ; i >= 1 ; -- i ) { if ( ( S - i ) >= 0 && possible ( x - 1 , S - i , i - 1 ) ) { S = S - i ; x -- ; v . add ( i ) ; } if ( S == 0 ) { break ; } } if ( S != 0 ) { System . out . print ( - 1 ) ; return ; } ArrayList < Integer > v1 = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= N ; ++ i ) { boolean it = v . contains ( i ) ; if ( ! it ) { v1 . add ( i ) ; } } int j = 0 , f = 0 ; for ( int i = 1 ; i < L ; ++ i ) { System . out . print ( v1 . get ( j ) + " ▁ " ) ; j ++ ; } for ( int i = L ; i <= R ; ++ i ) { System . out . print ( v . get ( f ) + " ▁ " ) ; f ++ ; } for ( int i = R + 1 ; i <= N ; ++ i ) { System . out . print ( v1 . get ( j ) + " ▁ " ) ; j ++ ; } } return ; } public static void main ( String args [ ] ) { int N + C3 : AC3 = 6 , L = 3 , R = 5 , S = 8 ; findPermutation ( N , L , R , S ) ; } }
Minimum count of indices to be skipped for every index of Array to keep sum till that index at most T | C ++ approach for above approach ; Function to calculate minimum indices to be skipped so that sum till i remains smaller than T ; Store the sum of all indices before i ; Store the elements that can be skipped ; Traverse the array , A [ ] ; Store the total sum of elements that needs to be skipped ; Store the number of elements need to be removed ; Traverse from the back of map so as to take bigger elements first ; Update sum ; Update map with the current element ; Driver code ; Given Input ; Function Call
import java . util . ArrayList ; import java . util . Map ; import java . util . TreeMap ; class GFG { public static void skipIndices ( int N , int T , int arr [ ] ) { int sum = 0 ; TreeMap < Integer , Integer > count = new TreeMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int d = sum + arr [ i ] - T ; int k = 0 ; if ( d > 0 ) { for ( Map . Entry < Integer , Integer > u : count . descendingMap ( ) . entrySet ( ) ) { int j = u . getKey ( ) ; int x = j * count . get ( j ) ; if ( d <= x ) { k += ( d + j - 1 ) / j ; break ; } k += count . get ( j ) ; d -= x ; } } sum += arr [ i ] ; if ( count . containsKey ( arr [ i ] ) ) { count . put ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . put ( arr [ i ] , 1 ) ; } System . out . print ( k + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 7 ; int T = 15 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; skipIndices ( N , T , arr ) ; } }
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | Java program for the above approach ; Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int countTriplets ( int N , int K ) { if ( K % 2 == 0 ) { int x = N / K ; int y = ( N + ( K / 2 ) ) / K ; return x * x * x + y * y * y ; } else { int x = N / K ; return x * x * x ; } } public static void main ( String [ ] args ) { int N = 2 , K = 2 ; System . out . print ( countTriplets ( N , K ) ) ; } }
Maximize count of 3 | Java program to implement the above approach ; Driver Code ; Function to count the maximum number of palindrome subsequences of length 3 considering the same index only once ; Index of the string S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value
import java . util . * ; class GFG { public static void main ( String [ ] args ) { String S = " geeksforg " ; System . out . println ( maxNumPalindrome ( S ) ) ; } static int maxNumPalindrome ( String S ) { int i = 0 ; int [ ] freq = new int [ 26 ] ; int freqPair = 0 ; int len = S . length ( ) / 3 ; while ( i < S . length ( ) ) { freq [ S . charAt ( i ) - ' a ' ] ++ ; i ++ ; } for ( i = 0 ; i < 26 ; i ++ ) { freqPair += ( freq [ i ] / 2 ) ; } return Math . min ( freqPair , len ) ; } }
Minimum Bipartite Groups | Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Drivers Code ; Adjacency List ; Adding edges to List
import java . util . * ; class GFG { static int height ( int s , Vector < Integer > adj [ ] , int [ ] visited ) { visited [ s ] = 1 ; int h = 0 ; for ( int child : adj [ s ] ) { if ( visited [ child ] == 0 ) { h = Math . max ( h , 1 + height ( child , adj , visited ) ) ; } } return h ; } static int minimumGroups ( Vector < Integer > adj [ ] , int N ) { int [ ] visited = new int [ N + 1 ] ; int groups = Integer . MIN_VALUE ; for ( int i = 1 ; i <= N ; i ++ ) { if ( visited [ i ] == 0 ) { int comHeight ; comHeight = height ( i , adj , visited ) ; groups = Math . max ( groups , comHeight ) ; } } return groups ; } static void addEdge ( Vector < Integer > adj [ ] , int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } public static void main ( String [ ] args ) { int N = 5 ; Vector < Integer > [ ] adj = new Vector [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 4 , 3 ) ; System . out . print ( minimumGroups ( adj , N ) ) ; } }