codeparrot/xlcost-text-to-code huggingface dataset:
Append X digits to the end of N to make it divisible by M | Java program for the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver code ; Stores the number by appending X digits on the right side of N
import java . io . * ; import java . util . * ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void main ( String [ ] args ) throws java . lang . Exception { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; System . out . println ( res ) ; } }
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | Java program for the above approach ; Function to find the minimum moves ; Stores the sum of the array ; Store the maximum element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver Code ; Given array ; Size of given array ; Function Call
import java . io . * ; class GFG { public static void minimumMoves ( int [ ] arr , int N ) { int sum = 0 ; int maxelement = - 1 ; if ( N == 2 ) { System . out . print ( "0" ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . max ( maxelement , arr [ i ] ) ; } int k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . max ( maxelement , k ) ; int ans = k * ( N - 1 ) - sum ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 7 } ; int N = arr . length ; minimumMoves ( arr , N ) ; } }
Split first N natural numbers into two subsequences with non | Java program for the above approach ; Function to split 1 to N into two subsequences with non - coprime sums ; Driver code
class GFG { public static void printSubsequence ( int N ) { System . out . print ( " { ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { System . out . print ( i + " , ▁ " ) ; } System . out . println ( N - 1 + " ▁ } " ) ; System . out . print ( " { ▁ " + N + " ▁ } " ) ; } public static void main ( String [ ] args ) { int N = 8 ; printSubsequence ( N ) ; } }
Generate an array with product of all subarrays of length exceeding one divisible by K | Java program for the above approach ; Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
class GFG { public static void array_divisbleby_k ( int N , int K ) { boolean flag = false ; int d1 = 0 , d2 = 0 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( d2 + " ▁ " ) ; } else { System . out . print ( d1 + " ▁ " ) ; } } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; } }
Minimum characters to be replaced to make a string concatenation of a K | Java program for the above approach ; Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given String ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver Code ; Function Call
import java . util . * ; class GFG { static void addEdge ( Vector < Integer > adj [ ] , int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } static int cnt = 0 ; static boolean [ ] visited ; static void DFS ( int u , Vector < Integer > adj [ ] , int fre [ ] , String S ) { visited [ u ] = true ; cnt ++ ; fre [ S . charAt ( u ) - ' a ' ] ++ ; for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( ! visited [ adj [ u ] . get ( i ) ] ) { DFS ( adj [ u ] . get ( i ) , adj , fre , S ) ; } } } static void minimumOperations ( String S , int m ) { int V = 100 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] adj = new Vector [ V ] ; int total = 0 , N = S . length ( ) ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( int i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new boolean [ V ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { int fre [ ] = new int [ 26 ] ; cnt = 0 ; int maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( int j = 0 ; j < 26 ; j ++ ) maxx = Math . max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } System . out . print ( total ) ; } public static void main ( String [ ] args ) { String S = " abaaba " ; int K = 2 ; minimumOperations ( S , K ) ; } }
Replace specified matrix elements such that no two adjacent elements are equal | Java program for the above approach ; Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Driver Code ; Given N and M ; Given matrix ; Function call
import java . io . * ; class GFG { public static void print ( char arr [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { char a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' F ' ) { arr [ i ] [ j ] = '1' ; } else if ( a == ' F ' ) { arr [ i ] [ j ] = '2' ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( arr [ i ] [ j ] ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 , m = 4 ; char arr [ ] [ ] = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } } ; print ( arr , n , m ) ; } }
Lexicographically smallest string formed by removing duplicates | Java program for the above approach ; Function that finds lexicographically smallest string after removing the duplicates from the given string ; Stores the frequency of characters ; Mark visited characters ; Stores count of each character ; Stores the resultant string ; Decrease the count of current character ; If character is not already in answer ; Last character > S [ i ] and its count > 0 ; Mark letter unvisited ; Add s [ i ] in res and mark it visited ; Return the resultant string ; Driver Code ; Given string S ; Function Call
import java . io . * ; class GFG { static String removeDuplicateLetters ( String s ) { int [ ] cnt = new int [ 26 ] ; int [ ] vis = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ s . charAt ( i ) - ' a ' ] ++ ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] -- ; if ( vis [ s . charAt ( i ) - ' a ' ] == 0 ) { int size = res . length ( ) ; while ( size > 0 && res . charAt ( size - 1 ) > s . charAt ( i ) && cnt [ res . charAt ( size - 1 ) - ' a ' ] > 0 ) { vis [ res . charAt ( size - 1 ) - ' a ' ] = 0 ; res = res . substring ( 0 , size - 1 ) ; size -- ; } res += s . charAt ( i ) ; vis [ s . charAt ( i ) - ' a ' ] = 1 ; } } return res ; } public static void main ( String [ ] args ) { String S = " acbc " ; System . out . println ( removeDuplicateLetters ( S ) ) ; } }
Number of pairs whose product is a power of 2 | Java program for the above approach ; Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function Call
import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Integer . bitCount ( arr [ i ] ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; System . out . print ( desiredPairs + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 7 , 2 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
Count ways to split N ! into two distinct co | Java Program for the above approach ; Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; MArk all its multiples as non - prime ; Count all primes upto MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into two co - prime factors ; Driver Code ; Calling sieve function ; Given N ; Function call
import java . util . * ; class GFG { static final int MAXN = 1000000 ; static int is_prime [ ] ; static int count_of_primes [ ] ; static void sieve ( ) { is_prime = new int [ MAXN ] ; count_of_primes = new int [ MAXN ] ; Arrays . fill ( is_prime , 0 ) ; Arrays . fill ( count_of_primes , 0 ) ; for ( int i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( int i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] == 1 ) { for ( int j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } } is_prime [ 2 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } static long power ( long x , long y , long p ) { long result = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } static void numberOfWays ( int N ) { long count = count_of_primes [ N ] - 1 ; long mod = 1000000007 ; long answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; long ans = answer ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { sieve ( ) ; int N = 7 ; numberOfWays ( N ) ; } }
Maximum possible sum of squares of stack elements satisfying the given properties | Java program to implement the above approach ; Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Print the result ; Driver Code ; Function call
import java . io . * ; import java . util . * ; class GFG { public static void maxSumOfSquares ( int N , int S ) { int res = 0 ; if ( S < N S > 9 * N ) { System . out . println ( - 1 ) ; return ; } S = S - N ; int c = 0 ; while ( S > 0 ) { c ++ ; if ( S / 8 > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; System . out . println ( res ) ; } public static void main ( String args [ ] ) { int N = 3 ; int S = 12 ; maxSumOfSquares ( N , S ) ; } }
Unset least significant K bits of a given number | Java program for the above approach ; Function to return the value after unsetting K LSBs ; Create a mask ; Bitwise AND operation with the number and the mask ; Driver Code ; Given N and K ; Function Call
import java . util . * ; class GFG { static int clearLastBit ( int N , int K ) { int mask = ( - 1 << K + 1 ) ; return N = N & mask ; } public static void main ( String [ ] args ) { int N = 730 , K = 3 ; System . out . print ( clearLastBit ( N , K ) ) ; } }
Check if quantities of 3 distinct colors can be converted to a single color by given merge | Java program for the above approach ; Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Driver Code ; Given colors ; Function Call
import java . util . * ; class GFG { static boolean isPossible ( int r , int b , int g ) { r = r % 3 ; b = b % 3 ; g = g % 3 ; if ( r == b b == g g == r ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int R = 1 , B = 3 , G = 6 ; if ( isPossible ( R , B , G ) ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } }
Minimize steps required to make all array elements same by adding 1 , 2 or 5 | Java program for the above approach ; Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
import java . util . * ; class GFG { static int calculate_steps ( Integer arr [ ] , int n , int minimum ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += arr [ i ] / 5 ; arr [ i ] = arr [ i ] % 5 ; count += arr [ i ] / 2 ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] > 0 ) { count ++ ; } } arr [ i ] = val ; } return count ; } static int solve ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int minimum = arr [ n - 1 ] ; int count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . min ( count1 , Math . min ( count2 , count3 ) ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 3 , 6 , 6 } ; int N = arr . length ; System . out . print ( solve ( arr , N ) ) ; } }
Count of subarrays having sum equal to its length | Java program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Decrementing all the elements of the array by 1 ; Making prefix sum array ; Declare map to store count of elements upto current element ; To count all the subarrays whose prefix sum is 0 ; Iterate the array ; Increment answer by count of current element of prefix array ; Return the answer ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int countOfSubarray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] -- ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int answer = 0 ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( pref [ i ] ) ) { answer += mp . get ( pref [ i ] ) ; mp . put ( pref [ i ] , mp . get ( pref [ i ] ) + 1 ) ; } else { mp . put ( pref [ i ] , 1 ) ; } } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 } ; int N = arr . length ; System . out . print ( countOfSubarray ( arr , N ) ) ; } }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Java program to print subset at the nth position ordered by the sum of the elements ; Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; Create a vector for storing the elements of subsets ; Doing until all the set bits of n are used ; This part is executed only when the last bit is set ; Right shift the bit by one position ; Increasing the count each time by one ; Printing the values os elements ; Driver function
import java . util . * ; import java . lang . * ; class GFG { static void printsubset ( int n , int k ) { int count = 0 , x = 0 ; ArrayList < Integer > vec = new ArrayList < > ( ) ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . add ( ( int ) Math . pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) System . out . print ( vec . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 7 , k = 4 ; printsubset ( n , k ) ; } }
Minimum digits to be removed to make either all digits or alternating digits same | Java program for the above approach ; Function to find longest possible subsequence of s beginning with x and y ; Iterate over the String ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Driver Code ; Given String s ; Find the size of the String ; Function Call ; This value is the count of minimum element to be removed
import java . util . * ; class GFG { static int solve ( String s , int x , int y ) { int res = 0 ; for ( char c : s . toCharArray ( ) ) { if ( c - '0' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } static int find_min ( String s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = Math . max ( count , solve ( s , i , j ) ) ; } } return count ; } public static void main ( String [ ] args ) { String s = "100120013" ; int n = s . length ( ) ; int answer = find_min ( s ) ; System . out . print ( ( n - answer ) ) ; } }
Count total set bits in all numbers from range L to R | Java program for the above approach ; Function to count set bits in x ; Base Case ; Recursive Call ; Function that returns count of set bits present in all numbers from 1 to N ; Initialize the result ; Return the setbit count ; Driver Code ; Given L and R ; Function Call
class GFG { static int countSetBitsUtil ( int x ) { if ( x <= 0 ) return 0 ; return ( ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( x / 2 ) ) ; } static int countSetBits ( int L , int R ) { int bitCount = 0 ; for ( int i = L ; i <= R ; i ++ ) { bitCount += countSetBitsUtil ( i ) ; } return bitCount ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . printf ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ % d " , countSetBits ( L , R ) ) ; } }
Possible values of Q such that , for any value of R , their product is equal to X times their sum | Java program for the above approach ; Function to find all possible values of Q ; Vector initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver code
import java . util . * ; class GFG { static void values_of_Q ( int X ) { ArrayList < Integer > val_Q = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . add ( X + i ) ; } } System . out . println ( val_Q . size ( ) ) ; for ( int i = 0 ; i < val_Q . size ( ) ; i ++ ) { System . out . print ( val_Q . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int X = 3 ; values_of_Q ( X ) ; } }
Program to find Greatest Common Divisor ( GCD ) of N strings | Java program for the above approach ; Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Driver Code ; Given array of strings ; Function Call
class GCD { static String gcd ( String str1 , String str2 ) { if ( str1 . length ( ) < str2 . length ( ) ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . startsWith ( str2 ) ) { return " " ; } else if ( str2 . isEmpty ( ) ) { return str1 ; } else { return gcd ( str1 . substring ( str2 . length ( ) ) , str2 ) ; } } static String findGCD ( String arr [ ] , int n ) { String result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { String arr [ ] = new String [ ] { " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " } ; int n = arr . length ; System . out . println ( findGCD ( arr , n ) ) ; } }
Check whether the given Matrix is balanced or not | Java program for the above approach ; Define size of matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Driver Code ; Given Matrix mat [ ] [ ] ; Function Call
import java . util . * ; class GFG { static final int N = 4 ; static final int M = 4 ; static String balancedMatrix ( int mat [ ] [ ] ) { boolean is_balanced = true ; for ( int i = 0 ; i < N && is_balanced ; i ++ ) { for ( int j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i ] [ j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i ] [ j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i ] [ j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " Balanced " ; else return " Unbalanced " ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 3 , 5 , 2 , 6 } , { 5 , 3 , 6 , 1 } , { 9 , 5 , 6 , 0 } } ; System . out . print ( balancedMatrix ( mat ) ) ; } }