codeparrot/xlcost-text-to-code huggingface dataset:
Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | Java implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
import java . util . * ; class GFG { static int ans ; static void subsetSum ( Vector < Integer > c ) { int L = c . size ( ) ; int mul = ( int ) Math . pow ( 2 , L - 1 ) ; for ( int i = 0 ; i < c . size ( ) ; i ++ ) ans += c . get ( i ) * mul ; } static void subsetGen ( int [ ] arr , int i , int n , Vector < Integer > c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . remove ( 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 } ; int n = arr . length ; Vector < Integer > c = new Vector < Integer > ( ) ; subsetGen ( arr , 0 , n , c ) ; System . out . println ( ans ) ; } }
Find the maximum possible value of a [ i ] % a [ j ] over all pairs of i and j | Java implementation of the approach ; Function that returns the second largest element in the array if exists , else 0 ; There must be at least two elements ; To store the maximum and the second maximum element from the array ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; No second maximum found ; Driver code
class GFG { static int getMaxValue ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { return 0 ; } first = second = Integer . MIN_VALUE ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) { second = arr [ i ] ; } } if ( second == Integer . MIN_VALUE ) { return 0 ; } else { return second ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 1 , 8 } ; int n = arr . length ; System . out . println ( getMaxValue ( arr , n ) ) ; } }
Maximize the value of the given expression | Java implementation of the approach ; Function to return the maximum result ; To store the count of negative integers ; Sum of all the three integers ; Product of all the three integers ; To store the smallest and the largest among all the three integers ; Calculate the count of negative integers ; Depending upon count of negatives ; When all three are positive integers ; For single negative integer ; For two negative integers ; For three negative integers ; Driver Code
class GFG { static int maximumResult ( int a , int b , int c ) { int countOfNegative = 0 ; int sum = a + b + c ; int product = a * b * c ; int largest = ( a > b ) ? ( ( a > c ) ? a : c ) : ( ( b > c ) ? b : c ) ; int smallest = ( a < b ) ? ( ( a < c ) ? a : c ) : ( ( b < c ) ? b : c ) ; if ( a < 0 ) countOfNegative ++ ; if ( b < 0 ) countOfNegative ++ ; if ( c < 0 ) countOfNegative ++ ; switch ( countOfNegative ) { case 0 : return ( sum - largest ) * largest ; case 1 : return ( product / smallest ) + smallest ; case 2 : return ( product / largest ) + largest ; case 3 : return ( sum - smallest ) * smallest ; } return - 1 ; } public static void main ( String [ ] args ) { int a = - 2 , b = - 1 , c = - 4 ; System . out . print ( maximumResult ( a , b , c ) ) ; } }
Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | Java Implementation of above approach . ; Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code
import java . util . * ; class GFG { static int check ( int n , List < Integer > marks ) { Integer x = Collections . max ( marks ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks . get ( i ) + bonus >= 50 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 5 ; List < Integer > marks = Arrays . asList ( 0 , 21 , 83 , 45 , 64 ) ; System . out . println ( check ( n , marks ) ) ; } }
Sum of first N natural numbers which are not powers of K | Java program to find the sum of first n natural numbers which are not positive powers of k ; Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code
import java . io . * ; class GFG { static int find_sum ( int n , int k ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int power = k ; while ( power <= n ) { total_sum -= power ; power *= k ; } return total_sum ; } public static void main ( String [ ] args ) { int n = 11 , k = 2 ; System . out . println ( find_sum ( n , k ) ) ; } }
Minimum number of operations required to delete all elements of the array | Java implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program
import java . util . * ; class Solution { static final int MAX = 10000 ; static int hashTable [ ] = new int [ MAX ] ; static int minOperations ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) hashTable [ arr [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] != 0 ) { for ( int j = i ; j < n ; j ++ ) if ( arr [ j ] % arr [ i ] == 0 ) hashTable [ arr [ j ] ] = 0 ; res ++ ; } } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 } ; int n = arr . length ; System . out . print ( minOperations ( arr , n ) ) ; } }
Sorting array with reverse around middle | Java program to find possibility to sort by multiple subarray reverse operation ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; driver code
import java . util . * ; class GFG { static boolean ifPossible ( int arr [ ] , int n ) { int copy [ ] = Arrays . copyOf ( arr , arr . length ) ; Arrays . sort ( copy ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == copy [ i ] ) && ! ( arr [ n - 1 - i ] == copy [ i ] ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 } ; int n = arr . length ; if ( ifPossible ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimum deletions in Array to make difference of adjacent elements non | Java program for the above approach ; the maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; initializing the dp table and setting all values to 0 ; Find the maximum size valid set that can be taken and then subtract its size from N to get minimum number of deletions ; when selecting only current element and deleting all elements from 0 to i - 1 inclusive ; if this is true moving from index j to i is possible ; iterate over all elements from 0 to diff and find the max ; take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call
import java . io . * ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int A [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ MAX ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; for ( int k = 0 ; k <= diff ; k ++ ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] [ k ] + 1 ) ; } } } } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . max ( maxSetSize , dp [ N - 1 ] [ i ] ) ; return N - maxSetSize ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . length ; System . out . println ( minimumDeletions ( A , N ) ) ; } }
Count N | Java program for the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Initializing vector to store count of strings . ; Summing up the total number of combinations . ; Return the result ; Function Call
import java . util . * ; public class Main { static int findNumberOfStrings ( int N ) { Vector < Integer > counts = new Vector < Integer > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) { counts . add ( 1 ) ; } for ( int i = 2 ; i <= N ; i ++ ) { for ( int j = 3 ; j >= 0 ; j -- ) counts . set ( j , counts . get ( j ) + counts . get ( j + 1 ) ) ; } int ans = 0 ; for ( Integer c : counts ) ans += c ; return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( findNumberOfStrings ( N ) ) ; } }
Count N | Java program for the above approach ; Stores the value of overlapping states ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Driver Code ; Function Call
import java . util . * ; public class Main { static int dp [ ] [ ] = new int [ 1000 + 5 ] [ 9000 + 5 ] ; static int mod = 1000000007 ; public static int countNumbers ( int n , int x , int y , int sum ) { for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; if ( n == 0 ) { return check ( sum , x , y ) ; } if ( dp [ n ] [ sum ] != - 1 ) { return dp [ n ] [ sum ] % mod ; } int option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod ; int option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod ; return dp [ n ] [ sum ] = ( option1 + option2 ) % mod ; } public static int check ( int sum , int x , int y ) { while ( sum > 0 ) { int ln = sum % 10 ; if ( ln != x && ln != y ) { return 0 ; } sum /= 10 ; } return 1 ; } public static void main ( String args [ ] ) { int N = 3 , X = 1 , Y = 5 ; System . out . println ( countNumbers ( N , X , Y , 0 ) % mod ) ; } }
Minimum cost to generate any permutation of the given string | Java program for the above approach ; Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that returns true if the current bit is set ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call
import java . util . * ; public class Main { public static int solve ( int a [ ] [ ] , String s , int n , int prev , int mask , int [ ] [ ] dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask ] [ prev + 1 ] != - 1 ) return dp [ mask ] [ prev + 1 ] ; int ans = 10000 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s . charAt ( i ) - ' a ' ; if ( check ( mask , id ) ) { if ( prev == - 1 ) { ans = Math . min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = Math . min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask ] [ prev + 1 ] = ans ; return ans ; } public static boolean check ( int mask , int i ) { int c = ( mask & ( 1 << i ) ) ; return c != 0 ; } static void generatePermutation ( int mask , int n , int a [ ] [ ] , String s ) { int dp [ ] [ ] = new int [ ( 1 << n ) + 5 ] [ n + 5 ] ; for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s . charAt ( i ) - ' a ' ; mask |= ( 1 << id ) ; } System . out . println ( solve ( a , s , n , - 1 , mask , dp ) ) ; } public static void main ( String args [ ] ) { int N = 5 ; String str = " abcde " ; int mat [ ] [ ] = { { 0 , 5 , 1 , 5 , 3 } , { 4 , 0 , 9 , 4 , 2 } , { 7 , 9 , 0 , 10 , 7 } , { 1 , 2 , 8 , 0 , 2 } , { 3 , 9 , 7 , 7 , 0 } } ; generatePermutation ( 0 , N , mat , str ) ; } }
Minimize Cost to reduce the Array to a single element by given operations | Java Program for the above approach ; Function to minimize the cost to add the array elements to a single element ; Check if the value is already stored in the array ; Compute left subproblem ; Compute left subproblem ; Calculate minimum cost ; Store the answer to avoid recalculation ; Function to generate the cost using Prefix Sum Array technique ; Function to combine the sum of the two subproblems ; Driver Code ; Initialise dp array ; Preprocessing the array
import java . util . * ; public class Main { static int inf = 10000000 ; public static int minCost ( int a [ ] , int i , int j , int k , int [ ] prefix , int [ ] [ ] dp ) { if ( i >= j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; int best_cost = inf ; for ( int pos = i ; pos < j ; pos ++ ) { int left = minCost ( a , i , pos , k , prefix , dp ) ; int right = minCost ( a , pos + 1 , j , k , prefix , dp ) ; best_cost = Math . min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) ; } return dp [ i ] [ j ] = best_cost ; } public static int [ ] preprocess ( int [ ] a , int n ) { int p [ ] = new int [ n ] ; p [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i ] = p [ i - 1 ] + a [ i ] ; return p ; } public static int Combine ( int [ ] p , int i , int j ) { if ( i == 0 ) return p [ j ] ; else return p [ j ] - p [ i - 1 ] ; } public static void main ( String args [ ] ) { int n = 4 ; int a [ ] = { 4 , 5 , 6 , 7 } ; int k = 3 ; int dp [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; int prefix [ ] = preprocess ( a , n ) ; System . out . println ( minCost ( a , 0 , n - 1 , k , prefix , dp ) ) ; } }
Minimize operations required to obtain N | Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initilal state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minm operations to obtain n ; Driver Code
class GFG { static int minOperations ( int n ) { int dp [ ] = new int [ n + 1 ] ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = Integer . MAX_VALUE ; if ( i % 2 == 0 ) { int x = dp [ i / 2 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { int x = dp [ i / 3 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } int x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( minOperations ( n ) ) ; } }
Longest Reverse Bitonic Sequence | Java program to find the longest Reverse bitonic Subsequence ; Function to return the length of the Longest Reverse Bitonic Subsequence in the array ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right for every index ; Initialize LDS for all indexes as 1 ; Compute LDS values for every index from right to left ; Find the maximum value of lis [ i ] + lds [ i ] - 1 in the array ; Return the maximum ; Driver code
import java . io . * ; class GFG { static int ReverseBitonic ( int arr [ ] , int N ) { int i , j ; int [ ] lds = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lds [ i ] = 1 ; } for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < i ; j ++ ) { if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) { lds [ i ] = lds [ j ] + 1 ; } } } int [ ] lis = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( i = N - 2 ; i >= 0 ; i -- ) { for ( j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] < arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } int max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < N ; i ++ ) { if ( lis [ i ] + lds [ i ] - 1 > max ) { max = lis [ i ] + lds [ i ] - 1 ; } } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ; int N = arr . length ; System . out . println ( " Length ▁ of ▁ LBS ▁ is ▁ " + ReverseBitonic ( arr , N ) ) ; } }
Maze With N doors and 1 Key | Java implementation of the approach ; Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; If current cell is the destination ; Either go down or right ; Driver code ; We are making these { { '0' , '0' , '1' } , { '1' , '0' , '1' } , { '1' , '1' , '0' } } ; ; If there is a path from the cell ( 0 , 0 )
import java . io . * ; import java . util . ArrayList ; class GFG { static boolean findPath ( ArrayList < ArrayList < Integer > > maze , int xpos , int ypos , boolean key ) { if ( xpos < 0 || xpos >= maze . size ( ) || ypos < 0 || ypos >= maze . size ( ) ) return false ; if ( maze . get ( xpos ) . get ( ypos ) == '1' ) { if ( key == true ) if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; } if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } static boolean mazeProb ( ArrayList < ArrayList < Integer > > maze , int xpos , int ypos ) { boolean key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } public static void main ( String [ ] args ) { int size = 3 ; ArrayList < ArrayList < Integer > > maze = new ArrayList < ArrayList < Integer > > ( size ) ; for ( int i = 0 ; i < size ; i ++ ) { maze . add ( new ArrayList < Integer > ( ) ) ; } maze . get ( 0 ) . add ( 0 ) ; maze . get ( 0 ) . add ( 0 ) ; maze . get ( 0 ) . add ( 1 ) ; maze . get ( 1 ) . add ( 1 ) ; maze . get ( 1 ) . add ( 0 ) ; maze . get ( 1 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 0 ) ; if ( mazeProb ( maze , 0 , 0 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Java implementation of the approach ; Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code
import java . util . * ; class GfG { static int FindMinimumCost ( int ind , int a [ ] , int n , int k , int dp [ ] ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != - 1 ) return dp [ ind ] ; else { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = Math . min ( ans , Math . abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } public static void main ( String [ ] args ) { int a [ ] = { 10 , 30 , 40 , 50 , 20 } ; int k = 3 ; int n = a . length ; int dp [ ] = new int [ n ] ; Arrays . fill ( dp , - 1 ) ; System . out . println ( FindMinimumCost ( 0 , a , n , k , dp ) ) ; } }
Sum of all even factors of numbers in the range [ l , r ] | Java implementation of the approach ; Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code
class GFG { static final int MAX = 100000 ; static long prefix [ ] = new long [ MAX ] ; static void sieve_modified ( ) { for ( int i = 2 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } static long sumEvenFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void main ( String args [ ] ) { sieve_modified ( ) ; int l = 6 , r = 10 ; System . out . print ( sumEvenFactors ( l , r ) ) ; } }
Count of numbers between range having only non | Java implementation of the approach ; states - position , sum , rem , tight sum can have values upto 162 , if we are dealing with numbers upto 10 ^ 18 when all 18 digits are 9 , then sum is 18 * 9 = 162 ; n is the sum of digits and number should be divisible by m ; Function to return the count of required numbers from 0 to num ; Last position ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is zero and nonz is 1 , we can 't place it ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver code
import java . util . * ; class GFG { static int M = 20 ; static int dp [ ] [ ] [ ] [ ] = new int [ M ] [ 165 ] [ M ] [ 2 ] ; static int n , m ; static int count ( int pos , int sum , int rem , int tight , int nonz , Vector < Integer > num ) { if ( pos == num . size ( ) ) { if ( rem == 0 && sum == n ) return 1 ; return 0 ; } if ( dp [ pos ] [ sum ] [ rem ] [ tight ] != - 1 ) return dp [ pos ] [ sum ] [ rem ] [ tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num . get ( pos ) ) ; for ( int d = 0 ; d <= limit ; d ++ ) { if ( d == 0 && nonz != 0 ) continue ; int currSum = sum + d ; int currRem = ( rem * 10 + d ) % m ; int currF = ( tight != 0 || ( d < num . get ( pos ) ) ) ? 1 : 0 ; ans += count ( pos + 1 , currSum , currRem , currF , ( nonz != 0 d != 0 ) ? 1 : 0 , num ) ; } return dp [ pos ] [ sum ] [ rem ] [ tight ] = ans ; } static int solve ( int x ) { Vector < Integer > num = new Vector < Integer > ( ) ; while ( x != 0 ) { num . add ( x % 10 ) ; x /= 10 ; } Collections . reverse ( num ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < 165 ; j ++ ) for ( int k = 0 ; k < M ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; return count ( 0 , 0 , 0 , 0 , 0 , num ) ; } public static void main ( String args [ ] ) { int L = 1 , R = 100 ; n = 8 ; m = 2 ; System . out . print ( solve ( R ) - solve ( L ) ) ; } }