codeparrot/xlcost-text-to-code huggingface dataset:
Hoare 's vs Lomuto partition scheme in QuickSort | Java implementation of QuickSort using Hoare 's partition scheme ; This function takes first element as pivot , and places all the elements smaller than the pivot on the left side and all the elements greater than the pivot on the right side . It returns the index of the last element on the smaller side ; Find leftmost element greater than or equal to pivot ; Find rightmost element smaller than or equal to pivot ; If two pointers met . ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print an array ; Driver Code
import java . io . * ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ low ] ; int i = low - 1 , j = high + 1 ; while ( true ) { do { i ++ ; } while ( arr [ i ] < pivot ) ; do { j -- ; } while ( arr [ j ] > pivot ) ; if ( i >= j ) return j ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } static void quickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi ) ; quickSort ( arr , pi + 1 , high ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( " ▁ " + arr [ i ] ) ; System . out . println ( ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 8 , 9 , 1 , 5 } ; int n = arr . length ; quickSort ( arr , 0 , n - 1 ) ; System . out . println ( " Sorted ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
K | Java program to find the Kth smallest element after removing some integer from first n natural number . ; Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest  element after removing  array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driven Code
import java . util . Arrays ; import java . io . * ; class GFG { static int ksmallest ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; if ( k < arr [ 0 ] ) return k ; if ( k == arr [ 0 ] ) return arr [ 0 ] + 1 ; if ( k > arr [ n - 1 ] ) return k + n ; if ( arr [ 0 ] == 1 ) k -- ; else k -= ( arr [ 0 ] - 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { int c = arr [ i ] - arr [ i - 1 ] - 1 ; if ( k <= c ) return arr [ i - 1 ] + k ; else k -= c ; } return arr [ n - 1 ] + k ; } public static void main ( String [ ] args ) { int k = 1 ; int arr [ ] = { 1 } ; int n = arr . length ; System . out . println ( ksmallest ( arr , n , k ) ) ; } }
Tree Sort | Java program to implement Tree Sort ; Class containing left and right child of current node and key value ; Root of BST ; Constructor ; This method mainly calls insertRec ( ) ; A recursive function to insert a new key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the root ; A function to do inorder traversal of BST ; Driver Code
class GFG { class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } Node root ; GFG ( ) { root = null ; } void insert ( int key ) { root = insertRec ( root , key ) ; } Node insertRec ( Node root , int key ) { if ( root == null ) { root = new Node ( key ) ; return root ; } if ( key < root . key ) root . left = insertRec ( root . left , key ) ; else if ( key > root . key ) root . right = insertRec ( root . right , key ) ; return root ; } void inorderRec ( Node root ) { if ( root != null ) { inorderRec ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; inorderRec ( root . right ) ; } } void treeins ( int arr [ ] ) { for ( int i = 0 ; i < arr . length ; i ++ ) { insert ( arr [ i ] ) ; } } public static void main ( String [ ] args ) { GFG tree = new GFG ( ) ; int arr [ ] = { 5 , 4 , 7 , 2 , 11 } ; tree . treeins ( arr ) ; tree . inorderRec ( tree . root ) ; } }
Check whether a given number is even or odd | Java program program to check for even or odd ; Returns true if n is even , else odd ; n & 1 is 1 , then odd , else even ; Driver code
class GFG { public static boolean isEven ( int n ) { if ( ( n & 1 ) == 0 ) return true ; else return false ; } return true ; else return false ; } public static void main ( String [ ] args ) { int n = 101 ; if ( isEven ( n ) == true ) System . out . print ( " Even " ) ; else System . out . print ( " Odd " ) ; } }
Count distinct occurrences as a subsequence | Java program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . An emptystring can 't have  another string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match,  then value is same as the value  without last character in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; uncomment this to print matrix mat for ( int i = 1 ; i <= m ; i ++ , cout << endl ) for ( int j = 1 ; j <= n ; j ++ ) System . out . println ( mat [ i ] [ j ] + " ▁ " ) ; ; Driver code to check above method
import java . io . * ; class GFG { static int findSubsequenceCount ( String S , String T ) { int m = T . length ( ) ; int n = S . length ( ) ; if ( m > n ) return 0 ; int mat [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( T . charAt ( i - 1 ) != S . charAt ( j - 1 ) ) mat [ i ] [ j ] = mat [ i ] [ j - 1 ] ; else mat [ i ] [ j ] = mat [ i ] [ j - 1 ] + mat [ i - 1 ] [ j - 1 ] ; } } return mat [ m ] [ n ] ; } public static void main ( String [ ] args ) { String T = " ge " ; String S = " geeksforgeeks " ; System . out . println ( findSubsequenceCount ( S , T ) ) ; } }
QuickSort Tail Call Optimization ( Reducing worst case space to Log n ) | A Simple implementation of QuickSort that makes two recursive calls . ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition
static void quickSort ( int arr [ ] , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi - 1 ) ; quickSort ( arr , pi + 1 , high ) ; } }
3 | Java program for 3 - way quick sort ; This function partitions a [ ] in three parts a ) a [ l . . i ] contains all elements smaller than pivot b ) a [ i + 1. . j - 1 ] contains all occurrences of pivot c ) a [ j . . r ] contains all elements greater than pivot ; From left , find the first element greater than or equal to v . This loop will definitely terminate as v is last element ; From right , find the first element smaller than or equal to v ; If i and j cross , then we are done ; Swap , so that smaller goes on left greater goes on right ; Move all same left occurrence of pivot to beginning of array and keep count using p ; Move all same right occurrence of pivot to end of array and keep count using q ; Move pivot element to its correct index ; Move all left same occurrences from beginning to adjacent to arr [ i ] ; Move all right same occurrences from end to adjacent to arr [ i ] ; 3 - way partition based quick sort ; Note that i and j are passed as reference ; Recur ; A utility function to print an array ; Driver code ; Function Call
import java . util . * ; class GFG { static int i , j ; static void partition ( int a [ ] , int l , int r ) { i = l - 1 ; j = r ; int p = l - 1 , q = r ; int v = a [ r ] ; while ( true ) { while ( a [ ++ i ] < v ) ; while ( v < a [ -- j ] ) if ( j == l ) break ; if ( i >= j ) break ; int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; if ( a [ i ] == v ) { p ++ ; temp = a [ i ] ; a [ i ] = a [ p ] ; a [ p ] = temp ; } if ( a [ j ] == v ) { q -- ; temp = a [ q ] ; a [ q ] = a [ j ] ; a [ j ] = temp ; } } int temp = a [ i ] ; a [ i ] = a [ r ] ; a [ r ] = temp ; j = i - 1 ; for ( int k = l ; k < p ; k ++ , j -- ) { temp = a [ k ] ; a [ k ] = a [ j ] ; a [ j ] = temp ; } i = i + 1 ; for ( int k = r - 1 ; k > q ; k -- , i ++ ) { temp = a [ i ] ; a [ i ] = a [ k ] ; a [ k ] = temp ; } } static void quicksort ( int a [ ] , int l , int r ) { if ( r <= l ) return ; i = 0 ; j = 0 ; partition ( a , l , r ) ; quicksort ( a , l , j ) ; quicksort ( a , i , r ) ; } static void printarr ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) System . out . printf ( " % d ▁ " , a [ i ] ) ; System . out . printf ( "NEW_LINE"); } public static void main ( String [ ] args ) { int a [ ] = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . length ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }
Check if a Regular Bracket Sequence can be formed with concatenation of given strings | Java program for the above approach ; Function to check possible RBS from the given Strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Make two pair vectors pos and neg ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int checkRBS ( String [ ] S ) { int N = S . length ; pair [ ] v = new pair [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { String s = S [ i ] ; int sum = 0 ; int pre = 0 ; for ( char c : s . toCharArray ( ) ) { if ( c == ' ( ' ) { ++ sum ; } else { -- sum ; } pre = Math . min ( sum , pre ) ; } v [ i ] = new pair ( sum , pre ) ; } Vector < pair > pos = new Vector < pair > ( ) ; Vector < pair > neg = new Vector < pair > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( v [ i ] . first >= 0 ) { pos . add ( new pair ( - v [ i ] . second , v [ i ] . first ) ) ; } else { neg . add ( new pair ( v [ i ] . first - v [ i ] . second , - v [ i ] . first ) ) ; } } Collections . sort ( pos , ( a , b ) -> a . first - b . first ) ; int open = 0 ; for ( pair p : pos ) { if ( open - p . first >= 0 ) { open += p . second ; } else { System . out . print ( " No " + "NEW_LINE"); return 0 ; } } Collections . sort ( neg , ( a , b ) -> a . first - b . first ) ; int negative = 0 ; for ( pair p : neg ) { if ( negative - p . first >= 0 ) { negative += p . second ; } else { System . out . print ( "NoNEW_LINE"); return 0 ; } } if ( open != negative ) { System . out . print ( "NoNEW_LINE"); return 0 ; } System . out . print ( "YesNEW_LINE"); return 0 ; } public static void main ( String [ ] args ) { String [ ] arr = { " ) " , " ( ) ( " } ; checkRBS ( arr ) ; } }
Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | Java program for the above approach ; Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
import java . io . * ; class GFG { static int maximumUniqueSquares ( int N ) { int ans = 0 ; if ( N < 4 ) { return 0 ; } int len = ( int ) ( Math . sqrt ( N ) ) ; N -= len * len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( int i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } public static void main ( String [ ] args ) { int N = 9 ; System . out . println ( maximumUniqueSquares ( N ) ) ; } }
Lexicographically smallest permutation number up to K having given array as a subsequence | Java program for the above approach ; Function to find the lexicographically smallest permutation such that the given array is a subsequence of it ; Stores the missing elements in arr in the range [ 1 , K ] ; Stores if the ith element is present in arr or not ; Loop to mark all integers present in the array as visited ; Loop to insert all the integers not visited into missing ; Append Integer . MAX_VALUE at end in order to prevent going out of bounds ; Pointer to the current element ; Pointer to the missing element ; Stores the required permutation ; Loop to conthe permutation using greedy approach ; If missing element is smaller that the current element insert missing element ; Insert current element ; Print the required Permutation ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findPermutation ( int K , Vector < Integer > arr ) { Vector < Integer > missing = new Vector < Integer > ( ) ; boolean visited [ ] = new boolean [ K + 1 ] ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { visited [ arr . get ( i ) ] = true ; } for ( int i = 1 ; i <= K ; i ++ ) { if ( ! visited [ i ] ) { missing . add ( i ) ; } } arr . add ( Integer . MAX_VALUE ) ; missing . add ( Integer . MAX_VALUE ) ; int p1 = 0 ; int p2 = 0 ; Vector < Integer > ans = new Vector < Integer > ( ) ; while ( ans . size ( ) < K ) { if ( arr . get ( p1 ) < missing . get ( p2 ) ) { ans . add ( arr . get ( p1 ) ) ; p1 ++ ; } else { ans . add ( missing . get ( p2 ) ) ; p2 ++ ; } } for ( int i = 0 ; i < K ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int K = 7 ; Integer [ ] a = { 6 , 4 , 2 , 1 } ; Vector < Integer > arr = new Vector < > ( Arrays . asList ( a ) ) ; findPermutation ( K , arr ) ; } }
Last element remaining after repeated removal of Array elements at perfect square indices | Java implementation of the above approach ; Function to find last remaining index after repeated removal of perfect square indices ; Initialize the ans variable as N ; Iterate a while loop and discard the possible values ; Total discarded values ; Check if this forms a perfect square ; Decrease answer by 1 ; Subtract the value from the current value of N ; Return the value remained ; Function to find last remaining element after repeated removal of array element at perfect square indices ; Find the remaining index ; Print the element at that index as the result ; Driver Code ; Given input
import java . util . * ; import java . lang . * ; import java . lang . Math ; class GFG { static int findRemainingIndex ( int N ) { int ans = N ; while ( N > 1 ) { int discard = ( int ) ( Math . sqrt ( N ) ) ; if ( discard * discard == N ) { ans -- ; } N -= discard ; } return ans ; } static void findRemainingElement ( int arr [ ] , int N ) { int remainingIndex = findRemainingIndex ( N ) ; System . out . print ( arr [ remainingIndex - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 4 , 2 , 4 , - 3 , 1 , 1 } ; int N = 9 ; findRemainingElement ( arr , N ) ; } }
Lexicographically largest possible by merging two strings by adding one character at a time | Java program for the approach ; Recursive bfunction for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Given Input ; Function Call
public class Main { static String largestMerge ( String s1 , String s2 ) { if ( s1 . length ( ) == 0 || s2 . length ( ) == 0 ) return s1 + s2 ; if ( s1 . compareTo ( s2 ) > 0 ) { return s1 . charAt ( 0 ) + largestMerge ( s1 . substring ( 1 ) , s2 ) ; } return s2 . charAt ( 0 ) + largestMerge ( s1 , s2 . substring ( 1 ) ) ; } public static void main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " forgeeks " ; System . out . print ( largestMerge ( s1 , s2 ) ) ; } }
Minimum distance between duplicates in a String | Java implementation of above approach ; This function is used to find minimum distance between same repeating characters ; Define a hashmap and an arraylist ; Temporary variable ; Traverse through string ; If character present in dictionary ; Difference between current position and last stored value ; Updating current position ; Storing difference in list ; If character not in dictionary assign it with initial of its position ; If no element inserted in list i . e . no repeating characterss ; Return minimum distance ; Driver code ; Given input ; Function call
import java . io . * ; import java . util . * ; class GFG { static int shortestDistance ( String S , int N ) { HashMap < Character , Integer > dic = new HashMap < Character , Integer > ( ) ; ArrayList < Integer > dis = new ArrayList < > ( ) ; int var ; for ( int i = 0 ; i < N ; i ++ ) { if ( dic . get ( S . charAt ( i ) ) != null ) { var = i - dic . get ( S . charAt ( i ) ) ; dic . put ( S . charAt ( i ) , i ) ; dis . add ( var ) ; } else { dic . put ( S . charAt ( i ) , i ) ; } } if ( dis . size ( ) == 0 ) return - 1 ; else return Collections . min ( dis ) - 1 ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = 13 ; System . out . println ( shortestDistance ( S , N ) ) ; } }
Number of open doors | TCS Coding Question | package whatever do not write package name here ; Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code
import java . io . * ; class GFG { static int countOpenDoors ( int N ) { int doorsOpen = ( int ) Math . sqrt ( N ) ; return doorsOpen ; } public static void main ( String [ ] args ) { int N = 100 ; System . out . println ( countOpenDoors ( N ) ) ; } }
Minimum number of candies required to distribute among children based on given conditions | Java program for the above approach ; Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Initialize with 1 all array element ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code
import java . io . * ; class GFG { static int countCandies ( int arr [ ] , int n ) { int sum = 0 ; int [ ] ans = new int [ n ] ; if ( n == 1 ) { return 1 ; } for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i + 1 ] > arr [ i ] && ans [ i + 1 ] <= ans [ i ] ) { ans [ i + 1 ] = ans [ i ] + 1 ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] && ans [ i ] <= ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } else if ( arr [ i ] == arr [ i + 1 ] && ans [ i ] < ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } sum += ans [ i ] ; } sum += ans [ n - 1 ] ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 2 } ; int N = arr . length ; System . out . println ( countCandies ( arr , N ) ) ; } }
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | Java program for the above approach ; Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver Code
import java . util . * ; class GFG { static void findMaximumProfit ( int [ ] arr , int M , int N ) { PriorityQueue < Integer > max_heap = new PriorityQueue < > ( ( a , b ) -> b - a ) ; int maxProfit = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_heap . add ( arr [ i ] ) ; while ( M > 0 ) { M -- ; int X = max_heap . poll ( ) ; maxProfit += X ; max_heap . add ( X - 1 ) ; } System . out . println ( maxProfit ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 6 } ; int M = 4 ; int N = arr . length ; findMaximumProfit ( arr , M , N ) ; } }
Generate an array with K positive numbers such that arr [ i ] is either | Java program for the above approach ; Function to generate the resultant sequence of first N natural numbers ; Initialize an array of size N ; Stores the sum of positive and negative elements ; Mark the first N - K elements as negative ; Update the value of arr [ i ] ; Update the value of sumNeg ; Mark the remaining K elements as positive ; Update the value of arr [ i ] ; Update the value of sumPos ; If the sum of the sequence is negative , then print - 1 ; Print the required sequence ; Driver code
import java . util . Arrays ; class GFG { static void findSequence ( int n , int k ) { int [ ] arr = new int [ n ] ; int sumPos = 0 , sumNeg = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { arr [ i ] = - ( i + 1 ) ; sumNeg += arr [ i ] ; } for ( int i = n - k ; i < n ; i ++ ) { arr [ i ] = i + 1 ; sumPos += arr [ i ] ; } if ( Math . abs ( sumNeg ) >= sumPos ) { System . out . print ( - 1 ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int N = 10 , K = 6 ; findSequence ( N , K ) ; } }