codeparrot/xlcost-text-to-code huggingface dataset:
Print string of odd length in ' X ' format | Java program to print cross pattern ; Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; driver code
class GFG { static void pattern ( String str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int j = len - 1 - i ; for ( int k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) System . out . print ( str . charAt ( k ) ) ; else System . out . print ( " ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; pattern ( str , len ) ; } }
Recursively print all sentences that can be formed from list of word lists | Java program to print all possible sentences from a list of word list ; A recursive function to print all possible sentences that can be formed from a list of word list ; Add current word to output array ; If this is last word of current output sentence , then print the output sentence ; Recur for next row ; A wrapper over printUtil ( ) ; Create an array to store sentence ; Consider all words for first row as starting points and print all sentences ; Driver program to test above functions
class GFG { static final int R = 3 ; static final int C = 3 ; static void printUtil ( String [ ] [ ] arr , int m , int n , String [ ] output ) { output [ m ] = arr [ m ] [ n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 ] [ i ] != " " && m < C ) printUtil ( arr , m + 1 , i , output ) ; } static void print ( String [ ] [ ] arr ) { String [ ] output = new String [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 ] [ i ] != " " ) printUtil ( arr , 0 , i , output ) ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = { { " you " , " we " , " " } , { " have " , " are " , " " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; } }
Check if characters of a given string can be rearranged to form a palindrome | Java implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code
import java . io . * ; import java . math . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean canFormPalindrome ( String str ) { int count [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ ( int ) ( str . charAt ( i ) ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Recursively remove all adjacent duplicates | Java Program for above approach ; Recursively removes adjacent duplicates from str and returns new string . las_removed is a pointer to last_removed character ; If length of string is 1 or 0 ; Recursively remove all the adjacent characters formed by removing the adjacent characters ; Have to check whether this is the repeated character that matches the last char of the parent String ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { private static String removeDuplicates ( String s , char ch ) { if ( s == null || s . length ( ) <= 1 ) { return s ; } int i = 0 ; while ( i < s . length ( ) ) { if ( i + 1 < s . length ( ) && s . charAt ( i ) == s . charAt ( i + 1 ) ) { int j = i ; while ( j + 1 < s . length ( ) && s . charAt ( j ) == s . charAt ( j + 1 ) ) { j ++ ; } char lastChar = i > 0 ? s . charAt ( i - 1 ) : ch ; String remStr = removeDuplicates ( s . substring ( j + 1 , s . length ( ) ) , lastChar ) ; s = s . substring ( 0 , i ) ; int k = s . length ( ) , l = 0 ; while ( remStr . length ( ) > 0 && s . length ( ) > 0 && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { while ( remStr . length ( ) > 0 && remStr . charAt ( 0 ) != ch && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { remStr = remStr . substring ( 1 , remStr . length ( ) ) ; } s = s . substring ( 0 , s . length ( ) - 1 ) ; } s = s + remStr ; i = j ; } else { i ++ ; } } return s ; } public static void main ( String [ ] args ) { String str1 = " mississipie " ; System . out . println ( removeDuplicates ( str1 , ' ▁ ' ) ) ; String str2 = " ocvvcolop " ; System . out . println ( removeDuplicates ( str2 , ' ▁ ' ) ) ; } }
Generate n | Java program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; print contents of arr ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > generateGray ( int n ) { if ( n <= 0 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; } } ; return temp ; } if ( n == 1 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; add ( "1" ) ; } } ; return temp ; } ArrayList < String > recAns = generateGray ( n - 1 ) ; ArrayList < String > mainAns = new ArrayList < String > ( ) ; for ( int i = 0 ; i < recAns . size ( ) ; i ++ ) { String s = recAns . get ( i ) ; mainAns . add ( "0" + s ) ; } for ( int i = recAns . size ( ) - 1 ; i >= 0 ; i -- ) { String s = recAns . get ( i ) ; mainAns . add ( "1" + s ) ; } return mainAns ; } static void generateGrayarr ( int n ) { ArrayList < String > arr = new ArrayList < String > ( ) ; arr = generateGray ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { System . out . println ( arr . get ( i ) ) ; } } public static void main ( String [ ] args ) { generateGrayarr ( 3 ) ; } }
Check whether two strings are anagram of each other | JAVA program to check if two strings are anagrams of each other ; function to check whether two strings are anagram of each other ; Create 2 count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; Compare count arrays ; Driver code ; Function call
import java . io . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean areAnagram ( char str1 [ ] , char str2 [ ] ) { int count1 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count1 , 0 ) ; int count2 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count2 , 0 ) ; int i ; for ( i = 0 ; i < str1 . length && i < str2 . length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void main ( String args [ ] ) { char str1 [ ] = ( " geeksforgeeks " ) . toCharArray ( ) ; char str2 [ ] = ( " forgeeksgeeks " ) . toCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) System . out . println ( " The ▁ two ▁ strings ▁ are " + " anagram ▁ of ▁ each ▁ other " ) ; else System . out . println ( " The ▁ two ▁ strings ▁ are ▁ not " + " ▁ anagram ▁ of ▁ each ▁ other " ) ; } }
Find the smallest window in a string containing all characters of another string | Java program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; Check if string ' s ▁ length ▁ is ▁ ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string Count of characters ; Count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ ▁ with ▁ pattern ' s char then increment count ; If all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver Method
public class GFG { static final int no_of_chars = 256 ; static String findSubString ( String str , String pat ) { int len1 = str . length ( ) ; int len2 = pat . length ( ) ; if ( len1 < len2 ) { System . out . println ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } int hash_pat [ ] = new int [ no_of_chars ] ; int hash_str [ ] = new int [ no_of_chars ] ; for ( int i = 0 ; i < len2 ; i ++ ) hash_pat [ pat . charAt ( i ) ] ++ ; int start = 0 , start_index = - 1 , min_len = Integer . MAX_VALUE ; int count = 0 ; for ( int j = 0 ; j < len1 ; j ++ ) { hash_str [ str . charAt ( j ) ] ++ ; if ( hash_str [ str . charAt ( j ) ] <= hash_pat [ str . charAt ( j ) ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str . charAt ( start ) ] > hash_pat [ str . charAt ( start ) ] || hash_pat [ str . charAt ( start ) ] == 0 ) { if ( hash_str [ str . charAt ( start ) ] > hash_pat [ str . charAt ( start ) ] ) hash_str [ str . charAt ( start ) ] -- ; start ++ ; } int len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == - 1 ) { System . out . println ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } return str . substring ( start_index , start_index + min_len ) ; } public static void main ( String [ ] args ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; String pat = " tist " ; System . out . print ( "Smallest window is :NEW_LINE " + findSubString ( str , pat ) ) ; } }
Remove duplicates from a given string | Java program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
import java . util . * ; class GFG { static String removeDuplicate ( char str [ ] , int n ) { int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) { if ( str [ i ] == str [ j ] ) { break ; } } if ( j == i ) { str [ index ++ ] = str [ i ] ; } } return String . valueOf ( Arrays . copyOf ( str , index ) ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; System . out . println ( removeDuplicate ( str , n ) ) ; } }
Remove duplicates from a given string | access time in unordered_map on is O ( 1 ) generally if no collisions occur and therefore it helps us check if an element exists in a String in O ( 1 ) time complexity with constant space . ; driver code
import java . util . * ; class GFG { static char [ ] removeDuplicates ( char [ ] s , int n ) { Map < Character , Integer > exists = new HashMap < > ( ) ; String st = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! exists . containsKey ( s [ i ] ) ) { st += s [ i ] ; exists . put ( s [ i ] , 1 ) ; } } return st . toCharArray ( ) ; } public static void main ( String [ ] args ) { char s [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = s . length ; System . out . print ( removeDuplicates ( s , n ) ) ; } }
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | Java program for the above approach ; Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; Integer . bitCount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int maximumXOR ( int arr [ ] , int n , int K ) { K ++ ; int maxXor = Integer . MIN_VALUE ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( Integer . bitCount ( i ) == K ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; int M = 2 ; System . out . println ( maximumXOR ( arr , N , M ) ) ; } }
Tower of Hanoi | Set 2 | Java program for the above approach ; Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
class GFG { static void TowerOfHanoi ( int N ) { for ( int x = 1 ; x <= Math . pow ( 2 , N ) - 1 ; x ++ ) { System . out . print ( " Move ▁ from ▁ Rod ▁ " + ( ( x & x - 1 ) % 3 + 1 ) + " ▁ to ▁ Rod ▁ " + ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 3 ; TowerOfHanoi ( N ) ; } }
Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | Java program for the above approach ; Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver Code
import java . util . * ; class GFG { static boolean isValid ( int [ ] [ ] board , int i , int j , int K ) { if ( board [ i ] [ j ] <= K ) { return true ; } return false ; } static boolean findPath ( int [ ] [ ] board , int X , int Y , int M , int N , int K ) { if ( X < 0 X == M Y < 0 Y == N ) { return true ; } if ( isValid ( board , X , Y , K ) ) { int board_XY = board [ X ] [ Y ] ; board [ X ] [ Y ] = Integer . MAX_VALUE ; if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) || findPath ( board , X - 1 , Y , M , N , K - board_XY ) || findPath ( board , X , Y + 1 , M , N , K - board_XY ) || findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) { return true ; } board [ X ] [ Y ] = board_XY ; } return false ; } public static void main ( String [ ] args ) { int [ ] [ ] grid = { { 25 , 5 , 25 , 25 , 25 , 25 } , { 25 , 1 , 1 , 5 , 12 , 25 } , { 25 , 1 , 12 , 0 , 15 , 25 } , { 22 , 1 , 11 , 2 , 19 , 15 } , { 25 , 2 , 2 , 1 , 12 , 15 } , { 25 , 9 , 10 , 1 , 11 , 25 } , { 25 , 25 , 25 , 25 , 25 , 25 } } ; int K = 17 ; int M = grid . length ; int N = grid [ 0 ] . length ; int X = 2 , Y = 3 ; if ( findPath ( grid , X , Y , M , N , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sum of all subsets whose sum is a Perfect Number from a given array | Java program for the above approach ; Function to check is a given number is a perfect number or not ; Stores sum of divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find the sum of all the subsets from an array whose sum is a perfect number ; Stores the total number of subsets , i . e . 2 ^ n ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider array elements from positions of set bits in the binary representation of n ; If sum of chosen elements is a perfect number ; Driver Code
import java . util . * ; class GFG { static int isPerfect ( int x ) { int sum_div = 1 ; for ( int i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } static void subsetSum ( int arr [ ] , int n ) { long total = 1 << n ; for ( long i = 0 ; i < total ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) sum += arr [ j ] ; if ( isPerfect ( sum ) != 0 ) { System . out . print ( sum + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 6 } ; int N = arr . length ; subsetSum ( arr , N ) ; } }
Pen Distribution Problem | Java implementation of the above approach ; Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
import java . util . * ; import java . lang . * ; public class GFG { static void solve ( int N , int P1 , int P2 , int X , int Move , boolean QuitP1 , boolean QuitP2 ) { if ( N == 0 || ( QuitP1 && QuitP2 ) ) { System . out . println ( " Number ▁ of ▁ pens ▁ remaining " + " ▁ in ▁ the ▁ box : ▁ " + N ) ; System . out . println ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P1 : ▁ " + P1 ) ; System . out . println ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P2 : ▁ " + P2 ) ; return ; } if ( Move == 0 && QuitP1 == false ) { int req_P1 = ( int ) ( Math . pow ( 2 , X ) ) ; if ( req_P1 <= N ) { P1 += req_P1 ; N -= req_P1 ; } else { QuitP1 = true ; } } else if ( Move == 1 && QuitP2 == false ) { int req_P2 = ( int ) ( Math . pow ( 3 , X ) ) ; if ( req_P2 <= N ) { P2 += req_P2 ; N -= req_P2 ; } else { QuitP2 = true ; } } X ++ ; Move = ( ( Move == 1 ) ? 0 : 1 ) ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } static void PenGame ( int N ) { int P1 = 0 ; int P2 = 0 ; int X = 0 ; int Move = 0 ; boolean QuitP1 = false ; boolean QuitP2 = false ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } public static void main ( String [ ] args ) { int N = 22 ; PenGame ( N ) ; } }
Print all possible K | Java program to implement the above approach ; Function to print all subsequences of length K from N natural numbers whose sum equal to N ; Base case ; Iterate over the range [ 1 , N ] ; Insert i into temp ; Pop i from temp ; Utility function to print all subsequences of length K with sum equal to N ; Store all subsequences of length K from N natural numbers ; Store current subsequence of length K from N natural numbers ; Stores total count of subsequences ; Print all subsequences ; Treaverse all subsequences ; Print current subsequence ; If current element is last element of subsequence ; If current subsequence is last subsequence from n natural numbers ; Driver code
import java . io . * ; import java . util . * ; import java . util . stream . Collectors ; class GFG { static void findSub ( List < List < Integer > > res , int sum , int K , int N , List < Integer > temp ) { if ( K == 0 && sum == 0 ) { List < Integer > newList = temp . stream ( ) . collect ( Collectors . toList ( ) ) ; res . add ( newList ) ; return ; } if ( sum <= 0 K <= 0 ) { return ; } for ( int i = 1 ; i <= N ; i ++ ) { temp . add ( i ) ; findSub ( res , sum - i , K - 1 , N , temp ) ; temp . remove ( temp . size ( ) - 1 ) ; } } static void UtilPrintSubsequncesOfKSumN ( int N , int K ) { @ SuppressWarnings ( " unchecked " ) List < List < Integer > > res = new ArrayList ( ) ; @ SuppressWarnings ( " unchecked " ) List < Integer > temp = new ArrayList ( ) ; findSub ( res , N , K , N , temp ) ; int sz = res . size ( ) ; System . out . print ( " { ▁ " ) ; for ( int i = 0 ; i < sz ; i ++ ) { System . out . print ( " { ▁ " ) ; for ( int j = 0 ; j < K ; j ++ ) { if ( j == K - 1 ) System . out . print ( res . get ( i ) . get ( j ) + " ▁ " ) ; else System . out . print ( res . get ( i ) . get ( j ) + " , ▁ " ) ; } if ( i == sz - 1 ) System . out . print ( " } " ) ; else System . out . print ( " } , ▁ " ) ; } System . out . print ( " ▁ } " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 2 ; UtilPrintSubsequncesOfKSumN ( N , K ) ; } }
Generate all N digit numbers having absolute difference as K between adjacent digits | Java program for the above approach ; Function that recursively finds the possible numbers and append into ans ; Base Case ; Check the sum of last digit and k less than or equal to 9 or not ; If k == 0 , then subtraction and addition does not make any difference Hence , subtraction is skipped ; Function to call checkUntil function for every integer from 1 to 9 ; check_util function recursively store all numbers starting from i ; Function to print the all numbers which satisfy the conditions ; Driver Code ; Given N and K ; To store the result ; Function Call ; Print Resultant Numbers
import java . util . * ; class GFG { static void checkUntil ( int num , int K , int N , Vector < Integer > ans ) { if ( N == 1 ) { ans . add ( num ) ; return ; } if ( ( num % 10 + K ) <= 9 ) checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans ) ; if ( K > 0 ) { if ( ( num % 10 - K ) >= 0 ) checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans ) ; } } static void check ( int K , int N , Vector < Integer > ans ) { for ( int i = 1 ; i <= 9 ; i ++ ) { checkUntil ( i , K , N , ans ) ; } } static void print ( Vector < Integer > ans ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 4 , K = 8 ; Vector < Integer > ans = new Vector < Integer > ( ) ; ; check ( K , N , ans ) ; print ( ans ) ; } }