codeparrot/xlcost-text-to-code huggingface dataset:
Longest subarray whose elements can be made equal by maximum K increments | Java code for the above approach ; Function to find maximum possible length of subarray ; Stores the sum of elements that needs to be added to the sub array ; Stores the index of the current position of subarray ; Stores the maximum length of subarray . ; Maximum element from each subarray length ; Previous index of the current subarray of maximum length ; Deque to store the indices of maximum element of each sub array ; For each array element , find the maximum length of required subarray ; Traverse the deque and update the index of maximum element . ; If it is first element then update maximum and dp [ ] ; Else check if current element exceeds max ; Update max and dp [ ] ; Update the index of the current maximum length subarray ; While current element being added to dp [ ] array exceeds K ; Update index of current position and the previous position ; Remove elements from deque and update the maximum element ; Update the maximum length of the required subarray . ; Driver code
import java . util . * ; class GFG { static int validSubArrLength ( int arr [ ] , int N , int K ) { int [ ] dp = new int [ N + 1 ] ; int pos = 0 ; int ans = 0 ; int mx = 0 ; int pre = 0 ; Deque < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { while ( ! q . isEmpty ( ) && arr [ q . getLast ( ) ] < arr [ i ] ) q . removeLast ( ) ; q . add ( i ) ; if ( i == 0 ) { mx = arr [ i ] ; dp [ i ] = arr [ i ] ; } else if ( mx <= arr [ i ] ) { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; mx = arr [ i ] ; } else { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; } if ( pre == 0 ) pos = 0 ; else pos = pre - 1 ; while ( ( i - pre + 1 ) * mx - ( dp [ i ] - dp [ pos ] ) > K && pre < i ) { pos = pre ; pre ++ ; while ( ! q . isEmpty ( ) && q . peek ( ) < pre && pre < i ) { q . removeFirst ( ) ; mx = arr [ q . peek ( ) ] ; } } ans = Math . max ( ans , i - pre + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 8 ; int arr [ ] = { 2 , 7 , 1 , 3 , 4 , 5 } ; System . out . print ( validSubArrLength ( arr , N , K ) ) ; } }
Partition of a set into K subsets with equal sum using BitMask and DP | Java program to check if the given array can be partitioned into K subsets with equal sum ; Function to check whether K required partitions are possible or not ; Return true as the entire array is the answer ; If total number of partitions exceeds size of the array ; If the array sum is not divisible by K ; No such partitions are possible ; Required sum of each subset ; Initialize dp array with - 1 ; Sum of empty subset is zero ; Iterate over all subsets / masks ; if current mask is invalid , continue ; Iterate over all array elements ; Check if the current element can be added to the current subset / mask ; Transition ; Driver Code
import java . util . * ; class GFG { static boolean isKPartitionPossible ( int arr [ ] , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int target = sum / K ; int [ ] dp = new int [ ( 1 << 15 ) ] ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { if ( dp [ mask ] == - 1 ) continue ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ( mask & ( 1 << i ) ) == 0 ) && dp [ mask ] + arr [ i ] <= target ) { dp [ mask ( 1 << i ) ] = ( dp [ mask ] + arr [ i ] ) % target ; } } } if ( dp [ ( 1 << N ) - 1 ] == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) { System . out . print ( " Partitions ▁ into ▁ equal ▁ " ) ; System . out . print ( "sum is possible.NEW_LINE"); } else { System . out . print ( " Partitions ▁ into ▁ equal ▁ " ) ; System . out . print ( "sum is not possible.NEW_LINE"); } } }
Minimum cost path in a Matrix by moving only on value difference of X | Java implementation to find the minimum number of operations required to move from ( 1 , 1 ) to ( N , M ) ; Function to find the minimum operations required to move to bottom - right cell of matrix ; Condition to check if the current cell is the bottom - right cell of the matrix ; Condition to check if the current cell is out of matrix ; Condition to check if the current indices is already computed ; Condition to check that the movement with the current value is not possible ; Recursive call to compute the number of operation required to compute the value ; Function to find the minimum number of operations required to reach the bottom - right cell ; Loop to iterate over every possible cell of the matrix ; Driver Code ; Function call
import java . lang . * ; import java . util . * ; class GFG { static final long MAX = ( long ) 1e18 ; static long n , m ; static List < List < Long > > v = new ArrayList < > ( 151 ) ; static long [ ] [ ] dp = new long [ 151 ] [ 151 ] ; static long min_operation ( long i , long j , long val , long x ) { if ( i == n - 1 && j == m - 1 ) { if ( val > v . get ( ( int ) i ) . get ( ( int ) j ) ) { return dp [ ( int ) i ] [ ( int ) j ] = MAX ; } else { return dp [ ( int ) i ] [ ( int ) j ] = v . get ( ( int ) i ) . get ( ( int ) j ) - val ; } } if ( i == n j == m ) { return dp [ ( int ) i ] [ ( int ) j ] = MAX ; } if ( dp [ ( int ) i ] [ ( int ) j ] != - 1 ) { return dp [ ( int ) i ] [ ( int ) j ] ; } if ( val > v . get ( ( int ) i ) . get ( ( int ) j ) ) { return dp [ ( int ) i ] [ ( int ) j ] = MAX ; } long tmp = v . get ( ( int ) i ) . get ( ( int ) j ) - val ; tmp += Math . min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) ) ; return dp [ ( int ) i ] [ ( int ) j ] = tmp ; } static long solve ( long x ) { long ans = Long . MAX_VALUE ; for ( long i = 0 ; i < n ; i ++ ) { for ( long j = 0 ; j < m ; j ++ ) { long val = v . get ( ( int ) i ) . get ( ( int ) j ) - x * ( i + j ) ; for ( int k = 0 ; k < dp . length ; k ++ ) for ( int l = 0 ; l < dp [ k ] . length ; l ++ ) dp [ k ] [ l ] = - 1 ; val = min_operation ( 0l , 0l , val , x ) ; ans = Math . min ( ans , val ) ; } } return ans ; } public static void main ( String [ ] args ) { n = 2 ; m = 2 ; long x = 3 ; v . add ( Arrays . asList ( 15l , 153l ) ) ; v . add ( Arrays . asList ( 135l , 17l ) ) ; System . out . println ( solve ( x ) ) ; } }
Minimum count of numbers required from given array to represent S | Java implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int printAllSubsetsRec ( int arr [ ] , int n , ArrayList < Integer > v , int sum ) { if ( sum == 0 ) { return ( int ) v . size ( ) ; } if ( sum < 0 ) return Integer . MAX_VALUE ; if ( n == 0 ) return Integer . MAX_VALUE ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . add ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . remove ( v . size ( ) - 1 ) ; return Math . min ( x , y ) ; } static int printAllSubsets ( int arr [ ] , int n , int sum ) { ArrayList < Integer > v = new ArrayList < > ( ) ; return printAllSubsetsRec ( arr , n , v , sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = arr . length ; System . out . println ( printAllSubsets ( arr , n , sum ) ) ; } }
Minimum count of numbers required from given array to represent S | Java implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code
import java . util . * ; class GFG { static int Count ( int S [ ] , int m , int n ) { int [ ] [ ] table = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { table [ 0 ] [ i ] = Integer . MAX_VALUE - 1 ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m ] [ n ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 6 , 5 , 1 } ; int m = arr . length ; System . out . print ( Count ( arr , m , 11 ) ) ; } }
Maximize product of same | Java implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code
class GFG { static final int INF = 10000000 ; static int maximum ( int A , int B , int C , int D ) { return Math . max ( Math . max ( A , B ) , Math . max ( C , D ) ) ; } static int maxProductUtil ( int X , int Y , int [ ] A , int [ ] B , int [ ] [ ] dp ) { if ( X < 0 Y < 0 ) return - INF ; if ( dp [ X ] [ Y ] != - 1 ) return dp [ X ] [ Y ] ; dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) ; return dp [ X ] [ Y ] ; } static int maxProduct ( int A [ ] , int N , int B [ ] , int M ) { int [ ] [ ] dp = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , 6 , - 2 , - 5 } ; int b [ ] = { - 3 , 4 , - 2 , 8 } ; int n = a . length ; int m = b . length ; System . out . print ( maxProduct ( a , n , b , m ) ) ; } }
Check if one string can be converted to other using given operation | Java implementation of above approach ; Function that prints whether is it possible to make a String equal to T by performing given operations ; Base case , if we put the last character at front of A ; Base case , if we put the last character at back of A ; Condition if current sequence is matchable ; Condition for front move to ( i - 1 ) th character ; Condition for back move to ( i - 1 ) th character ; Condition if it is possible to make String A equal to String T ; Print final answer ; Driver Code
import java . util . * ; class GFG { static void twoStringsEquality ( String s , String t ) { int n = s . length ( ) ; int [ ] [ ] dp = new int [ n ] [ n + 1 ] ; if ( s . charAt ( n - 1 ) == t . charAt ( 0 ) ) dp [ n - 1 ] [ 1 ] = 1 ; if ( s . charAt ( n - 1 ) == t . charAt ( n - 1 ) ) dp [ n - 1 ] [ 0 ] = 1 ; for ( int i = n - 1 ; i > 0 ; i -- ) { for ( int j = 0 ; j <= n - i ; j ++ ) { if ( dp [ i ] [ j ] > 0 ) { if ( s . charAt ( i - 1 ) == t . charAt ( j ) ) dp [ i - 1 ] [ j + 1 ] = 1 ; if ( s . charAt ( i - 1 ) == t . charAt ( i + j - 1 ) ) dp [ i - 1 ] [ j ] = 1 ; } } } boolean ans = false ; for ( int i = 0 ; i <= n ; i ++ ) { if ( dp [ 0 ] [ i ] == 1 ) { ans = true ; break ; } } if ( ans == true ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } public static void main ( String [ ] args ) { String S = " abab " ; String T = " baab " ; twoStringsEquality ( S , T ) ; } }
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence | Java program to find the Maximum sum of all elements which are not a part of longest increasing sub sequence ; Function to find maximum sum ; Find total sum of array ; Maintain a 2D array ; Update the dp array along with sum in the second row ; In case of greater length Update the length along with sum ; In case of equal length find length update length with minimum sum ; Find the sum that need to be subtracted from total sum ; Return the sum ; Driver code
class GFG { static int findSum ( int [ ] arr , int n ) { int totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; } int [ ] [ ] dp = new int [ 2 ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 ] [ i ] = 1 ; dp [ 1 ] [ i ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) { dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1 ; dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ] ; } else if ( dp [ 0 ] [ i ] == dp [ 0 ] [ j ] + 1 ) { dp [ 1 ] [ i ] = Math . min ( dp [ 1 ] [ i ] , dp [ 1 ] [ j ] + arr [ i ] ) ; } } } } int maxm = 0 ; int subtractSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ 0 ] [ i ] > maxm ) { maxm = dp [ 0 ] [ i ] ; subtractSum = dp [ 1 ] [ i ] ; } else if ( dp [ 0 ] [ i ] == maxm ) { subtractSum = Math . min ( subtractSum , dp [ 1 ] [ i ] ) ; } } return totalSum - subtractSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 1 , 2 , 3 , 8 } ; int n = arr . length ; System . out . print ( findSum ( arr , n ) ) ; } }
Count of possible permutations of a number represented as a sum of 2 ' s , ▁ 4' s and 6 's only | Java code for above implementation ; Returns number of ways to reach score n ; table [ i ] will store count of solutions for value i . ; Initialize all table values as 0 ; Base case ( If given value is 0 , 1 , 2 , or 4 ) ; Driver Code
import java . util . * ; class GFG { static int count ( int n ) { if ( n == 2 ) return 1 ; else if ( n == 4 ) return 2 ; else if ( n == 6 ) return 4 ; int table [ ] = new int [ n + 1 ] ; int i ; for ( i = 0 ; i < n + 1 ; i ++ ) table [ i ] = 0 ; table [ 0 ] = 0 ; table [ 2 ] = 1 ; table [ 4 ] = 2 ; table [ 6 ] = 4 ; for ( i = 8 ; i <= n ; i = i + 2 ) { table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ; } return table [ n ] ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . print ( count ( n ) ) ; } }
Count of ways to split a given number into prime segments | Java implementation to count total number of ways to split a string to get prime numbers ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; 1 based indexing ; Consider every suffix up to 6 digits ; Number should not have a leading zero and it should be a prime number ; Return the final result ; Driver code
import java . util . * ; class GFG { static final int MOD = 1000000007 ; static boolean isPrime ( String number ) { int num = Integer . valueOf ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) { if ( ( num % i ) == 0 ) return false ; } return num > 1 ? true : false ; } static int countPrimeStrings ( String number , int i ) { if ( i == 0 ) return 1 ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number . charAt ( i - j ) != '0' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += countPrimeStrings ( number , i - j ) ; cnt %= MOD ; } } return cnt ; } public static void main ( String [ ] args ) { String s1 = "3175" ; int l = s1 . length ( ) ; System . out . print ( countPrimeStrings ( s1 , l ) ) ; } }
Count of ways to split a given number into prime segments | Java implementation to count total number of ways to split a String to get prime numbers ; Function to build sieve ; If p is a prime ; Update all multiples of p as non prime ; Function to check whether a number is a prime number or not ; Function to find the count of ways to split String into prime numbers ; Number should not have a leading zero and it should be a prime number ; Function to count the number of prime Strings ; Driver code
import java . util . * ; class GFG { static int MOD = 1000000007 ; static boolean [ ] sieve = new boolean [ 1000000 ] ; static void buildSieve ( ) { Arrays . fill ( sieve , true ) ; sieve [ 0 ] = false ; sieve [ 1 ] = false ; for ( int p = 2 ; p * p <= 1000000 ; p ++ ) { if ( sieve [ p ] == true ) { for ( int i = p * p ; i < 1000000 ; i += p ) sieve [ i ] = false ; } } } static boolean isPrime ( String number ) { int num = Integer . valueOf ( number ) ; return sieve [ num ] ; } static int rec ( String number , int i , int [ ] dp ) { if ( dp [ i ] != - 1 ) return dp [ i ] ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number . charAt ( i - j ) != '0' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += rec ( number , i - j , dp ) ; cnt %= MOD ; } } return dp [ i ] = cnt ; } static int countPrimeStrings ( String number ) { int n = number . length ( ) ; int [ ] dp = new int [ n + 1 ] ; Arrays . fill ( dp , - 1 ) ; dp [ 0 ] = 1 ; return rec ( number , n , dp ) ; } public static void main ( String [ ] args ) { buildSieve ( ) ; String s1 = "3175" ; System . out . print ( countPrimeStrings ( s1 ) + "NEW_LINE"); } }
Check if a matrix contains a square submatrix with 0 as boundary element | Java implementation of the above approach ; Function checks if square with all 0 's in boundary  exists in the matrix ; r1 is the top row , c1 is the left col r2 is the bottom row , c2 is the right ; Function checks if the boundary of the square consists of 0 's ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int squareOfZeroes ( int [ ] [ ] matrix ) { int lastIdx = matrix . length - 1 ; Map < String , Boolean > cache = new HashMap < String , Boolean > ( ) ; return ( hasSquareOfZeroes ( matrix , 0 , 0 , lastIdx , lastIdx , cache ) ) ? 1 : 0 ; } static boolean hasSquareOfZeroes ( int [ ] [ ] matrix , int r1 , int c1 , int r2 , int c2 , Map < String , Boolean > cache ) { if ( r1 >= r2 c1 >= c2 ) return false ; String key = r1 + " - " + c1 + " - " + r2 + " - " + c2 ; if ( cache . containsKey ( key ) ) return cache . get ( key ) ; cache . put ( key , isSquareOfZeroes ( matrix , r1 , c1 , r2 , c2 ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 + 1 , r2 - 1 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 , r2 , c2 - 1 , cache ) || hasSquareOfZeroes ( matrix , r1 + 1 , c1 + 1 , r2 , c2 , cache ) || hasSquareOfZeroes ( matrix , r1 , c1 , r2 - 1 , c2 - 1 , cache ) ) ; return cache . get ( key ) ; } static boolean isSquareOfZeroes ( int [ ] [ ] matrix , int r1 , int c1 , int r2 , int c2 ) { for ( int row = r1 ; row < r2 + 1 ; row ++ ) { if ( matrix [ row ] [ c1 ] != 0 matrix [ row ] [ c2 ] != 0 ) return false ; } for ( int col = c1 ; col < c2 + 1 ; col ++ ) { if ( matrix [ r1 ] [ col ] != 0 matrix [ r2 ] [ col ] != 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 1 , 1 , 1 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 1 } } ; int ans ; ans = squareOfZeroes ( matrix ) ; if ( ans == 1 ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } }
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Java program to calculate the number of ways ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
class GFG { public static void CountWays ( int a , int b , int c ) { int x = b + c + 1 ; int [ ] arr = new int [ x ] ; for ( int i = a ; i <= b ; i ++ ) { for ( int j = b ; j <= c ; j ++ ) { arr [ i + j ] ++ ; } } for ( int i = 1 ; i < x ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Java program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . ; Printing the array ; Driver code
import java . util . * ; class GFG { static void CountWays ( int a , int b , int c ) { int x = b + c + 2 ; int arr [ ] = new int [ x ] ; for ( int i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( int i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
Count of distinct possible strings after performing given operations | Java implementation of the above approach ; Function that prints the number of different strings that can be formed ; Computing the length of the given string ; Base case ; Traverse the given string ; If two consecutive 1 ' s ▁ ▁ or ▁ 2' s are present ; Otherwise take the previous value ; Driver code
import java . io . * ; class GFG { static void differentStrings ( String s ) { int n = s . length ( ) ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) && ( s . charAt ( i ) == '1' || s . charAt ( i ) == '2' ) ) dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] ; else dp [ i + 1 ] = dp [ i ] ; } System . out . println ( dp [ n ] ) ; } public static void main ( String [ ] args ) { String S = "0111022110" ; differentStrings ( S ) ; } }
Sum of cost of all paths to reach a given cell in a Matrix | Java implementation to find the sum of cost of all paths to reach ( M , N ) ; Function for computing combination ; Function to find the factorial of N ; Loop to find the factorial of a given number ; Function for coumputing the sum of all path cost ; Loop to find the contribution of each ( i , j ) in the all possible ways ; Count number of times ( i , j ) visited ; Add the contribution of grid [ i ] [ j ] in the result ; Driver code ; Function Call
import java . util . * ; class GFG { static int Col = 3 ; static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int sumPathCost ( int grid [ ] [ ] , int m , int n ) { int sum = 0 , count ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { count = nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ; sum += count * grid [ i ] [ j ] ; } } return sum ; } public static void main ( String [ ] args ) { int m = 2 ; int n = 2 ; int grid [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; System . out . println ( sumPathCost ( grid , m , n ) ) ; } }