codeparrot/xlcost-text-to-code huggingface dataset:
Longest Geometric Progression | Java program to find length of the longest geometric progression in a given set ; Returns length of the longest GP subset of set [ ] ; Base cases ; Let us sort the set first ; An entry L [ i ] [ j ] in this table stores LLGP with set [ i ] and set [ j ] as first two elements of GP and j > i . ; Initialize result ( A single element is always a GP ) ; Initialize values of last column ; Consider every element as second element of GP ; Search for i and k for j ; Two cases when i , j and k don 't form a GP. ; i , j and k form GP , LLGP with i and j as first two elements is equal to LLGP with j and k as first two elements plus 1. L [ j ] [ k ] must have been filled before as we run the loop from right side ; Update overall LLGP ; Change i and k to fill more L [ i ] [ j ] values for current j ; If the loop was stopped due to k becoming more than n - 1 , set the remaining entries in column j as 1 or 2 based on divisibility of set [ j ] by set [ i ] ; Return result ; Driver code
import java . util . * ; class GFG { static int lenOfLongestGP ( int set [ ] , int n ) { if ( n < 2 ) { return n ; } if ( n == 2 ) { return ( set [ 1 ] % set [ 0 ] == 0 ? 2 : 1 ) ; } Arrays . sort ( set ) ; int L [ ] [ ] = new int [ n ] [ n ] ; int llgp = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( set [ n - 1 ] % set [ i ] == 0 ) { L [ i ] [ n - 1 ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ n - 1 ] = 1 ; } } L [ n - 1 ] [ n - 1 ] = 1 ; for ( int j = n - 2 ; j >= 1 ; -- j ) { int i = j - 1 , k = j + 1 ; while ( i >= 0 && k <= n - 1 ) { if ( set [ i ] * set [ k ] < set [ j ] * set [ j ] ) { ++ k ; } else if ( set [ i ] * set [ k ] > set [ j ] * set [ j ] ) { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ j ] = 1 ; } -- i ; } else { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = L [ j ] [ k ] + 1 ; if ( L [ i ] [ j ] > llgp ) { llgp = L [ i ] [ j ] ; } } else { L [ i ] [ j ] = 1 ; } -- i ; ++ k ; } } while ( i >= 0 ) { if ( set [ j ] % set [ i ] == 0 ) { L [ i ] [ j ] = 2 ; if ( 2 > llgp ) llgp = 2 ; } else { L [ i ] [ j ] = 1 ; } -- i ; } } return llgp ; } public static void main ( String [ ] args ) { int set1 [ ] = { 1 , 3 , 9 , 27 , 81 , 243 } ; int n1 = set1 . length ; System . out . print ( lenOfLongestGP ( set1 , n1 ) + "NEW_LINE"); int set2 [ ] = { 1 , 3 , 4 , 9 , 7 , 27 } ; int n2 = set2 . length ; System . out . print ( lenOfLongestGP ( set2 , n2 ) + "NEW_LINE"); int set3 [ ] = { 2 , 3 , 5 , 7 , 11 , 13 } ; int n3 = set3 . length ; System . out . print ( lenOfLongestGP ( set3 , n3 ) + "NEW_LINE"); } }
Printing Longest Bitonic Subsequence | Dynamic Programming solution to print Longest Bitonic Subsequence ; Utility function to print Longest Bitonic Subsequence ; Function to construct and print Longest Bitonic Subsequence ; LIS [ i ] stores the length of the longest increasing subsequence ending with arr [ i ] ; initialize LIS [ 0 ] to arr [ 0 ] ; Compute LIS values from left to right ; for every j less than i ; LDS [ i ] stores the length of the longest decreasing subsequence starting with arr [ i ] ; initialize LDS [ n - 1 ] to arr [ n - 1 ] ; Compute LDS values from right to left ; for every j greater than i ; reverse as vector as we 're inserting at end ; * LDS [ i ] now stores Maximum Decreasing Subsequence * of arr [ i . . n ] that starts with arr [ i ] ; Find maximum value of size of LIS [ i ] + size of LDS [ i ] - 1 ; print all but last element of LIS [ maxIndex ] vector ; print all elements of LDS [ maxIndex ] vector ; Driver Code
import java . util . * ; class GFG { static void print ( Vector < Integer > arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr . elementAt ( i ) + " ▁ " ) ; } static void printLBS ( int [ ] arr , int n ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] LIS = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = new Vector < > ( ) ; LIS [ 0 ] . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && LIS [ j ] . size ( ) > LIS [ i ] . size ( ) ) { for ( int k : LIS [ j ] ) if ( ! LIS [ i ] . contains ( k ) ) LIS [ i ] . add ( k ) ; } } LIS [ i ] . add ( arr [ i ] ) ; } @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] LDS = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) LDS [ i ] = new Vector < > ( ) ; LDS [ n - 1 ] . add ( arr [ n - 1 ] ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = n - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] && LDS [ j ] . size ( ) > LDS [ i ] . size ( ) ) for ( int k : LDS [ j ] ) if ( ! LDS [ i ] . contains ( k ) ) LDS [ i ] . add ( k ) ; } LDS [ i ] . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) Collections . reverse ( LDS [ i ] ) ; int max = 0 ; int maxIndex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( LIS [ i ] . size ( ) + LDS [ i ] . size ( ) - 1 > max ) { max = LIS [ i ] . size ( ) + LDS [ i ] . size ( ) - 1 ; maxIndex = i ; } } print ( LIS [ maxIndex ] , LIS [ maxIndex ] . size ( ) - 1 ) ; print ( LDS [ maxIndex ] , LDS [ maxIndex ] . size ( ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 11 , 2 , 10 , 4 , 5 , 2 , 1 } ; int n = arr . length ; printLBS ( arr , n ) ; } }
Find if string is K | Java program to find if given string is K - Palindrome or not ; find if given string is K - Palindrome or not ; Create a table to store results of subproblems ; Fill dp [ ] [ ] in bottom up manner ; If first string is empty , only option is to remove all characters of second string ; Min . operations = j ; If second string is empty , only option is to remove all characters of first string ; Min . operations = i ; If last characters are same , ignore last character and recur for remaining string ; If last character are different , remove it and find minimum ; Remove from str1 Remove from str2 ; Returns true if str is k palindrome . ;  ; Driver program
class GFG { static int isKPalDP ( String str1 , String str2 , int m , int n ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) { dp [ i ] [ j ] = j ; } else if ( j == 0 ) { dp [ i ] [ j ] = i ; } else if ( str1 . charAt ( i - 1 ) == str2 . charAt ( j - 1 ) ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } } return dp [ m ] [ n ] ; } static boolean isKPal ( String str , int k ) { String revStr = str ; revStr = reverse ( revStr ) ; int len = str . length ( ) ; return ( isKPalDP ( str , revStr , len , len ) <= k * 2 ) ; } static String reverse ( String str ) { StringBuilder sb = new StringBuilder ( str ) ; sb . reverse ( ) ; return sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " acdcb " ; int k = 2 ; if ( isKPal ( str , k ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
A Space Optimized Solution of LCS | Java Code for A Space Optimized Solution of LCS ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Find lengths of two strings ; Binary index , used to index current row and previous row . ; Compute current binary index ; Last filled entry contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] ; Driver Code
class GFG { public static int lcs ( String X , String Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ ] [ ] = new int [ 2 ] [ n + 1 ] ; int bi = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { bi = i & 1 ; for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ bi ] [ j ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) L [ bi ] [ j ] = L [ 1 - bi ] [ j - 1 ] + 1 ; else L [ bi ] [ j ] = Math . max ( L [ 1 - bi ] [ j ] , L [ bi ] [ j - 1 ] ) ; } } return L [ bi ] [ n ] ; } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; System . out . println ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y ) ) ; } }
Count number of subsets having a particular XOR value | Java dynamic programming solution to finding the number of subsets having xor of their elements as k ; Returns count of subsets of arr [ ] with XOR value equals to k . ; Find maximum element in arr [ ] ; Maximum possible XOR value ; The value of dp [ i ] [ j ] is the number of subsets having XOR of their elements as j from the set arr [ 0. . . i - 1 ] ; Initializing all the values of dp [ i ] [ j ] as 0 ; The xor of empty subset is 0 ; Fill the dp table ; The answer is the number of subset from set arr [ 0. . n - 1 ] having XOR of elements as k ; Driver code
class GFG { static int subsetXOR ( int [ ] arr , int n , int k ) { int max_ele = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; int m = ( 1 << ( int ) ( Math . log ( max_ele ) / Math . log ( 2 ) + 1 ) ) - 1 ; if ( k > m ) { return 0 ; } int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = 0 ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j ^ arr [ i - 1 ] ] ; return dp [ n ] [ k ] ; } public static void main ( String arg [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int k = 4 ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ subsets ▁ is ▁ " + subsetXOR ( arr , n , k ) ) ; } }
Partition a set into two subsets such that the difference of subset sums is minimum | A Recursive java program to solve minimum sum partition problem . ; Returns the minimum value of the difference of the two sets . ; Calculate sum of all elements ; Create an array to store results of subproblems ; Initialize first column as true . 0 sum is possible with all elements . ; Initialize top row , except dp [ 0 ] [ 0 ] , as false . With 0 elements , no other sum except 0 is possible ; Fill the partition table in bottom up manner ; If i 'th element is excluded ; If i 'th element is included ; Initialize difference of two sums . ; Find the largest j such that dp [ n ] [ j ] is true where j loops from sum / 2 t0 0 ; Driver program
import java . io . * ; class GFG { static int findMin ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) dp [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; if ( arr [ i - 1 ] <= j ) dp [ i ] [ j ] |= dp [ i - 1 ] [ j - arr [ i - 1 ] ] ; } } int diff = Integer . MAX_VALUE ; for ( int j = sum / 2 ; j >= 0 ; j -- ) { if ( dp [ n ] [ j ] == true ) { diff = sum - 2 * j ; break ; } } return diff ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 2 , 2 , 1 } ; int n = arr . length ; System . out . println ( " The ▁ minimum ▁ difference ▁ between ▁ 2 ▁ sets ▁ is ▁ " + findMin ( arr , n ) ) ; } }
Count number of paths with at | Java program to count number of paths with maximum k turns allowed ; table to store results of subproblems ; Returns count of paths to reach ( i , j ) from ( 0 , 0 ) using at - most k turns . d is current direction d = 0 indicates along row , d = 1 indicates along column . ; If invalid row or column indexes ; If current cell is top left itself ; If 0 turns left ; If direction is row , then we can reach here only if direction is row and row is 0. ; If direction is column , then we can reach here only if direction is column and column is 0. ; If this subproblem is already evaluated ; If current direction is row , then count paths for two cases 1 ) We reach here through previous row . 2 ) We reach here through previous column , so number of turns k reduce by 1. ; Similar to above if direction is column ; This function mainly initializes ' dp ' array as - 1 and calls countPathsUtil ( ) ; If ( 0 , 0 ) is target itself ; Initialize ' dp ' array ; Recur for two cases : moving along row and along column return countPathsUtil ( i - 1 , j , k , 1 ) + Moving along row countPathsUtil ( i , j - 1 , k , 0 ) ; Moving along column ; Driver Code
import java . util . * ; class GFG { static int MAX = 100 ; static int [ ] [ ] [ ] [ ] dp = new int [ MAX ] [ MAX ] [ MAX ] [ 2 ] ; static int countPathsUtil ( int i , int j , int k , int d ) { if ( i < 0 j < 0 ) return 0 ; if ( i == 0 && j == 0 ) return 1 ; if ( k == 0 ) { if ( d == 0 && i == 0 ) return 1 ; if ( d == 1 && j == 0 ) return 1 ; return 0 ; } if ( dp [ i ] [ j ] [ k ] [ d ] != - 1 ) return dp [ i ] [ j ] [ k ] [ d ] ; if ( d == 0 ) return dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i , j - 1 , k , d ) + countPathsUtil ( i - 1 , j , k - 1 , d == 1 ? 0 : 1 ) ; return dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i - 1 , j , k , d ) + countPathsUtil ( i , j - 1 , k - 1 , d == 1 ? 0 : 1 ) ; } static int countPaths ( int i , int j , int k ) { if ( i == 0 && j == 0 ) return 1 ; for ( int p = 0 ; p < MAX ; p ++ ) { for ( int q = 0 ; q < MAX ; q ++ ) { for ( int r = 0 ; r < MAX ; r ++ ) for ( int s = 0 ; s < 2 ; s ++ ) dp [ p ] [ q ] [ r ] [ s ] = - 1 ; } } } public static void main ( String [ ] args ) { int m = 3 , n = 3 , k = 2 ; System . out . println ( " Number ▁ of ▁ paths ▁ is ▁ " + countPaths ( m - 1 , n - 1 , k ) ) ; } }
Find minimum possible size of array with given rules for removing elements | Java program to find size of minimum possible array after removing elements according to given rules ; dp [ i ] [ j ] denotes the minimum number of elements left in the subarray arr [ i . . j ] . ; If already evaluated ; If size of array is less than 3 ; Initialize result as the case when first element is separated ( not removed using given rules ) ; Now consider all cases when first element forms a triplet and removed . Check for all possible triplets ( low , i , j ) ; Check if this triplet follows the given rules of removal . And elements between ' low ' and ' i ' , and between ' i ' and ' j ' can be recursively removed . ; Insert value in table and return result ; This function mainly initializes dp table and calls recursive function minSizeRec ; Driver code
class GFG { static int MAX = 1000 ; static int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; static int minSizeRec ( int arr [ ] , int low , int high , int k ) { if ( dp [ low ] [ high ] != - 1 ) { return dp [ low ] [ high ] ; } if ( ( high - low + 1 ) < 3 ) { return high - low + 1 ; } int res = 1 + minSizeRec ( arr , low + 1 , high , k ) ; for ( int i = low + 1 ; i <= high - 1 ; i ++ ) { for ( int j = i + 1 ; j <= high ; j ++ ) { if ( arr [ i ] == ( arr [ low ] + k ) && arr [ j ] == ( arr [ low ] + 2 * k ) && minSizeRec ( arr , low + 1 , i - 1 , k ) == 0 && minSizeRec ( arr , i + 1 , j - 1 , k ) == 0 ) { res = Math . min ( res , minSizeRec ( arr , j + 1 , high , k ) ) ; } } } return ( dp [ low ] [ high ] = res ) ; } static int minSize ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return minSizeRec ( arr , 0 , n - 1 , k ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 4 } ; int n = arr . length ; int k = 1 ; System . out . println ( minSize ( arr , n , k ) ) ; } }
Find number of solutions of a linear equation of n variables | A naive recursive Java program to find number of non - negative solutions for a given linear equation ; Recursive function that returns count of solutions for given rhs value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code
import java . io . * ; class GFG { static int countSol ( int coeff [ ] , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } public static void main ( String [ ] args ) { int coeff [ ] = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . length ; System . out . println ( countSol ( coeff , 0 , n - 1 , rhs ) ) ; } }
Maximum weight transformation of a given string | Java program to find maximum weight transformation of a given string ; Returns weight of the maximum weight transformation ; Base case ; If this subproblem is already solved ; Don 't make pair, so  weight gained is 1 ; If we can make pair ; If elements are dissimilar , weight gained is 4 ; if elements are similar so for making a pair we toggle any of them . Since toggle cost is 1 so overall weight gain becomes 3 ; save and return maximum of above cases ; Initializes lookup table and calls getMaxRec ( ) ; Create and initialize lookup table ; Call recursive function ; Driver Code
class GFG { static int getMaxRec ( String str , int i , int n , int [ ] lookup ) { if ( i >= n ) { return 0 ; } if ( lookup [ i ] != - 1 ) { return lookup [ i ] ; } int ans = 1 + getMaxRec ( str , i + 1 , n , lookup ) ; if ( i + 1 < n ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) { ans = Math . max ( 4 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } else { ans = Math . max ( 3 + getMaxRec ( str , i + 2 , n , lookup ) , ans ) ; } } return lookup [ i ] = ans ; } static int getMaxWeight ( String str ) { int n = str . length ( ) ; int [ ] lookup = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { lookup [ i ] = - 1 ; } return getMaxRec ( str , 0 , str . length ( ) , lookup ) ; } public static void main ( String [ ] args ) { String str = " AAAAABB " ; System . out . println ( " Maximum ▁ weight ▁ of ▁ a " + " ▁ transformation ▁ of ▁ " + str + " ▁ is ▁ " + getMaxWeight ( str ) ) ; } }
Minimum steps to reach a destination | Java program to count number of steps to reach a point ; source -> source vertex step -> value of last step taken dest -> destination vertex ; base cases ; if we go on positive side ; if we go on negative side ; minimum of both cases ; Driver Code
import java . io . * ; class GFG { static int steps ( int source , int step , int dest ) { if ( Math . abs ( source ) > ( dest ) ) return Integer . MAX_VALUE ; if ( source == dest ) return step ; int pos = steps ( source + step + 1 , step + 1 , dest ) ; int neg = steps ( source - step - 1 , step + 1 , dest ) ; return Math . min ( pos , neg ) ; } public static void main ( String [ ] args ) { int dest = 11 ; System . out . println ( " No . ▁ of ▁ steps ▁ required " + " ▁ to ▁ reach ▁ " + dest + " ▁ is ▁ " + steps ( 0 , 0 , dest ) ) ; } }
Longest Common Substring | DP | Java implementation of the above approach ; Function to find the length of the longest LCS ; Create DP table ; Driver Code ; Function call
class GFG { static int LCSubStr ( String s , String t , int n , int m ) { int dp [ ] [ ] = new int [ 2 ] [ m + 1 ] ; int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( s . charAt ( i - 1 ) == t . charAt ( j - 1 ) ) { dp [ i % 2 ] [ j ] = dp [ ( i - 1 ) % 2 ] [ j - 1 ] + 1 ; if ( dp [ i % 2 ] [ j ] > res ) res = dp [ i % 2 ] [ j ] ; } else dp [ i % 2 ] [ j ] = 0 ; } } return res ; } public static void main ( String [ ] args ) { String X = " OldSite : GeeksforGeeks . org " ; String Y = " NewSite : GeeksQuiz . com " ; int m = X . length ( ) ; int n = Y . length ( ) ; System . out . println ( LCSubStr ( X , Y , m , n ) ) ; } }
Longest Common Substring | DP | Java program using to find length of the longest common substring recursion ; Returns length of function for longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Driver code
class GFG { static String X , Y ; static int lcs ( int i , int j , int count ) { if ( i == 0 j == 0 ) { return count ; } if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { count = lcs ( i - 1 , j - 1 , count + 1 ) ; } count = Math . max ( count , Math . max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) ; return count ; } public static void main ( String [ ] args ) { int n , m ; X = " abcdxyz " ; Y = " xyzabcd " ; n = X . length ( ) ; m = Y . length ( ) ; System . out . println ( lcs ( n , m , 0 ) ) ; } }
Make Array elements equal by replacing adjacent elements with their XOR | Java Program of the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the XOR of all elements of array A [ ] ; Case 1 , check if the XOR of the array A [ ] is 0 ; Maintains the XOR till the current element ; Iterate over the array ; If the current XOR is equal to the total XOR increment the count and initialize current XOR as 0 ; Print Answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void possibleEqualArray ( int A [ ] , int N ) { int tot_XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { tot_XOR ^= A [ i ] ; } if ( tot_XOR == 0 ) { System . out . print ( " YES " ) ; return ; } int cur_XOR = 0 ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_XOR ^= A [ i ] ; if ( cur_XOR == tot_XOR ) { cnt ++ ; cur_XOR = 0 ; } } if ( cnt > 2 ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 0 , 2 , 2 } ; int N = ( A . length ) ; possibleEqualArray ( A , N ) ; } }
Count of palindromes that can be obtained by concatenating equal length prefix and substrings | Java program for the above approach ; Function to calculate the number of palindromes ; Calculation of Z - array ; Calculation of sigma ( Z [ i ] + 1 ) ; Return the count ; Driver Code ; Given String
import java . util . * ; class GFG { static int countPalindromes ( String S ) { int N = ( int ) S . length ( ) ; int [ ] Z = new int [ ( N ) ] ; int l = 0 , r = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( i <= r ) Z [ i ] = Math . min ( r - i + 1 , Z [ i - l ] ) ; while ( i + Z [ i ] < N && S . charAt ( Z [ i ] ) == S . charAt ( i + Z [ i ] ) ) { Z [ i ] ++ ; } if ( i + Z [ i ] - 1 > r ) { l = i ; r = i + Z [ i ] - 1 ; } } int sum = 0 ; for ( int i = 0 ; i < Z . length ; i ++ ) { sum += Z [ i ] + 1 ; } return sum ; } public static void main ( String [ ] args ) { String S = " abab " ; System . out . println ( countPalindromes ( S ) ) ; } }
Extract substrings between any pair of delimiters | Java program to implement the above approach ; Function to print Strings present between any pair of delimeters ; Stores the indices of ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of subString ; Extract the subString ; Driver Code
import java . util . * ; class GFG { static void printSubsInDelimeters ( String str ) { Stack < Integer > dels = new Stack < Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' [ ' ) { dels . add ( i ) ; } else if ( str . charAt ( i ) == ' ] ' && ! dels . isEmpty ( ) ) { int pos = dels . peek ( ) ; dels . pop ( ) ; int len = i - 1 - pos ; String ans = str . substring ( pos + 1 , pos + 1 + len ) ; System . out . print ( ans + "NEW_LINE"); } } } public static void main ( String [ ] args ) { String str = " [ This ▁ is ▁ first ] ▁ ignored ▁ text ▁ [ This ▁ is ▁ second ] " ; printSubsInDelimeters ( str ) ; } }