codeparrot/xlcost-text-to-code huggingface dataset:
Count of Rectangles with area K made up of only 1 s from given Binary Arrays | Java Program to implement the above approach ; Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If A [ ] is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array A [ ] ; Stores the count of subarrays of each size consisting of only 1 s from array B [ ] ; Iterating over all subarrays consisting of only 1 s in A [ ] ; If i is a factor of K , then there is a subarray of size K / i in B [ ] ; Driver Code
class GFG { static int [ ] findSubarrays ( int [ ] a ) { int n = a . length ; int [ ] freq = new int [ n + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { if ( count == 0 ) continue ; else { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } count = 0 ; } } else count ++ ; } if ( count > 0 ) { int value = count ; for ( int j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } } return freq ; } static void countRectangles ( int [ ] a , int [ ] b , int K ) { int n = a . length ; int m = b . length ; int [ ] subA = findSubarrays ( a ) ; int [ ] subB = findSubarrays ( b ) ; int total = 0 ; for ( int i = 1 ; i < subA . length ; i ++ ) { if ( K % i == 0 && ( K / i ) <= m ) { total = total + subA [ i ] * subB [ K / i ] ; } } System . out . print ( total ) ; } public static void main ( String [ ] args ) { int [ ] a = { 0 , 0 , 1 , 1 } ; int [ ] b = { 1 , 0 , 1 } ; int K = 2 ; countRectangles ( a , b , K ) ; } }
Print Longest Bitonic subsequence ( Space Optimized Approach ) | Java program to implement the above approach ; Function to print the longest bitonic subsequence ; Function to generate the longest bitonic subsequence ; Store the lengths of LIS ending at every index ; Store the lengths of LDS ending at every index ; Compute LIS for all indices ; Compute LDS for all indices ; Find the index having maximum value of lis [ i ] + lds [ i ] - 1 ; Stores the count of elements in increasing order in Bitonic subsequence ; Store the increasing subsequence ; Sort the bitonic subsequence to arrange smaller elements at the beginning ; Stores the count of elements in decreasing order in Bitonic subsequence ; Print the longest bitonic sequence ; Driver Code
import java . util . * ; class GFG { static void printRes ( Vector < Integer > res ) { Enumeration enu = res . elements ( ) ; while ( enu . hasMoreElements ( ) ) { System . out . print ( enu . nextElement ( ) + " ▁ " ) ; } } static void printLBS ( int arr [ ] , int N ) { int lis [ ] = new int [ N ] ; int lds [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { lis [ i ] = lds [ i ] = 1 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { if ( lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } } for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { if ( lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; } } } int MaxVal = arr [ 0 ] , inx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( MaxVal < lis [ i ] + lds [ i ] - 1 ) { MaxVal = lis [ i ] + lds [ i ] - 1 ; inx = i ; } } int ct1 = lis [ inx ] ; Vector < Integer > res = new Vector < Integer > ( ) ; for ( int i = inx ; i >= 0 && ct1 > 0 ; i -- ) { if ( lis [ i ] == ct1 ) { res . add ( arr [ i ] ) ; ct1 -- ; } } Collections . reverse ( res ) ; int ct2 = lds [ inx ] - 1 ; for ( int i = inx ; i < N && ct2 > 0 ; i ++ ) { if ( lds [ i ] == ct2 ) { res . add ( arr [ i ] ) ; ct2 -- ; } } printRes ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 80 , 60 , 30 , 40 , 20 , 10 } ; int N = arr . length ; printLBS ( arr , N ) ; } }
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | Java program for the above approach ; Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver code ; Function call
import java . util . * ; import java . io . * ; class GFG { public static boolean possible ( int T , int arr [ ] , int n , int d ) { int partition = 1 ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } public static void calcT ( int n , int d , int arr [ ] ) { int mx = - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } int lb = mx ; int ub = sum ; while ( lb < ub ) { int T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } System . out . println ( lb ) ; } public static void main ( String args [ ] ) { int d = 2 ; int arr [ ] = { 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; calcT ( n , d , arr ) ; } }
Minimize splits to generate monotonous Substrings from given String | Java Program to implement the above approach ; Function to return final result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code
import java . util . * ; class GFG { static int minReqSubstring ( String s , int n ) { char ongoing = ' N ' ; int count = 0 , l = s . length ( ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( ongoing == ' N ' ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' N ' ; } else { ongoing = ' I ' ; } } else if ( ongoing == ' I ' ) { if ( s . charAt ( i ) > s . charAt ( i - 1 ) ) { ongoing = ' I ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' I ' ; } else { ongoing = ' N ' ; count += 1 ; } } else { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else { ongoing = ' N ' ; count += 1 ; } } } return count + 1 ; } public static void main ( String [ ] args ) { String S = " aeccdhba " ; int n = S . length ( ) ; System . out . print ( minReqSubstring ( S , n ) ) ; } }
Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | Java program to implement the above approach ; Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code
import java . util . * ; class GFG { static int minimum_required_operations ( int arr [ ] , int n , int k ) { int answer = 0 ; long mod = 1000000007 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer %= mod ; } } return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 50 , 4 , 14 , 42 , 89 } ; int k = 10 ; int n = a . length ; System . out . print ( minimum_required_operations ( a , n , k ) ) ; } }
Lexicographic smallest permutation of a String containing the second String as a Substring | Java program to implement the above approach ; Function to print the desired lexicographic smaller String ; Calculate length of the String ; Stores the frequencies of characters of String str1 ; Stores the frequencies of characters of String str2 ; Decrease the frequency of second String from that of of the first String ; To store the resultant String ; To find the index of first character of the String str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of String str2 append str2 ; Return the resultant String ; Driver Code
import java . util . * ; class GFG { static String findSmallestString ( String str1 , String str2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; Arrays . fill ( freq1 , 0 ) ; Arrays . fill ( freq2 , 0 ) ; int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } String res = " " ; int minIndex = str2 . charAt ( 0 ) - ' a ' ; for ( int i = 0 ; i < 26 ; ++ i ) { for ( int j = 0 ; j < freq1 [ i ] ; ++ j ) { res += ( char ) ( i + ' a ' ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeksfor " ; String str2 = " for " ; System . out . print ( findSmallestString ( str1 , str2 ) ) ; } }
Determine the winner of a game of deleting Characters from a String | Java program for the above approach ; Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
import java . util . * ; class GFG { static void determineWinner ( String str ) { int [ ] A = new int [ 10 ] ; int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { A [ ( int ) str . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { System . out . print ( " - 1" ) ; } else if ( sum1 > sum2 ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } public static void main ( String [ ] args ) { String str = "78787" ; determineWinner ( str ) ; } }
Generate a String from given Strings P and Q based on the given conditions | Java Program to implement the above approach ; Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in freq [ ] to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void manipulateStrings ( String P , String Q ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < P . length ( ) ; i ++ ) { freq [ P . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < Q . length ( ) ; i ++ ) { freq [ Q . charAt ( i ) - ' a ' ] -- ; } StringBuilder sb = new StringBuilder ( ) ; int pos = Q . charAt ( 0 ) - ' a ' ; for ( int i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb . append ( c ) ; freq [ i ] -- ; } } sb . append ( Q ) ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb . append ( c ) ; freq [ i ] -- ; } } System . out . println ( sb ) ; } public static void main ( String [ ] args ) { String P = " geeksforgeeksfor " ; String Q = " for " ; manipulateStrings ( P , Q ) ; } }
Minimum moves required to type a word in QWERTY based keyboard | Java program for the above approach ; Function that calculates the moves required to print the current String ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the String ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call
import java . util . * ; class GFG { static int numberMoves ( String s ) { int row [ ] = { 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 } ; int n = s . length ( ) ; int move = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( row [ s . charAt ( i ) - ' a ' ] != row [ s . charAt ( i - 1 ) - ' a ' ] ) { move ++ ; } } return move ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( numberMoves ( str ) ) ; } }
Queries to calculate the Sum of Array elements in the range [ L , R ] having indices as multiple of K | Java program to implement the above approach ; Structure of a Query ; Function to calculate the sum of array elements at indices from range [ L , R ] which are multiples of K for each query ; Stores Prefix Sum ; prefixSum [ i ] [ j ] : Stores the sum from indices [ 0 , j ] which are multiples of i ; If index j is a multiple of i ; Compute prefix sum ; Otherwise ; Traverse each query ; Sum of all indices upto R which are a multiple of K ; Sum of all indices upto L - 1 which are a multiple of K ; Calculate the difference ; Driver Code
import java . util . * ; class GFG { static class Node { int L ; int R ; int K ; } ; static void kMultipleSum ( int arr [ ] , Node Query [ ] , int N , int Q ) { int prefixSum [ ] [ ] = new int [ N + 1 ] [ N ] ; for ( int i = 1 ; i <= N ; i ++ ) { prefixSum [ i ] [ 0 ] = arr [ 0 ] ; for ( int j = 0 ; j < N ; j ++ ) { if ( j % i == 0 ) { if ( j != 0 ) prefixSum [ i ] [ j ] = arr [ j ] + prefixSum [ i ] [ j - 1 ] ; } else { prefixSum [ i ] [ j ] = prefixSum [ i ] [ j - 1 ] ; } } } for ( int i = 0 ; i < Q ; i ++ ) { int last = prefixSum [ Query [ i ] . K ] [ Query [ i ] . R ] ; int first ; if ( Query [ i ] . L == 0 ) { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L ] ; } else { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L - 1 ] ; } System . out . print ( last - first + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int Q = 2 ; Node Query [ ] = new Node [ Q ] ; for ( int i = 0 ; i < Q ; i ++ ) Query [ i ] = new Node ( ) ; Query [ 0 ] . L = 2 ; Query [ 0 ] . R = 5 ; Query [ 0 ] . K = 2 ; Query [ 1 ] . L = 3 ; Query [ 1 ] . R = 5 ; Query [ 1 ] . K = 5 ; kMultipleSum ( arr , Query , N , Q ) ; } }
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | Java Program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
import java . util . * ; import java . io . * ; public class Main { static int mod = 1000000007 ; public static long countS ( String str [ ] , int n , int m ) { Map < Integer , Set < Character > > counts = new HashMap < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { counts . put ( i , new HashSet < > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts . get ( j ) . add ( s . charAt ( j ) ) ; } } long result = 1 ; for ( int index : counts . keySet ( ) ) result = ( result * counts . get ( index ) . size ( ) ) % mod ; return result ; } public static void main ( String [ ] args ) { String str [ ] = { "112" , "211" } ; int N = 2 , M = 3 ; System . out . println ( countS ( str , N , M ) ) ; } }
Longest Subarrays having each Array element as the maximum | Java Program to implement the above approach ; Function to find the maximum length of Subarrays for each element of the array having it as the maximum ; Initialize the bounds ; Iterate to find greater element on the left ; If greater element is found ; Decrement left pointer ; If boundary is exceeded ; Iterate to find greater element on the right ; If greater element is found ; Increment right pointer ; If boundary is exceeded ; Length of longest subarray where arr [ i ] is the largest ; Print the answer ; Driver Code
import java . util . * ; class GFG { static void solve ( int n , int arr [ ] ) { int i , ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { int left = Math . max ( i - 1 , 0 ) ; int right = Math . min ( n - 1 , i + 1 ) ; while ( left >= 0 ) { if ( arr [ left ] > arr [ i ] ) { left ++ ; break ; } left -- ; } if ( left < 0 ) left ++ ; while ( right < n ) { if ( arr [ right ] > arr [ i ] ) { right -- ; break ; } right ++ ; } if ( right >= n ) right -- ; ans = 1 + right - left ; System . out . print ( ans + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 } ; int n = arr . length ; solve ( n , arr ) ; } }
Count of Substrings having Sum equal to their Length | Java program to implement the above approach ; Function to count the number of subStrings with sum equal to length ; Stores the count of subStrings ; Add character to sum ; Add count of subStrings to result ; Increase count of subarrays ; Return count ; Driver Code
import java . util . * ; class GFG { static int countSubStrings ( String s , int n ) { int count = 0 , sum = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { sum += ( s . charAt ( i ) - '0' ) ; count += mp . containsKey ( sum - ( i + 1 ) ) == true ? mp . get ( sum - ( i + 1 ) ) : 0 ; if ( ! mp . containsKey ( sum - ( i + 1 ) ) ) mp . put ( sum - ( i + 1 ) , 1 ) ; else mp . put ( sum - ( i + 1 ) , mp . get ( sum - ( i + 1 ) ) + 1 ) ; } return count ; } public static void main ( String [ ] args ) { String str = "112112" ; int n = str . length ( ) ; System . out . print ( countSubStrings ( str , n ) + "NEW_LINE"); } }
Count of ways to split an Array into three contiguous Subarrays having increasing Sum | Java Program to implement the above approach ; Function to count the number of ways to split array into three contiguous subarrays of the required type ; Stores the prefix sums ; Stores the suffix sums ; Traverse the given array ; Updating curr_subarray_sum until it is less than prefix_sum [ s - 1 ] ; Increase count ; Decrease curr_subarray_sum by arr [ s [ ] ; Return count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int findCount ( int arr [ ] , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int s = 1 , e = 1 ; int curr_subarray_sum = 0 , count = 0 ; while ( s < n - 1 && e < n - 1 ) { while ( e < n - 1 && curr_subarray_sum < prefix_sum [ s - 1 ] ) { curr_subarray_sum += arr [ e ++ ] ; } if ( curr_subarray_sum <= suffix_sum [ e ] ) { count ++ ; } curr_subarray_sum -= arr [ s ++ ] ; } return count ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 1 , 7 } ; int n = arr . length ; System . out . println ( findCount ( arr , n ) ) ; } }
Minimum number of operations required to maximize the Binary String | Java program to implement the above approach ; Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
class GFG { static int minOperation ( String s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s . charAt ( i ) == '0' ) cnt0 ++ ; } return cnt0 ; } public static void main ( String [ ] args ) { int n = 8 ; String s = "01001011" ; int ans = minOperation ( s , n ) ; System . out . print ( ans + "NEW_LINE"); } }
Minimum number of operations required to maximize the Binary String | Java program to implement the above approach ; Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver code
import java . util . * ; class GFG { static int minOperation ( String s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s . charAt ( i ) == '0' && s . charAt ( j ) == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s . charAt ( i ) == '1' ) { i ++ ; } if ( s . charAt ( j ) == '0' ) { j -- ; } } return ans ; } public static void main ( String [ ] args ) { int n = 8 ; String s = "10100101" ; System . out . println ( minOperation ( s , n ) ) ; } }
Count of N digit Numbers having no pair of equal consecutive Digits | Java Program to implement of the above approach ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp [ ] [ ] table ; Calculate the count of required N - digit numbers ; Driver Code
import java . util . * ; class GFG { public static void count ( int N ) { if ( N == 1 ) { System . out . println ( 10 ) ; return ; } int dp [ ] [ ] = new int [ N ] [ 10 ] ; for ( int i = 1 ; i < 10 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 ] [ j ] ; for ( int j = 0 ; j < 10 ; j ++ ) dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; } int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 ] [ i ] ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 2 ; count ( N ) ; } }
Count of N digit Numbers having no pair of equal consecutive Digits | Java Program to implement of the above approach ; Iterative Function to calculate ( x ^ y ) % mod in O ( log y ) ; Initialize result ; Update x if x >= mod ; If x is divisible by mod ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Driver Code
import java . util . * ; class GFG { static int power ( int x , int y , int mod ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } public static void count ( int N ) { if ( N == 1 ) { System . out . println ( 10 ) ; return ; } System . out . println ( power ( 9 , N , 1000000007 ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; count ( N ) ; } }