codeparrot/xlcost-text-to-code huggingface dataset:
Iterative program to count leaf nodes in a Binary Tree | Java program to count leaf nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; Helper function that allocates a new Node with the given data and null left and right pointers . ; Driver Code ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static int getLeafCount ( Node node ) { if ( node == null ) { return 0 ; } Queue < Node > q = new LinkedList < > ( ) ; int count = 0 ; q . add ( node ) ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } if ( temp . left == null && temp . right == null ) { count ++ ; } } return count ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void main ( String [ ] args ) { { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; System . out . println ( getLeafCount ( root ) ) ; } } }
Find the Kth smallest element in the sorted generated array | Java implementation of the approach ; Pair class implementation to use Pair ; Function to return the Kth element in B [ ] ; Initialize the count Array ; Reduce N repeatedly to half its value ; Add count to start ; Subtract same count after end index ; Store each element of Array [ ] with their count ; If Kth element is in range of element [ i ] return element [ i ] ; If K is out of bound ; Driver code
import java . util . Vector ; class GFG { static class Pair { private int first ; private int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } public int getFirst ( ) { return first ; } public int getSecond ( ) { return second ; } } static int solve ( int [ ] Array , int N , int K ) { int [ ] count_Arr = new int [ N + 2 ] ; int factor = 1 ; int size = N ; while ( size > 0 ) { int start = 1 ; int end = size ; count_Arr [ 1 ] += factor * N ; count_Arr [ end + 1 ] -= factor * N ; factor ++ ; size /= 2 ; } for ( int i = 2 ; i <= N ; i ++ ) count_Arr [ i ] += count_Arr [ i - 1 ] ; Vector < Pair > element = new Vector < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Pair x = new Pair ( Array [ i ] , count_Arr [ i + 1 ] ) ; element . add ( x ) ; } int start = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int end = start + element . elementAt ( 0 ) . getSecond ( ) - 1 ; if ( K >= start && K <= end ) return element . elementAt ( i ) . getFirst ( ) ; start += element . elementAt ( i ) . getSecond ( ) ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 1 } ; int N = arr . length ; int K = 13 ; System . out . println ( solve ( arr , N , K ) ) ; } }
Find the lexicographical next balanced bracket sequence | Java implementation of the approach ; makes a string containing char d c number of times ; Function to find the lexicographically next balanced bracket expression if possible ; Decrement the depth for every opening bracket ; Increment for the closing brackets ; Last opening bracket ; Generate the required String ; Driver code
class Sol { static String string ( int c , char d ) { String s = " " ; for ( int i = 0 ; i < c ; i ++ ) s += d ; return s ; } static String next_balanced_sequence ( String s ) { String next = " - 1" ; int length = s . length ( ) ; int depth = 0 ; for ( int i = length - 1 ; i >= 0 ; -- i ) { if ( s . charAt ( i ) == ' ( ' ) depth -- ; else depth ++ ; if ( s . charAt ( i ) == ' ( ' && depth > 0 ) { depth -- ; int open = ( length - i - 1 - depth ) / 2 ; int close = length - i - 1 - open ; next = s . substring ( 0 , i ) + ' ) ' + string ( open , ' ( ' ) + string ( close , ' ) ' ) ; break ; } } return next ; } public static void main ( String args [ ] ) { String s = " ( ( ( ) ) ) " ; System . out . println ( next_balanced_sequence ( s ) ) ; } }
Maximum number that can be display on Seven Segment Display using N segments | Java implementation of above approach ; Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver Code
class GFG { public static void printMaxNumber ( int n ) { if ( n % 2 != 0 ) { System . out . print ( "7" ) ; for ( int i = 0 ; i < ( n - 3 ) / 2 ; i ++ ) System . out . print ( "1" ) ; } else { for ( int i = 0 ; i < n / 2 ; i ++ ) System . out . print ( "1" ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printMaxNumber ( n ) ; } }
Find the sum of digits of a number at even and odd places | Java implementation of the approach ; Function to find the sum of the odd and even positioned digits in a number ; To store the respective sum ; Converting integer to String ; Traversing the String ; Driver code
import java . util . * ; class GFG { static void getSum ( int n ) { int sumOdd = 0 ; int sumEven = 0 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) if ( i % 2 == 0 ) sumOdd = sumOdd + ( num . charAt ( i ) - '0' ) ; else sumEven = sumEven + ( num . charAt ( i ) - '0' ) ; System . out . println ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; System . out . println ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void main ( String [ ] args ) { int n = 457892 ; getSum ( n ) ; } }
Iterative program to count leaf nodes in a Binary Tree | Java Program for above approach ; Node class ; Program to count leaves ; If the node itself is " null " return 0 , as there are no leaves ; It the node is a leaf then both right and left children will be " null " ; Now we count the leaves in the left and right subtrees and return the sum ; Class newNode of Node type ; Driver Code ; get leaf count of the above created tree
import java . util . LinkedList ; import java . util . Queue ; import java . io . * ; import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int countLeaves ( Node node ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return 1 ; return countLeaves ( node . left ) + countLeaves ( node . right ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; System . out . println ( countLeaves ( root ) ) ; } }
Partition an array such into maximum increasing segments | Java program to divide into maximum number of segments ; Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver code ; Find minimum value from right for every index
import java . util . Arrays ; class GFG { static int sorted_partitions ( int arr [ ] ) { int n = arr . length ; int [ ] right_min = new int [ n + 1 ] ; right_min [ n ] = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right_min [ i ] = Math . min ( right_min [ i + 1 ] , arr [ i ] ) ; } int partitions = 0 ; for ( int current_max = arr [ 0 ] , i = 0 ; i < n ; i ++ ) { current_max = Math . max ( current_max , arr [ i ] ) ; if ( current_max <= right_min [ i + 1 ] ) partitions ++ ; } return partitions ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 3 , 1 , 2 , 4 , 100 , 7 , 9 } ; int ans = sorted_partitions ( arr ) ; System . out . println ( ans ) ; } }
Minimize Cost with Replacement with other allowed | Java Program for the above approach ; This function returns the minimum cost of the array ; Driver code
import java . util . Collections ; import java . util . Arrays ; public class GfG { public static int getMinCost ( Integer arr [ ] , int n ) { int min_ele = Collections . min ( Arrays . asList ( arr ) ) ; return min_ele * ( n - 1 ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 4 , 2 , 5 } ; int n = arr . length ; System . out . println ( getMinCost ( arr , n ) ) ; } }
Minimum swaps required to make a binary string alternating | Java implementation of the above approach ; function to count minimum swaps required to make binary String alternating ; stores total number of ones ; stores total number of zeroes ; checking impossible condition ; odd length String ; number of even positions ; stores number of zeroes and ones at even positions ; even length String ; stores number of ones at odd and even position respectively ; Driver code
import java . util . * ; class GFG { static int countMinSwaps ( String s ) { int N = s . length ( ) ; int one = 0 ; int zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) one ++ ; else zero ++ ; } if ( one > zero + 1 zero > one + 1 ) return - 1 ; if ( N % 2 == 1 ) { int num = ( N + 1 ) / 2 ; int one_even = 0 , zero_even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( s . charAt ( i ) == '1' ) one_even ++ ; else zero_even ++ ; } } if ( one > zero ) return num - one_even ; else return num - zero_even ; } else { int one_odd = 0 , one_even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) { if ( i % 2 == 1 ) one_odd ++ ; else one_even ++ ; } } return Math . min ( N / 2 - one_odd , N / 2 - one_even ) ; } } public static void main ( String [ ] args ) { String s = "111000" ; System . out . print ( countMinSwaps ( s ) ) ; } }
Check if it is possible to return to the starting position after moving in the given directions | Java implementation of above approach ; Main method ; int n = 0 ; Count of North int s = 0 ; Count of South int e = 0 ; Count of East int w = 0 ; Count of West
public class GFG { public static void main ( String args [ ] ) { String st = " NNNWEWESSS " ; int len = st . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( st . charAt ( i ) == ' N ' ) n += 1 ; if ( st . charAt ( i ) == ' S ' ) s += 1 ; if ( st . charAt ( i ) == ' W ' ) w += 1 ; if ( st . charAt ( i ) == ' E ' ) e += 1 ; } if ( n == s && w == e ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Minimum cost to make array size 1 by removing larger of pairs | Java program to find minimum cost to reduce array size to 1 , ; function to calculate the minimum cost ; find the minimum using for loop ; Minimum cost is n - 1 multiplied with minimum element . ; driver code .
import java . lang . * ; public class GFG { static int cost ( int [ ] a , int n ) { int min = a [ 0 ] ; for ( int i = 1 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return ( n - 1 ) * min ; } static public void main ( String [ ] args ) { int [ ] a = { 4 , 3 , 2 } ; int n = a . length ; System . out . println ( cost ( a , n ) ) ; } }
Count Non | Java program to count total number of non - leaf nodes in a binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Computes the number of non - leaf nodes in a tree . ; Base cases . ; If root is Not NULL and its one of its child is also not NULL ; Driver program to test size function
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int countNonleaf ( Node root ) { if ( root == null || ( root . left == null && root . right == null ) ) return 0 ; return 1 + countNonleaf ( root . left ) + countNonleaf ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; System . out . println ( countNonleaf ( root ) ) ; } }
Minimum cost for acquiring all coins with k extra coins allowed with every coin | Java program to acquire all n coins ; function to calculate min cost ; sort the coins value ; calculate no . of coins needed ; calculate sum of all selected coins ; Driver code
import java . util . Arrays ; class GFG { static int minCost ( int coin [ ] , int n , int k ) { Arrays . sort ( coin ) ; int coins_needed = ( int ) Math . ceil ( 1.0 * n / ( k + 1 ) ) ; int ans = 0 ; for ( int i = 0 ; i <= coins_needed - 1 ; i ++ ) ans += coin [ i ] ; return ans ; } public static void main ( String arg [ ] ) { int coin [ ] = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = coin . length ; int k = 3 ; System . out . print ( minCost ( coin , n , k ) ) ; } }
Program for First Fit algorithm in Memory Management | Java implementation of First - Fit algorithm ; Method to allocate memory to blocks as per First fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver Code
class GFG { static void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; firstFit ( blockSize , m , processSize , n ) ; } }
Greedy Algorithm to find Minimum number of Coins | Java program to find minimum number of denominations ; All denominations of Indian Currency ; Initialize result ; Traverse through all denomination ; Find denominations ; Print result ; Driver code
import java . util . Vector ; class GFG { static int deno [ ] = { 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 } ; static int n = deno . length ; static void findMin ( int V ) { Vector < Integer > ans = new Vector < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( V >= deno [ i ] ) { V -= deno [ i ] ; ans . add ( deno [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( " ▁ " + ans . elementAt ( i ) ) ; } } public static void main ( String [ ] args ) { int n = 93 ; System . out . print ( " Following ▁ is ▁ minimal ▁ number ▁ " + " of ▁ change ▁ for ▁ " + n + " : ▁ " ) ; findMin ( n ) ; } }
Minimize count of array elements to be removed such that at least K elements are equal to their index values | Java program for the above approach ; Function to minimize the removals of array elements such that atleast K elements are equal to their indices ; Store the array as 1 - based indexing Copy of first array ; Make a dp - table of ( N * N ) size ; Delete the current element ; Take the current element ; Check for the minimum removals ; Driver Code
import java . io . * ; class GFG { static int MinimumRemovals ( int a [ ] , int N , int K ) { int b [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i + 1 ] = a [ i ] ; } int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { dp [ i + 1 ] [ j ] = Math . max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j ] ) ; dp [ i + 1 ] [ j + 1 ] = Math . max ( dp [ i + 1 ] [ j + 1 ] , dp [ i ] [ j ] + ( ( b [ i + 1 ] == j + 1 ) ? 1 : 0 ) ) ; } } for ( int j = N ; j >= 0 ; j -- ) { if ( dp [ N ] [ j ] >= K ) { return ( N - j ) ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 2 , 3 } ; int K = 2 ; int N = arr . length ; System . out . println ( MinimumRemovals ( arr , N , K ) ) ; } }
Count of Arrays of size N having absolute difference between adjacent elements at most 1 | Java program of the above approach ; Function to find the count of possible arrays such that the absolute difference between any adjacent elements is atmost 1 ; Stores the dp states where dp [ i ] [ j ] represents count of arrays of length i + 1 having their last element as j ; Case where 1 st array element is missing ; All integers in range [ 1 , M ] are reachable ; Only reachable integer is arr [ 0 ] ; Iterate through all values of i ; If arr [ i ] is not missing ; Only valid value of j is arr [ i ] ; If arr [ i ] is missing ; Iterate through all possible values of j in range [ 1 , M ] ; Stores the count of valid arrays ; Calculate the total count of valid arrays ; Return answer ; Driver Code ; Function Call
class GFG { public static int countArray ( int arr [ ] , int N , int M ) { int [ ] [ ] dp = new int [ N ] [ M + 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M + 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } if ( arr [ 0 ] == - 1 ) { for ( int j = 1 ; j <= M ; j ++ ) { dp [ 0 ] [ j ] = 1 ; } } else { dp [ 0 ] [ arr [ 0 ] ] = 1 ; } for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != - 1 ) { int j = arr [ i ] ; dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] ; } if ( arr [ i ] == - 1 ) { for ( int j = 1 ; j <= M ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ] ; } } } int arrCount = 0 ; for ( int j = 1 ; j <= M ; j ++ ) { arrCount += dp [ N - 1 ] [ j ] ; } return arrCount ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , - 1 , 2 , 1 , - 1 , - 1 } ; int N = arr . length ; int M = 10 ; System . out . println ( countArray ( arr , N , M ) ) ; } }
Maximize sum of product of neighbouring elements of the element removed from Array | Java program for the above approach ; Stores the dp state where dp [ i ] [ j ] represents the maximum possible score in the subarray from index i to j ; Iterate through all possible lengths of the subarray ; Iterate through all the possible starting indices i having length len ; Stores the rightmost index of the current subarray ; Initial dp [ i ] [ j ] will be 0. ; Iterate through all possible values of k in range [ i + 1 , j - 1 ] ; Return the answer ; Driver Code ; Function Call ; Function Call
import java . io . * ; class GFG { static int maxMergingScore ( int A [ ] , int N ) { int [ ] [ ] dp = new int [ 101 ] [ 101 ] ; for ( int i = 0 ; i < 101 ; i ++ ) { { for ( int j = 0 ; j < 101 ; j ++ ) dp [ i ] [ j ] = 0 ; } } for ( int len = 1 ; len < N ; ++ len ) { for ( int i = 0 ; i + len < N ; ++ i ) { int j = i + len ; dp [ i ] [ j ] = 0 ; for ( int k = i + 1 ; k < j ; ++ k ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 1 , 2 , 3 , 4 } ; System . out . println ( maxMergingScore ( A , N ) ) ; N = 2 ; int B [ ] = { 1 , 55 } ; System . out . println ( maxMergingScore ( B , N ) ) ; } }
Longest subarray with all even or all odd elements | Java implementation for the above approach ; Function to calculate longest subString with odd or even elements ; Initializing dp ; Initializing dp with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp with ( previous dp value ) + 1 ; Storing max element so far to ans ; Returning the final answer ; Driver code ; Input ; Function call
import java . util . * ; class GFG { static int LongestOddEvenSubarray ( int A [ ] , int N ) { int dp ; dp = 1 ; int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 0 && A [ i - 1 ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ i - 1 ] % 2 != 0 ) ) { dp = dp + 1 ; ans = Math . max ( ans , dp ) ; } else dp = 1 ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 } ; int N = A . length ; System . out . print ( LongestOddEvenSubarray ( A , N ) ) ; } }
Count of N | Java program for the above approach ; Function to find number of ' N ' digit numbers such that the element is mean of sum of its adjacent digits ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; If current position is 2 , then any digit from [ 1 - 9 ] can be placed . ; previous digit selected is the mean . ; mean = ( current + prev2 ) / 2 current = ( 2 * mean ) - prev2 ; Check if current and current + 1 can be valid placements ; return answer ; Driver code ; Initializing dp array with - 1. ; Given Input ; Function call
import java . util . * ; class GFG { static int dp [ ] [ ] [ ] = new int [ 100 ] [ 10 ] [ 10 ] ; static int countOfNumbers ( int digit , int prev1 , int prev2 , int n ) { if ( digit == n + 1 ) { return 1 ; } int val = dp [ digit ] [ prev1 ] [ prev2 ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else if ( digit == 2 ) { for ( int i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else { int mean = prev1 ; int current = ( 2 * mean ) - prev2 ; if ( current >= 0 && current <= 9 ) val += countOfNumbers ( digit + 1 , current , prev1 , n ) ; if ( ( current + 1 ) >= 0 && ( current + 1 ) <= 9 ) val += countOfNumbers ( digit + 1 , current + 1 , prev1 , n ) ; } return val ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 10 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } int n = 2 ; System . out . println ( countOfNumbers ( 1 , 0 , 0 , n ) ) ; } }