codeparrot/xlcost-text-to-code huggingface dataset:
Count of even and odd set bit with array element after XOR with K | Java program to count the set bits after taking XOR with a number K ; Function to get no of set bits in binary representation of positive integer n ; Function to store EVEN and odd variable ; Store the count of even and odd set bit ; Count the set bit using in built function ; Count of set - bit of K ; If y is odd then , count of even and odd set bit will be interchanged ; Else it will remain same as the original array ; Driver 's Code ; Function call to count even and odd
class GFG { static int __builtin_popcount ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static void countEvenOdd ( int arr [ ] , int n , int K ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = __builtin_popcount ( arr [ i ] ) ; if ( x % 2 == 0 ) even ++ ; else odd ++ ; } int y ; y = __builtin_popcount ( K ) ; if ( ( y & 1 ) != 0 ) { System . out . println ( " Even ▁ = ▁ " + odd + " , ▁ Odd ▁ = ▁ " + even ) ; } else { System . out . println ( " Even ▁ = ▁ " + even + " , ▁ Odd ▁ = ▁ " + odd ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int K = 3 ; int n = arr . length ; countEvenOdd ( arr , n , K ) ; } }
Number of ways to choose a pair containing an even and an odd number from 1 to N | Java implementation of the above approach ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void main ( String args [ ] ) { int N = 6 ; int Even = N / 2 ; int Odd = N - Even ; System . out . println ( Even * Odd ) ; } }
Longest subsequence from an array of pairs having first element increasing and second element decreasing . | Java program for the above approach ; Recursive function to find the length of the longest subsequence of pairs whose first element is increasing and second is decreasing ; Base case ; Not include the current pair in the longest subsequence ; Including the current pair in the longest subsequence ; Function ; Driver Code ; Given Input ; Function Call
class GFG { public static Integer longestSubSequence ( int [ ] [ ] A , int N , int ind , int lastf , int lasts ) { ind = ( ind > 0 ? ind : 0 ) ; lastf = ( lastf > 0 ? lastf : Integer . MIN_VALUE ) ; lasts = ( lasts > 0 ? lasts : Integer . MAX_VALUE ) ; if ( ind == N ) return 0 ; int ans = longestSubSequence ( A , N , ind + 1 , lastf , lasts ) ; if ( A [ ind ] [ 0 ] > lastf && A [ ind ] [ 1 ] < lasts ) ans = Math . max ( ans , longestSubSequence ( A , N , ind + 1 , A [ ind ] [ 0 ] , A [ ind ] [ 1 ] ) + 1 ) ; return ans ; } public static int longestSubSequence ( int [ ] [ ] A , int N ) { return longestSubSequence ( A , N , 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . length ; System . out . println ( longestSubSequence ( A , N ) ) ; } }
Count triples with Bitwise AND equal to Zero | Java program for the above approach ; Function to find the number of triplets whose Bitwise AND is 0. ; Stores the count of triplets having bitwise AND equal to 0 ; Stores frequencies of all possible A [ i ] & A [ j ] ; Traverse the array ; Update frequency of Bitwise AND of all array elements with a ; Traverse the array ; Iterate the map ; If bitwise AND of triplet is zero , increment cnt ; Return the number of triplets whose Bitwise AND is 0. ; Driver Code ; Input Array ; Function Call
import java . util . * ; class GFG { static int countTriplets ( int [ ] A ) { int cnt = 0 ; HashMap < Integer , Integer > tuples = new HashMap < Integer , Integer > ( ) ; for ( int a : A ) for ( int b : A ) { if ( tuples . containsKey ( a & b ) ) tuples . put ( a & b , tuples . get ( a & b ) + 1 ) ; else tuples . put ( a & b , 1 ) ; } for ( int a : A ) for ( Map . Entry < Integer , Integer > t : tuples . entrySet ( ) ) if ( ( t . getKey ( ) & a ) == 0 ) cnt += t . getValue ( ) ; return cnt ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , 1 , 3 } ; System . out . print ( countTriplets ( A ) ) ; } }
Count ways to reach a score using 1 and 2 with no consecutive 2 s | Bottom up approach for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; noOfWays [ i ] will store count for value i . 3 extra values are to take care of cornser case n = 0 ; Loop till " n + 1" to compute value for " n " ; number of ways if first run is 1 ; Driver code
import java . util . Arrays ; class GfG { static int CountWays ( int n ) { int noOfWays [ ] = new int [ n + 3 ] ; noOfWays [ 0 ] = 1 ; noOfWays [ 1 ] = 1 ; noOfWays [ 2 ] = 1 + 1 ; for ( int i = 3 ; i < n + 1 ; i ++ ) { noOfWays [ i ] = noOfWays [ i - 1 ] + noOfWays [ i - 3 ] ; } return noOfWays [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( CountWays ( n ) ) ; } }
Program to print Spiral Pattern |  ; Create row and col to traverse rows and columns ; Variable to determine the movement r = right , l = left , d = down , u = upper ; Array for matrix ; Assign the value ; switch - case to determine the next index ; If right , go right ; if left , go left ; if up , go up ; if down , go down ; Check if the matrix has reached array boundary ; Add the left size for the next boundary ; If 2 rotations has been made , decrease the size left by 1 ; switch - case to rotate the movement ; if right , rotate to down ; if down , rotate to left ; if left , rotate to up ; if up , rotate to right ; Print the matrix ; Driver Code ; Get the size of size ; Print the Spiral Pattern
public class GFG { public static void printSpiral ( int size ) { int row = 0 , col = 0 ; int boundary = size - 1 ; int sizeLeft = size - 1 ; int flag = 1 ; char move = ' r ' ; int matrix [ ] [ ] = new int [ size ] [ size ] ; for ( int i = 1 ; i < size * size + 1 ; i ++ ) { matrix [ row ] [ col ] = i ; switch ( move ) { case ' r ' : col += 1 ; break ; case ' l ' : col -= 1 ; break ; case ' u ' : row -= 1 ; break ; case ' d ' : row += 1 ; break ; } if ( i == boundary ) { boundary += sizeLeft ; if ( flag != 2 ) { flag = 2 ; } else { flag = 1 ; sizeLeft -= 1 ; } switch ( move ) { case ' r ' : move = ' d ' ; break ; case ' d ' : move = ' l ' ; break ; case ' l ' : move = ' u ' ; break ; case ' u ' : move = ' r ' ; break ; } } } for ( row = 0 ; row < size ; row ++ ) { for ( col = 0 ; col < size ; col ++ ) { int n = matrix [ row ] [ col ] ; System . out . print ( ( n < 10 ) ? ( n + " ▁ " ) : ( n + " ▁ " ) ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int size = 5 ; printSpiral ( size ) ; } }
Find the last player to be able to flip a character in a Binary String | Java program for the above approach ; Function to check if player A wins the game or not ; Stores size of the groups of 0 s ; Stores size of the group of 0 s ; Traverse the array ; Increment c by 1 if a [ i ] is 0 ; Otherwise , push the size in array and reset c to 0 ; If there is no substring of odd length consisting only of 0 s ; If there is only 1 substring of odd length consisting only of 0 s ; Otherwise ; Stores the size of the largest and second largest substrings of 0 s ; Traverse the array v [ ] ; If current element is greater than first , then update both first and second ; If arr [ i ] is in between first and second , then update second ; If the condition is satisfied ; Driver code
import java . util . * ; public class GFG { static void findWinner ( String a , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) == '0' ) { c ++ ; } else { if ( c != 0 ) v . add ( c ) ; c = 0 ; } } if ( c != 0 ) v . add ( c ) ; if ( v . size ( ) == 0 ) { System . out . print ( " Player ▁ B " ) ; return ; } if ( v . size ( ) == 1 ) { if ( ( v . get ( 0 ) & 1 ) != 0 ) System . out . print ( " Player ▁ A " ) ; else System . out . print ( " Player ▁ B " ) ; return ; } int first = Integer . MIN_VALUE ; int second = Integer . MIN_VALUE ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( a . charAt ( i ) > first ) { second = first ; first = a . charAt ( i ) ; } else if ( a . charAt ( i ) > second && a . charAt ( i ) != first ) second = a . charAt ( i ) ; } if ( ( first & 1 ) != 0 && ( first + 1 ) / 2 > second ) System . out . print ( " Player ▁ A " ) ; else System . out . print ( " Player ▁ B " ) ; } public static void main ( String [ ] args ) { String S = "1100011" ; int N = S . length ( ) ; findWinner ( S , N ) ; } }
Check if K palindromic strings can be formed from a given string | Java program to check whether the string is K palindrome or not ; Function to check whether the string is K palindrome or not ; Map to frequency of character ; Check when k is given as same as length of string ; Storing the frequency of every character in map ; If K is greater than size of then return false ; Check that number of character having the odd frequency ; If k is less than number of odd frequency character then it is again false otherwise true ; Driver Code
import java . util . * ; class GFG { static boolean can_Construct ( String S , int K ) { Map < Character , Integer > m = new HashMap < > ( ) ; int p = 0 ; if ( S . length ( ) == K ) return true ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) m . put ( S . charAt ( i ) , m . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; if ( K > S . length ( ) ) return false ; else { for ( Integer h : m . values ( ) ) { if ( h % 2 != 0 ) p = p + 1 ; } } if ( K < p ) return false ; return true ; } public static void main ( String [ ] args ) { String S = " annabelle " ; int K = 4 ; if ( can_Construct ( S , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if two strings are same ignoring their cases | A Java Program to find the longest common prefix ; Function to compare two strings ignoring their cases ; Convert to lowercase using toLowerCase function ; Comparing both using inbuilt function ; if strings are equal , return true otherwise false ; Function to print the same or not same if strings are equal or not equal ; Driver Code
class GFG { static boolean equalIgnoreCase ( String str1 , String str2 ) { int i = 0 ; str1 = str1 . toLowerCase ( ) ; str2 = str2 . toLowerCase ( ) ; int x = str1 . compareTo ( str2 ) ; return x == 0 ; } static void equalIgnoreCaseUtil ( String str1 , String str2 ) { boolean res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) System . out . println ( " Same " ) ; else System . out . println ( " Not ▁ Same " ) ; } public static void main ( String [ ] args ) { String str1 , str2 ; str1 = " Geeks " ; str2 = " geeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " Geek " ; str2 = " geeksforgeeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; } }
Program to print Step Pattern | Java Program to print Step Pattern ; function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Driver code ; Get the String and the number n ; calling the function
import java . util . * ; class solution { static void steps ( String str , int n ) { boolean flag = false ; int x = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( x == 0 ) flag = true ; if ( x == n - 1 ) flag = false ; for ( int j = 0 ; j < x ; j ++ ) System . out . print ( " * " ) ; System . out . print ( str . charAt ( i ) + "NEW_LINE"); if ( flag == true ) x ++ ; else x -- ; } } public static void main ( String args [ ] ) { int n = 4 ; String str = " GeeksForGeeks " ; System . out . println ( " String : ▁ " + str ) ; System . out . println ( " Max ▁ Length ▁ of ▁ Steps : ▁ " + n ) ; steps ( str , n ) ; } }
Frequency Measuring Techniques for Competitive Programming | Java program to count frequencies of array items ; Mark all array elements as not visited ; Traverse through array elements and count frequencies ; Skip this element if already processed ; Count frequency ; Driver Code
import java . util . * ; class GFG { static void countFreq ( int arr [ ] , int n ) { boolean [ ] visited = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == true ) continue ; int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { visited [ j ] = true ; count ++ ; } } System . out . println ( arr [ i ] + " ▁ " + count ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 } ; int n = arr . length ; countFreq ( arr , n ) ; } }
Check divisibility of binary string by 2 ^ k | Java implementation to check whether given binary number is evenly divisible by 2 ^ k or not ; function to check whether given binary number is evenly divisible by 2 ^ k or not ; count of number of 0 from last ; if count = k , number is evenly divisible , so returns true else false ; Driver program to test above ; first example ; Second example
class GFG { static boolean isDivisible ( String str , int k ) { int n = str . length ( ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str . charAt ( n - i - 1 ) == '0' ) c ++ ; return ( c == k ) ; } public static void main ( String args [ ] ) { String str1 = "10101100" ; int k = 2 ; if ( isDivisible ( str1 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str2 = "111010100" ; k = 2 ; if ( isDivisible ( str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if any anagram of a string is palindrome or not | Java program to Check if any anagram of a string is palindrome or not ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver program to test to print printDups
public class GFG { static final int NO_OF_CHARS = 256 ; static boolean canFormPalindrome ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str . charAt ( i ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) != 0 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { System . out . println ( canFormPalindrome ( " geeksforgeeks " ) ? " Yes " : " No " ) ; System . out . println ( canFormPalindrome ( " geeksogeeks " ) ? " Yes " : " No " ) ; } }
Program to check if input is an integer or a string | Java program to check if a given string is a valid integer ; Returns true if s is a number else false ; Driver code ; Saving the input in a string ; Function returns 1 if all elements are in range '0 ▁ - ▁ 9' ; Function returns 0 if the input is not an integer
import java . io . * ; public class GFG { static boolean isNumber ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( Character . isDigit ( s . charAt ( i ) ) == false ) return false ; return true ; } static public void main ( String [ ] args ) { String str = "6790" ; if ( isNumber ( str ) ) System . out . println ( " Integer " ) ; else System . out . println ( " String " ) ; } }
Print reverse of a string using recursion | Java program to reverse a string using recursion ; Function to print reverse of the passed string ; Driver program to test above function
class StringReverse { void reverse ( String str ) { if ( ( str == null ) || ( str . length ( ) <= 1 ) ) System . out . println ( str ) ; else { System . out . print ( str . charAt ( str . length ( ) - 1 ) ) ; reverse ( str . substring ( 0 , str . length ( ) - 1 ) ) ; } } public static void main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; StringReverse obj = new StringReverse ( ) ; obj . reverse ( str ) ; } }
Probability of distributing given balls into two halves having equal count of distinct colors | Java program for the above approach ; Stores the count of distinct colors in box1 ; Stores the count of distinct colors in box2 ; Function to calculate the required probability ; Calculate factorial from [ 1 , 10 ] ; Assign all distinct balls to second box ; Total number of balls ; Calculate total number of balls ; If K is an odd number ; Total ways of distributing the balls in two equal halves ; Required number of ways ; Return the required probability ; Function to calculate total number of possible distributions which satisfies the given conditions ; If used balls is equal to K / 2 ; If box1 is equal to box2 ; Base condition ; Stores the number of ways of distributing remaining balls without including the current balls in box1 ; Increment box1 by one ; Iterate over the range [ 1 , balls [ i ] ] ; If all the balls goes to box1 , then decrease box2 by one ; Total number of ways of selecting j balls ; Increment res by total number of valid ways of distributing the remaining balls ; Decrement box1 by one ; Increment box2 by 1 ; Function to calculate factorial of N ; Base Case ; Iterate over the range [ 1 , N ] ; Function to calculate NcR ; Driver Code ; Print the result
import java . io . * ; import java . util . * ; class GFG { static int box1 = 0 ; static int box2 = 0 ; static int [ ] fact = new int [ 11 ] ; public static double getProbability ( int [ ] balls ) { factorial ( 10 ) ; box2 = balls . length ; int K = 0 ; for ( int i = 0 ; i < balls . length ; i ++ ) K += balls [ i ] ; if ( K % 2 == 1 ) return 0 ; long all = comb ( K , K / 2 ) ; long validPermutations = validPermutations ( K / 2 , balls , 0 , 0 ) ; return ( double ) validPermutations / all ; } static long validPermutations ( int n , int [ ] balls , int usedBalls , int i ) { if ( usedBalls == n ) { return box1 == box2 ? 1 : 0 ; } if ( i >= balls . length ) return 0 ; long res = validPermutations ( n , balls , usedBalls , i + 1 ) ; box1 ++ ; for ( int j = 1 ; j <= balls [ i ] ; j ++ ) { if ( j == balls [ i ] ) box2 -- ; long combinations = comb ( balls [ i ] , j ) ; res += combinations * validPermutations ( n , balls , usedBalls + j , i + 1 ) ; } box1 -- ; box2 ++ ; return res ; } static void factorial ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static long comb ( int n , int r ) { long res = fact [ n ] / fact [ r ] ; res /= fact [ n - r ] ; return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 } ; int N = 4 ; System . out . println ( getProbability ( arr ) ) ; } }
Area of a n | Java Program to find the area of a regular polygon with given radius ; Function to find the area of a regular polygon ; Side and radius cannot be negative ; Area degree converted to radians ; Driver code
import java . util . * ; class GFG { static double polyarea ( double n , double r ) { if ( r < 0 && n < 0 ) return - 1 ; double A = ( ( r * r * n ) * Math . sin ( ( 360 / n ) * 3.14159 / 180 ) ) / 2 ; return A ; } public static void main ( String [ ] args ) { float r = 9 , n = 6 ; System . out . println ( polyarea ( n , r ) ) ; } }
Check if a line at 45 degree can divide the plane into two equal weight parts |  ; Checking if a plane can be divide by a line at 45 degrees such that weight sum is equal ; Rotating each point by 45 degrees and calculating prefix sum . Also , finding maximum and minimum x coordinates ; storing weight sum upto x - y point ; Finding prefix sum ; Line passes through i , so it neither falls left nor right . ; Driven code
import java . util . * ; class GFG { static void is_partition_possible ( int n , int x [ ] , int y [ ] , int w [ ] ) { Map < Integer , Integer > weight_at_x = new HashMap < Integer , Integer > ( ) ; int max_x = ( int ) - 2e3 , min_x = ( int ) 2e3 ; for ( int i = 0 ; i < n ; i ++ ) { int new_x = x [ i ] - y [ i ] ; max_x = Math . max ( max_x , new_x ) ; min_x = Math . min ( min_x , new_x ) ; if ( weight_at_x . containsKey ( new_x ) ) { weight_at_x . put ( new_x , weight_at_x . get ( new_x ) + w [ i ] ) ; } else { weight_at_x . put ( new_x , w [ i ] ) ; } } Vector < Integer > sum_till = new Vector < > ( ) ; sum_till . add ( 0 ) ; for ( int s = min_x ; s <= max_x ; s ++ ) { if ( weight_at_x . get ( s ) == null ) sum_till . add ( sum_till . lastElement ( ) ) ; else sum_till . add ( sum_till . lastElement ( ) + weight_at_x . get ( s ) ) ; } int total_sum = sum_till . lastElement ( ) ; int partition_possible = 0 ; for ( int i = 1 ; i < sum_till . size ( ) ; i ++ ) { if ( sum_till . get ( i ) == total_sum - sum_till . get ( i ) ) partition_possible = 1 ; if ( sum_till . get ( i - 1 ) == total_sum - sum_till . get ( i ) ) partition_possible = 1 ; } System . out . printf ( partition_possible == 1 ? "YES " ▁ : ▁ " NO "); } public static void main ( String [ ] args ) { int n = 3 ; int x [ ] = { - 1 , - 2 , 1 } ; int y [ ] = { 1 , 1 , - 1 } ; int w [ ] = { 3 , 1 , 4 } ; is_partition_possible ( n , x , y , w ) ; } }
Slope of perpendicular to line | Java program find slope of perpendicular line ; Function to find the Slope of other line ; Driver code
import java . io . * ; import java . util . * ; class GFG { static double findPCSlope ( double m ) { return - 1.0 / m ; } public static void main ( String [ ] args ) { double m = 2.0 ; System . out . println ( findPCSlope ( m ) ) ; } }
Program to find area of a Circular Segment | Java Program to find area of segment of a circle ; Function to find area of segment ; Calculating area of sector ; Calculating area of triangle ; Driver Function
class GFG { static float pi = 3.14159f ; static float area_of_segment ( float radius , float angle ) { float area_of_sector = pi * ( radius * radius ) * ( angle / 360 ) ; float area_of_triangle = ( float ) 1 / 2 * ( radius * radius ) * ( float ) Math . sin ( ( angle * pi ) / 180 ) ; return area_of_sector - area_of_triangle ; } public static void main ( String [ ] args ) { float radius = 10.0f , angle = 90.0f ; System . out . println ( " Area ▁ of ▁ minor ▁ segment ▁ = ▁ " + area_of_segment ( radius , angle ) ) ; System . out . println ( " Area ▁ of ▁ major ▁ segment ▁ = ▁ " + area_of_segment ( radius , ( 360 - angle ) ) ) ; } }
Area of a Circular Sector | Java program to find Area of a Sector ; Calculating area of the sector ; Driver code
class GFG { static void SectorArea ( double radius , double angle ) { if ( angle >= 360 ) System . out . println ( " Angle ▁ not ▁ possible " ) ; else { double sector = ( ( 22 * radius * radius ) / 7 ) * ( angle / 360 ) ; System . out . println ( sector ) ; } } public static void main ( String [ ] args ) { double radius = 9 ; double angle = 60 ; SectorArea ( radius , angle ) ; } }