codeparrot/xlcost-text-to-code huggingface dataset:
Maximum amount of capital required for selecting at most K projects | java program for the above approach ; Function to calculate maximum capital obtained after choosing at most K projects whose capital is less than the given cost of projects ; Stores all projects with capital at most W ; Stores the pair of { C [ i ] , i } ; Traverse the vector C [ ] ; Sort the vector v ; If capital is at most W ; Add the profit into priority queue ; Increment j by one ; If pq is not empty ; Update the capital W ; Delete the top of pq ; Decrement K by one ; Return the maximum capital ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int maximizedCapital ( int K , int W , int profits [ ] , int capital [ ] ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( Collections . reverseOrder ( ) ) ; ArrayList < int [ ] > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < capital . length ; i ++ ) { v . add ( new int [ ] { capital [ i ] , i } ) ; } Collections . sort ( v , ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] - b [ 0 ] ; return a [ 1 ] - b [ 1 ] ; } ) ; int j = 0 ; while ( K != 0 ) { while ( j < capital . length && v . get ( j ) [ 0 ] <= W ) { pq . add ( profits [ v . get ( j ) [ 1 ] ] ) ; j ++ ; } if ( ! pq . isEmpty ( ) ) { W = W + pq . peek ( ) ; pq . poll ( ) ; } K -- ; } return W ; } public static void main ( String [ ] args ) { int K = 2 ; int W = 0 ; int P [ ] = { 1 , 2 , 3 } ; int C [ ] = { 0 , 1 , 1 } ; System . out . println ( maximizedCapital ( K , W , P , C ) ) ; } }
Split array into K non | Java program for the above approach ; Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int maximumSum ( int arr [ ] , int S [ ] , int N , int K ) { int ans = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ N - 1 - i ] ; arr [ N - 1 - i ] = temp ; } for ( int i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; Arrays . sort ( S ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -- ; } int counter = K - 1 ; for ( int i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 13 , 7 , 17 } ; int S [ ] = { 1 , 3 } ; int N = arr . length ; int K = S . length ; System . out . println ( maximumSum ( arr , S , N , K ) ) ; } }
Modify an array by sorting after reversal of bits of each array element | Java implementation of the above approach ; Function to convert binary number to equivalent decimal ; Set base value to 1 , i . e 2 ^ 0 ; Function to convert a decimal to equivalent binary representation ; Stores the binary representation ; Since ASCII value of '0' , '1' are 48 and 49 ; As the string is already reversed , no further reversal is required ; Function to convert the reversed binary representation to equivalent integer ; Stores reversed binary representation of given decimal ; Stores equivalent decimal value of the binary representation ; Return the resultant integer ; Utility function to print the sorted array ; Sort the array ; Traverse the array ; Print the array elements ; Utility function to reverse the binary representations of all array elements and sort the modified array ; Traverse the array ; Passing array elements to reversedBinaryDecimal function ; Pass the array to the sorted array ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static String decimalToBinary ( int n ) { String binstr = " " ; while ( n > 0 ) { binstr += ( char ) ( n % 2 + 48 ) ; n = n / 2 ; } return binstr ; } static int reversedBinaryDecimal ( int N ) { String decimal_to_binar = decimalToBinary ( N ) ; int binary_to_decimal = binaryToDecimal ( decimal_to_binar ) ; return binary_to_decimal ; } static void printSortedArray ( int arr [ ] , int size ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void modifyArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) ; } printSortedArray ( arr , size ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 98 , 43 , 66 , 83 } ; int n = arr . length ; modifyArray ( arr , n ) ; } }
Check if sequence of removed middle elements from an array is sorted or not | Java program for the above approach ; Function to check if sequence of removed middle elements from an array is sorted or not ; Points to the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
import java . util . * ; class GFG { static boolean isSortedArray ( int [ ] arr , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 3 , 1 , 2 , 5 } ; int N = arr . length ; if ( isSortedArray ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | Java program for the above approach ; Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Driver Code ; Given array ; Store the size of the array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void maxLenSubset ( int a [ ] , int n ) { Arrays . sort ( a ) ; int index = 0 , maxlen = - 1 ; int i = 0 ; while ( i < n ) { int j = i ; int len = 1 ; while ( j < n - 1 ) { if ( 2 * a [ j ] >= a [ j + 1 ] ) { len ++ ; } else break ; j ++ ; } if ( maxlen < len ) { maxlen = len ; index = i ; } j ++ ; i = j ; } i = index ; while ( maxlen > 0 ) { System . out . print ( a [ i ] + " ▁ " ) ; maxlen -- ; i ++ ; } } public static void main ( String [ ] args ) { int a [ ] = { 3 , 1 , 5 , 11 } ; int n = a . length ; maxLenSubset ( a , n ) ; } }
Maximize count of intersecting line segments | Java program for the above approach ; Function to find the maximum number of intersecting line segments possible ; Stores pairs of line segments { X [ i ] , Y [ i ] ) ; Add { X [ i ] , Y [ i ] } into p ; Sort p in ascending order of points on X number line ; Stores the points on Y number line in ascending order ; Insert the first Y point from p ; Binary search to find the floor value of p . get ( i ) [ 1 ] ; If floor value doesn 't exist ; Insert p . get ( i ) [ 1 ] into the set ; Erase the next floor value from the set ; Insert p . get ( i ) [ 1 ] into the set ; Return the size of the set as the final result ; Driver Code ; Given Input ; Function call to find the maximum number of intersecting line segments
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int solve ( int N , int X [ ] , int Y [ ] ) { ArrayList < int [ ] > p = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { p . add ( new int [ ] { X [ i ] , Y [ i ] } ) ; } Collections . sort ( p , ( p1 , p2 ) -> { if ( p1 [ 0 ] != p2 [ 0 ] ) return p1 [ 0 ] - p2 [ 0 ] ; return p1 [ 1 ] - p2 [ 1 ] ; } ) ; TreeSet < Integer > s = new TreeSet < > ( ) ; s . add ( p . get ( 0 ) [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { Integer it = s . floor ( p . get ( i ) [ 1 ] ) ; if ( it == null ) { s . add ( p . get ( i ) [ 1 ] ) ; } else { s . remove ( it ) ; s . add ( p . get ( i ) [ 1 ] ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; int X [ ] = { 1 , 2 , 0 } ; int Y [ ] = { 2 , 0 , 1 } ; int maxintersection = solve ( N , X , Y ) ; System . out . println ( maxintersection ) ; } }
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Java program for the above approach ; Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driven Code ; Function Call to check if an array can be reduced to a single element
import java . util . * ; class GFG { static void canReduceArray ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int b [ ] = new int [ N ] ; int j = N ; for ( int i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = arr . length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }
Lexicographically smallest string possible by merging two sorted strings | Java program for the above approach ; Function to find lexicographically smallest string possible by merging two sorted strings ; Stores length of string s1 ; Stores length of string s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
import java . io . * ; class GFG { static void mergeStrings ( String s1 , String s2 ) { int len1 = s1 . length ( ) ; int len2 = s2 . length ( ) ; int pntr1 = 0 ; int pntr2 = 0 ; String ans = " " ; while ( pntr1 < len1 && pntr2 < len2 ) { if ( s1 . charAt ( pntr1 ) < s2 . charAt ( pntr2 ) ) { ans += s1 . charAt ( pntr1 ) ; pntr1 ++ ; } else { ans += s2 . charAt ( pntr2 ) ; pntr2 ++ ; } } if ( pntr1 < len1 ) { ans += s1 . substring ( pntr1 , len1 ) ; } if ( pntr2 < len2 ) { ans += s2 . substring ( pntr2 , len2 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S1 = " abdcdtx " ; String S2 = " achilp " ; mergeStrings ( S1 , S2 ) ; } }
Sort a string without altering the position of vowels | Java program for the above approach ; Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void sortStr ( String str ) { char S [ ] = str . toCharArray ( ) ; int N = S . length ; ArrayList < Character > temp = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp . add ( S [ i ] ) ; } if ( temp . size ( ) != 0 ) Collections . sort ( temp ) ; int ptr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) S [ i ] = temp . get ( ptr ++ ) ; } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; sortStr ( S ) ; } }
Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | Java program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Print the result ; Driver Code ; Given array , arr [ ] ; Store the size of the array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { public static int upper_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; } static void countPairs ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int high = upper_bound ( arr , k * arr [ i ] ) ; ans += high - i - 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 9 , 5 } ; int n = arr . length ; int k = 2 ; countPairs ( arr , n , k ) ; } }
Kth highest XOR of diagonal elements from a Matrix | Java Program to implement the above approach ; Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
import java . util . * ; class GFG { static void findXOR ( int [ ] [ ] mat , int K ) { int N = mat . length ; int M = mat [ 0 ] . length ; ArrayList < Integer > digXOR = new ArrayList < Integer > ( ) ; for ( int l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { int s_col = Math . max ( 0 , l - N ) ; int count = Math . min ( l , Math . min ( ( M - s_col ) , N ) ) ; int currXOR = 0 ; for ( int j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . add ( currXOR ) ; } Collections . sort ( digXOR ) ; System . out . print ( digXOR . get ( N + M - 1 - K ) ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int K = 3 ; findXOR ( mat , K ) ; } }
Maximum sum of absolute differences between distinct pairs of a triplet from an array | Java program for the above approach ; Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code
import java . util . * ; class GFG { static void maximumSum ( int [ ] arr , int N ) { int sum ; Arrays . sort ( arr ) ; sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 2 } ; int N = arr . length ; maximumSum ( arr , N ) ; } }
Maximum and minimum sum of Bitwise XOR of pairs from an array | Java code of above approach ; Function to find the required sum ; Keeps the track of the visited array elements ; Stores the result ; Keeps count of visited elements ; Traverse the vector , V ; If n elements are visited , break out of the loop ; Store the pair ( i , j ) and their Bitwise XOR ; If i and j both are unvisited ; Add xorResult to res and mark i and j as visited ; Increment count by 2 ; Return the result ; Function to find the maximum and minimum possible sum of Bitwise XOR of all the pairs from the array ; Stores the XOR of all pairs ( i , j ) ; Store the XOR of all pairs ( i , j ) ; Update Bitwise XOR ; Sort the vector ; Initialize variables to store maximum and minimum possible sums ; Find the minimum sum possible ; Reverse the vector , v ; Find the maximum sum possible ; Print the result ; Driver code
import java . util . * ; class pair { int first , second , third ; pair ( int first , int second , int third ) { this . first = first ; this . second = second ; this . third = third ; } } class GFG { static int findSum ( ArrayList < pair > v , int n ) { Map < Integer , Boolean > um = new HashMap < > ( ) ; int res = 0 ; int cnt = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( cnt == n ) break ; int x = v . get ( i ) . second ; int y = v . get ( i ) . third ; int xorResult = v . get ( i ) . first ; if ( um . get ( x ) == null && um . get ( y ) == null ) { res += xorResult ; um . put ( x , true ) ; um . put ( y , true ) ; cnt += 2 ; } } return res ; } static void findMaxMinSum ( int a [ ] , int n ) { ArrayList < pair > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xorResult = a [ i ] ^ a [ j ] ; v . add ( new pair ( xorResult , a [ i ] , a [ j ] ) ) ; } } Collections . sort ( v , ( aa , b ) -> aa . first - b . first ) ; int maxi = 0 , mini = 0 ; mini = findSum ( v , n ) ; Collections . reverse ( v ) ; maxi = findSum ( v , n ) ; System . out . print ( mini + " ▁ " + maxi ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; findMaxMinSum ( arr , N ) ; } }
Lexicographically smallest permutation having maximum sum of differences between adjacent elements | Java program for the above approach ; Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Print the required permutation ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void maximumSumPermutation ( int [ ] arr ) { int N = arr . length ; Arrays . sort ( arr ) ; int temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; for ( int i : arr ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; } }
Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | Java program for the above approach ; Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code
import java . util . * ; public class GFG { static void findMaxNumbers ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int opr = ( int ) Math . ceil ( ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } System . out . println ( cnt ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 8 , 4 , 7 } ; int N = arr . length ; int K = 5 ; findMaxNumbers ( arr , N , K ) ; } }
Mean of given array after removal of K percent of smallest and largest array elements | Java program for the above approach ; Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
import java . util . * ; public class GFG { static void meanOfRemainingElements ( int [ ] arr , int N , int K ) { Arrays . sort ( arr ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0f ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = ( sum / ( N - 2 * kthPercent ) ) ; System . out . format ( " % .5f " , mean ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = arr . length ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; } }
Make the array elements equal by performing given operations minimum number of times | Java Program for the above approach ; Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array
import java . io . * ; class GFG { static void minOperation ( int a [ ] , int N ) { int totOps = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { totOps += Math . abs ( a [ i ] - a [ i + 1 ] ) ; } int maxOps = Math . max ( Math . abs ( a [ 0 ] - a [ 1 ] ) , Math . abs ( a [ N - 1 ] - a [ N - 2 ] ) ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxOps = Math . max ( maxOps , Math . abs ( a [ i ] - a [ i - 1 ] ) + Math . abs ( a [ i ] - a [ i + 1 ] ) - Math . abs ( a [ i - 1 ] - a [ i + 1 ] ) ) ; } System . out . println ( totOps - maxOps ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , 0 , 1 , 1 } ; int N = arr . length ; minOperation ( arr , N ) ; } }