codeparrot/xlcost-text-to-code huggingface dataset:
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | Java program for the above approach ; Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code
class GFG { static int minCount ( int A [ ] , int B [ ] , int N ) { int i = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) { i ++ ; } } return N - i ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = { 1 , 5 , 2 , 3 , 4 } ; int N = A . length ; System . out . println ( minCount ( A , B , N ) ) ; } }
Minimize maximum array element possible by at most K splits on the given array | Java program for the above approach ; Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
import java . util . * ; class GFG { static boolean possible ( int A [ ] , int N , int mid , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += ( A [ i ] - 1 ) / mid ; } return count <= K ; } static int minimumMaximum ( int A [ ] , int N , int K ) { int lo = 1 ; Arrays . sort ( A ) ; int hi = A [ N - 1 ] ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 4 ; int N = arr . length ; System . out . println ( minimumMaximum ( arr , N , K ) ) ; } }
Maximum value of X such that difference between any array element and X does not exceed K | Java program for the above approach ; Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
import java . util . * ; class GFG { static void maximumNumber ( int arr [ ] , int N , int K ) { int minimum = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int ans = minimum + K ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . abs ( arr [ i ] - ans ) > K ) { ans = - 1 ; break ; } } System . out . print ( ans ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 5 } ; int K = 2 ; int N = arr . length ; maximumNumber ( arr , N , K ) ; } }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Java program for the above approach ; Function to check if a number is prime or not ; Base Cases ; Check if n is divisible by 2 or 3 ; Iterate over every 6 number from the range [ 5 , sqrt ( N ) ] ; If n is found to be non - prime ; Otherwise , return true ; Function to check if a number can be represented as the sum of a prime number and a perfect square or not ; Stores all perfect squares less than N ; Store the perfect square in the array ; Iterate over all perfect squares ; Store the difference of perfect square from n ; If difference is prime ; Update flag ; Break out of the loop ; If N is the sum of a prime number and a perfect square ; Driver Code
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static void sumOfPrimeSquare ( int n ) { int i = 0 ; ArrayList < Integer > squares = new ArrayList < Integer > ( ) ; while ( i * i < n ) { squares . add ( i * i ) ; i ++ ; } boolean flag = false ; for ( i = 0 ; i < squares . size ( ) ; i ++ ) { int difference = n - squares . get ( i ) ; if ( isPrime ( difference ) ) { flag = true ; break ; } } if ( flag ) { System . out . print ( " Yes " ) ; } else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 27 ; sumOfPrimeSquare ( N ) ; } }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Java program for the above approach ; Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , Math . sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Code
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void sumOfPrimeSquare ( int n ) { boolean flag = false ; boolean [ ] prime = new boolean [ n + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , n ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) continue ; int dif = n - i ; if ( Math . ceil ( ( double ) Math . sqrt ( dif ) ) == Math . floor ( ( double ) Math . sqrt ( dif ) ) ) { flag = true ; break ; } } if ( flag ) { System . out . print ( " Yes " ) ; } else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 27 ; sumOfPrimeSquare ( N ) ; } }
Check if any subarray of length M repeats at least K times consecutively or not | Java program for the above approach ; Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean check ( int arr [ ] , int M , int K , int ind ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < K ; j ++ ) { if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) { return false ; } } } return true ; } static boolean SubarrayRepeatsKorMore ( int arr [ ] , int N , int M , int K ) { for ( int ind = 0 ; ind <= N - M * K ; ind ++ ) { if ( check ( arr , M , K , ind ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = arr . length ; if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Check if any subarray of length M repeats at least K times consecutively or not | Java program for the above approach ; Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean checkExists ( int arr [ ] , int N , int M , int K ) { int count = 0 ; for ( int i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = arr . length ; if ( checkExists ( arr , N , M , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Number of common digits present in two given numbers | Java program for the above approach ; Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Driver Code ; Input
import java . util . * ; class GFG { static int CommonDigits ( int N , int M ) { int count = 0 ; int freq1 [ ] = new int [ 10 ] ; int freq2 [ ] = new int [ 10 ] ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = N / 10 ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = M / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 748294 ; int M = 34298156 ; System . out . print ( CommonDigits ( N , M ) ) ; } }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Java program for the above approach ; Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumNumber ( int K , int X ) { if ( K > X ) { System . out . println ( " - 1" ) ; return ; } int ans = 0 ; int sum = 0 ; for ( int i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Java program for the above approach ; Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean isGreaterEqual ( int N , int K , int X ) { return ( ( N * 1L * ( N + 1 ) / 2 ) - ( ( K - 1 ) * 1L * K / 2 ) ) >= X ; } static void minimumNumber ( int K , int X ) { if ( K > X ) { System . out . println ( " - 1" ) ; return ; } int low = K , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , K , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }
Count pairs with product of indices equal to the product of elements present at those indices | Java program for the above approach ; Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
import java . util . * ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 3 , 2 , 6 } ; int N = arr . length ; System . out . println ( CountPairs ( arr , N ) ) ; } }
Maximize remainder of sum of a pair of array elements with different parity modulo K | Java program for the above approach ; Function to find the maximum remainder of sum of a pair of array elements modulo K ; Stores all even numbers ; Stores all odd numbers ; Segregate remainders of even and odd numbers in respective sets ; Stores the maximum remainder obtained ; Find the complement of remainder of each even number in odd set ; Find the complement of remainder x ; Print the answer ; Driver Code ; Given array ; Size of the array ; Given value of K
import java . util . * ; class GFG { static void maxRemainder ( int A [ ] , int N , int K ) { HashSet < Integer > even = new HashSet < > ( ) ; TreeSet < Integer > odd = new TreeSet < > ( ) ; for ( int num : A ) { if ( num % 2 == 0 ) even . add ( num % K ) ; else odd . add ( num % K ) ; } int max_rem = 0 ; for ( int x : even ) { int y = K - 1 - x ; if ( odd . floor ( y ) != null ) max_rem = Math . max ( max_rem , x + odd . floor ( y ) ) ; } System . out . print ( max_rem ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 11 , 6 , 7 } ; int N = arr . length ; int K = 7 ; maxRemainder ( arr , N , K ) ; } }
Length of the longest path ending at vertex V in a Graph | Java program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex v from any source node ; Stores the maximum length of the path ending at vertex v ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver code
import java . util . ArrayList ; class GFG { static int distance ; private static void dfs ( int src , ArrayList < ArrayList < Integer > > Adj , ArrayList < Boolean > visited , int level ) { visited . set ( src , true ) ; distance = Math . max ( distance , level ) ; for ( int child : Adj . get ( src ) ) { if ( ( child != src ) && ( visited . get ( child ) == false ) ) { dfs ( child , Adj , visited , level + 1 ) ; } } visited . set ( src , false ) ; } private static int maximumLength ( int [ ] [ ] mat , int v ) { distance = 0 ; int N = mat [ 0 ] . length ; ArrayList < Boolean > visited = new ArrayList < Boolean > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { visited . add ( false ) ; } ArrayList < ArrayList < Integer > > Adj = new ArrayList < ArrayList < Integer > > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { Adj . add ( new ArrayList < Integer > ( ) ) ; } int i , j ; for ( i = 0 ; i < mat [ 0 ] . length ; i ++ ) { for ( j = 0 ; j < mat . length ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { Adj . get ( i ) . add ( j ) ; } } } dfs ( v , Adj , visited , 0 ) ; return distance ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 } , { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } } ; int v = 2 ; System . out . print ( maximumLength ( mat , v ) ) ; } }
Minimum removals required such that given string consists only of a pair of alternating characters | Java program for the above approach ; Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Driver Code ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
class GFG { static int findLength ( String s , char i , char j ) { char required = i ; int length = 0 ; for ( int k = 0 ; k < s . length ( ) ; k ++ ) { char curr = s . charAt ( k ) ; if ( curr == required ) { length += 1 ; if ( required == i ) required = j ; else required = i ; } } return length ; } static int minimumDeletions ( String S ) { int len = 0 ; int n = S . length ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = i + 1 ; j < 26 ; j ++ ) { int newLen = findLength ( S , ( char ) ( i + 97 ) , ( char ) ( j + 97 ) ) ; len = Math . max ( len , newLen ) ; } } return n - len ; } public static void main ( String [ ] args ) { String S = " adebbeeaebd " ; System . out . print ( minimumDeletions ( S ) ) ; } }
Smallest value of X satisfying the condition X % A [ i ] = B [ i ] for two given arrays | Java program for the above approach ; Function to calculate modulo inverse of a w . r . t m using Extended Euclid Algorithm ; Base Case ; Perform extended euclid algorithm ; q is quotient ; m is remainder now , process same as euclid 's algorithm ; If x1 is negative ; Make x1 positive ; Function to implement Chinese Remainder Theorem to find X ; Stores the product of array elements ; Traverse the array ; Update product ; Initialize the result ; Apply the above formula ; Function to calculate the product of all elements of the array a [ ] ; Stores product of all array elements ; Traverse the array ; Return the product ; Function to find the value of X that satisfies the given condition ; Stores the required smallest value using Chinese Remainder Theorem ; Stores the product of all array elements ; The equation is Y + K * M >= P Therefore , calculate K = ceil ( ( P - Y ) / M ) ; So , X = Y + K * M ; Print the resultant value of X ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class Main { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int A [ ] , int B [ ] , int N ) { int prod = 1 ; for ( int i = 0 ; i < N ; i ++ ) prod *= A [ i ] ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pp = prod / A [ i ] ; result += B [ i ] * inv ( pp , A [ i ] ) * pp ; } return result % prod ; } static int product ( int a [ ] ) { int ans = 1 ; for ( int i = 0 ; i < a . length ; i ++ ) { ans *= a [ i ] ; } return ans ; } public static void findSmallestInteger ( int A [ ] , int B [ ] , int P ) { int Y = findMinX ( A , B , A . length ) ; int M = product ( A ) ; int K = ( int ) Math . ceil ( ( ( double ) P - ( double ) Y ) / ( double ) M ) ; int X = Y + K * M ; System . out . println ( X ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 3 , 1 } ; int P = 72 ; findSmallestInteger ( A , B , P ) ; } }
Minimum number of jumps required to sort the given array in ascending order | Java program for the above approach ; Function to count minimum number of jumps required to sort the array ; Stores minimum number of jumps ; Stores distances of jumps ; Stores the array elements with their starting indices ; Push the pairs { arr [ i ] , i + 1 } into the vector of pairs vect ; Update vect ; Populate the array temp [ ] ; Update temp [ arr [ i ] ] ; Sort the vector in the ascending order ; Jump till the previous index <= current index ; Update vect [ i ] ; Increment the number of jumps ; Print the minimum number of jumps required ; Driver Code ; Input ; Size of the array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minJumps ( int arr [ ] , int jump [ ] , int N ) { int jumps = 0 ; int temp [ ] = new int [ 1000 ] ; int vect [ ] [ ] = new int [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { vect [ i ] [ 0 ] = arr [ i ] ; vect [ i ] [ 1 ] = i + 1 ; } for ( int i = 0 ; i < N ; i ++ ) { temp [ arr [ i ] ] = jump [ i ] ; } Arrays . sort ( vect , ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] - b [ 0 ] ; return a [ 1 ] - b [ 1 ] ; } ) ; for ( int i = 1 ; i < N ; i ++ ) { while ( vect [ i ] [ 1 ] <= vect [ i - 1 ] [ 1 ] ) { vect [ i ] [ 0 ] = vect [ i ] [ 0 ] ; vect [ i ] [ 1 ] = vect [ i ] [ 1 ] + temp [ vect [ i ] [ 0 ] ] ; jumps ++ ; } } System . out . println ( jumps ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int jump [ ] = { 1 , 1 , 1 } ; int N = arr . length ; minJumps ( arr , jump , N ) ; } }
Minimum number of basic logic gates required to realize given Boolean expression | Java implementation of the above approach ; Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Driver Code ; Input ; Function call to count the total number of gates required
class GFG { static void numberOfGates ( String s ) { int N = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' . ' || s . charAt ( i ) == ' + ' || s . charAt ( i ) == '1' ) { ans ++ ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " ( 1 - A ) . B + C " ; numberOfGates ( S ) ; } }
Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | Java program for the above approach ; Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
import java . util . Arrays ; class GFG { static void findPairs ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] = Math . abs ( arr [ i ] ) ; Arrays . sort ( arr ) ; int left = 0 ; int ans = 0 ; for ( int right = 0 ; right < N ; right ++ ) { while ( 2 * arr [ left ] < arr [ right ] ) left ++ ; ans += ( right - left ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 } ; int N = arr . length ; findPairs ( arr , N ) ; } }