codeparrot/xlcost-text-to-code huggingface dataset:
Rearrange two given arrays such that sum of same indexed elements lies within given range | Java program for the above approach ; Function to check if there exists any arrangements of the arrays such that sum of element lie in the range [ K / 2 , K ] ; Sort the array arr1 [ ] in increasing order ; Sort the array arr2 [ ] in decreasing order ; Traverse the array ; If condition is not satisfied break the loop ; Print the result ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void checkArrangement ( Integer [ ] A1 , Integer [ ] A2 , int n , int k ) { Arrays . sort ( A1 ) ; Arrays . sort ( A2 , Collections . reverseOrder ( ) ) ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( A1 [ i ] + A2 [ i ] > k ) || ( A1 [ i ] + A2 [ i ] < k / 2 ) ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { Integer [ ] arr1 = { 1 , 3 , 4 , 5 } ; Integer [ ] arr2 = { 2 , 0 , 1 , 1 } ; int K = 6 ; int N = arr1 . length ; checkArrangement ( arr1 , arr2 , N , K ) ; } }
Rearrange two given arrays to maximize sum of same indexed elements | Java program to implement the above approach ; Function to find the maximum possible sum by rearranging the given array elements ; Sort the array A [ ] in ascending order ; Sort the array B [ ] in descending order ; Stores maximum possible sum by rearranging array elements ; Traverse both the arrays ; Update maxSum ; Driver code
import java . lang . Math ; import java . util . Arrays ; import java . util . Collections ; class GFG { static int MaxRearrngeSum ( Integer A [ ] , Integer B [ ] , int N ) { Arrays . sort ( A ) ; Arrays . sort ( B , Collections . reverseOrder ( ) ) ; int maxSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { maxSum += Math . abs ( A [ i ] - B [ i ] ) ; } return maxSum ; } public static void main ( String [ ] args ) { Integer A [ ] = { 1 , 2 , 2 , 4 , 5 } ; Integer B [ ] = { 5 , 5 , 5 , 6 , 6 } ; int N = A . length ; System . out . println ( MaxRearrngeSum ( A , B , N ) ) ; } }
Area of the largest rectangle possible from given coordinates | Java program for the above approach ; Function to find the maximum possible area of a rectangle ; Initialize variables ; Sort array arr1 [ ] ; Sort array arr2 [ ] ; Traverse arr1 [ ] and arr2 [ ] ; If arr1 [ i ] is same as arr2 [ j ] ; If no starting point is found yet ; Update maximum end ; If arr [ i ] > arr2 [ j ] ; If no rectangle is found ; Return the area ; Driver Code ; Given point ; Given length ; Given points ; Given length ; Function Call
import java . util . * ; class GFG { static int largestArea ( int arr1 [ ] , int n , int arr2 [ ] , int m ) { int end = 0 , start = 0 , i = 0 , j = 0 ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr1 ) ; while ( i < n && j < m ) { if ( arr1 [ i ] == arr2 [ j ] ) { if ( start == 0 ) start = arr1 [ i ] ; else end = arr1 [ i ] ; i ++ ; j ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) j ++ ; else i ++ ; } if ( end == 0 start == 0 ) return 0 ; else return ( end - start ) ; } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 } ; int N = arr1 . length ; int arr2 [ ] = { 1 , 3 , 4 } ; int M = arr2 . length ; System . out . println ( largestArea ( arr1 , N , arr2 , M ) ) ; } }
Count pairs from two arrays with difference exceeding K | Java program to implement the above approach ; Function to count pairs that satisfy the given conditions ; Stores index of the left pointer . ; Stores index of the right pointer ; Stores count of total pairs that satisfy the conditions ; Sort arr [ ] array ; Sort brr [ ] array ; Traverse both the array and count then pairs ; If the value of ( brr [ j ] - arr [ i ] ) exceeds K ; Update cntPairs ; Update ; Update j ; Driver Code
import java . util . * ; class GFG { static int count_pairs ( int arr [ ] , int brr [ ] , int N , int M , int K ) { int i = 0 ; int j = 0 ; int cntPairs = 0 ; Arrays . sort ( arr ) ; Arrays . sort ( brr ) ; while ( i < N && j < M ) { if ( brr [ j ] - arr [ i ] > K ) { cntPairs += ( M - j ) ; i ++ ; } else { j ++ ; } } return cntPairs ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 9 , 1 , 8 } ; int brr [ ] = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = arr . length ; int M = brr . length ; System . out . println ( count_pairs ( arr , brr , N , M , K ) ) ; } }
Merge two sorted arrays in O ( 1 ) extra space using Heap | Java program to implement the above approach ; Function to perform min heapify on array brr [ ] ; Stores index of left child of i . ; Stores index of right child of i . ; Stores index of the smallest element in ( arr [ i ] , arr [ left ] , arr [ right ] ) ; Check if arr [ left ] less than arr [ smallest ] ; Update smallest ; Check if arr [ right ] less than arr [ smallest ] ; Update smallest ; if i is not the index of smallest element ; Swap arr [ i ] and arr [ smallest ] ; Perform heapify on smallest ; Function to merge two sorted arrays ; Traverse the array arr [ ] ; Check if current element is less than brr [ 0 ] ; Swap arr [ i ] and brr [ 0 ] ; Perform heapify on brr [ ] ; Sort array brr [ ] ; Function to print array elements ; Traverse array arr [ ] ; Driver Code ; Function call to merge two array ; Print first array ; Print Second array .
import java . util . * ; class GFG { static void minHeapify ( int brr [ ] , int i , int M ) { int left = 2 * i + 1 ; int right = 2 * i + 2 ; int smallest = i ; if ( left < M && brr [ left ] < brr [ smallest ] ) { smallest = left ; } if ( right < M && brr [ right ] < brr [ smallest ] ) { smallest = right ; } if ( smallest != i ) { int temp = brr [ i ] ; brr [ i ] = brr [ smallest ] ; brr [ smallest ] = temp ; minHeapify ( brr , smallest , M ) ; } } static void merge ( int arr [ ] , int brr [ ] , int N , int M ) { for ( int i = 0 ; i < N ; ++ i ) { if ( arr [ i ] > brr [ 0 ] ) { int temp = arr [ i ] ; arr [ i ] = brr [ 0 ] ; brr [ 0 ] = temp ; minHeapify ( brr , 0 , M ) ; } } Arrays . sort ( brr ) ; } static void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 23 , 35 , 235 , 2335 } ; int brr [ ] = { 3 , 5 } ; int N = arr . length ; int M = brr . length ; merge ( arr , brr , N , M ) ; printArray ( arr , N ) ; printArray ( brr , M ) ; } }
Merge two sorted arrays in O ( 1 ) extra space using QuickSort partition | Java program to implement the above approach ; Function to perform the partition around the pivot element ; Stores index of each element of the array , arr [ ] ; Stores index of each element of the array , brr [ ] ; Traverse both the array ; If pivot is smaller than arr [ l ] ; If Pivot is greater than brr [ r ] ; If either arr [ l ] > Pivot or brr [ r ] < Pivot ; Function to merge the two sorted array ; Stores index of each element of the array arr [ ] ; Stores index of each element of the array brr [ ] ; Stores index of each element the final sorted array ; Stores the pivot element ; Traverse both the array ; If pivot element is not found or index < N ; If pivot element is not found or index < N ; Place the first N elements of the sorted array into arr [ ] and the last M elements of the sorted array into brr [ ] ; Sort both the arrays ; Print the first N elements in sorted order ; Print the last M elements in sorted order ; Driver Code
import java . util . * ; class GFG { static void partition ( int arr [ ] , int N , int brr [ ] , int M , int Pivot ) { int l = N - 1 ; int r = 0 ; while ( l >= 0 && r < M ) { if ( arr [ l ] < Pivot ) l -- ; else if ( brr [ r ] > Pivot ) r ++ ; else { int t = arr [ l ] ; arr [ l ] = brr [ r ] ; brr [ r ] = t ; l -- ; r ++ ; } } } static void Merge ( int arr [ ] , int N , int brr [ ] , int M ) { int l = 0 ; int r = 0 ; int index = - 1 ; int Pivot = 0 ; while ( index < N && l < N && r < M ) { if ( arr [ l ] < brr [ r ] ) { Pivot = arr [ l ++ ] ; } else { Pivot = brr [ r ++ ] ; } index ++ ; } while ( index < N && l < N ) { Pivot = arr [ l ++ ] ; index ++ ; } while ( index < N && r < M ) { Pivot = brr [ r ++ ] ; index ++ ; } partition ( arr , N , brr , M , Pivot ) ; Arrays . sort ( arr ) ; Arrays . sort ( brr ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; for ( int i = 0 ; i < M ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 9 } ; int brr [ ] = { 2 , 4 , 7 , 10 } ; int N = arr . length ; int M = brr . length ; Merge ( arr , N , brr , M ) ; } }
Count array elements with rank not exceeding K | Java program for the above approach ; Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Driver Code ; Given array ; Given K ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static int rankLessThanK ( int [ ] arr , int k , int n ) { int rank = 1 ; int position = 1 ; Arrays . sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i == n - 1 arr [ i ] != arr [ i + 1 ] ) { rank = position ; if ( rank > k ) return position - 1 ; } position ++ ; } return n ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 4 , 5 } ; int N = arr . length ; int K = 4 ; System . out . println ( rankLessThanK ( arr , K , N ) ) ; } }
Remove Sub | Java program for the above approach ; Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void eraseSubdirectory ( ArrayList < String > dir ) { ArrayList < String > res = new ArrayList < String > ( ) ; Collections . sort ( dir ) ; res . add ( dir . get ( 0 ) ) ; System . out . print ( " { " + dir . get ( 0 ) + " , ▁ " ) ; for ( int i = 1 ; i < dir . size ( ) ; i ++ ) { String curr = dir . get ( i ) ; String prev = res . get ( res . size ( ) - 1 ) ; int l = prev . length ( ) ; if ( curr . length ( ) > l && curr . charAt ( l ) == ' / ' && prev . equals ( curr . substring ( 0 , l ) ) ) continue ; res . add ( curr ) ; System . out . print ( curr + " , ▁ " ) ; } System . out . print ( "}NEW_LINE"); } public static void main ( String [ ] args ) { ArrayList < String > dir = new ArrayList < String > ( Arrays . asList ( " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ) ) ; eraseSubdirectory ( dir ) ; } }
Maximize count of pairs ( i , j ) from two arrays having element from first array not exceeding that from second array | Java program for the above approach ; Function to return the maximum number of required pairs ; Max Heap to add values of arr2 [ ] ; Sort the array arr1 [ ] ; Push all arr2 [ ] into Max Heap ; Initialize the ans ; Traverse the arr1 [ ] in decreasing order ; Remove element until a required pair is found ; Return maximum number of pairs ; Driver Code ; Given arrays ; Function call
import java . io . * ; import java . util . * ; class GFG { static int numberOfPairs ( int [ ] arr1 , int n , int [ ] arr2 , int m ) { PriorityQueue < Integer > pQueue = new PriorityQueue < Integer > ( new Comparator < Integer > ( ) { public int compare ( Integer lhs , Integer rhs ) { if ( lhs < rhs ) { return + 1 ; } if ( lhs . equals ( rhs ) ) { return 0 ; } return - 1 ; } } ) ; int i , j ; Arrays . sort ( arr1 ) ; for ( j = 0 ; j < m ; j ++ ) { pQueue . add ( arr2 [ j ] ) ; } int ans = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( pQueue . peek ( ) >= 2 * arr1 [ i ] ) { ans ++ ; pQueue . poll ( ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 3 , 1 , 2 } ; int [ ] arr2 = { 3 , 4 , 2 , 1 } ; int N = 3 ; int M = 4 ; System . out . println ( numberOfPairs ( arr1 , N , arr2 , M ) ) ; } }
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | Java program for the above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given arr [ ] ; Function Calling
import java . util . * ; class GFG { static void minCost ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . print ( 0 + "NEW_LINE"); return ; } Arrays . sort ( arr ) ; float raised = 1 / ( float ) ( n - 1 ) ; float temp = ( float ) Math . pow ( arr [ n - 1 ] , raised ) ; int r = ( int ) ( temp ) + 1 ; int i , j , min_cost = Integer . MAX_VALUE ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . min ( min_cost , curr_cost ) ; common_ratio = j ; } } System . out . print ( min_cost + " ▁ " ) ; System . out . print ( common_ratio + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; } }
Mth bit in Nth binary string from a sequence generated by the given operations | Java program for the above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the String Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code
import java . util . * ; class GFG { static final int maxN = 10 ; static void calculateFib ( int fib [ ] , int n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( int x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } static int find_mth_bit ( int n , int m , int fib [ ] ) { if ( n <= 1 ) { return n ; } int len_left = fib [ n - 2 ] ; int len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } static void find_mth_bitUtil ( int n , int m ) { int [ ] fib = new int [ maxN ] ; calculateFib ( fib , maxN ) ; int ans = find_mth_bit ( n , m , fib ) ; System . out . print ( ans + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ; } }
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | Java program to implement the above approach ; Function to count the minimum number of adjacent swaps ; Stores the size of the given matrix ; Stores the count of zero at the end of each row ; Traverse the given matrix ; Count of 0 s at the end of the ith row ; Stores the count of swaps ; Traverse the cntZero array ; If count of zero in the i - th row < ( N - i - 1 ) ; Stores the index of the row where count of zero > ( N - i - 1 ) ; If no row found that satisfy the condition ; Swap the adjacent row ; Driver Code
import java . util . * ; class GFG { static int minAdjSwaps ( int [ ] [ ] mat ) { int N = mat . length ; int [ ] cntZero = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 && mat [ i ] [ j ] == 0 ; j -- ) { cntZero [ i ] ++ ; } } int cntSwaps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntZero [ i ] < ( N - i - 1 ) ) { int First = i ; while ( First < N && cntZero [ First ] < ( N - i - 1 ) ) { First ++ ; } if ( First == N ) { return - 1 ; } while ( First > i ) { cntZero = swap ( cntZero , First , First - 1 ) ; First -- ; cntSwaps ++ ; } } } return cntSwaps ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 0 , 2 } , { 3 , 1 , 0 } , { 4 , 0 , 0 } } ; System . out . print ( minAdjSwaps ( mat ) ) ; } }
Largest area in a grid unbounded by towers | Java program for the above approach ; Function to calculate the largest area unguarded by towers ; Sort the x - coordinates of the list ; Sort the y - coordinates of the list ; dx -- > maximum uncovered tiles in x coordinates ; dy -- > maximum uncovered tiles in y coordinates ; Calculate the maximum uncovered distances for both x and y coordinates ; Largest unguarded area is max ( dx ) - 1 * max ( dy ) - 1 ; Driver Code ; Length and width of the grid ; No of guard towers ; Array to store the x and y coordinates ; Function call
import java . io . * ; import java . util . * ; class GFG { static void maxArea ( int [ ] point_x , int [ ] point_y , int n , int length , int width ) { Arrays . sort ( point_x ) ; Arrays . sort ( point_y ) ; int dx = point_x [ 0 ] ; int dy = point_y [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dx = Math . max ( dx , point_x [ i ] - point_x [ i - 1 ] ) ; dy = Math . max ( dy , point_y [ i ] - point_y [ i - 1 ] ) ; } dx = Math . max ( dx , ( length + 1 ) - point_x [ n - 1 ] ) ; dy = Math . max ( dy , ( width + 1 ) - point_y [ n - 1 ] ) ; System . out . println ( ( dx - 1 ) * ( dy - 1 ) ) ; } public static void main ( String [ ] args ) { int length = 15 , width = 8 ; int n = 3 ; int point_x [ ] = { 3 , 11 , 8 } ; int point_y [ ] = { 8 , 2 , 6 } ; maxArea ( point_x , point_y , n , length , width ) ; } }
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | Java program for the above approach ; Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Driver Code ; Given array and K ; Function call
import java . util . * ; class GFG { static int make_cuts ( int arr [ ] , int n , int K ) { int ans = 0 ; Vector < Integer > poss = new Vector < Integer > ( ) ; int ce = 0 ; int co = 0 ; for ( int x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . add ( Math . abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } Collections . sort ( poss ) ; for ( int x : poss ) { if ( K >= x ) { ans ++ ; K -= x ; } else break ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 4 ; int arr [ ] = { 1 , 2 , 5 , 10 , 15 , 20 } ; System . out . print ( make_cuts ( arr , N , K ) ) ; } }
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | Java program to implement the above approach ; Function to check if given elements can be arranged such that sum of its neighbors is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { static void Arrange ( int arr [ ] , int n ) { int cost = 0 ; HashMap < Integer , Integer > index = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index . put ( arr [ i ] , i ) ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } } System . out . print ( cost ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 1 , 3 } ; int N = arr . length ; Arrange ( arr , N ) ; } }
XOR of all possible pairwise sum from two given Arrays | Java program to implement the above approach ; Function to calculate the sum of XOR of the sum of every pair ; Stores the XOR of sums of every pair ; Iterate to generate all possible pairs ; Update XOR ; Return the answer ; Driver Code
import java . io . * ; class GFG { static int XorSum ( int A [ ] , int B [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { ans = ans ^ ( A [ i ] + B [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 6 , 0 , 0 , 3 , 3 } ; int B [ ] = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = A . length ; System . out . println ( XorSum ( A , B , N ) ) ; } }
Maximum non | Java program for the above approach ; Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the final answer ; Count this character if freq 1 ; Return count of distinct characters ; Driver Code ; Given String ; Given k ; Function Call
import java . util . * ; class GFG { static int maxDistinctChar ( char [ ] s , int n , int k ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( ( int ) s [ i ] ) ) { freq . put ( ( int ) s [ i ] , freq . get ( ( int ) s [ i ] ) + 1 ) ; } else { freq . put ( ( int ) s [ i ] , 1 ) ; } } Vector < Integer > v = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : freq . entrySet ( ) ) { v . add ( it . getValue ( ) ) ; } Collections . sort ( v ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = Math . min ( v . get ( i ) - 1 , k ) ; v . set ( i , v . get ( i ) - mn ) ; k -= mn ; } if ( k > 0 ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = Math . min ( v . get ( i ) , k ) ; v . set ( i , v . get ( i ) - mn ) ; k -= mn ; } } int res = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( v . get ( i ) == 1 ) res ++ ; return res ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . length ( ) ; int K = 1 ; System . out . print ( maxDistinctChar ( S . toCharArray ( ) , N , K ) ) ; } }