codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs | Java program for the above approach ; Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally ; Store the size of matrix ; If N is odd , then no such pair exists ; Store the number of required pairs ; Initialize an auxiliary matrix and fill it with 0 s ; Traverse the matrix v [ ] [ ] ; Check if i + j is odd or even ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1 ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1 ; Iterate in range [ 0 , k - 1 ] using i ; Iterate in range [ i + 1 , k - 1 ] using j ; Update the ans ; Print the answer ; Driver Code
import java . io . * ; class GFG { static void numberofpairs ( int [ ] [ ] v , int k ) { int n = v . length ; if ( n % 2 == 1 ) { System . out . println ( 0 ) ; return ; } int ans = 0 ; int dp [ ] [ ] = new int [ k ] [ 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( i + j ) % 2 == 0 ) dp [ v [ i ] [ j ] - 1 ] [ 0 ] ++ ; else dp [ v [ i ] [ j ] - 1 ] [ 1 ] ++ ; } } for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i + 1 ; j < k ; j ++ ) { ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ] ; ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ] ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 } , { 3 , 4 } } ; int K = 4 ; numberofpairs ( mat , K ) ; } }
Maximize sum by selecting X different | Java program for the above approach ; Store overlapping subproblems of the recurrence relation ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; If the subproblem already computed ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
import java . util . * ; class GFG { static int [ ] [ ] [ ] [ ] dp = new int [ 50 ] [ 50 ] [ 50 ] [ 50 ] ; static int FindMaxS ( int X , int Y , int Z , int n , int [ ] A , int [ ] B , int [ ] C ) { if ( X < 0 Y < 0 Z < 0 ) return Integer . MIN_VALUE ; if ( n < 0 ) return 0 ; if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) { return dp [ n ] [ X ] [ Y ] [ Z ] ; } int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return dp [ n ] [ X ] [ Y ] [ Z ] = maximum ; } public static void main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int [ ] A = { 10 , 0 , 5 } ; int [ ] B = { 5 , 10 , 0 } ; int [ ] C = { 0 , 5 , 10 } ; int n = B . length ; for ( int i = 0 ; i < 50 ; i ++ ) for ( int j = 0 ; j < 50 ; j ++ ) for ( int k = 0 ; k < 50 ; k ++ ) for ( int l = 0 ; l < 50 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; System . out . print ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }
Minimize swaps of adjacent characters to sort every possible rearrangement of given Binary String | Java program for the above approach ; Precalculate the values of power of 2 ; Function to calculate 2 ^ N % mod ; Function to find sum of inversions ; Initialise a list of 0 s and ? s ; Traverse the string in the reversed manner ; If the current character is 1 ; Effectively calculate a * b ^ ( b - 1 ) ; If the current character is 0 ; Increment count of zeroes ; Double count the zeroes ; Find b * 2 ^ ( b - 1 ) ; Increment count of questions ; Return the final count ; Driver Code ; Given string S ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int MOD = 1000000007 ; static Integer array [ ] = { 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 } ; static Vector < Integer > MEM = new Vector < Integer > ( Arrays . asList ( array ) ) ; static int mod_pow2 ( int n ) { while ( n >= MEM . size ( ) ) MEM . add ( ( MEM . get ( MEM . size ( ) - 1 ) * 2 ) % MOD ) ; return MEM . get ( n ) ; } static int inversions ( char [ ] bstr ) { int total = 0 , zeros = 0 , questions = 0 ; int j = bstr . length - 1 ; for ( int i = 0 ; i < bstr . length / 2 ; i ++ ) { char temp = bstr [ i ] ; bstr [ i ] = bstr [ j ] ; bstr [ j ] = temp ; j -- ; } for ( char x : bstr ) { int q ; if ( x == '1' ) { int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; } else if ( x == '0' ) { zeros += 1 ; } else { total *= 2 ; int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; questions += 1 ; } } return total ; } public static void main ( String [ ] args ) { char S [ ] = " ? 0 ? " . toCharArray ( ) ; System . out . println ( inversions ( S ) ) ; } }
Minimum removals required to convert given array to a Mountain Array | Java program of the above approach ; Utility function to count array elements required to be removed to make array a mountain array ; Stores length of increasing subsequence from [ 0 , i - 1 ] ; Stores length of increasing subsequence from [ i + 1 , n - 1 ] ; Iterate for each position up to N - 1 to find the length of subsequence ; If j is less than i , then i - th position has leftIncreasing [ j ] + 1 lesser elements including itself ; Check if it is the maximum obtained so far ; Search for increasing subsequence from right ; Find the position following the peak condition and have maximum leftIncreasing [ i ] + rightIncreasing [ i ] ; Function to count elements to be removed to make array a mountain array ; Print the answer ; Driver Code ; Given array ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int minRemovalsUtil ( int [ ] arr ) { int result = 0 ; if ( arr . length < 3 ) { return - 1 ; } int [ ] leftIncreasing = new int [ arr . length ] ; int [ ] rightIncreasing = new int [ arr . length ] ; for ( int i = 1 ; i < arr . length ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { leftIncreasing [ i ] = Math . max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ; } } } for ( int i = arr . length - 2 ; i >= 0 ; i -- ) { for ( int j = arr . length - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { rightIncreasing [ i ] = Math . max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 ) ; } } } for ( int i = 0 ; i < arr . length ; i ++ ) { if ( leftIncreasing [ i ] != 0 && rightIncreasing [ i ] != 0 ) { result = Math . max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ; } } return arr . length - ( result + 1 ) ; } public static void minRemovals ( int [ ] arr ) { int ans = minRemovalsUtil ( arr ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 } ; minRemovals ( arr ) ; } }
Count of numbers from range [ L , R ] whose sum of digits is Y | Set 2 | Java program for the above approach ; Function to find the sum of digits of numbers in the range [ 0 , X ] ; Check if count of digits in a number greater than count of digits in X ; Check Iif sum of digits of a number is equal to Y ; Stores count of numbers whose sum of digits is Y ; Check if the number exceeds Y or not ; Iterate over all possible values of i - th digits ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose sum of digits is Y ; Base Case ; Stores numbers in the form of its equivalent String ; Stores count of numbers in the range [ 0 , R ] ; Update str ; Stores count of numbers in the range [ 0 , L - 1 ] ; Driver Code
import java . util . * ; class GFG { static int cntNum ( String X , int i , int sum , int tight ) { if ( i >= X . length ( ) sum < 0 ) { if ( sum == 0 ) { return 1 ; } return 0 ; } int res = 0 ; int end = tight != 0 ? X . charAt ( i ) - '0' : 9 ; for ( int j = 0 ; j <= end ; j ++ ) { res += cntNum ( X , i + 1 , sum - j , ( tight > 0 & ( j == end ) ) == true ? 1 : 0 ) ; } return res ; } static int UtilCntNumRange ( int L , int R , int Y ) { if ( R == 0 && Y == 0 ) { return 1 ; } String str = String . valueOf ( R ) ; int cntR = cntNum ( str , 0 , Y , 1 ) ; str = String . valueOf ( L - 1 ) ; int cntL = cntNum ( str , 0 , Y , 1 ) ; return ( cntR - cntL ) ; } public static void main ( String [ ] args ) { int L = 20 , R = 10000 , Y = 14 ; System . out . print ( UtilCntNumRange ( L , R , Y ) ) ; } }
Minimum number of jumps to obtain an element of opposite parity | Java program for above approach ; Bfs for odd numbers are source ; Initialize queue ; Stores for each node , the nodes visited and their distances ; If parity is 0 -> odd Otherwise -> even ; Perform multi - source bfs ; Extract the front element of the queue ; Traverse nodes connected to the current node ; If u is not visited ; If element with opposite parity is obtained ; Store its distance from source in ans [ ] ; Push the current neighbour to the queue ; Function to find the minimum jumps required by each index to reach element of opposite parity ; Initialise Inverse Graph ; Stores the result for each index ; For the jumped index ; If the ind is valid then add reverse directed edge ; Multi - source bfs with odd numbers as source by passing 0 ; Multi - source bfs with even numbers as source by passing 1 ; Print the answer ; Driver function
import java . util . * ; import java . lang . * ; class Gfg { static void bfs ( int n , int [ ] a , ArrayList < ArrayList < Integer > > invGr , int [ ] ans , int parity ) { Queue < Integer > q = new LinkedList < > ( ) ; int [ ] vis = new int [ n + 1 ] ; int [ ] dist = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( ( a [ i ] + parity ) & 1 ) != 0 ) { q . add ( i ) ; vis [ i ] = 1 ; } } while ( ! q . isEmpty ( ) ) { int v = q . peek ( ) ; q . poll ( ) ; for ( Integer u : invGr . get ( v ) ) { if ( vis [ u ] == 0 ) { dist [ u ] = dist [ v ] + 1 ; vis [ u ] = 1 ; if ( ( a [ u ] + parity ) % 2 == 0 ) { if ( ans [ u ] == - 1 ) ans [ u ] = dist [ u ] ; } q . add ( u ) ; } } } } static void minJumps ( int [ ] a , int [ ] jump , int n ) { ArrayList < ArrayList < Integer > > invGr = new ArrayList < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) invGr . add ( new ArrayList < Integer > ( ) ) ; int [ ] ans = new int [ n + 1 ] ; Arrays . fill ( ans , - 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + jump [ i ] >= 1 && i + jump [ i ] <= n ) { invGr . get ( i + jump [ i ] ) . add ( i ) ; } if ( i - jump [ i ] >= 1 && i - jump [ i ] <= n ) { invGr . get ( i - jump [ i ] ) . add ( i ) ; } } bfs ( n , a , invGr , ans , 0 ) ; bfs ( n , a , invGr , ans , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 4 , 2 , 5 , 2 , 1 } ; int [ ] jump = { 0 , 1 , 2 , 3 , 1 , 2 } ; int N = arr . length ; minJumps ( arr , jump , N - 1 ) ; } }
Longest subsequence having maximum GCD between any pair of distinct elements | Java program for the above approach ; Recursive function to return gcd of a and b ; Function to find GCD of pair with maximum GCD ; Stores maximum element of arr [ ] ; Find the maximum element ; Maintain a count array ; Store the frequency of arr [ ] ; Stores the multiples of a number ; Iterate over the range [ MAX , 1 ] ; Iterate from current potential GCD till it is less than MAX ; A multiple found ; Increment potential GCD by itself io check i , 2 i , 3 i ... ; If 2 multiples found max GCD found ; Function to find longest subsequence such that GCD of any two distinct integers is maximum ; Base Cases ; Compare current GCD to the maximum GCD ; If true increment and move the pointer ; Return max of either subsequences ; Driver Code ; Sorted array ; Function call to calculate GCD of pair with maximum GCD ; Print the result
import java . util . * ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int findMaxGCD ( int arr [ ] , int N ) { int high = 0 ; for ( int i = 0 ; i < N ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } int [ ] count = new int [ high + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { count [ arr [ i ] ] += 1 ; } int counter = 0 ; for ( int i = high ; i > 0 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 0 ; } static int maxlen ( int i , int j , int arr [ ] , int arr1 [ ] , int N , int maxgcd ) { int a = 1 ; if ( i >= N j >= N ) return 0 ; if ( __gcd ( arr [ i ] , arr1 [ j ] ) == maxgcd && arr [ i ] != arr1 [ j ] ) { a = Math . max ( a , 1 + maxlen ( i , j + 1 , arr , arr1 , N , maxgcd ) ) ; return a ; } return Math . max ( maxlen ( i + 1 , j , arr , arr1 , N , maxgcd ) , maxlen ( i , j + 1 , arr , arr1 , N , maxgcd ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 8 , 5 , 6 } ; int arr1 [ ] = { 1 , 2 , 8 , 5 , 6 } ; int n = arr . length ; Arrays . sort ( arr ) ; int maxgcd = findMaxGCD ( arr , n ) ; System . out . print ( maxlen ( 0 , 0 , arr , arr1 , n , maxgcd ) ) ; } }
Count unique paths is a matrix whose product of elements contains odd number of divisors | Java program for the above approach ; Stores the results ; Count of unique product paths ; Function to check whether number is perfect square or not ; If square root is an integer ; Function to calculate and store all the paths product in vector ; Store the value a [ 0 ] [ 0 ] ; Initialize first row of dp ; Find prefix product ; Initialize first column of dp ; Find the prefix product ; Iterate over range ( 1 , 1 ) to ( N , M ) ; Copy dp [ i - 1 ] [ j ] in top [ ] ; Copy dp [ i ] [ j - 1 ] into left [ ] ; Compute the values of current state and store it in curr [ ] ; Find the product of a [ i ] [ j ] with elements at top [ ] ; Find the product of a [ i ] [ j ] with elements at left [ ] ; Update the current state ; Traverse dp [ m - 1 ] [ n - 1 ] ; Check if perfect square ; Driver code ; Given matrix mat [ ] [ ] ; Function Call ; Print the final count
import java . util . * ; class GFG { static ArrayList < ArrayList < ArrayList < Integer > > > dp ; static int countPaths = 0 ; static boolean isPerfectSquare ( int n ) { double sr = Math . sqrt ( n ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static void countUniquePaths ( int a [ ] [ ] , int m , int n , int ans ) { dp . get ( 0 ) . get ( 0 ) . add ( a [ 0 ] [ 0 ] ) ; for ( int i = 1 ; i < m ; i ++ ) { a [ i ] [ 0 ] *= a [ i - 1 ] [ 0 ] ; dp . get ( i ) . get ( 0 ) . add ( a [ i ] [ 0 ] ) ; } for ( int i = 1 ; i < n ; i ++ ) { a [ 0 ] [ i ] *= a [ 0 ] [ i - 1 ] ; dp . get ( 0 ) . get ( i ) . add ( a [ 0 ] [ i ] ) ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { ArrayList < Integer > top = dp . get ( i - 1 ) . get ( j ) ; ArrayList < Integer > left = dp . get ( i ) . get ( j - 1 ) ; ArrayList < Integer > curr = new ArrayList < > ( ) ; for ( int k = 0 ; k < top . size ( ) ; k ++ ) { curr . add ( top . get ( k ) * a [ i ] [ j ] ) ; } for ( int k = 0 ; k < left . size ( ) ; k ++ ) { curr . add ( left . get ( k ) * a [ i ] [ j ] ) ; } dp . get ( i ) . set ( j , curr ) ; } } for ( Integer i : dp . get ( m - 1 ) . get ( n - 1 ) ) { if ( isPerfectSquare ( i ) ) { countPaths ++ ; } } } public static void main ( String [ ] args ) { int M = 3 , N = 4 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 1 } , { 3 , 1 , 2 , 4 } , { 2 , 3 , 1 , 1 } } ; dp = new ArrayList < > ( ) ; for ( int i = 0 ; i < 105 ; i ++ ) { dp . add ( new ArrayList < > ( ) ) ; for ( int j = 0 ; j < 105 ; j ++ ) dp . get ( i ) . add ( new ArrayList < Integer > ( ) ) ; } countUniquePaths ( mat , M , N , 1 ) ; System . out . println ( countPaths ) ; } }
Generate a combination of minimum coins that sums to a given value | Java program for the above approach ; dp array to memoize the results ; List to store the result ; Function to find the minimum number of coins to make the sum equals to X ; Base case ; If previously computed subproblem occurred ; Initialize result ; Try every coin that has smaller value than n ; Check for Integer . MAX_VALUE to avoid overflow and see if result can be minimized ; Memoizing value of current state ; Function to find the possible combination of coins to make the sum equal to X ; Base Case ; Print Solutions ; Try every coin that has value smaller than n ; Add current denominations ; Backtrack ; Function to find the minimum combinations of coins for value X ; Initialize dp with - 1 ; Min coins ; If no solution exists ; Backtrack to find the solution ; Driver code ; Set of possible denominations ; Function Call
import java . util . * ; class GFG { static final int MAX = 100000 ; static int [ ] dp = new int [ MAX + 1 ] ; static List < Integer > denomination = new LinkedList < Integer > ( ) ; static int countMinCoins ( int n , int C [ ] , int m ) { if ( n == 0 ) { dp [ 0 ] = 0 ; return 0 ; } if ( dp [ n ] != - 1 ) return dp [ n ] ; int ret = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( C [ i ] <= n ) { int x = countMinCoins ( n - C [ i ] , C , m ) ; if ( x != Integer . MAX_VALUE ) ret = Math . min ( ret , 1 + x ) ; } } dp [ n ] = ret ; return ret ; } static void findSolution ( int n , int C [ ] , int m ) { if ( n == 0 ) { for ( int it : denomination ) { System . out . print ( it + " ▁ " ) ; } return ; } for ( int i = 0 ; i < m ; i ++ ) { if ( n - C [ i ] >= 0 && dp [ n - C [ i ] ] + 1 == dp [ n ] ) { denomination . add ( C [ i ] ) ; findSolution ( n - C [ i ] , C , m ) ; break ; } } } static void countMinCoinsUtil ( int X , int C [ ] , int N ) { for ( int i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; int isPossible = countMinCoins ( X , C , N ) ; if ( isPossible == Integer . MAX_VALUE ) { System . out . print ( " - 1" ) ; } else { findSolution ( X , C , N ) ; } } public static void main ( String [ ] args ) { int X = 21 ; int arr [ ] = { 2 , 3 , 4 , 5 } ; int N = arr . length ; countMinCoinsUtil ( X , arr , N ) ; } }
Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal | Java Program to implement the above approach ; Function to calculate Binomial Coefficient C ( n , r ) ; C ( n , r ) = C ( n , n - r ) ; [ n * ( n - 1 ) * -- - * ( n - r + 1 ) ] / [ r * ( r - 1 ) * -- -- * 1 ] ; Function to calculate the total possible paths ; Update n to n - 1 as ( N - 1 ) catalan number is the result ; Stores 2 nCn ; Stores Nth Catalan number ; Stores the required answer ; Driver Code
import java . util . * ; class GFG { static int binCoff ( int n , int r ) { int val = 1 ; int i ; if ( r > ( n - r ) ) { r = ( n - r ) ; } for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static int findWays ( int n ) { n -- ; int a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; ans = b ; return ans ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( findWays ( n ) ) ; } }
Minimum operations to transform given string to another by moving characters to front or end | Java program for the above approach ; Function that finds the minimum number of steps to find the minimum characters must be moved to convert String s to t ; r = maximum value over all dp [ i ] [ j ] computed so far ; dp [ i ] [ j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ] ; Update the maximum length ; Return the resulting length ; Driver Code ; Given String s , t ; Function Call
class GFG { static int [ ] [ ] dp = new int [ 1010 ] [ 1010 ] ; static int solve ( String s , String t ) { int n = s . length ( ) ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; if ( i > 0 ) { dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) ; } if ( s . charAt ( i ) == t . charAt ( j ) ) { int ans = 1 ; if ( i > 0 && j > 0 ) { ans = 1 + dp [ i - 1 ] [ j - 1 ] ; } dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , ans ) ; r = Math . max ( r , dp [ i ] [ j ] ) ; } } } return ( n - r ) ; } public static void main ( String [ ] args ) { String s = " abcde " ; String t = " edacb " ; System . out . print ( solve ( s , t ) ) ; } }
Longest substring whose characters can be rearranged to form a Palindrome | Java program for the above approach ; Function to get the length of longest substring whose characters can be arranged to form a palindromic string ; To keep track of the last index of each xor ; Initialize answer with 0 ; Now iterate through each character of the string ; Convert the character from [ a , z ] to [ 0 , 25 ] ; Turn the temp - th bit on if character occurs odd number of times and turn off the temp - th bit off if the character occurs ever number of times ; If a mask is present in the index Therefore a palindrome is found from index [ mask ] to i ; If x is not found then add its position in the index dict . ; Check for the palindrome of odd length ; We cancel the occurrence of a character if it occurs odd number times ; Driver code ; Given String ; Length of given string ; Function call
import java . util . * ; class GFG { static int longestSubstring ( String s , int n ) { Map < Integer , Integer > index = new HashMap < > ( ) ; int answer = 0 ; int mask = 0 ; index . put ( mask , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( int ) s . charAt ( i ) - 97 ; mask ^= ( 1 << temp ) ; if ( index . containsKey ( mask ) ) { answer = Math . max ( answer , i - index . get ( mask ) ) ; } else index . put ( mask , i ) ; for ( int j = 0 ; j < 26 ; j ++ ) { int mask2 = mask ^ ( 1 << j ) ; if ( index . containsKey ( mask2 ) ) { answer = Math . max ( answer , i - index . get ( mask2 ) ) ; } } } return answer ; } public static void main ( String [ ] args ) { String s = " adbabd " ; int n = s . length ( ) ; System . out . print ( longestSubstring ( s , n ) ) ; } }
Count of N | Java program for the above approach ; Function to find count of N - digit numbers with single digit XOR ; dp [ i ] [ j ] stores the number of i - digit numbers with XOR equal to j ; For 1 - 9 store the value ; Iterate till N ; Calculate XOR ; Store in DP table ; Initialize count ; Print the answer ; Driver Code ; Given number N ; Function Call
class GFG { public static void countNums ( int N ) { int dp [ ] [ ] = new int [ N ] [ 16 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 16 ; k ++ ) { int xor = j ^ k ; dp [ i ] [ xor ] += dp [ i - 1 ] [ k ] ; } } } int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) count += dp [ N - 1 ] [ i ] ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 1 ; countNums ( N ) ; } }