codeparrot/xlcost-text-to-code huggingface dataset:
Count minimum factor jumps required to reach the end of an Array | Java code to count minimum factor jumps to reach the end of array ; vector to store factors of each integer ; dp array ; Precomputing all factors of integers from 1 to 100000 ; Function to count the minimum jumps ; If we reach the end of array , no more jumps are required ; If the jump results in out of index , return Integer . MAX_VALUE ; If the answer has been already computed , return it directly ; Else compute the answer using the recurrence relation ; Iterating over all choices of jumps ; Considering current factor as a jump ; Jump leads to the destination ; Return ans and memorize it ; Driver code ; pre - calculating the factors
import java . util . * ; class GFG { static Vector < Integer > [ ] factors = new Vector [ 100005 ] ; static int [ ] dp = new int [ 100005 ] ; static void precompute ( ) { for ( int i = 0 ; i < factors . length ; i ++ ) factors [ i ] = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) { factors [ j ] . add ( i ) ; } } } static int solve ( int arr [ ] , int k , int n ) { if ( k == n - 1 ) { return 0 ; } if ( k >= n ) { return Integer . MAX_VALUE ; } if ( dp [ k ] != 0 ) { return dp [ k ] ; } int ans = Integer . MAX_VALUE ; for ( int j : factors [ arr [ k ] ] ) { int res = solve ( arr , k + j , n ) ; if ( res != Integer . MAX_VALUE ) { ans = Math . min ( ans , res + 1 ) ; } } return dp [ k ] = ans ; } public static void main ( String [ ] args ) { precompute ( ) ; int arr [ ] = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = arr . length ; System . out . print ( solve ( arr , 0 , n ) ) ; } }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Java Program to find the maximum distance from a 0 - cell to a 1 - cell ; If the matrix consists of only 0 ' s ▁ ▁ or ▁ only ▁ 1' s ; If it 's a 0-cell ; calculate its distance with every 1 - cell ; Compare and store the minimum ; Compare ans store the maximum ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ ] [ ] grid ) { Vector < pair > one = new Vector < pair > ( ) ; int M = grid . length ; int N = grid [ 0 ] . length ; int ans = - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) one . add ( new pair ( i , j ) ) ; } } if ( one . isEmpty ( ) || M * N == one . size ( ) ) return - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) continue ; int dist = Integer . MAX_VALUE ; for ( pair p : one ) { int d = Math . abs ( p . first - i ) + Math . abs ( p . second - j ) ; dist = Math . min ( dist , d ) ; if ( dist <= ans ) break ; } ans = Math . max ( ans , dist ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; System . out . print ( maxDistance ( arr ) + "NEW_LINE"); } }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Java program to find the maximum distance from a 0 - cell to a 1 - cell ; Function to find the maximum distance ; Queue to store all 1 - cells ; Grid dimensions ; Directions traversable from a given a particular cell ; If the grid contains only 0 s or only 1 s ; Access every 1 - cell ; Traverse all possible directions from the cells ; Check if the cell is within the boundaries or contains a 1 ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ ] [ ] grid ) { Queue < pair > q = new LinkedList < pair > ( ) ; int M = grid . length ; int N = grid [ 0 ] . length ; int ans = - 1 ; int dirs [ ] [ ] = { { 0 , 1 } , { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } } ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) q . add ( new pair ( i , j ) ) ; } } if ( q . isEmpty ( ) || M * N == q . size ( ) ) return - 1 ; while ( q . size ( ) > 0 ) { int cnt = q . size ( ) ; while ( cnt -- > 0 ) { pair p = q . peek ( ) ; q . remove ( ) ; for ( int [ ] dir : dirs ) { int x = p . first + dir [ 0 ] ; int y = p . second + dir [ 1 ] ; if ( x < 0 x >= M y < 0 y >= N grid [ x ] [ y ] > 0 ) continue ; q . add ( new pair ( x , y ) ) ; grid [ x ] [ y ] = 1 ; } } ++ ans ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 1 } } ; System . out . print ( maxDistance ( arr ) + "NEW_LINE"); } }
Count the number of ways to give ranks for N students such that same ranks are possible | Java program to calculate the number of ways to give ranks for N students such that same ranks are possible ; Initializing a table in order to store the bell triangle ; Function to calculate the K - th bell number ; If we have already calculated the bell numbers until the required N ; Base case ; First Bell Number ; If the value of the bell triangle has already been calculated ; Fill the defined dp table ; Function to return the number of ways to give ranks for N students such that same ranks are possible ; Resizing the dp table for the given value of n ; Variables to store the answer and the factorial value ; Iterating till N ; Simultaneously calculate the k ! ; Computing the K - th bell number and multiplying it with K ! ; Driver code
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ ] [ ] dp ; static int f ( int n , int k ) { if ( n < k ) return 0 ; if ( n == k ) return 1 ; if ( k == 1 ) return 1 ; if ( dp [ n ] [ k ] != - 1 ) return dp [ n ] [ k ] ; return dp [ n ] [ k ] = ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ; } static long operation ( int n ) { dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } long ans = 0 , fac = 1 ; for ( int k = 1 ; k <= n ; k ++ ) { fac *= k ; ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( operation ( n ) + "NEW_LINE"); } }
Count the number of ordered sets not containing consecutive numbers | Java program to count the number of ordered sets not containing consecutive numbers ; Function to calculate the count of ordered set for a given size ; Base cases ; Function returns the count of all ordered sets ; Prestore the factorial value ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive ; Multiply ny size ! for all the arrangements because sets are ordered ; Add to total answer ; Driver code
class GFG { static int CountSets ( int x , int pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; int answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return answer ; } static int CountOrderedSets ( int n ) { int [ ] factorial = new int [ 10000 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( CountOrderedSets ( N ) ) ; } }
Maximum sum such that exactly half of the elements are selected and no two adjacent | Java program to find maximum sum possible such that exactly Math . floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Intitialising the dp table ; Base case ; Condition to select the current element ; Condition to not select the current element if possible ; Driver code
class GFG { static int MaximumSum ( int a [ ] , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) dp [ i ] [ j ] = Integer . MIN_VALUE ; } for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int val = Integer . MIN_VALUE ; if ( ( i - 2 >= 0 && dp [ i - 2 ] [ j - 1 ] != Integer . MIN_VALUE ) i - 2 < 0 ) { val = a [ i - 1 ] + ( i - 2 >= 0 ? dp [ i - 2 ] [ j - 1 ] : 0 ) ; } if ( i - 1 >= j ) { val = Math . max ( val , dp [ i - 1 ] [ j ] ) ; } dp [ i ] [ j ] = val ; } } return dp [ n ] [ n / 2 ] ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . length ; System . out . print ( MaximumSum ( A , N ) ) ; } }
Check if N can be converted to the form K power K by the given operation | Java implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
import java . util . * ; class GFG { static HashSet < Integer > kPowKform = new HashSet < Integer > ( ) ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . contains ( n ) ) return 1 ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) == 1 ) { answer = 1 ; break ; } } x /= 10 ; } return answer ; } static void canBeConverted ( int n ) { if ( func ( n ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 90 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ; } }
Count of subarrays of an Array having all unique digits | Java program to find the count of subarrays of an Array having all unique digits ; Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
import java . util . * ; class GFG { static boolean check ( Vector < Integer > v ) { HashSet < Integer > digits = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { HashSet < Integer > d = new HashSet < Integer > ( ) ; while ( v . get ( i ) > 0 ) { d . add ( v . get ( i ) % 10 ) ; v . set ( i , v . get ( i ) / 10 ) ; } for ( int it : d ) { if ( digits . contains ( it ) ) return false ; } for ( int it : d ) digits . add ( it ) ; } return true ; } static int numberOfSubarrays ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { Vector < Integer > temp = new Vector < Integer > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) temp . add ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 1 , 12 , 23 , 34 } ; System . out . print ( numberOfSubarrays ( A , N ) ) ; } }
Find the row whose product has maximum count of prime factors | Java implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a boolean array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code
import java . util . * ; class GFG { static final int N = 3 ; static final int M = 5 ; static int Large = ( int ) 1e6 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] isPrime = new boolean [ Large + 1 ] ; Arrays . fill ( isPrime , true ) ; for ( int p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . add ( p ) ; } static void Display ( int arr [ ] [ ] , int row ) { for ( int i = 0 ; i < M ; i ++ ) System . out . print ( arr [ row ] [ i ] + " ▁ " ) ; } static void countDivisorsMult ( int arr [ ] [ ] ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int row_no = 0 ; long max_factor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int no = arr [ i ] [ j ] ; for ( int k = 0 ; k < prime . size ( ) ; k ++ ) { while ( no > 1 && no % prime . get ( k ) == 0 ) { no /= prime . get ( k ) ; if ( mp . containsKey ( prime . get ( k ) ) ) mp . put ( prime . get ( k ) , prime . get ( k ) + 1 ) ; else mp . put ( prime . get ( k ) , 1 ) ; } if ( no == 1 ) break ; } } int res = 1 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { res *= ( it . getValue ( ) + 1L ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . clear ( ) ; } Display ( arr , row_no ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 10 , 23 } , { 4 , 5 , 6 , 7 , 8 } , { 7 , 8 , 9 , 15 , 45 } } ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ; } }
Smallest N digit number whose sum of square of digits is a Perfect Square | Java implementation to find Smallest N digit number whose sum of square of digits is a Perfect Square ; function to check if number is a perfect square ; Function to calculate the smallest N digit number ; Place digits greater than equal to prev ; Check if placing this digit leads to a solution then return it ; Else backtrack ; Create a string representing the N digit number ; Driver code ; Initialise N
class GFG { static int isSquare ( int n ) { int k = ( int ) Math . sqrt ( n ) ; return k * k == n ? 1 : 0 ; } static int calculate ( int pos , int prev , int sum , int [ ] v ) { if ( pos == v . length ) return isSquare ( sum ) ; for ( int i = prev ; i <= 9 ; i ++ ) { v [ pos ] = i ; sum += i * i ; if ( calculate ( pos + 1 , i , sum , v ) != 0 ) return 1 ; sum -= i * i ; } return 0 ; } static String minValue ( int n ) { int [ ] v = new int [ n ] ; if ( calculate ( 0 , 1 , 0 , v ) != 0 ) { String answer = " " ; for ( int i = 0 ; i < v . length ; i ++ ) answer += ( char ) ( v [ i ] + '0' ) ; return answer ; } else return " - 1" ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( minValue ( N ) ) ; } }
Sum of GCD of all possible sequences | Java implementation of the above approach ; A recursive function that generates all the sequence and find GCD ; If we reach the sequence of length N g is the GCD of the sequence ; Initialise answer to 0 ; Placing all possible values at this position and recursively find the GCD of the sequence ; Take GCD of GCD calculated uptill now i . e . g with current element ; Take modulo to astatic void overflow ; Return the final answer ; Function that finds the sum of GCD of all the subsequence of N length ; Recursive function that generates the sequence and return the GCD ; Driver Code ; Function Call
class GFG { static int MOD = ( int ) 1e9 + 7 ; static int calculate ( int pos , int g , int n , int k ) { if ( pos == n ) { return g ; } int answer = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { answer = ( answer % MOD + calculate ( pos + 1 , __gcd ( g , i ) , n , k ) % MOD ) ; answer %= MOD ; } return answer ; } static int sumofGCD ( int n , int k ) { return calculate ( 0 , 0 , n , k ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; System . out . print ( sumofGCD ( N , K ) ) ; } }
Sum of GCD of all possible sequences | Java implementation of the above approach ; Function to find a ^ b in log ( b ) ; Function that finds the sum of GCD of all the subsequence of N length ; To stores the number of sequences with gcd i ; Find contribution of each gcd to happen ; To count multiples ; possible sequences with overcounting ; to astatic void overflow ; Find extra element which will not form gcd = i ; Find overcounting ; Remove the overcounting ; To store the final answer ; Return Final answer ; Driver Code ; Function call
class GFG { static int MOD = ( int ) 1e9 + 7 ; static int fastexpo ( int a , int b ) { int res = 1 ; a %= MOD ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res * a ) % MOD ; a *= a ; a %= MOD ; b >>= 1 ; } return res ; } static int sumofGCD ( int n , int k ) { int [ ] count = new int [ k + 1 ] ; for ( int g = k ; g >= 1 ; g -- ) { int count_multiples = k / g ; int temp ; temp = fastexpo ( count_multiples , n ) ; temp %= MOD ; int extra = 0 ; for ( int j = g * 2 ; j <= k ; j += g ) { extra = ( extra + count [ j ] ) ; extra %= MOD ; } count [ g ] = ( temp - extra + MOD ) ; count [ g ] %= MOD ; } int sum = 0 ; int add ; for ( int i = 1 ; i <= k ; ++ i ) { add = ( count [ i ] % MOD * i % MOD ) ; add %= MOD ; sum += add ; sum %= MOD ; } return sum ; } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; System . out . print ( sumofGCD ( N , K ) ) ; } }
Split the given string into Odds : Digit DP | Java program to split the given string into odds ; Function to check whether a string is an odd number or not ; A function to find the minimum number of segments the given string can be divided such that every segment is a odd number ; Declare a splitdp [ ] array and initialize to - 1 ; Build the DP table in a bottom - up manner ; Initially Check if the entire prefix is odd ; If the Given Prefix can be split into Odds then for the remaining string from i to j Check if Odd . If yes calculate the minimum split till j ; To check if the substring from i to j is a odd number or not ; If it is an odd number , then update the dp array ; Return the minimum number of splits for the entire string ; Driver code
class GFG { static int checkOdd ( String number ) { int n = number . length ( ) ; int num = number . charAt ( n - 1 ) - '0' ; return ( num & 1 ) ; } static int splitIntoOdds ( String number ) { int numLen = number . length ( ) ; int splitDP [ ] = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . substring ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . substring ( i , i + j ) ) == 1 ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . println ( splitIntoOdds ( "123456789123456789123" ) ) ; } }
Split the given string into Primes : Digit DP | Java implementation of the above approach ; Function to check whether a String is a prime number or not ; A recursive function to find the minimum number of segments the given String can be divided such that every segment is a prime ; If the number is null ; checkPrime function is called to check if the number is a prime or not . ; A very large number denoting maximum ; Consider a minimum of 6 and length since the primes are less than 10 ^ 6 ; Recursively call the function to check for the remaining String ; Evaluating minimum splits into Primes for the suffix ; Checks if no combination found ; Driver code
import java . util . * ; class GFG { static boolean checkPrime ( String number ) { int num = Integer . valueOf ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { if ( number . length ( ) == 0 ) return 0 ; if ( number . length ( ) <= 6 && checkPrime ( number ) ) return 1 ; else { int numLen = number . length ( ) ; int ans = 1000000 ; for ( int i = 1 ; i <= 6 && i <= numLen ; i ++ ) { if ( checkPrime ( number . substring ( 0 , i ) ) ) { int val = splitIntoPrimes ( number . substring ( i ) ) ; if ( val != - 1 ) { ans = Math . min ( ans , 1 + val ) ; } } } if ( ans == 1000000 ) return - 1 ; return ans ; } } public static void main ( String [ ] args ) { System . out . print ( splitIntoPrimes ( "13499315" ) + "NEW_LINE"); System . out . print ( splitIntoPrimes ( "43" ) + "NEW_LINE"); } }
Find the numbers from 1 to N that contains exactly k non | Java implementation of the above approach ; Function to find number less than N having k non - zero digits ; Store the memorised values ; Initialise ; Base ; Calculate all states For every state , from numbers 1 to N , the count of numbers which contain exactly j non zero digits is being computed and updated in the dp array . ; Return the required answer ; Driver code ; Function call
class Geeks { static int k_nonzero_numbers ( String s , int n , int k ) { int dp [ ] [ ] [ ] = new int [ n + 1 ] [ 2 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int x = 0 ; x <= k ; x ++ ) dp [ i ] [ j ] [ x ] = 0 ; dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int sm = 0 ; while ( sm < 2 ) { for ( int j = 0 ; j < k + 1 ; ++ j ) { int x = 0 ; while ( x <= ( sm != 0 ? 9 : s . charAt ( i ) - '0' ) ) { if ( j + ( x > 0 ? 1 : 0 ) < k + 1 ) { dp [ i + 1 ] [ ( sm != 0 x < ( s . charAt ( i ) - '0' ) ) ? 1 : 0 ] [ j + ( x > 0 ? 1 : 0 ) ] += dp [ i ] [ sm ] [ j ] ; } ++ x ; } } ++ sm ; } } return dp [ n ] [ 0 ] [ k ] + dp [ n ] [ 1 ] [ k ] ; } public static void main ( String [ ] args ) { String s = "25" ; int k = 2 ; int n = s . length ( ) ; System . out . println ( k_nonzero_numbers ( s , n , k ) ) ; } }
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | Java implementation to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequencies of subsequence ; Loop to find the frequencies of subsequence of length 1 ; Loop to find the frequencies subsequence of length 2 ; Finding maximum frequency ; Driver Code
import java . util . * ; class GFG { static int maximumOccurrence ( String s ) { int n = s . length ( ) ; HashMap < String , Integer > freq = new HashMap < String , Integer > ( ) ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { String temp = " " ; temp += s . charAt ( i ) ; if ( freq . containsKey ( temp ) ) { freq . put ( temp , freq . get ( temp ) + 1 ) ; } else { freq . put ( temp , 1 ) ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { String temp = " " ; temp += s . charAt ( i ) ; temp += s . charAt ( j ) ; if ( freq . containsKey ( temp ) ) freq . put ( temp , freq . get ( temp ) + 1 ) ; else freq . put ( temp , 1 ) ; } } int answer = Integer . MIN_VALUE ; for ( int it : freq . values ( ) ) answer = Math . max ( answer , it ) ; return answer ; } public static void main ( String [ ] args ) { String s = " xxxyy " ; System . out . print ( maximumOccurrence ( s ) ) ; } }