codeparrot/xlcost-text-to-code huggingface dataset:
Dynamic Programming | High | A naive recursive Java program to find maximum tasks . ; Returns maximum amount of task that can be done till day n ; If n is less than equal to 0 , then no solution exists ; Determines which task to choose on day n , then returns the maximum till that day ; Driver code
class GFG { static int maxTasks ( int high [ ] , int low [ ] , int n ) { if ( n <= 0 ) return 0 ; return Math . max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; int high [ ] = { 3 , 6 , 8 , 7 , 6 } ; int low [ ] = { 1 , 5 , 4 , 5 , 3 } ; System . out . println ( maxTasks ( high , low , n ) ) ; } }
Binomial Coefficient | DP |  ; function to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; base case ; better time complexity for lesser r value ; array of elements from n - r + 1 to n ; for numbers from 1 to r find arr [ j ] such that gcd ( i , arr [ j ] ) > 1 ; if gcd > 1 , divide both by gcd ; if i becomes 1 , no need to search arr ; for ( int i : arr ) single pass to multiply the numerator ; Driver code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int nCr ( int n , int r ) { if ( r > n ) return 0 ; if ( r > n - r ) C ( n , r ) = C ( n , n - r ) r = n - r ; int mod = 1000000007 ; int [ ] arr = new int [ r ] ; for ( int i = n - r + 1 ; i <= n ; i ++ ) { arr [ i + r - n - 1 ] = i ; } long ans = 1 ; for ( int k = 1 ; k < r + 1 ; k ++ ) { int j = 0 , i = k ; while ( j < arr . length ) { int x = gcd ( i , arr [ j ] ) ; if ( x > 1 ) { arr [ j ] /= x ; i /= x ; } if ( i == 1 ) break ; j += 1 ; } } ans = ( ans * i ) % mod ; return ( int ) ans ; } public static void main ( String [ ] args ) { int n = 5 , r = 2 ; System . out . print ( " Value ▁ of ▁ C ( " + n + " , ▁ " + r + " ) ▁ is ▁ " + nCr ( n , r ) + "NEW_LINE"); } }
Kth character after replacing each character of String by its frequency exactly X times | Java program for the above approach ; Function to find the Kth character after X days ; Variable to store the KthChar ; Traverse the string ; Convert char into int ; Calculate characters ; If K is less than sum than ans = str [ i ] ; Return answer ; Driver code ; Given Input ; Function Call
class GFG { static char FindKthChar ( String str , int K , int X ) { char ans = ' ▁ ' ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int digit = ( int ) str . charAt ( i ) - 48 ; int range = ( int ) Math . pow ( digit , X ) ; sum += range ; if ( K <= sum ) { ans = str . charAt ( i ) ; break ; } } return ans ; } public static void main ( String [ ] args ) { String str = "123" ; int K = 9 ; int X = 3 ; char ans = FindKthChar ( str , K , X ) ; System . out . println ( ans ) ; } }
Total character pairs from two strings , with equal number of set bits in their ascii value | Java implementation of the approach ; Function to return the count of valid pairs ; Default Initialise both arrays 0 Store frequency of number of set bits for s1 ; Store frequency of number of set bits for s2 ; Calculate total pairs ; Return the count of valid pairs ; Driver code
class GFG { static int totalPairs ( String s1 , String s2 ) { int count = 0 ; int [ ] arr1 = new int [ 7 ] ; int [ ] arr2 = new int [ 7 ] ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { int set_bits = Integer . bitCount ( s1 . charAt ( i ) ) ; arr1 [ set_bits ] ++ ; } for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { int set_bits = Integer . bitCount ( s2 . charAt ( i ) ) ; arr2 [ set_bits ] ++ ; } for ( int i = 1 ; i <= 6 ; i ++ ) { count += ( arr1 [ i ] * arr2 [ i ] ) ; } return count ; } public static void main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " forgeeks " ; System . out . println ( totalPairs ( s1 , s2 ) ) ; } }
Count substrings that starts with character X and ends with character Y | Java implementation to count substrings starting with character X and ending with character Y ; function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countSubstr ( String str , int n , char x , char y ) { int tot_count = 0 ; int count_x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == x ) count_x ++ ; if ( str . charAt ( i ) == y ) tot_count += count_x ; } return tot_count ; } public static void main ( String args [ ] ) { String str = " abbcaceghcak " ; int n = str . length ( ) ; char x = ' a ' , y = ' c ' ; System . out . print ( " Count ▁ = ▁ " + countSubstr ( str , n , x , y ) ) ; } }
Count words in a given string | Java program to count no of words from given input string . ; returns number of words in str ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Move to next character ; Driver program to test above functions
public class GFG { static final int OUT = 0 ; static final int IN = 1 ; static int countWords ( String str ) { int state = OUT ; int wc = 0 ; int i = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) == ' ▁ ' || str . charAt ( i ) == 'NEW_LINE' || str . charAt ( i ) == ' TABSYMBOL ' ) state = OUT ; else if ( state == OUT ) { state = IN ; ++ wc ; } ++ i ; } return wc ; } public static void main ( String args [ ] ) { String str = "One two	three four five "; System . out . println ( " No ▁ of ▁ words ▁ : ▁ " + countWords ( str ) ) ; } }
Enneadecagonal number | Java program to find nth Enneadecagonal number ; Function to calculate Enneadecagonal number ; Formula for finding nth Enneadecagonal number ; Driver Code
import java . io . * ; class GFG { static int nthEnneadecagonal ( int n ) { return ( 17 * n * n - 15 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . print ( n + " th ▁ Enneadecagonal ▁ number ▁ : " ) ; System . out . println ( nthEnneadecagonal ( n ) ) ; } }
Area of a Circumscribed Circle of a Square | Java program to calculate area of a circumscribed circle - square ; Utility Function ; Driver Function
import java . io . * ; class Gfg { static float areacircumscribed ( float a ) { float PI = 3.14159265f ; return ( a * a * ( PI / 2 ) ) ; } public static void main ( String arg [ ] ) { float a = 6 ; System . out . print ( " Area ▁ of ▁ an ▁ circumscribed " + " circle ▁ is ▁ : " ) ; System . out . println ( areacircumscribed ( a ) ) ; } }
Find Nth item distributed from infinite items of infinite types based on given conditions | Java program for the above approach ; Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate to find the Nth day present is given out ; Find the number of presents given on day is day * ( day + 1 ) / 2 ; Iterate over the type ; Return the resultant type ; Driver Code
import java . util . * ; class GFG { static int itemType ( int n ) { int count = 0 ; int day = 1 ; while ( count + day * ( day + 1 ) / 2 < n ) { count += day * ( day + 1 ) / 2 ; day ++ ; } for ( int type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) { return type ; } } return 0 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( itemType ( N ) ) ; } }
Check if linked list is sorted ( Iterative and Recursive ) | Java program to check Linked List is sorted in descending order or not ; Linked list node ; function to Check Linked List is sorted in descending order or not ; Traverse the list till last node and return false if a node is smaller than or equal its next . ; Driver Code
class GFG { static class Node { int data ; Node next ; } ; static boolean isSortedDesc ( Node head ) { if ( head == null ) return true ; for ( Node t = head ; t . next != null ; t = t . next ) if ( t . data <= t . next . data ) return false ; return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . next = null ; temp . data = data ; return temp ; } public static void main ( String [ ] args ) { Node head = newNode ( 7 ) ; head . next = newNode ( 5 ) ; head . next . next = newNode ( 4 ) ; head . next . next . next = newNode ( 3 ) ; if ( isSortedDesc ( head ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Maximum length of consecutive 1 s or 0 s after flipping at most K characters | Java program for the above approach ; Function to find the maximum length continuos segment of character c after flipping at most K characters ; Stores the maximum length ; Stores the count of char ' c ' ; Start of window ; Remove the extra ' c ' from left ; Increment the value of the left ; Update the resultant maximum length of character ch ; Function to find the maximum length of consecutive 0 s or 1 s by flipping at most K characters of the string ; Print the maximum of the maximum length of 0 s or 1 s ; Driver Code
public class GFG { static int maxLength ( String str , int n , char c , int k ) { int ans = - 1 ; int cnt = 0 ; int left = 0 ; for ( int right = 0 ; right < n ; right ++ ) { if ( str . charAt ( right ) == c ) { cnt ++ ; } while ( cnt > k ) { if ( str . charAt ( left ) == c ) { cnt -- ; } left ++ ; } ans = Math . max ( ans , right - left + 1 ) ; } return ans ; } static int maxConsecutiveSegment ( String S , int K ) { int N = S . length ( ) ; return Math . max ( maxLength ( S , N , '0' , K ) , maxLength ( S , N , '1' , K ) ) ; } int main ( ) { return 0 ; } public static void main ( String [ ] args ) { String S = "1001" ; int K = 1 ; System . out . println ( maxConsecutiveSegment ( S , K ) ) ; } }
Minimize coins required to obtain all possible values up to N | Java program to implement the above approach ; Function to find minimum count of { 1 , 2 , 5 } valued coins required to make a change of all values in the range [ 1 , N ] ; Number of 5 valueds coins required ; Number of 1 valued coins required ; Number of 2 valued coins required ; Driver Code
import java . util . * ; class GFG { static void find ( int N ) { int T , F , O ; F = ( int ) ( ( N - 4 ) / 5 ) ; if ( ( ( N - 5 * F ) % 2 ) == 0 ) { O = 2 ; } else { O = 1 ; } T = ( int ) Math . floor ( ( N - 5 * F - O ) / 2 ) ; System . out . println ( " Count ▁ of ▁ 5 ▁ valueds ▁ coins : ▁ " + F ) ; System . out . println ( " Count ▁ of ▁ 2 ▁ valueds ▁ coins : ▁ " + T ) ; System . out . println ( " Count ▁ of ▁ 1 ▁ valueds ▁ coins : ▁ " + O ) ; } public static void main ( String args [ ] ) { int N = 8 ; find ( N ) ; } }
Replace ' ? ' to convert given string to a binary string with maximum count of '0' and "10" | Java program to implement the above approach ; Function to maximize count of 0 and 10 by replacing character ' ? ' to '0' or '1' ; Traverse the given String ; If current character is ' ? ' ; Replace str [ i ] to '0' ; Driver Code ; Given String
class GFG { static void findMaxOccurence ( char [ ] str , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ? ' ) { str [ i ] = '0' ; } } System . out . print ( str ) ; } public static void main ( String [ ] args ) { String str = "10?0?11" ; int N = str . length ( ) ; findMaxOccurence ( str . toCharArray ( ) , N ) ; } }
Check if given string is a substring of string formed by repeated concatenation of z to a | Java program for the above approach ; Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Driver code ; Given string ; Function call
class GFG { public static void checkInfinite ( String s ) { boolean flag = true ; int N = s . length ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s . charAt ( i ) == ( char ) ( ( int ) ( s . charAt ( i + 1 ) ) + 1 ) ) { continue ; } else if ( s . charAt ( i ) == ' a ' && s . charAt ( i + 1 ) == ' z ' ) { continue ; } else { flag = false ; break ; } } if ( ! flag ) System . out . print ( " NO " ) ; else System . out . print ( " YES " ) ; } public static void main ( String [ ] args ) { String s = " ecbaz " ; checkInfinite ( s ) ; } }
Minimum change in lanes required to cross all barriers | Java program for the above approach ; Function to find the minimum number of changes of lane required ; If there is a barrier , then add very large value ; Add the minimum value to move forword with or without crossing barrier ; Return the minimum value of dp [ 0 ] , dp [ 1 ] and dp [ 2 ] ; Driver Code
class GFG { static int minChangeInLane ( int barrier [ ] , int n ) { int dp [ ] = { 1 , 0 , 1 } ; for ( int j = 0 ; j < n ; j ++ ) { int val = barrier [ j ] ; if ( val > 0 ) { dp [ val - 1 ] = ( int ) 1e6 ; } for ( int i = 0 ; i < 3 ; i ++ ) { if ( val != i + 1 ) { dp [ i ] = Math . min ( dp [ i ] , Math . min ( dp [ ( i + 1 ) % 3 ] , dp [ ( i + 2 ) % 3 ] ) + 1 ) ; } } } return Math . min ( dp [ 0 ] , Math . min ( dp [ 1 ] , dp [ 2 ] ) ) ; } public static void main ( String [ ] args ) { int barrier [ ] = { 0 , 1 , 2 , 3 , 0 } ; int N = barrier . length ; System . out . print ( minChangeInLane ( barrier , N ) ) ; } }
Queries to count groups of N students possible having sum of ratings within given range | Java program for the above approach ; Function to count number of ways to get given sum groups ; Initialise dp array ; Mark all 1 st row values as 1 since the mat [ 0 ] [ i ] is all possible sums in first row ; Fix the ith row ; Fix the sum ; Iterate through all values of ith row ; If sum can be obtained ; Find the prefix sum of last row ; Traverse each query ; No of ways to form groups ; Driver Code ; Given N batches and K students ; Given ratings ; Given Queries ; Function Call
import java . util . * ; public class Main { public static void numWays ( int [ ] [ ] ratings , int queries [ ] [ ] , int n , int k ) { int dp [ ] [ ] = new int [ n ] [ 10000 + 2 ] ; for ( int i = 0 ; i < k ; i ++ ) dp [ 0 ] [ ratings [ 0 ] [ i ] ] += 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int sum = 0 ; sum <= 10000 ; sum ++ ) { for ( int j = 0 ; j < k ; j ++ ) { if ( sum >= ratings [ i ] [ j ] ) dp [ i ] [ sum ] += dp [ i - 1 ] [ sum - ratings [ i ] [ j ] ] ; } } } for ( int sum = 1 ; sum <= 10000 ; sum ++ ) { dp [ n - 1 ] [ sum ] += dp [ n - 1 ] [ sum - 1 ] ; } for ( int q = 0 ; q < queries . length ; q ++ ) { int a = queries [ q ] [ 0 ] ; int b = queries [ q ] [ 1 ] ; System . out . print ( dp [ n - 1 ] [ b ] - dp [ n - 1 ] [ a - 1 ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 2 , K = 3 ; int ratings [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; int queries [ ] [ ] = { { 6 , 6 } , { 1 , 6 } } ; numWays ( ratings , queries , N , K ) ; } }
Number of permutation with K inversions | Set 2 | Java program for the above approach ; Function to count permutations with K inversions ; Store number of permutations with K inversions ; If N = 1 only 1 permutation with no inversion ; For K = 0 only 1 permutation with no inversion ; Otherwise Update each dp state as per the reccurrance relation formed ; Print final count ; Driver Code ; Given N and K ; Function Call
import java . io . * ; class GFG { static void numberOfPermWithKInversion ( int N , int K ) { int [ ] [ ] dp = new int [ 2 ] [ K + 1 ] ; int mod = 1000000007 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 0 ; j <= K ; j ++ ) { if ( i == 1 ) { dp [ i % 2 ] [ j ] = ( j == 0 ) ? 1 : 0 ; } else if ( j == 0 ) dp [ i % 2 ] [ j ] = 1 ; else { int maxm = Math . max ( j - ( i - 1 ) ) ; dp [ i % 2 ] [ j ] = ( dp [ i % 2 ] [ j - 1 ] % mod + ( dp [ 1 - i % 2 ] [ j ] - ( ( Math . max ( j - ( i - 1 ) , 0 ) == 0 ) ? 0 : dp [ 1 - i % 2 ] [ maxm , 0 ) - 1 ] ) + mod ) % mod ) % mod ; } } } System . out . println ( dp [ N % 2 ] [ K ] ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; numberOfPermWithKInversion ( N , K ) ; } }
Maximum sum path in a Matrix | No of rows and columns ; Declaring the matrix of maximum 100 rows and 100 columns ; Variable visited is used to keep track of all the visited positions Variable dp is used to store maximum sum till current position ; For storing current sum ; For continuous update of maximum sum required ; Function to Input the matrix of size n * m ; Function to calculate maximum sum of path ; Checking boundary condition ; Checking whether or not ( i , j ) is visited ; Marking ( i , j ) is visited ; Checking whether the position hasn 't  visited the last row or the last column.  Making recursive call for all the possible  moves from the current cell and then adding the  maximum returned by the calls and updating it. ; Checking whether position has reached last row ; If the position is in the last column ; Updating the maximum sum till the current position in the dp ; Returning the updated maximum value ; Driver Code ; Calling the implemented function
class GFG { static final int N = 100 ; static int n , m ; static int a [ ] [ ] = new int [ N ] [ N ] ; static int dp [ ] [ ] = new int [ N ] [ N ] ; static int visited [ ] [ ] = new int [ N ] [ N ] ; static int current_sum = 0 ; static int total_sum = 0 ; static void inputMatrix ( ) { n = 3 ; m = 3 ; a [ 0 ] [ 0 ] = 500 ; a [ 0 ] [ 1 ] = 100 ; a [ 0 ] [ 2 ] = 230 ; a [ 1 ] [ 0 ] = 1000 ; a [ 1 ] [ 1 ] = 300 ; a [ 1 ] [ 2 ] = 100 ; a [ 2 ] [ 0 ] = 200 ; a [ 2 ] [ 1 ] = 1000 ; a [ 2 ] [ 2 ] = 200 ; } static int maximum_sum_path ( int i , int j ) { if ( i == n - 1 && j == m - 1 ) return a [ i ] [ j ] ; if ( visited [ i ] [ j ] != 0 ) return dp [ i ] [ j ] ; visited [ i ] [ j ] = 1 ; int total_sum = 0 ; if ( i < n - 1 & j < m - 1 ) { int current_sum = Math . max ( maximum_sum_path ( i , j + 1 ) , Math . max ( maximum_sum_path ( i + 1 , j + 1 ) , maximum_sum_path ( i + 1 , j ) ) ) ; total_sum = a [ i ] [ j ] + current_sum ; } else if ( i == n - 1 ) total_sum = a [ i ] [ j ] + maximum_sum_path ( i , j + 1 ) ; else total_sum = a [ i ] [ j ] + maximum_sum_path ( i + 1 , j ) ; dp [ i ] [ j ] = total_sum ; return total_sum ; } public static void main ( String [ ] args ) { inputMatrix ( ) ; int maximum_sum = maximum_sum_path ( 0 , 0 ) ; System . out . println ( maximum_sum ) ; } }
Treasure and Cities | k is current index and col is previous color . ; base case ; check if color of this city is equal to prev visited city ; return max of both options ; Driver code ; Initially begin with color 0
class GFG { static int MaxProfit ( int treasure [ ] , int color [ ] , int n , int k , int col , int A , int B ) { int sum = 0 ; if ( k == n ) return 0 ; if ( col == color [ k ] ) sum += Math . max ( A * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; else sum += Math . max ( B * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; return sum ; } public static void main ( String [ ] args ) { int A = - 5 , B = 7 ; int treasure [ ] = { 4 , 8 , 2 , 9 } ; int color [ ] = { 2 , 2 , 6 , 2 } ; int n = color . length ; System . out . print ( MaxProfit ( treasure , color , n , 0 , 0 , A , B ) ) ; } }
Tetranacci Numbers | A simple recursive Java program to print the nth tetranacci numbers . ; Function to return the N - th tetranacci number ; base cases ; base cases ; base cases ; function to print the Nth tetranacci number ; Driver code
class GFG { static int printTetraRec ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return 1 ; if ( n == 3 ) return 2 ; else return printTetraRec ( n - 1 ) + printTetraRec ( n - 2 ) + printTetraRec ( n - 3 ) + printTetraRec ( n - 4 ) ; } static void printTetra ( int n ) { System . out . println ( printTetraRec ( n ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 ; printTetra ( n ) ; } }
Sum of products of all combination taken ( 1 to n ) at a time | Program to find SOP of all combination taken ( 1 to N ) at a time using brute force ; to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver code ; storing numbers from 1 - N in array ; calling allCombination
import java . io . * ; class GFG { static int sum = 0 ; static void Combination ( int [ ] a , int [ ] combi , int n , int r , int depth , int index ) { if ( index == r ) { int product = 1 ; for ( int i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( int i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } static void allCombination ( int [ ] a , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int [ ] combi = new int [ i ] ; Combination ( a , combi , n , i , 0 , 0 ) ; System . out . print ( " f ( " + i + " ) ▁ - - > ▁ " + sum + "NEW_LINE"); sum = 0 ; } } public static void main ( String args [ ] ) { int n = 5 ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ; } }
Dynamic Programming | High | A DP based Java program to find maximum tasks . ; Returns the maximum among the 2 numbers ; Returns maximum amount of task that can be done till day n ; An array task_dp that stores the maximum task done ; If n = 0 , no solution exists ; If n = 1 , high effort task on that day will be the solution ; Fill the entire array determining which task to choose on day i ; Driver code
class GFG { static int max ( int x , int y ) { return ( x > y ? x : y ) ; } static int maxTasks ( int [ ] high , int [ ] low , int n ) { int [ ] task_dp = new int [ n + 1 ] ; task_dp [ 0 ] = 0 ; task_dp [ 1 ] = high [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) task_dp [ i ] = Math . max ( high [ i - 1 ] + task_dp [ i - 2 ] , low [ i - 1 ] + task_dp [ i - 1 ] ) ; return task_dp [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] high = { 3 , 6 , 8 , 7 , 6 } ; int [ ] low = { 1 , 5 , 4 , 5 , 3 } ; System . out . println ( maxTasks ( high , low , n ) ) ; } }
Permutation Coefficient | A O ( n ) time and O ( 1 ) extra space solution to calculate the Permutation Coefficient ; Compute n ! and ( n - k ) ! ; Driver Code
import java . io . * ; class GFG { static int PermutationCoeff ( int n , int k ) { int Fn = 1 , Fk = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { Fn *= i ; if ( i == n - k ) Fk = Fn ; } int coeff = Fn / Fk ; return coeff ; } public static void main ( String args [ ] ) { int n = 10 , k = 2 ; System . out . println ( " Value ▁ of ▁ P ( ▁ " + n + " , " + k + " ) ▁ is ▁ " + PermutationCoeff ( n , k ) ) ; } }