codeparrot/xlcost-text-to-code huggingface dataset:
Find the missing digit in given product of large positive integers | Java program for the above approach ; Function to find the replaced digit in the product of a * b ; Keeps track of the sign of the current digit ; Stores the value of a % 11 ; Find the value of a mod 11 for large value of a as per the derived formula ; Stores the value of b % 11 ; Find the value of b mod 11 for large value of a as per the derived formula ; Stores the value of c % 11 ; Keeps track of the sign of x ; If the current digit is the missing digit , then keep the track of its sign ; Find the value of x using the derived equation ; Check if x has a negative sign ; Return positive equivaluent of x mod 11 ; Driver Code
class GFG { public static int findMissingDigit ( String a , String b , String c ) { int w = 1 ; int a_mod_11 = 0 ; for ( int i = a . length ( ) - 1 ; i >= 0 ; i -- ) { a_mod_11 = ( a_mod_11 + w * ( a . charAt ( i ) - '0' ) ) % 11 ; w = w * - 1 ; } int b_mod_11 = 0 ; w = 1 ; for ( int i = b . length ( ) - 1 ; i >= 0 ; i -- ) { b_mod_11 = ( b_mod_11 + w * ( b . charAt ( i ) - '0' ) ) % 11 ; w = w * - 1 ; } int c_mod_11 = 0 ; boolean xSignIsPositive = true ; w = 1 ; for ( int i = c . length ( ) - 1 ; i >= 0 ; i -- ) { if ( c . charAt ( i ) == ' x ' ) { xSignIsPositive = ( w == 1 ) ; } else { c_mod_11 = ( c_mod_11 + w * ( c . charAt ( i ) - '0' ) ) % 11 ; } w = w * - 1 ; } int x = ( ( a_mod_11 * b_mod_11 ) - c_mod_11 ) % 11 ; if ( ! xSignIsPositive ) { x = - x ; } return ( x % 11 + 11 ) % 11 ; } public static void main ( String args [ ] ) { String A = "123456789" ; String B = "987654321" ; String C = "12193263111x635269" ; System . out . println ( findMissingDigit ( A , B , C ) ) ; } }
Check if a string can be made empty by repeatedly removing given subsequence | Java program for the above approach ; Function to check if a string can be made empty by removing all subsequences of the form " GFG " or not ; Driver Code
import java . io . * ; class GFG { public static void findIfPossible ( int N , String str ) { int countG = 0 , countF = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) countG ++ ; else countF ++ ; } if ( 2 * countF != countG ) { System . out . println ( " No " ) ; } else { int id = 0 ; boolean flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) { countG -- ; id ++ ; } else { countF -- ; id -- ; } if ( id < 0 ) { flag = false ; break ; } if ( countG < countF ) { flag = false ; break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { int n = 6 ; String str = " GFGFGG " ; findIfPossible ( n , str ) ; } }
Check whether second string can be formed from characters of first string used any number of times | Java implementation of the above approach ; Function to check if str2 can be made by characters of str1 or not ; To store the occurrence of every character ; Length of the two strings ; Assume that it is possible to compose the string str2 from str1 ; Iterate over str1 ; Store the presence of every character ; Iterate over str2 ; Ignore the spaces ; Check for the presence of character in str1 ; If it is possible to make str2 from str1 ; Driver Code ; Given strings ; Function Call
import java . util . Arrays ; class GFG { public static void isPossible ( String str1 , String str2 ) { int arr [ ] = new int [ 256 ] ; Arrays . fill ( arr , 0 ) ; int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; int i , j ; boolean possible = true ; for ( i = 0 ; i < l1 ; i ++ ) { arr [ str1 . charAt ( i ) ] = 1 ; } for ( i = 0 ; i < l2 ; i ++ ) { if ( str2 . charAt ( i ) != ' ▁ ' ) { if ( arr [ str2 . charAt ( i ) ] == 1 ) continue ; else { possible = false ; break ; } } } if ( possible ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { String str1 = " we ▁ all ▁ love ▁ geeksforgeeks " ; String str2 = " we ▁ all ▁ love ▁ geeks " ; isPossible ( str1 , str2 ) ; } }
Minimum number of flipping adjacent bits required to make given Binary Strings equal | Java program for the above approach ; Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
import java . util . * ; class GFG { static int find_Min_Inversion ( int n , char [ ] s1 , char [ ] s2 ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( s1 [ i ] == '1' ) { s1 [ i ] = '0' ; } else { s1 [ i ] = '1' ; } if ( s1 [ i + 1 ] == '1' ) { s1 [ i + 1 ] = '0' ; } else { s1 [ i + 1 ] = '1' ; } count ++ ; } } if ( String . copyValueOf ( s1 ) . equals ( String . copyValueOf ( s2 ) ) ) { return count ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 4 ; String s1 = "0101" ; String s2 = "1111" ; System . out . print ( find_Min_Inversion ( n , s1 . toCharArray ( ) , s2 . toCharArray ( ) ) + "NEW_LINE"); } }
Longest subsequence with consecutive English alphabets | C # program for the above approach ; Function to find the length of subsequence starting with character ch ; Length of the string ; Stores the maximum length ; Traverse the given string ; If s [ i ] is required character ch ; Increment ans by 1 ; Increment character ch ; Return the current maximum length with character ch ; Function to find the maximum length of subsequence of consecutive characters ; Stores the maximum length of consecutive characters ; Update ans ; Return the maximum length of subsequence ; Driver Code ; Input ; Function Call
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static int findSubsequence ( String S , char ch ) { int N = S . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ch ) { ans ++ ; ch ++ ; } } return ans ; } static int findMaxSubsequence ( String S ) { int ans = 0 ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { ans = Math . max ( ans , findSubsequence ( S , ch ) ) ; } return ans ; } public static void main ( String [ ] args ) { String S = " abcabefghijk " ; System . out . print ( findMaxSubsequence ( S ) ) ; } }
Minimum number of alternate subsequences required to be removed to empty a Binary String | Java program for the above approach ; Function to find the minimum number of operations to empty a binary string ; Stores the resultant number of operations ; Stores the number of 0 s ; Stores the number of 1 s ; Traverse the given string ; To balance 0 with 1 if possible ; Increment the value of cn0 by 1 ; To balance 1 with 0 if possible ; Increment the value of cn1 ; Update the maximum number of unused 0 s and 1 s ; Print the resultant count ; Driver Code
import java . util . * ; class GFG { static void minOpsToEmptyString ( String s ) { int ans = Integer . MIN_VALUE ; int cn0 = 0 ; int cn1 = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { if ( cn1 > 0 ) cn1 -- ; cn0 ++ ; } else { if ( cn0 > 0 ) cn0 -- ; cn1 ++ ; } ans = Math . max ( ans , Math . max ( cn0 , cn1 ) ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = "010101" ; minOpsToEmptyString ( S ) ; } }
Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2 | Java program for the above approach ; Function to find the length of the smallest string possible by removing substrings "01" and "11" ; Stores the length of the smallest string ; Traverse the string S ; If st is greater than 0 and S [ i ] is '1' ; Delete the last character and decrement st by 1 ; Otherwise ; Increment st by 1 ; Return the answer in st ; Driver code ; Input ; Function call
public class GFG_JAVA { static int shortestString ( String S , int N ) { int st = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st > 0 && S . charAt ( i ) == '1' ) { st -- ; } else { st ++ ; } } return st ; } public static void main ( String [ ] args ) { String S = "1010" ; int N = S . length ( ) ; System . out . println ( shortestString ( S , N ) ) ; } }
Longest Non | Java program for the above approach ; Function to find the length of the longest non - increasing subsequence ; Stores the prefix and suffix count of 1 s and 0 s respectively ; Initialize the array ; Store the number of '1' s up to current index i in pre ; Find the prefix sum ; If the current element is '1' , update the pre [ i ] ; Store the number of '0' s over the range [ i , N - 1 ] ; Find the suffix sum ; If the current element is '0' , update post [ i ] ; Stores the maximum length ; Find the maximum value of pre [ i ] + post [ i ] ; Return the answer ; Driver Code
class GFG { static int findLength ( String str , int n ) { int pre [ ] = new int [ n ] ; int post [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { pre [ i ] = 0 ; post [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 ) { pre [ i ] += pre [ i - 1 ] ; } if ( str . charAt ( i ) == '1' ) { pre [ i ] += 1 ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i != n - 1 ) post [ i ] += post [ i + 1 ] ; if ( str . charAt ( i ) == '0' ) post [ i ] += 1 ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , pre [ i ] + post [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "0101110110100001011" ; System . out . println ( findLength ( S , S . length ( ) ) ) ; } }
Number of substrings having an equal number of lowercase and uppercase letters | Java program for the above approach ; Function to find the count of substrings having an equal number of uppercase and lowercase characters ; Stores the count of prefixes having sum S considering uppercase and lowercase characters as 1 and - 1 ; Stores the count of substrings having equal number of lowercase and uppercase characters ; Stores the sum obtained so far ; If the character is uppercase ; Otherwise ; If currsum is 0 ; If the current sum exists in the HashMap prevSum ; Increment the resultant count by 1 ; Update the frequency of the current sum by 1 ; Return the resultant count of the subarrays ; Driver Code
import java . util . HashMap ; class GFG { static int countSubstring ( String S , int N ) { HashMap < Integer , Integer > prevSum = new HashMap < > ( ) ; int res = 0 ; int currentSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) >= ' A ' && S . charAt ( i ) <= ' Z ' ) { currentSum ++ ; } else currentSum -- ; if ( currentSum == 0 ) res ++ ; if ( prevSum . containsKey ( currentSum ) ) { res += prevSum . get ( currentSum ) ; } prevSum . put ( currentSum , prevSum . getOrDefault ( currentSum , 0 ) + 1 ) ; } return res ; } public static void main ( String [ ] args ) { String S = " gEEk " ; System . out . println ( countSubstring ( S , S . length ( ) ) ) ; } }
Number of substrings with each character occurring even times | Java program for the above approach ; Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int subString ( String s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = i + 1 ; len <= n ; len ++ ) { String test_str = s . substring ( i , len ) ; HashMap < Character , Integer > res = new HashMap < > ( ) ; for ( char keys : test_str . toCharArray ( ) ) { res . put ( keys , res . getOrDefault ( keys , 0 ) + 1 ) ; } int flag = 0 ; for ( char keys : res . keySet ( ) ) { if ( res . get ( keys ) % 2 != 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) count += 1 ; } } return count ; } public static void main ( String [ ] args ) { String S = " abbaa " ; int N = S . length ( ) ; System . out . println ( subString ( S , N ) ) ; } }
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Java program for the above approach ; Function to count new pairs of strings that can be obtained by swapping first characters of any pair of strings ; Stores the count of pairs ; Generate all possible pairs of strings from the array arr [ ] ; Stores the current pair of strings ; Swap the first characters ; Check if they are already present in the array or not ; If both the strings are not present ; Increment the ans by 1 ; Print the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countStringPairs ( String a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { char p [ ] = a [ i ] . toCharArray ( ) ; char q [ ] = a [ j ] . toCharArray ( ) ; if ( p [ 0 ] != q [ 0 ] ) { char temp = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = temp ; int flag1 = 0 ; int flag2 = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( a [ k ] . equals ( new String ( p ) ) ) { flag1 = 1 ; } if ( a [ k ] . equals ( new String ( q ) ) ) { flag2 = 1 ; } } if ( flag1 == 0 && flag2 == 0 ) { ans = ans + 1 ; } } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " good " , " bad " , " food " } ; int N = arr . length ; countStringPairs ( arr , N ) ; } }
Modify string by replacing characters by alphabets whose distance from that character is equal to its frequency | Java program for the above approach ; Function to modify string by replacing characters by the alphabet present at distance equal to frequency of the string ; Stores frequency of characters ; Stores length of the string ; Traverse the given string S ; Increment frequency of current character by 1 ; Traverse the string ; Store the value to be added to the current character ; Check if after adding the frequency , the character is less than ' z ' or not ; Otherwise , update the value of add so that s [ i ] doesn ' t ▁ exceed ▁ ' z ' ; Print the modified string ; Driver Code
class GFG { static void addFrequencyToCharacter ( char [ ] s ) { int frequency [ ] = new int [ 26 ] ; int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ s [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < n ; i ++ ) { int add = frequency [ s [ i ] - ' a ' ] % 26 ; if ( ( int ) ( s [ i ] ) + add <= ( int ) ( ' z ' ) ) s [ i ] = ( char ) ( ( int ) ( s [ i ] ) + add ) ; else { add = ( ( int ) ( s [ i ] ) + add ) - ( ( int ) ( ' z ' ) ) ; s [ i ] = ( char ) ( ( int ) ( ' a ' ) + add - 1 ) ; } } System . out . println ( s ) ; } public static void main ( String [ ] args ) { String str = " geeks " ; addFrequencyToCharacter ( str . toCharArray ( ) ) ; } }
Check if it is possible to reach any point on the circumference of a given circle from origin | Java program for the above approach ; Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , ' R ' ; Stores the count of ' U ' , ' D ' ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String isPossible ( String S , int R , int N ) { int cntl = 0 , cntr = 0 ; int cntu = 0 , cntd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' L ' ) cntl ++ ; else if ( S . charAt ( i ) == ' R ' ) cntr ++ ; else if ( S . charAt ( i ) == ' U ' ) cntu ++ ; else cntd ++ ; } if ( Math . max ( Math . max ( cntl , cntr ) , Math . max ( cntu , cntd ) ) >= R ) return " Yes " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int r_square = R * R ; for ( int i = 1 ; i * i <= r_square ; i ++ ) { mp . put ( i * i , i ) ; if ( mp . containsKey ( r_square - i * i ) ) { if ( Math . max ( cntl , cntr ) >= mp . get ( r_square - i * i ) && Math . max ( cntu , cntd ) >= i ) return " Yes " ; if ( Math . max ( cntl , cntr ) >= i && Math . max ( cntu , cntd ) >= mp . get ( r_square - i * i ) ) return " Yes " ; } } return " No " ; } public static void main ( String [ ] args ) { String S = " RDLLDDLDU " ; int R = 5 ; int N = S . length ( ) ; System . out . println ( isPossible ( S , R , N ) ) ; } }
Modify characters of a string by adding integer values of same | Java program for the above approach ; Function to modify a given String by adding ASCII value of characters from a String S to integer values of same indexed characters in String N ; Traverse the String ; Stores integer value of character in String N ; Stores ASCII value of character in String S ; If b exceeds 122 ; Replace the character ; Print resultant String ; Driver Code ; Given Strings ; Function call to modify String S by given operations
import java . util . * ; class GFG { static void addASCII ( char [ ] S , char [ ] N ) { for ( int i = 0 ; i < S . length ; i ++ ) { int a = ( int ) ( N [ i ] ) - '0' ; int b = ( int ) ( S [ i ] ) + a ; if ( b > 122 ) b -= 26 ; S [ i ] = ( char ) ( b ) ; } System . out . print ( S ) ; } public static void main ( String [ ] args ) { String S = " sun " , N = "966" ; addASCII ( S . toCharArray ( ) , N . toCharArray ( ) ) ; } }
Modify array by removing characters from their Hexadecimal representations which are present in a given string | java program for the above approach ; Function to convert a decimal number to its equivalent hexadecimal number ; Function to convert hexadecimal number to its equavalent decimal number ; Stores characters with their respective hexadecimal values ; Stores answer ; Traverse the string ; If digit ; If character ; Return the answer ; Function to move all the alphabets to front ; Function to modify each array element by removing characters from their hexadecimal representation which are present in a given string ; Traverse the array ; Stores hexadecimal value ; Remove the characters from hexadecimal representation present in string S ; Stores decimal value ; Replace array element ; Print the modified array ; Driver Code ; Given array ; Given string ; Function call to modify array by given operations
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String decHex ( int n ) { char alpha [ ] = { ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ; StringBuilder ans = new StringBuilder ( " " ) ; while ( n > 0 ) { if ( n % 16 < 10 ) { ans . append ( Integer . toString ( n % 16 ) ) ; } else { ans . append ( alpha [ n % 16 - 10 ] ) ; } n /= 16 ; } ans = ans . reverse ( ) ; return ans . toString ( ) ; } static int hexDec ( String convertedHex ) { char mp [ ] = { 10 , 11 , 12 , 13 , 14 , 15 } ; int ans = 0 ; int pos = 0 ; StringBuilder s = new StringBuilder ( convertedHex ) ; convertedHex = s . reverse ( ) . toString ( ) ; for ( char ch : convertedHex . toCharArray ( ) ) { if ( Character . isDigit ( ch ) ) { ans += ( ( int ) Math . pow ( 16 , pos ) ) * ( ch - '0' ) ; } else { ans += ( ( int ) Math . pow ( 16 , pos ) ) * mp [ ch - ' A ' ] ; } pos += 1 ; } return ans ; } static String removeChars ( String hexaVal , String S ) { HashSet < Character > setk = new HashSet < > ( ) ; for ( char ch : S . toCharArray ( ) ) { setk . add ( ch ) ; } String ans = " " ; for ( char ch : hexaVal . toCharArray ( ) ) { if ( setk . contains ( ch ) ) { continue ; } ans += ch ; } return ans ; } static void convertArr ( int arr [ ] , String S ) { for ( int i = 0 ; i < arr . length ; i ++ ) { String hexaVal = decHex ( arr [ i ] ) ; String convertedHex = removeChars ( hexaVal , S ) ; int decVal = hexDec ( convertedHex ) ; arr [ i ] = decVal ; } for ( int val : arr ) { System . out . print ( val + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 74 , 91 , 31 , 122 } ; String S = "1AB " ; convertArr ( arr , S ) ; } }
Minimum number of chairs required to ensure that every worker is seated at any instant | Java program for the above approach ; Function to find the minimum number of chairs required to ensure that every worker is seated at any time ; Stores the number of chairs required ; Pointer to iterate ; Stores minimum number of chairs required ; Iterate over every character ; If character is ' E ' ; Increase the count ; Otherwise ; Update maximum value of count obtained at any given time ; Return mini ; Driver code ; Given String ; Function call to find the minimum number of chairs
import java . io . * ; import java . util . * ; class GFG { static int findMinimumChairs ( String s ) { int count = 0 ; int i = 0 ; int mini = Integer . MIN_VALUE ; while ( i < s . length ( ) ) { if ( s . charAt ( i ) == ' E ' ) count ++ ; else count -- ; mini = Math . max ( count , mini ) ; i ++ ; } return mini ; } public static void main ( String [ ] args ) { String s = " EELEE " ; System . out . print ( findMinimumChairs ( s ) ) ; } }
Modify string by inserting characters such that every K | Java Program to implement the above approach ; Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void fillString ( String str , int k ) { char s [ ] = str . toCharArray ( ) ; HashMap < Integer , Character > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp . put ( i % k , s [ i ] ) ; } } for ( int i = 0 ; i < s . length ; i ++ ) { if ( ! mp . containsKey ( i % k ) ) { System . out . println ( - 1 ) ; return ; } s [ i ] = mp . getOrDefault ( i % k , s [ i ] ) ; } System . out . println ( new String ( s ) ) ; } public static void main ( String [ ] args ) { String S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; } }