codeparrot/xlcost-text-to-code huggingface dataset:
Check if both halves of a string are Palindrome or not | Java Program to check whether both halves of a string is Palindrome or not ; Function to check and return if both halves of a string are palindromic or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver Code
public class Main { public static void checkPalindrome ( String S ) { int N = S . length ( ) ; boolean first_half = true ; boolean second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( cnt ) ) { first_half = false ; break ; } if ( S . charAt ( N / 2 + i ) != S . charAt ( N / 2 + cnt ) ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = " momdad " ; checkPalindrome ( S ) ; } }
Count of pairs of strings whose concatenation forms a palindromic string | Java program to find palindromic string ; Stores frequency array and its count ; Total number of pairs ; Initializing array of size 26 to store count of character ; Counting occurrence of each character of current string ; Convert each count to parity ( 0 or 1 ) on the basis of its frequency ; Adding to answer ; Frequency of single character can be possibly changed , so change its parity ; Driver code
import java . util . * ; class GFG { static int getCount ( int N , List < String > s ) { Map < List < Integer > , Integer > mp = new HashMap < > ( ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { List < Integer > a = new ArrayList < > ( 26 ) ; for ( int k = 0 ; k < 26 ; k ++ ) a . add ( 0 ) ; for ( int j = 0 ; j < s . get ( i ) . length ( ) ; j ++ ) { a . set ( ( s . get ( i ) . charAt ( j ) - ' a ' ) , a . get ( s . get ( i ) . charAt ( j ) - ' a ' ) + 1 ) ; } for ( int j = 0 ; j < 26 ; j ++ ) { a . set ( j , a . get ( j ) % 2 ) ; } ans += mp . getOrDefault ( a , 0 ) ; for ( int j = 0 ; j < 26 ; j ++ ) { List < Integer > changedCount = new ArrayList < > ( ) ; changedCount . addAll ( a ) ; if ( a . get ( j ) == 0 ) changedCount . set ( j , 1 ) ; else changedCount . set ( j , 0 ) ; ans += mp . getOrDefault ( changedCount , 0 ) ; } mp . put ( a , mp . getOrDefault ( a , 0 ) + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; List < String > A = Arrays . asList ( " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " ) ; System . out . print ( getCount ( N , A ) ) ; } }
Kth Smallest element in a Perfect Binary Search Tree | Java program to find K - th smallest element in a perfect BST ; A BST node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty ; Recur down the left subtree for smaller values ; Recur down the right subtree for smaller values ; Return the ( unchanged ) node pointer ; FUnction to find Kth Smallest element in a perfect BST ; Find the median ( division operation is floored ) ; If the element is at the median ; calculate the number of nodes in the right and left sub - trees ( division operation is floored ) ; If median is located higher ; If median is located lower ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85 ; Function call
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static int kth_smallest ; public static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static boolean KSmallestPerfectBST ( Node root , int k , int treeSize ) { if ( root == null ) return false ; int median_loc = ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root . key ; return true ; } int newTreeSize = treeSize / 2 ; if ( k < median_loc ) { return KSmallestPerfectBST ( root . left , k , newTreeSize ) ; } int newK = k - median_loc ; return KSmallestPerfectBST ( root . right , newK , newTreeSize ) ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; int n = 15 , k = 5 ; if ( KSmallestPerfectBST ( root , k , n ) ) { System . out . print ( kth_smallest + " ▁ " ) ; } } }
Check if given Binary string follows then given condition or not | Java program for the above approach ; Function to check if the String follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function call
import java . util . * ; class GFG { static boolean checkrules ( String s ) { if ( s . length ( ) == 0 ) return true ; if ( s . charAt ( 0 ) != '1' ) return false ; if ( s . length ( ) > 2 ) { if ( s . charAt ( 1 ) == '0' && s . charAt ( 2 ) == '0' ) return checkrules ( s . substring ( 3 ) ) ; } return checkrules ( s . substring ( 1 ) ) ; } public static void main ( String [ ] args ) { String str = "1111" ; if ( checkrules ( str ) ) { System . out . print ( " Valid ▁ String " ) ; } else { System . out . print ( " Invalid ▁ String " ) ; } } }
Length of longest subsequence in an Array having all elements as Nude Numbers | Java program for the above approach ; Function to check if the number is a Nude number ; Variable initialization ; Integer ' copy ' is converted to a String ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Driver Code ; Given array arr [ ] ; Function call
class GFG { static boolean isNudeNum ( int n ) { int copy , length , flag = 0 ; copy = n ; String temp ; temp = String . valueOf ( copy ) ; length = temp . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { int num = temp . charAt ( i ) - '0' ; if ( num == 0 n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } static int longestNudeSubseq ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 } ; int n = arr . length ; System . out . print ( longestNudeSubseq ( arr , n ) + "NEW_LINE"); } }
Longest subarray with difference exactly K between any two distinct values | Java implementation to find the longest subarray consisting of only two values with difference K ; Function to return the length of the longest sub - array ; Initialize set ; Store 1 st element of sub - array into set ; Check absolute difference between two elements ; If the new element is not present in the set ; If the set contains two elements ; Otherwise ; Update the maximum length ; Remove the set elements ; Driver Code
import java . util . * ; class GFG { static int longestSubarray ( int arr [ ] , int n , int k ) { int i , j , Max = 1 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { s . add ( arr [ i ] ) ; for ( j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] - arr [ j ] ) == 0 || Math . abs ( arr [ i ] - arr [ j ] ) == k ) { if ( ! s . contains ( arr [ j ] ) ) { if ( s . size ( ) == 2 ) break ; else s . add ( arr [ j ] ) ; } } else break ; } if ( s . size ( ) == 2 ) { Max = Math . max ( Max , j - i ) ; s . clear ( ) ; } else s . clear ( ) ; } return Max ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 2 , 2 , 5 , 5 , 5 } ; int N = arr . length ; int K = 1 ; int length = longestSubarray ( arr , N , K ) ; if ( length == 1 ) System . out . print ( - 1 ) ; else System . out . print ( length ) ; } }
Longest subarray of non | Java program for the above approach ; Function to find the maximum length of a subarray of 1 s after removing at most one 0 ; Stores the count of consecutive 1 's from left ; Stores the count of consecutive 1 's from right ; Traverse left to right ; If cell is non - empty ; Increase count ; If cell is empty ; Store the count of consecutive 1 's  till (i - 1)-th index ; Traverse from right to left ; Store the count of consecutive 1 s till ( i + 1 ) - th index ; Stores the length of longest subarray ; Store the maximum ; If array a contains only 1 s return n else return ans ; Driver code
class GFG { public static int longestSubarray ( int [ ] a , int n ) { int [ ] l = new int [ n ] ; int [ ] r = new int [ n ] ; for ( int i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( int i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } int ans = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = Math . max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( longestSubarray ( arr , n ) ) ; } }
Find largest factor of N such that N / F is less than K | Java program for the above approach ; Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Print the value of packages ; Driver code ; Given N and K ; Function call
import java . util . Arrays ; class GFG { static void findMaxValue ( int N , int K ) { int packages ; int maxi = 1 ; for ( int i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = Math . max ( maxi , i ) ; } packages = N / maxi ; System . out . println ( packages ) ; } public static void main ( String [ ] args ) { int N = 8 , K = 7 ; findMaxValue ( N , K ) ; } }
Largest square which can be formed using given rectangular blocks | Java program for the above approach ; Function to find maximum side length of square ; Sort array in asc order ; Traverse array in desc order ; Driver code ; Given array arr [ ] ; Function Call
import java . util . Arrays ; class GFG { static void maxSide ( int a [ ] , int n ) { int sideLength = 0 ; Arrays . sort ( a ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } System . out . println ( sideLength ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = new int [ ] { 3 , 2 , 1 , 5 , 2 , 4 } ; maxSide ( arr , N ) ; } }
Minimize difference after changing all odd elements to even | Java program for the above approach ; Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { public static void minDiff ( long [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } Arrays . sort ( a ) ; long mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } System . out . println ( mindifference ) ; } public static void main ( String [ ] args ) { long [ ] arr = { 3 , 8 , 13 , 30 , 50 } ; int n = arr . length ; minDiff ( arr , n ) ; } }
Longest subarray having sum K | Set 2 | Java program for the above approach ; To store the prefix sum array ; Function for searching the lower bound of the subarray ; Iterate until low less than equal to high ; For each mid finding sum of sub array less than or equal to k ; Return the final answer ; Function to find the length of subarray with sum K ; Initialize sum to 0 ; Push the prefix sum of the array arr [ ] in prefix [ ] ; Search r for each i ; Update ans ; Print the length of subarray found in the array ; Driver Code ; Given array arr [ ] ; Given sum K ; Function call
import java . util . * ; class GFG { static Vector < Integer > v = new Vector < Integer > ( ) ; static int bin ( int val , int k , int n ) { int lo = 0 ; int hi = n ; int mid ; int ans = - 1 ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( v . get ( mid ) - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } static void findSubarraySumK ( int arr [ ] , int N , int K ) { int sum = 0 ; v . add ( 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . add ( sum ) ; } int l = 0 , ans = 0 , r ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { r = bin ( v . get ( i ) , K , N ) ; ans = Math . max ( ans , r - i ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 8 , 14 , 9 , 4 , 11 , 10 } ; int N = arr . length ; int K = 13 ; findSubarraySumK ( arr , N , K ) ; } }
Partition a set into two subsets such that difference between max of one and min of other is minimized | java program for the above approach ; Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int splitArray ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int result = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . length ; System . out . print ( splitArray ( arr , N ) ) ; } }
Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | Java implementation to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; Function to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; First element of a given row ; Element in the given column ; Driver code ; Function call
import java . io . * ; class GFG { static int findValue ( int R , int C ) { int k = ( R * ( R - 1 ) ) / 2 + 1 ; int diff = R + 1 ; for ( int i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } public static void main ( String [ ] args ) { int R = 4 ; int C = 4 ; int k = findValue ( R , C ) ; System . out . println ( k ) ; } }
Maximize number of groups formed with size not smaller than its largest element | Java implementation of above approach ; Function that prints the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Printing maximum number of groups ; Driver Code
import java . util . * ; class GFG { static void makeGroups ( int a [ ] , int n ) { int [ ] v = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } int no_of_groups = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { no_of_groups += v [ i ] / i ; v [ i ] = v [ i ] % i ; } int i = 1 ; int total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { int rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } System . out . print ( no_of_groups + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 , 2 } ; int size = arr . length ; makeGroups ( arr , size ) ; } }
Leftmost Column with atleast one 1 in a row | Java program to calculate leftmost column having at least a 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver code
import java . util . * ; class GFG { static final int N = 3 ; static final int M = 4 ; static int FindColumn ( int mat [ ] [ ] ) { int row = 0 , col = M - 1 ; int flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row ] [ col ] == 1 ) { col -- ; flag = 1 ; } else { row ++ ; } } col ++ ; if ( flag != 0 ) return col + 1 ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 1 } } ; System . out . print ( FindColumn ( mat ) ) ; } }
Find two numbers made up of a given digit such that their difference is divisible by N | Java implementation of the above approach ; Function to implement the above approach ; Hashmap to store remainder - length of the number as key - value pairs ; Iterate till N + 1 length ; Search remainder in the map ; If remainder is not already present insert the length for the corresponding remainder ; Keep increasing M ; To keep M in range of integer ; Length of one number is the current Length ; Length of the other number is the length paired with current remainder in map ; Driver code
import java . util . * ; class GFG { static void findNumbers ( int N , int M ) { int m = M ; Map < Integer , Integer > remLen = new HashMap < > ( ) ; int len , remainder = 0 ; for ( len = 1 ; len <= N + 1 ; ++ len ) { remainder = M % N ; if ( ! remLen . containsKey ( remainder ) ) { remLen . put ( remainder , len ) ; } else { break ; } M = M * 10 + m ; M = M % N ; } int LenA = len ; int LenB = remLen . getOrDefault ( remainder , 0 ) ; for ( int i = 0 ; i < LenB ; ++ i ) System . out . print ( m ) ; System . out . print ( " ▁ " ) ; for ( int i = 0 ; i < LenA ; ++ i ) System . out . print ( m ) ; } public static void main ( String [ ] args ) { int N = 8 , M = 2 ; findNumbers ( N , M ) ; } }
Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements | Java implementation to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; 2D prefix sum for submatrix sum query for matrix ; Function to find the prefix sum of the matrix from i and j ; Function to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; Combining the two arrays ; Calculating the 2D prefix sum ; Iterating through all the elements of matrix and considering them to be the bottom right ; Applying binary search over side length ; If sum of this submatrix >= k then new search space will be [ low , mid ] ; Else new search space will be [ mid + 1 , high ] ; Adding the total submatrices ; Driver Code
class GFG { static int [ ] [ ] prefix_2D = new int [ 2005 ] [ 2005 ] ; static int subMatrixSum ( int i , int j , int len ) { return prefix_2D [ i ] [ j ] - prefix_2D [ i ] [ j - len ] - prefix_2D [ i - len ] [ j ] + prefix_2D [ i - len ] [ j - len ] ; } static int numberOfWays ( int a [ ] , int b [ ] , int n , int m , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( a [ i - 1 ] == b [ j - 1 ] ) prefix_2D [ i ] [ j ] = 1 ; else prefix_2D [ i ] [ j ] = 0 ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i ] [ j - 1 ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i - 1 ] [ j ] ; } } int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int low = 1 ; int high = Math . min ( i , j ) ; while ( low < high ) { int mid = ( low + high ) >> 1 ; if ( subMatrixSum ( i , j , mid ) >= k ) { high = mid ; } else { low = mid + 1 ; } } if ( subMatrixSum ( i , j , low ) >= k ) { answer += ( Math . min ( i , j ) - low + 1 ) ; } } } return answer ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; int A [ ] = { 1 , 2 } ; int B [ ] = { 1 , 2 , 3 } ; int K = 1 ; System . out . print ( numberOfWays ( A , B , N , M , K ) ) ; } }
Find lexicographically smallest string in at most one swaps | Java implementation of the above approach ; Function to return the lexicographically smallest String that can be formed by swapping at most one character . The characters might not necessarily be adjacent . ; Store last occurrence of every character ; Set - 1 as default for every character . ; Character index to fill in the last occurrence array ; If this is true then this character is being visited for the first time from the last Thus last occurrence of this character is stored in this index ; Character to replace ; Find the last occurrence of this character . ; Swap this with the last occurrence ; Driver code
import java . util . * ; class GFG { static String findSmallest ( char [ ] s ) { int len = s . length ; int [ ] loccur = new int [ 26 ] ; Arrays . fill ( loccur , - 1 ) ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } char [ ] sorted_s = s ; Arrays . sort ( sorted_s ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; char temp = s [ last_occ ] ; s [ last_occ ] = s [ i ] ; s [ i ] = temp ; break ; } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = " geeks " ; System . out . print ( findSmallest ( s . toCharArray ( ) ) ) ; } }