codeparrot/xlcost-text-to-code huggingface dataset:
Count of non decreasing Arrays with ith element in range [ A [ i ] , B [ i ] ] | Java program for the above approach ; Function to count the total number of possible valid arrays ; Make a 2D DP table ; Make a 2D prefix sum table ; Initialize all values to 0 ; Base Case ; Initialize the prefix values ; Iterate over the range and update the dp table accordingly ; Add the dp values to the prefix sum ; Update the prefix sum table ; Find the result count of arrays formed ; Return the total count of arrays ; Driver Code
public class GFG { static int totalValidArrays ( int a [ ] , int b [ ] , int N ) { int dp [ ] [ ] = new int [ N + 1 ] [ b [ N - 1 ] + 1 ] ; int pref [ ] [ ] = new int [ N + 1 ] [ b [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) for ( int j = 0 ; j < b [ N - 1 ] + 1 ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 0 ; i < N + 1 ; i ++ ) for ( int j = 0 ; j < b [ N - 1 ] + 1 ; j ++ ) pref [ i ] [ j ] = 0 ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i <= b [ N - 1 ] ; i ++ ) { pref [ 0 ] [ i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = a [ i - 1 ] ; j <= b [ i - 1 ] ; j ++ ) { dp [ i ] [ j ] += pref [ i - 1 ] [ j ] ; pref [ i ] [ j ] += dp [ i ] [ j ] ; } for ( int j = 0 ; j <= b [ N - 1 ] ; j ++ ) { if ( j > 0 ) { pref [ i ] [ j ] += pref [ i ] [ j - 1 ] ; } } } int ans = 0 ; for ( int i = a [ N - 1 ] ; i <= b [ N - 1 ] ; i ++ ) { ans += dp [ N ] [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 } ; int B [ ] = { 2 , 3 } ; int N = A . length ; System . out . println ( totalValidArrays ( A , B , N ) ) ; } }
Query to find length of the longest subarray consisting only of 1 s | Java Program for the above approach ; Function to calculate the longest subarray consisting of 1 s only ; Stores the maximum length of subarray containing 1 s only ; Traverse the array ; If current element is '1' ; Increment length ; Otherwise ; Reset length ; Update maximum subarray length ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Flip the character ; Size of array ; Given array ; Given queries ; Number of queries
import java . util . * ; public class Main { static int longestsubarray ( int a [ ] , int N ) { int maxlength = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . max ( maxlength , sum ) ; } return maxlength ; } static void solveQueries ( int arr [ ] , int n , Vector < Vector < Integer > > Q , int k ) { int cntQuery = Q . size ( ) ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q . get ( i ) . get ( 0 ) == 1 ) { System . out . print ( longestsubarray ( arr , n ) + " ▁ " ) ; } else { arr [ Q . get ( i ) . get ( 1 ) - 1 ] ^= 1 ; } } } public static void main ( String [ ] args ) { int N = 10 ; int arr [ ] = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; Vector < Vector < Integer > > Q = new Vector < Vector < Integer > > ( ) ; Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; Vector < Integer > v3 = new Vector < Integer > ( ) ; v1 . add ( 1 ) ; v2 . add ( 2 ) ; v2 . add ( 3 ) ; v3 . add ( 1 ) ; Q . add ( v1 ) ; Q . add ( v2 ) ; Q . add ( v3 ) ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }
Count of integers in range [ L , R ] having even frequency of each digit | Java program for the above approach ; Stores the upper limit of the range ; Stores the overlapping states ; Recursive Function to calculate the count of valid integers in the range [ 1 , s ] using memoization ; Base Case ; If current integer has even count of digits and is not repeated ; If current state is already considered ; Stores the maximum valid digit at the current index ; Stores the count of valid integers ; If the current digit is not the most significant digit , i . e , the integer is already started ; Iterate through all valid digits ; Recursive call for ith digit at the current index ; Recursive call for integers having leading zeroes in the beginning ; Iterate through all valid digits as most significant digits ; Recursive call for ith digit at the current index ; Return answer ; Function to calculate valid number in the range [ 1 , X ] ; Initialize dp array with - 1 ; Store the range in form of string ; Return Count ; Function to find the count of integers in the range [ L , R ] such that the frequency of each digit is even ; Driver Code
import java . util . * ; class GFG { static String s ; static int [ ] [ ] [ ] [ ] dp = new int [ 1024 ] [ 10 ] [ 2 ] [ 2 ] ; static boolean toBoolean ( int smaller ) { if ( smaller >= 1 ) return true ; else return false ; } static int calcCnt ( int mask , int len , int smaller , int started ) { + X3D3F3 : W3F3 : Y3F3 : X3 if ( len == s . length ( ) ) { if ( mask == 0 && started != 0 ) return 1 ; else return 0 ; } if ( dp [ mask ] [ len ] [ smaller ] [ started ] != - 1 ) return dp [ mask ] [ len ] [ smaller ] [ started ] ; int mx = 9 ; if ( smaller == 0 ) { mx = ( int ) s . charAt ( len ) - 48 ; } int ans = 0 ; if ( started != 0 ) { for ( int i = 0 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , toBoolean ( smaller ) || ( i < ( int ) s . charAt ( len ) - 48 ) ? 1 : 0 , 1 ) ; } } else { ans = calcCnt ( mask , len + 1 , 1 , 0 ) ; for ( int i = 1 ; i <= mx ; i ++ ) { ans += calcCnt ( mask ^ ( 1 << i ) , len + 1 , toBoolean ( smaller ) || ( i < ( int ) s . charAt ( len ) - 48 ) ? 1 : 0 , 1 ) ; } } dp [ mask ] [ len ] [ smaller ] [ started ] = ans ; return ans ; } static int countInt ( int x ) { for ( int i = 0 ; i < 1024 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } s = Integer . toString ( x ) ; return calcCnt ( 0 , 0 , 0 , 0 ) ; } static int countIntInRange ( int L , int R ) { return countInt ( R ) - countInt ( L - 1 ) ; } public static void main ( String [ ] args ) { int L = 32 , R = 1010 ; System . out . println ( countIntInRange ( L , R ) ) ; } }
Queries to find minimum absolute difference between adjacent array elements in given ranges | Java program for the above approach ; Structure for query range ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
class GFG { static class Query { int L , R ; public Query ( int l , int r ) { super ( ) ; L = l ; R = r ; } } ; static int MAX = 5000 ; static void minDifference ( int arr [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; int ans = MAX ; for ( int j = L ; j < R ; j ++ ) { ans = Math . min ( ans , arr [ j ] ) ; } System . out . println ( ans ) ; } } static void minimumDifference ( int arr [ ] , Query q [ ] , int N , int m ) { int [ ] diff = new int [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; minDifference ( diff , N - 1 , q , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 1 , 8 , 3 , 4 } ; int N = arr . length ; Query Q [ ] = { new Query ( 0 , 3 ) , new Query ( 1 , 5 ) , new Query ( 4 , 5 ) } ; int M = Q . length ; minimumDifference ( arr , Q , N , M ) ; } }
Count of N digit numbers which contains all single digit primes | Java program for the above approach ; Stores the index of prime numbers ; Function ; If index = N + 1 ; Find count of distinct prime numbers by counting number of set bits . ; If count of distinct prime numbers is equal to 4 return 1. ; 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 the digit is a prime number , set the index of the digit to 1 in the bitmask . ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; Return the answer . ; Driver Code ; Initializing dp array with - 1. ; Indexing prime numbers in ascending order ; Given Input ; Function call
import java . io . * ; import java . util . * ; class GFG { public static int [ ] [ ] dp = new int [ 100 ] [ ( 1 << 4 ) ] ; public static HashMap < Integer , Integer > primeIndex = new HashMap < > ( ) ; public static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static int countOfNumbers ( int index , int mask , int N ) { if ( index == N + 1 ) { int countOfPrimes = countSetBits ( mask ) ; if ( countOfPrimes == 4 ) { return 1 ; } return 0 ; } int val = dp [ index ] [ mask ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( primeIndex . containsKey ( digit ) ) { int newMask = mask | ( 1 << primeIndex . get ( digit ) ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( primeIndex . containsKey ( digit ) ) { int newMask = mask | ( 1 << primeIndex . get ( digit ) ) ; val += countOfNumbers ( index + 1 , newMask , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } return val ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < ( 1 << 4 ) ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } primeIndex . put ( 2 , 0 ) ; primeIndex . put ( 3 , 1 ) ; primeIndex . put ( 5 , 2 ) ; primeIndex . put ( 7 , 3 ) ; int N = 4 ; System . out . println ( countOfNumbers ( 1 , 0 , N ) ) ; } }
Count of N | Java program for the above approach ; Stores the dp - states ; Returns number of set bits in a number ; Recursive Function to find number of N - digit numbers which has equal count of distinct odd & even digits ; If index is N + 1 ; Find the count of set bits in the evenMask ; Find the count of set bits in the oddMask ; If the count of set bits in both masks are equal then return 1 as they have equal number of distinct odd and even digits ; If the state has already been computed ; If N = 1 , 0 can be also placed ; If digit is odd ; Set the ( digit / 2 ) th bit of the oddMask ; Set the ( digit / 2 ) th bit of the number evenMask ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If digit is odd ; Set the ( digit / 2 ) th bit of oddMask ; Set the ( digit / 2 ) th bit of evenMask ; Return the answer ; Function to find number of N - digit numbers which has equal count of distinct odd and even digits ; Initialize dp array with - 1 ; Function Call ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 1 << 5 ] [ 1 << 5 ] ; static int __builtin_popcount ( int n ) { int d , t = 0 ; while ( n > 0 ) { d = n % 2 ; n = n / 2 ; if ( d == 1 ) t ++ ; } return t ; } static int countOfNumbers ( int index , int evenMask , int oddMask , int N ) { if ( index == N + 1 ) { int countOfEvenDigits = __builtin_popcount ( evenMask ) ; int countOfOddDigits = __builtin_popcount ( oddMask ) ; if ( countOfOddDigits == countOfEvenDigits ) { return 1 ; } return 0 ; } int val = dp [ index ] [ evenMask ] [ oddMask ] ; if ( val != - 1 ) return val ; val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( ( digit & 1 ) != 0 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( ( digit & 1 ) != 0 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } return val ; } static void countNDigitNumber ( int N ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < ( 1 << 5 ) ; j ++ ) { for ( int k = 0 ; k < ( 1 << 5 ) ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; countNDigitNumber ( N ) ; } }
Minimum number of flips or swaps of adjacent characters required to make two strings equal | Java program for the above approach ; Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Update the value of A [ i ] ; Iterate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Driver code ; Given Input ; Function Call
import java . io . * ; class GFG { static int countMinSteps ( String A , String B , int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( A . charAt ( i - 1 ) == B . charAt ( i - 1 ) ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = dp [ i - 1 ] + 1 ; } if ( i >= 2 && A . charAt ( i - 2 ) == B . charAt ( i - 1 ) && A . charAt ( i - 1 ) == B . charAt ( i - 2 ) ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i - 2 ] + 1 ) ; } } return dp [ N ] ; } public static void main ( String [ ] args ) { String A = "0101" ; String B = "0011" ; int N = A . length ( ) ; System . out . println ( countMinSteps ( A , B , N ) ) ; } }
Count of N | Java program for the above approach ; Function to find the number of N digit numbers such that at least one digit occurs more than once ; Base Case ; If repeated is true , then for remaining positions any digit can be placed ; If the current state has already been computed , then return it ; Stores the count of number for the current recursive calls ; If n = 1 , 0 can be also placed ; If a digit has occurred for the second time , then set repeated to 1 ; Otherwise ; For remaining positions any digit can be placed ; If a digit has occurred for the second time , then set repeated to 1 ; Return the resultant count for the current recursive call ; Function to count all the N - digit numbers having at least one digit 's  occurrence more than once ; Initialize dp array with - 1 ; Function to count all possible number satisfying the given criteria ; Driver Code
import java . util . Arrays ; class GFG { public static int [ ] [ ] [ ] dp = new int [ 50 ] [ 1 << 10 ] [ 2 ] ; public static int countOfNumbers ( int digit , int mask , int repeated , int n ) { if ( digit == n + 1 ) { if ( repeated == 1 ) { return 1 ; } return 0 ; } if ( repeated == 1 ) { return ( int ) Math . pow ( 10 , n - digit + 1 ) ; } int val = dp [ digit ] [ mask ] [ repeated ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { if ( ( mask & ( 1 << i ) ) > 0 ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( ( mask & ( 1 << i ) ) > 0 ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } return val ; } public static void countNDigitNumber ( int N ) { for ( int i = 0 ; i < 50 ; i ++ ) { for ( int j = 0 ; j < 1 << 10 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void main ( String args [ ] ) { int N = 2 ; countNDigitNumber ( N ) ; } }
Construct the largest number whose sum of cost of digits is K | Java program for the above approach ; Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code
import java . util . * ; public class Main { static String getMaximum ( String S , String T ) { if ( S . indexOf ( "0" ) > - 1 ) return T ; if ( T . indexOf ( "0" ) > - 1 ) return S ; return S . length ( ) > T . length ( ) ? S : T ; } static String recursion ( int [ ] arr , int idx , int N , int K , Vector < Vector < String > > dp ) { if ( K == 0 ) { return " " ; } if ( K < 0 idx == N ) { return "0" ; } if ( dp . get ( idx ) . get ( K ) != " - 1" ) return dp . get ( idx ) . get ( K ) ; String include = String . valueOf ( idx + 1 ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; String exclude = recursion ( arr , idx + 1 , N , K , dp ) ; dp . get ( idx ) . set ( K , getMaximum ( include , exclude ) ) ; return dp . get ( idx ) . get ( K ) ; } static String largestNumber ( int [ ] arr , int N , int K ) { Vector < Vector < String > > dp = new Vector < Vector < String > > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) { dp . add ( new Vector < String > ( ) ) ; for ( int j = 0 ; j < K + 1 ; j ++ ) { dp . get ( i ) . add ( " - 1" ) ; } } String ans = recursion ( arr , 0 , N , K , dp ) ; return ans == " " ? "0" : ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 } ; int K = 14 ; int N = arr . length ; System . out . print ( largestNumber ( arr , N , K ) ) ; } }
Count of N | Java program for the above approach ; Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; 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 . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; 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 ] ; static int countOfNumbers ( int digit , int prev , int n ) { if ( digit == n + 1 ) { return 1 ; } int val = dp [ digit ] [ prev ] ; 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 , n ) ; } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( ( i & prev ) == 0 ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } } return val ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int N = 3 ; System . out . println ( countOfNumbers ( 1 , 0 , N ) ) ; } }
Maximum sum among all ( a x b ) submatrices for given Q queries | Java program for the above approach ; Function to preprcess input mat [ N ] [ M ] . This function mainly fills dp [ N ] [ M ] such that dp [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to dp [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using dp [ ] [ ] which is built by the preprocess function ; Result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add dp [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Function to find the maximum sum among all ( a x b ) sub - matrices of the matrix ; Function call ; Run a loop for finding answer for all queries ; Driver Code ; Given input ; Function call ; Print answer for all queries
public class Solution { static void preProcess ( int mat [ ] [ ] , int dp [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < m ; i ++ ) { dp [ 0 ] [ i ] = mat [ 0 ] [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + mat [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } static int sumQuery ( int dp [ ] [ ] , int tli , int tlj , int rbi , int rbj ) { int res = dp [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - dp [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - dp [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + dp [ tli - 1 ] [ tlj - 1 ] ; return res ; } static int [ ] maxSubMatrixSumQueries ( int [ ] [ ] mat , int n , int m , int [ ] [ ] queries , int q ) { int dp [ ] [ ] = new int [ n ] [ m ] ; preProcess ( mat , dp , n , m ) ; int maxSum [ ] = new int [ queries . length ] ; for ( int qi = 0 ; qi < q ; qi ++ ) { for ( int i = 0 ; i < n - queries [ qi ] [ 0 ] + 1 ; i ++ ) { for ( int j = 0 ; j < m - queries [ qi ] [ 1 ] + 1 ; j ++ ) { maxSum [ qi ] = Math . max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi ] [ 0 ] - 1 , j + queries [ qi ] [ 1 ] - 1 ) ) ; } } } return maxSum ; } public static void main ( String args [ ] ) { int n = 3 , m = 4 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 9 } , { 4 , 5 , 6 , 2 } , { 8 , 3 , 2 , 6 } } ; int Q = 3 ; int Queries [ ] [ ] = { { 1 , 1 } , { 2 , 2 } , { 3 , 3 } } ; int maxSum [ ] = maxSubMatrixSumQueries ( mat , n , m , Queries , Q ) ; for ( int i = 0 ; i < Q ; i ++ ) { System . out . print ( maxSum [ i ] + " ▁ " ) ; } } }
Number of ways such that only K bars are visible from the left | Java implementation for the above approach ; dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Initialize dp array ; Function call
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1005 ] [ 1005 ] ; static int KvisibleFromLeft ( int N , int K ) { if ( dp [ N ] [ K ] != - 1 ) return dp [ N ] [ K ] ; if ( N == K ) return dp [ N ] [ K ] = 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N ] [ K ] = ans ; } return dp [ N ] [ K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void main ( String [ ] args ) { int N = 5 , K = 2 ; for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . print ( KvisibleFromLeft ( N , K ) ) ; } }