codeparrot/xlcost-text-to-code huggingface dataset:
Largest Sum Contiguous Subarray having unique elements | Java program for the above approach ; Function to calculate required maximum subarray sum ; Initialize two pointers ; Stores the unique elements ; Insert the first element ; Current max sum ; Global maximum sum ; Update sum & increment j ; Add the current element ; Update sum and increment i and remove arr [ i ] from set ; Remove the element from start position ; Return the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call ; Print the maximum sum
import java . io . * ; import java . lang . Math ; import java . util . * ; public class GFG { public static int maxSumSubarray ( int [ ] arr ) { int i = 0 , j = 1 ; HashSet < Integer > set = new HashSet < Integer > ( ) ; set . add ( arr [ 0 ] ) ; int sum = arr [ 0 ] ; int maxsum = sum ; while ( i < arr . length - 1 && j < arr . length ) { if ( ! set . contains ( arr [ j ] ) ) { sum = sum + arr [ j ] ; maxsum = Math . max ( sum , maxsum ) ; set . add ( arr [ j ++ ] ) ; } else { sum -= arr [ i ] ; set . remove ( arr [ i ++ ] ) ; } } return maxsum ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 1 , 5 } ; int ans = maxSumSubarray ( arr ) ; System . out . println ( ans ) ; } }
Count of numbers having only one unset bit in a range [ L , R ] | Java program for the above approach ; Function to count numbers in the range [ l , r ] having exactly one unset bit ; Stores the required count ; Iterate over the range ; Calculate number of bits ; Calculate number of set bits ; If count of unset bits is 1 ; Increment answer ; Return the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; for ( int n = L ; n <= R ; n ++ ) { int no_of_bits = ( int ) ( Math . log ( n ) + 1 ) ; int no_of_set_bits = Integer . bitCount ( n ) ; if ( no_of_bits - no_of_set_bits == 1 ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 9 ; System . out . print ( count_numbers ( L , R ) ) ; } }
Count of numbers having only one unset bit in a range [ L , R ] | Java program for the above approach ; Function to count numbers in the range [ L , R ] having exactly one unset bit ; Stores the count elements having one zero in binary ; Stores the maximum number of bits needed to represent number ; Loop over for zero bit position ; Number having zero_bit as unset and remaining bits set ; Sets all bits before zero_bit ; Set the bit at position j ; Set the bit position at j ; If cur is in the range [ L , R ] , then increment ans ; Return ans ; Driver Code ; Function Call
import java . util . * ; class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; int LogR = ( int ) ( Math . log ( R ) + 1 ) ; for ( int zero_bit = 0 ; zero_bit < LogR ; zero_bit ++ ) { int cur = 0 ; for ( int j = 0 ; j < zero_bit ; j ++ ) { cur |= ( 1L << j ) ; } for ( int j = zero_bit + 1 ; j < LogR ; j ++ ) { cur |= ( 1L << j ) ; if ( cur >= L && cur <= R ) { ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 9 ; System . out . print ( count_numbers ( L , R ) ) ; } }
Sum of all distances between occurrences of same characters in a given string | Java program for the above approach ; Function to calculate the sum of distances between occurrences of same characters in a String ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int findSum ( String s ) { int sum = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { sum += ( j - i ) ; } } } return sum ; } public static void main ( String [ ] args ) { String s = " ttt " ; System . out . print ( findSum ( s ) + "NEW_LINE"); } }
Count array elements that can be represented as sum of at least two consecutive array elements | Java Program for above approach ; Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { static int countElements ( int [ ] a , int n ) { int [ ] cnt = new int [ n + 1 ] ; int ans = 0 ; for ( int k : a ) { ++ cnt [ k ] ; } for ( int l = 0 ; l < n ; ++ l ) { int sum = 0 ; for ( int r = l ; r < n ; ++ r ) { sum += a [ r ] ; if ( l == r ) continue ; if ( sum <= n ) { ans += cnt [ sum ] ; cnt [ sum ] = 0 ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 1 , 1 } ; System . out . println ( countElements ( a , a . length ) ) ; } }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Java program for the above approach ; Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
import java . util . * ; class GFG { static int maxGCD ( int n ) { int maxHcf = Integer . MIN_VALUE ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { maxHcf = Math . max ( maxHcf , __gcd ( i , j ) ) ; } } return maxHcf ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( maxGCD ( n ) ) ; } }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Java implementation of the above approach ; Function to find the maximum GCD among all the pairs from first n natural numbers ; Return max GCD ; Driver code
class GFG { static int maxGCD ( int n ) { return ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( maxGCD ( n ) ) ; } }
Remove odd indexed characters from a given string | Java program to implement the above approach ; Function to remove odd indexed characters from a given string ; Stores the resultant string ; If the current index is odd ; Skip the character ; Otherwise , append the character ; Return the modified string ; Driver Code ; Remove the characters which have odd index
import java . util . * ; class GFG { static String removeOddIndexCharacters ( String s ) { String new_string = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 1 ) continue ; new_string += s . charAt ( i ) ; } return new_string ; } public static void main ( String [ ] args ) { String str = " abcdef " ; str = removeOddIndexCharacters ( str ) ; System . out . print ( str ) ; } }
Longest subarray forming a Geometic Progression ( GP ) | Java program to implement the above approach ; Function to return the length of the longest subarray forming a GP in a sorted array ; Base Case ; Stores the length of GP and the common ratio ; Stores the maximum length of the GP ; Traverse the array ; Check if the common ratio is valid for GP ; If the current common ratio is equal to previous common ratio ; Increment the length of the GP ; Store the max length of GP ; Otherwise ; Update the common ratio ; Update the length of GP ; Store the max length of GP ; Update the length of GP ; Store the max length of GP ; Return the max length of GP ; Driver code ; Given array ; Length of the array ; Function call
import java . io . * ; class GFG { static int longestGP ( int A [ ] , int N ) { if ( N < 2 ) return N ; int length = 1 , common_ratio = 1 ; int maxlength = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i + 1 ] % A [ i ] == 0 ) { if ( A [ i + 1 ] / A [ i ] == common_ratio ) { length = length + 1 ; maxlength = Math . max ( maxlength , length ) ; } else { common_ratio = A [ i + 1 ] / A [ i ] ; length = 2 ; } } else { maxlength = Math . max ( maxlength , length ) ; length = 1 ; } } maxlength = Math . max ( maxlength , length ) ; return maxlength ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 } ; int N = arr . length ; System . out . println ( longestGP ( arr , N ) ) ; } }
Maximum number of uncrossed lines between two given arrays | Java program for the above approach ; Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Driver Code ; Given array A [ ] and B [ ] ; Function call
import java . io . * ; class GFG { static int uncrossedLines ( int [ ] a , int [ ] b , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 9 , 2 } ; int B [ ] = { 3 , 2 , 9 } ; int N = A . length ; int M = B . length ; System . out . print ( uncrossedLines ( A , B , N , M ) ) ; } }
Minimize flips required to make all shortest paths from top | Java program for the above approach ; Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Driver Code ; Given Matrix ; Given path as a string ; Function Call
class GFG { static int minFlips ( int mat [ ] [ ] , String s ) { int N = mat . length ; int M = mat [ 0 ] . length ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s . charAt ( i + j ) - '0' ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; String s = "10001" ; System . out . print ( minFlips ( mat , s ) ) ; } }
Generate a pair of integers from a range [ L , R ] whose LCM also lies within the range | Java implementation of the above approach ; Checking if any pair is possible or not in range ( l , r ) ; If not possible print ( - 1 ) ; Print LCM pair ; Driver code ; Function call
import java . util . * ; class GFG { static void lcmpair ( int l , int r ) { int x , y ; x = l ; y = 2 * l ; if ( y > r ) { System . out . print ( "-1NEW_LINE"); } else { System . out . print ( " X ▁ = ▁ " + x + " ▁ Y ▁ = ▁ " + y + "NEW_LINE"); } } public static void main ( String [ ] args ) { int l = 13 , r = 69 ; lcmpair ( l , r ) ; } }
Count non | Java program to implement the above approach ; Function to reverse a number ; Store the reverse of N ; Return reverse of N ; Function to get the count of non - palindromic numbers having same first and last digit ; Store the required count ; Traverse the array ; Store reverse of arr [ i ] ; Check for palindrome ; IF non - palindromic ; Check if first and last digits are equal ; Driver code
import java . io . * ; class GFG { static int revNum ( int N ) { int x = 0 ; while ( N != 0 ) { x = x * 10 + N % 10 ; N = N / 10 ; } return x ; } static int ctNonPalin ( int arr [ ] , int N ) { int Res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = revNum ( arr [ i ] ) ; if ( x == arr [ i ] ) { continue ; } else { if ( arr [ i ] % 10 == x % 10 ) Res += 1 ; } } return Res ; } public static void main ( String [ ] args ) { int arr [ ] = { 121 , 134 , 2342 , 4514 } ; int N = arr . length ; System . out . println ( ctNonPalin ( arr , N ) ) ; } }
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Java program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code
import java . util . * ; class GFG { static int minlt ( int [ ] arr , int target , int n ) { int [ ] [ ] dp = new int [ arr . length + 1 ] [ target + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; for ( int i = 0 ; i < arr . length + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 ] [ j ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= arr . length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , ( dp [ i ] [ j - arr [ i - 1 ] ] ) != Integer . MAX_VALUE ? ( dp [ i ] [ j - arr [ i - 1 ] ] + 1 ) : Integer . MAX_VALUE ) ; } } } if ( dp [ arr . length ] [ target ] == Integer . MAX_VALUE ) { return - 1 ; } else { return dp [ arr . length ] [ target ] ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . length ; System . out . print ( minlt ( arr , target , n ) ) ; } }
Minimum absolute difference of server loads | Java program to implement the above approach ; Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function call
import java . util . * ; class GFG { static int minServerLoads ( int n , int [ ] servers ) { int totalLoad = 0 ; for ( int i = 0 ; i < servers . length ; i ++ ) totalLoad += servers [ i ] ; int requiredLoad = totalLoad / 2 ; int dp [ ] [ ] = new int [ n + 1 ] [ requiredLoad + 1 ] ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n ] [ requiredLoad ] ; } public static void main ( String [ ] args ) { int N = 5 ; int servers [ ] = { 1 , 2 , 3 , 4 , 5 } ; System . out . print ( minServerLoads ( N , servers ) ) ; } }
Length of longest subarray with positive product | Java program to implement the above approach ; Function to find the length of longest subarray whose product is positive ; Stores the length of current subarray with positive product ; Stores the length of current subarray with negative product ; Stores the length of the longest subarray with positive product ; Reset the value ; If current element is positive ; Increment the length of subarray with positive product ; If at least one element is present in the subarray with negative product ; Update res ; If current element is negative ; Increment the length of subarray with negative product ; If at least one element is present in the subarray with positive product ; Update res ; Driver Code
import java . util . * ; class GFG { static int maxLenSub ( int arr [ ] , int N ) { int Pos = 0 ; int Neg = 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Pos = Neg = 0 ; } else if ( arr [ i ] > 0 ) { Pos += 1 ; if ( Neg != 0 ) { Neg += 1 ; } res = Math . max ( res , Pos ) ; } else { Pos = Pos + Neg ; Neg = Pos - Neg ; Neg = Pos - Neg ; Neg += 1 ; if ( Pos != 0 ) { Pos += 1 ; } res = Math . max ( res , Pos ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 2 , - 3 , 0 , 1 } ; int N = arr . length ; System . out . print ( maxLenSub ( arr , N ) ) ; } }
Print all strings of maximum length from an array of strings | Java program to implement the above approach ; Function to find the length of the longest String from the given array of Strings ; Traverse the array ; Stores the length of current String ; Update maximum length ; Return the maximum length ; Function to print the longest Strings from the array ; Find the Strings having length equals to len ; Print the resultant vector of Strings ; Function to print all the longest Strings from the array ; Find the length of longest String ; Find and print all the Strings having length equals to max ; Driver Code
import java . util . * ; class GFG { static int maxLength ( String [ ] arr ) { int len = Integer . MIN_VALUE ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . length ( ) ; if ( len < l ) { len = l ; } } return len ; } static void maxStrings ( String [ ] arr , int len ) { int N = arr . length ; Vector < String > ans = new Vector < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . length ( ) ) { ans . add ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } static void printStrings ( String [ ] arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; } }
Minimize count of flips required such that no substring of 0 s have length exceeding K | Java program to implement the above approach ; Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current subString ; If current character is 0 ; Continue ongoing subString ; Start a new subString ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
class GFG { static int min_flips ( String str , int k ) { if ( str . length ( ) == 0 ) return 0 ; int ans = 0 ; int cnt_zeros = 0 ; for ( char ch : str . toCharArray ( ) ) { if ( ch == '0' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } public static void main ( String [ ] args ) { String str = "11100000011" ; int k = 3 ; System . out . print ( min_flips ( str , k ) ) ; } }
Find all possible subarrays having product less than or equal to K | Java Program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static List < List < Integer > > maxSubArray ( int [ ] arr , int K ) { List < List < Integer > > solution = new ArrayList < > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . length <= 1 K < 0 ) { return new ArrayList < > ( ) ; } for ( int i = 0 ; i < arr . length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < Integer > list = new ArrayList < > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . add ( 0 , arr [ j ] ) ; solution . add ( new ArrayList < > ( list ) ) ; } } return solution ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; System . out . println ( maxSubArray ( arr , K ) ) ; } }
Count of collisions at a point ( X , Y ) | Java program to implement the above approach ; Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function call
import java . util . * ; import java . lang . * ; class GFG { static double solve ( int [ ] [ ] D , int N , int X , int Y ) { ArrayList < Double > T = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x = D [ i ] [ 0 ] ; int y = D [ i ] [ 1 ] ; double speed = D [ i ] [ 2 ] ; double time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . add ( time ) ; } Collections . sort ( T ) ; int i = 0 ; int total = 0 ; while ( i < ( T . size ( ) - 1 ) ) { int count = 1 ; while ( ( i < ( T . size ( ) - 1 ) ) && ( Double . compare ( T . get ( i ) , T . get ( i + 1 ) ) == 0 ) ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int [ ] [ ] D = { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { - 10 , 24 , 2 } , { 7 , 24 , 2 } , { - 24 , 7 , 2 } } ; int X = 0 , Y = 0 ; System . out . println ( solve ( D , N , X , Y ) ) ; } }
Longest subarray forming an Arithmetic Progression ( AP ) | Java Program to implement the above approach ; Function to return the length of longest subarray forming an AP ; Minimum possible length of required subarray is 2 ; Stores the length of the current subarray ; Stores the common difference of the current AP ; Stores the common difference of the previous AP ; If the common differences are found to be equal ; Continue the previous subarray ; Start a new subarray ; Update the length to store maximum length ; Update the length to store maximum length ; Return the length of the longest subarray ; Driver Code
import java . util . * ; class GFG { static int getMaxLength ( int arr [ ] , int N ) { int res = 2 ; int dist = 2 ; int curradj = ( arr [ 1 ] - arr [ 0 ] ) ; int prevadj = ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < N ; i ++ ) { curradj = arr [ i ] - arr [ i - 1 ] ; if ( curradj == prevadj ) { dist ++ ; } else { prevadj = curradj ; res = Math . max ( res , dist ) ; dist = 2 ; } } res = Math . max ( res , dist ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 7 , 4 , 6 , 8 , 10 , 11 } ; int N = arr . length ; System . out . print ( getMaxLength ( arr , N ) ) ; } }
Minimize cost to empty a given string by removing characters alphabetically | Java program for the above approach ; Function to find the minimum cost required to remove each character of the string in alphabetical order ; Stores the frequency of characters of the string ; Iterate through the string ; Count the number of characters smaller than the present character ; If no smaller character precedes current character ; Increase the frequency of the current character ; Return the total cost ; Driver Code ; Given string str ; Function call
import java . io . * ; class GFG { static int minSteps ( String str , int N ) { int smaller , cost = 0 ; int f [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = str . charAt ( i ) - ' a ' ; smaller = 0 ; for ( int j = 0 ; j <= curr_ele ; j ++ ) { if ( f [ j ] != 0 ) smaller += f [ j ] ; } if ( smaller == 0 ) cost += ( i + 1 ) ; else cost += ( i - smaller + 1 ) ; f [ str . charAt ( i ) - ' a ' ] ++ ; } return cost ; } public static void main ( String [ ] args ) { String str = " abcab " ; int N = str . length ( ) ; System . out . println ( minSteps ( str , N ) ) ; } }