codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all Perfect Squares lying in the range [ L , R ] for Q queries | Java program to find the sum of all perfect squares in the given range ; Array to precompute the sum of squares from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to precompute the perfect squares upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
class GFG { static int [ ] pref = new int [ 100010 ] ; static int isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ? x : 0 ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectSquare ( i ) ; } } static void printSum ( int L , int R ) { int sum = pref [ R ] - pref [ L - 1 ] ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 4 ; int arr [ ] [ ] = { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }
Program to find all Factors of a Number using recursion | Java program to find all the factors of a number using recursion ; Recursive function to print factors of a number ; Checking if the number is less than N ; Calling the function recursively for the next number ; Driver code
class GFG { static void factors ( int n , int i ) { if ( i <= n ) { if ( n % i == 0 ) { System . out . print ( i + " ▁ " ) ; } factors ( n , i + 1 ) ; } } public static void main ( String args [ ] ) { int N = 16 ; factors ( N , 1 ) ; } }
Maximize value of ( a + b ) such that ( a * a | Java program to maximize the value of ( a + b ) such that ( a * a - b * b = N ) ; Function to maximize the value of ( a + b ) such that ( a * a - b * b = n ) ; Driver code
class GFG { static int maxValue ( int n ) { return n ; } public static void main ( String [ ] args ) { int n = 1 ; System . out . print ( maxValue ( n ) ) ; } }
Maximum number of edges that N | Java implementation to find the maximum number of edges for triangle free graph ; Function to find the maximum number of edges in a N - vertex graph . ; According to the Mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; Driver code
class GFG { public static int solve ( int n ) { int ans = ( n * n / 4 ) ; return ans ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( solve ( n ) ) ; } }
Multiplication table till N rows where every Kth row is table of K upto Kth term | Java program to print multiplication table till N rows where every Kth row is the table of K up to Kth term ; Function to print the multiplication table upto K - th term ; For loop to iterate from 1 to N where i serves as the value of K ; Inner loop which at every iteration goes till i ; Printing the table value for i ; New line after every row ; Driver code
class GFG { public static void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( ( i * j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int N = 5 ; printMultiples ( N ) ; } }
Boundary Value Analysis : Nature of Roots of a Quadratic equation | Java program to check the nature of the roots ; BVA for nature of roots of a quadratic equation ; If a = 0 , D / 2 a will yield exception Hence it is not a valid Quadratic Equation ; If D > 0 , it will be Real Roots ; If D == 0 , it will be Equal Roots ; If D < 0 , it will be Imaginary Roots ; Function to check for all testcases ; Driver Code
import java . util . * ; class GFG { static void nature_of_roots ( int a , int b , int c ) { if ( a == 0 ) { System . out . print ( " Not ▁ a ▁ Quadratic ▁ Equation " + "NEW_LINE"); return ; } int D = b * b - 4 * a * c ; if ( D > 0 ) { System . out . print ( " Real ▁ Roots " + "NEW_LINE"); } else if ( D == 0 ) { System . out . print ( " Equal ▁ Roots " + "NEW_LINE"); } else { System . out . print ( " Imaginary ▁ Roots " + "NEW_LINE"); } } static void checkForAllTestCase ( ) { System . out . print ( " Testcase " + " TABSYMBOL a TABSYMBOL b TABSYMBOL c TABSYMBOL Actual ▁ Output " + "NEW_LINE"); System . out . println ( ) ; int a , b , c ; a = b = c = 0 ; int testcase = 1 ; while ( testcase <= 13 ) { if ( testcase == 1 ) { a = 0 ; b = 50 ; c = 50 ; } else if ( testcase == 2 ) { a = 1 ; b = 50 ; c = 50 ; } else if ( testcase == 3 ) { a = 50 ; b = 50 ; c = 50 ; } else if ( testcase == 4 ) { a = 99 ; b = 50 ; c = 50 ; } else if ( testcase == 5 ) { a = 100 ; b = 50 ; c = 50 ; } else if ( testcase == 6 ) { a = 50 ; b = 0 ; c = 50 ; } else if ( testcase == 7 ) { a = 50 ; b = 1 ; c = 50 ; } else if ( testcase == 8 ) { a = 50 ; b = 99 ; c = 50 ; } else if ( testcase == 9 ) { a = 50 ; b = 100 ; c = 50 ; } else if ( testcase == 10 ) { a = 50 ; b = 50 ; c = 0 ; } else if ( testcase == 11 ) { a = 50 ; b = 50 ; c = 1 ; } else if ( testcase == 12 ) { a = 50 ; b = 50 ; c = 99 ; } else if ( testcase == 13 ) { a = 50 ; b = 50 ; c = 100 ; } System . out . print ( " TABSYMBOL " + testcase + " TABSYMBOL " + a + " TABSYMBOL " + b + " TABSYMBOL " + c + " TABSYMBOL " ) ; nature_of_roots ( a , b , c ) ; System . out . println ( ) ; testcase ++ ; } } public static void main ( String [ ] args ) { checkForAllTestCase ( ) ; } }
Find if the Vacation can be taken or not | Java program to find if the Vacation can be taken or not ; Function to find if the Vacation is possible or not ; Find the required number of hours of study ; find the hours of study that can be done if the vacation is taken ; check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken ; Driver Code
class GFG { static int isPossible ( int N , int S , int C , int H , int L , int T ) { int total_time_required = S * C * H ; int available_time_after_vacation = ( N - L ) * T ; if ( available_time_after_vacation >= total_time_required ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int N = 12 , S = 5 , C = 8 , H = 3 , L = 2 , T = 20 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); N = 1 ; S = 2 ; C = 3 ; H = 4 ; L = 5 ; T = 6 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
Find Maximum and Minimum of two numbers using Absolute function | Java program to find maximum and minimum using Absolute function ; Function to return maximum among the two numbers ; Function to return minimum among the two numbers ; Driver code ; Displaying the maximum value ; Displaying the minimum value
class GFG { static int maximum ( int x , int y ) { return ( ( x + y + Math . abs ( x - y ) ) / 2 ) ; } static int minimum ( int x , int y ) { return ( ( x + y - Math . abs ( x - y ) ) / 2 ) ; } public static void main ( String [ ] args ) { int x = 99 , y = 18 ; System . out . println ( " Maximum : ▁ " + maximum ( x , y ) ) ; System . out . println ( " Minimum : ▁ " + minimum ( x , y ) ) ; } }
Find an array of size N having exactly K subarrays with sum S | Java program to find array with K subarrays with sum S ; Function to find array with K subarrays with sum S ; Driver Code ; Function call
class GFG { static void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) System . out . print ( s + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) System . out . print ( s + 1 + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; } }
Minimum number of cuts required to pay salary from N length Gold Bar | JAVA Implementation to find the minimum number of cuts to pay the worker . ; Function to find the minimum number of cuts to pay the worker . ; Nearest Integer to the Log value of the number N ; Driver code ; Cuts Required in the Length of 15
class GFG { static int pay ( int n ) { int cuts = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return cuts ; } public static void main ( String [ ] args ) { int n = 5 ; int cuts = pay ( n ) ; System . out . print ( cuts + "NEW_LINE"); n = 15 ; cuts = pay ( n ) ; System . out . print ( cuts ) ; } }
Sum of Maximum and Minimum prime factor of every number in the Array | Java implementation of the approach ; max_prime [ i ] represent maximum prime number that divides the number i ; min_prime [ i ] represent minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number min_prime number that divide prime number and max_prime number that divide prime number is the number itself . ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to find the sum of the minimum and the maximum prime factors of every number from the given array ; Pre - calculation ; For every element of the given array ; The sum of its smallest and largest prime factor ; Driver code
class GFG { static int MAX = 100000 ; static int max_prime [ ] = new int [ MAX + 1 ] ; static int min_prime [ ] = new int [ MAX + 1 ] ; static void sieve ( int n ) { for ( int i = 2 ; i <= n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j <= n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static void findSum ( int arr [ ] , int n ) { sieve ( MAX ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = min_prime [ arr [ i ] ] + max_prime [ arr [ i ] ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 15 , 20 , 25 , 30 } ; int n = arr . length ; findSum ( arr , n ) ; } }
Pair of integers ( a , b ) which satisfy the given equations | Java program to count the pair of integers ( a , b ) which satisfy the equation a ^ 2 + b = n and a + b ^ 2 = m ; Function to count valid pairs ; Driver code
class GFG { static int pairCount ( int n , int m ) { int cnt = 0 , b , a ; for ( b = 0 ; b <= Math . sqrt ( m ) ; b ++ ) { a = m - b * b ; if ( a * a + b == n ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int n = 9 , m = 3 ; System . out . print ( pairCount ( n , m ) + "NEW_LINE"); } }
Largest Divisor for each element in an array other than 1 and the number itself | Java implementation of the approach ; Divisors array to keep track of the maximum divisor ; Function to pre - compute the prime numbers and largest divisors ; Visited array to keep track of prime numbers ; 0 and 1 are not prime numbers ; Initialising divisors [ i ] = i ; For all the numbers divisible by 2 the maximum divisor will be number / 2 ; If divisors [ i ] is not equal to i then this means that divisors [ i ] contains minimum prime divisor for the number ; Update the answer to i / smallest_prime_divisor [ i ] ; Condition if i is a prime number ; If divisors [ j ] is equal to j then this means that i is the first prime divisor for j so we update divi [ j ] = i ; If the current element is prime then it has no divisors other than 1 and itself ; Driver code
class GFG { final static int maxin = 10001 ; static int divisors [ ] = new int [ maxin + 1 ] ; static void Calc_Max_Div ( int arr [ ] , int n ) { int vis [ ] = new int [ maxin + 1 ] ; for ( int i = 0 ; i < maxin + 1 ; i ++ ) vis [ i ] = 1 ; vis [ 0 ] = vis [ 1 ] = 0 ; for ( int i = 1 ; i <= maxin ; i ++ ) divisors [ i ] = i ; for ( int i = 4 ; i <= maxin ; i += 2 ) { vis [ i ] = 0 ; divisors [ i ] = i / 2 ; } for ( int i = 3 ; i <= maxin ; i += 2 ) { if ( divisors [ i ] != i ) { divisors [ i ] = i / divisors [ i ] ; } if ( vis [ i ] == 1 ) { for ( int j = i * i ; j < maxin ; j += i ) { vis [ j ] = 0 ; if ( divisors [ j ] == j ) divisors [ j ] = i ; } } } for ( int i = 0 ; i < n ; i ++ ) { if ( divisors [ arr [ i ] ] == arr [ i ] ) System . out . print ( " - 1 ▁ " ) ; else System . out . print ( divisors [ arr [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 7 , 8 , 9 , 10 } ; int n = arr . length ; Calc_Max_Div ( arr , n ) ; } }
Given a number N in decimal base , find the sum of digits in any base B | Java Implementation to Compute Sum of Digits of Number N in Base B ; Function to compute sum of Digits of Number N in base B ; Initialize sum with 0 ; Compute unit digit of the number ; Add unit digit in sum ; Update value of Number ; Driver code
class GFG { static int sumOfDigit ( int n , int b ) { int unitDigit , sum = 0 ; while ( n > 0 ) { unitDigit = n % b ; sum += unitDigit ; n = n / b ; } return sum ; } public static void main ( String [ ] args ) { int n = 50 ; int b = 2 ; System . out . print ( sumOfDigit ( n , b ) ) ; } }
Find the Nth digit from right in base B of the given number in Decimal base | Java Implementation to find Nth digit from right in base B ; Function to compute Nth digit from right in base B ; Skip N - 1 Digits in Base B ; Nth Digit from right in Base B ; Driver Code
import java . util . * ; class GFG { static int nthDigit ( int a , int n , int b ) { for ( int i = 1 ; i < n ; i ++ ) a = a / b ; return a % b ; } public static void main ( String [ ] args ) { int a = 100 ; int n = 3 ; int b = 4 ; System . out . print ( nthDigit ( a , n , b ) ) ; } }
Find three prime numbers with given sum | Java implementation of the approach ; The vector primes holds the prime numbers ; Function to generate prime numbers ; Initialize the array elements to 0 s ; Set the non - primes to true ; Fill the vector primes with prime numbers which are marked as false in the numbers array ; Function to print three prime numbers which sum up to the number N ; Take the first prime number ; Take the second prime number ; Subtract the two prime numbers from the N to obtain the third number ; If the third number is prime ; Print the three prime numbers if the solution exists ; Driver code ; Function for generating prime numbers using Sieve of Eratosthenes ; Function to print the three prime numbers whose sum is equal to N
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MAX = 100001 ; static ArrayList < Integer > primes ; static void initialize ( ) { boolean [ ] numbers = new boolean [ MAX + 1 ] ; int n = MAX ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( ! numbers [ i ] ) for ( int j = i * i ; j <= n ; j += i ) numbers [ j ] = true ; for ( int i = 2 ; i <= n ; i ++ ) if ( numbers [ i ] == false ) primes . add ( i ) ; } static void findNums ( int num ) { boolean ans = false ; int first = - 1 , second = - 1 , third = - 1 ; for ( int i = 0 ; i < num ; i ++ ) { first = primes . get ( i ) ; for ( int j = 0 ; j < num ; j ++ ) { second = primes . get ( j ) ; third = num - first - second ; if ( Collections . binarySearch ( primes , third ) >= 0 ) { ans = true ; break ; } } if ( ans ) break ; } if ( ans ) System . out . println ( first + " ▁ " + second + " ▁ " + third ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 101 ; primes = new ArrayList < > ( ) ; initialize ( ) ; findNums ( n ) ; } }
Print all maximal increasing contiguous sub | Java Implementation to print all the Maximal Increasing Sub - array of array ; Function to print each of maximal contiguous increasing subarray ; Loop to iterate through the array and print the maximal contiguous increasing subarray . ; Condition to check whether the element at i , is greater than its next neighbouring element or not . ; Driver code
import java . util . * ; class GFG { static void printmaxSubseq ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && arr [ i ] < arr [ i + 1 ] ) System . out . print ( arr [ i ] + " ▁ " ) ; else System . out . print ( arr [ i ] + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 } ; int n = arr . length ; printmaxSubseq ( arr , n ) ; } }
Count of non decreasing arrays of length N formed with values in range L to R | Java implementation of the approach ; Function to return the count of different arrays ; No such combination exists ; Arrays formed with single elements ; Driver code
class GFG { static int countSum ( int N , int L , int R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 4 , L = 4 , R = 6 ; System . out . print ( countSum ( N , L , R ) ) ; } }
Minimum decrement operations to make Array elements equal by only decreasing K each time | Java implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int solve ( int arr [ ] , int n , int k ) { int minx = INT_MAX ; int i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . min ( minx , arr [ i ] ) ; } int decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } public static void main ( String [ ] args ) { int n , k ; n = 3 ; k = 3 ; int arr [ ] = { 12 , 9 , 15 } ; System . out . println ( solve ( arr , n , k ) ) ; } }
Count non decreasing subarrays of size N from N Natural numbers | Java program to count non decreasing subarrays of size N from N Natural numbers ; Returns value of Binomial Coefficient C ( n , k ) ; Since nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Function to find the count of required subarrays ; The required count is the binomial coefficient as explained in the approach above ; Driver Function
class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int count_of_subarrays ( int N ) { int count = binomialCoeff ( 2 * N - 1 , N ) ; return count ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( count_of_subarrays ( N ) + "NEW_LINE"); } }
Length of Smallest subarray in range 1 to N with sum greater than a given value | Java implementation of the above approach . ; Function to do a binary search on a given range . ; Total sum is the sum of N numbers . ; Sum until mid ; If remaining sum is < the required value , then the required number is in the right half ; Driver code
class GFG { static int usingBinarySearch ( int start , int end , int N , int S ) { if ( start >= end ) return start ; int mid = start + ( end - start ) / 2 ; int totalSum = ( N * ( N + 1 ) ) / 2 ; int midSum = ( mid * ( mid + 1 ) ) / 2 ; if ( ( totalSum - midSum ) <= S ) { return usingBinarySearch ( start , mid , N , S ) ; } return usingBinarySearch ( mid + 1 , end , N , S ) ; } public static void main ( String [ ] args ) { int N , S ; N = 5 ; S = 11 ; System . out . println ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) ; } }
Distribution of a Number in Array within a Range | Java implementation of the approach ; Function for the distribution of the number ; Distribute the number among k elements ; If there is some remaining sum to distribute ; If there are elements remaining to distribute i . e . ( n - k ) ; Divide the remaining sum into n - k elements ; Print the distribution ; Driver code
class GFG { static void distribution ( int n , int k , int l , int r , int S , int Sk ) { int a [ ] = new int [ n ] ; int len = k , temp , rem , s ; int diff = S - Sk ; for ( int i = 0 ; i < len ; i ++ ) { temp = Sk / k ; rem = Sk % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { System . out . print ( " - 1" ) ; return ; } Sk = Sk - a [ i ] ; k = k - 1 ; } if ( Sk > 0 ) { System . out . print ( " - 1" ) ; return ; } if ( len != 0 ) { k = n - len ; for ( int i = len ; i < n ; i ++ ) { temp = diff / k ; rem = diff % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { System . out . print ( " - 1" ) ; return ; } diff = diff - a [ i ] ; k = k - 1 ; } if ( diff != 0 ) { System . out . print ( " - 1" ) ; return ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 , k = 3 , l = 1 , r = 5 , S = 13 , Sk = 9 ; distribution ( n , k , l , r , S , Sk ) ; } }
Number of valid indices in the permutation of first N natural numbers | Java implementation of the approach ; Function to return the number of i 's  such that Pi <= Pj for all 1 <= j <= i  in the permutation of first N natural numbers ; To store the count of such indices ; Store the mini value ; For all the elements ; Return the required answer ; Driver code
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int min_index ( int p [ ] , int n ) { int ans = 0 ; int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] <= mini ) mini = p [ i ] ; if ( mini == p [ i ] ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int P [ ] = { 4 , 2 , 5 , 1 , 3 } ; int n = P . length ; System . out . println ( min_index ( P , n ) ) ; } }
Check if Sum and XOR of all elements of array is equal | Java Implementation to Check if Sum and XOR of all Elements of Array is equal ; Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal
class GFG { static void equal_xor_sum ( int arr [ ] , int n ) { int Sum = 0 ; int Xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Sum = Sum + arr [ i ] ; Xor = Xor ^ arr [ i ] ; } if ( Sum == Xor ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 7 , 10 } ; int n = arr . length ; equal_xor_sum ( arr , n ) ; } }