codeparrot/xlcost-text-to-code huggingface dataset:
Count of subarrays with maximum value as K | Java program for the above approach ; Function to count the subarrays with maximum not greater than K ; If arr [ i ] > k then arr [ i ] cannot be a part of any subarray . ; Count the number of elements where arr [ i ] is not greater than k . ; Summation of all possible subarrays in the variable ans . ; Function to count the subarrays with maximum value is equal to K ; Stores count of subarrays with max <= k - 1. ; Stores count of subarrays with max >= k + 1. ; Stores count of subarrays with max = k . ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static int totalSubarrays ( int arr [ ] , int n , int k ) { int ans = 0 , i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } int count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } ans += ( ( count * ( count + 1 ) ) / 2 ) ; } return ans ; } static int countSubarrays ( int arr [ ] , int n , int k ) { int count1 = totalSubarrays ( arr , n , k - 1 ) ; int count2 = totalSubarrays ( arr , n , k ) ; int ans = count2 - count1 ; return ans ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; int arr [ ] = { 2 , 1 , 3 , 4 } ; System . out . println ( countSubarrays ( arr , n , k ) ) ; } }
Maximum subsequence sum such that no three are consecutive in O ( 1 ) space | Java program for the above approach ; Function to calculate the maximum subsequence sum such that no three elements are consecutive ; when N is 1 , answer would be the only element present ; when N is 2 , answer would be sum of elements ; variable to store sum up to i - 3 ; variable to store sum up to i - 2 ; variable to store sum up to i - 1 ; variable to store the final sum of the subsequence ; find the maximum subsequence sum up to index i ; update first , second and third ; return ans ; ; Driver code ; Input ; Function call
import java . io . * ; class GFG { public static int maxSumWO3Consec ( int A [ ] , int N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; int third = A [ 0 ] ; int second = third + A [ 1 ] ; int first = Math . max ( second , A [ 1 ] + A [ 2 ] ) ; int sum = Math . max ( Math . max ( third , second ) , first ) ; for ( int i = 3 ; i < N ; i ++ ) { sum = Math . max ( Math . max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } public static void main ( String [ ] args ) { int A [ ] = { 3000 , 2000 , 1000 , 3 , 10 } ; int N = A . length ; int res = maxSumWO3Consec ( A , N ) ; System . out . println ( res ) ; } }
Minimum number of given operations required to reduce a number to 2 | Java program for the above approach ; Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Driver code ; Given Input ; Function Call ; Print the result
public class GFG { static int findMinOperations ( int n ) { int i = 0 ; int dp [ ] = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 28 ; int m = findMinOperations ( n ) ; if ( m != 9999 ) System . out . println ( m ) ; else System . out . println ( - 1 ) ; } }
Number of M | Java program for the above approach ; Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; Create an array of size M + 1 ; Base cases ; Fill the dp table ; dp [ j ] will be equal to maximum number of sorted array of size j when elements are taken from 1 to i ; Here dp [ m ] will be equal to the maximum number of sorted arrays when element are taken from 1 to i ; Return the result ; Driver code ; Given Input ; Function Call
public class Main { static int countSortedArrays ( int n , int m ) { int [ ] dp = new int [ ( m + 1 ) ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . print ( countSortedArrays ( n , m ) ) ; } }
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | Java program for the above approach ; Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Base Case ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver Code
import java . util . * ; public class Main { static int getValue ( int [ ] arr , int N ) { int dp [ ] = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { int min = arr [ i ] ; int max = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { min = Math . min ( arr [ j ] , min ) ; max = Math . max ( arr [ j ] , max ) ; dp [ i ] = Math . max ( dp [ i ] , max - min + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . length ; System . out . println ( getValue ( arr , N ) ) ; } }
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Java program for the above approach ; Function to count 0 's in a string ; Stores count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive function to find the length of longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; If idx is equal to N or A + B is equal to 0 ; Stores the count of 0 's in arr[idx] ; Stores the count of 1 's in arr[idx] ; Stores the length of the subset if arr [ i ] is included ; If zero is less than or equal to A and one is less than or equal to B ; Stores the length of the subset if arr [ i ] is excluded ; Returns max of inc and exc ; Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; Return ;
import java . util . * ; class GFG { static int count0 ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { count ++ ; } } return count ; } static int solve ( String [ ] vec , int A , int B , int idx ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . length ( ) - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } int exc = solve ( vec , A , B , idx + 1 ) ; return Math . max ( inc , exc ) ; } static int MaxSubsetlength ( String [ ] arr , int A , int B ) { return solve ( arr , A , B , 0 ) ; } public static void main ( String [ ] args ) { String [ ] arr = { "1" , "0" , "10" } ; int A = 1 , B = 1 ; System . out . print ( MaxSubsetlength ( arr , A , B ) ) ; } }
Maximum score possible by removing substrings made up of single distinct character | Java program for the above approach ; Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver Code
import java . util . * ; class GFG { static boolean isUnique ( String s ) { HashSet < Character > set = new HashSet < > ( ) ; for ( char c : s . toCharArray ( ) ) set . add ( c ) ; return set . size ( ) == 1 ; } static int maxScore ( String s , int [ ] a ) { int n = s . length ( ) ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { String sub = s . substring ( i , j + 1 ) ; if ( isUnique ( sub ) ) mx = Math . max ( mx , a [ sub . length ( ) - 1 ] + maxScore ( s . substring ( 0 , i ) + s . substring ( j + 1 ) , a ) ) ; } } return mx ; } public static void main ( String args [ ] ) { String s = "011" ; int a [ ] = { 1 , 3 , 1 } ; System . out . print ( maxScore ( s , a ) ) ; } }
Maximum score possible by removing substrings made up of single distinct character | Java program for the above approach ; Initialize a dictionary to store the precomputed results ; Function to calculate the maximum score possible by removing substrings ; If s is present in dp [ ] array ; Base Cases : ; If length of string is 0 ; If length of string is 1 ; Put head pointer at start ; Initialize the max variable ; Generate the substrings using two pointers ; If s [ head ] and s [ tail ] are different ; Move head to tail and break ; Store the substring ; Update the maximum ; Move the tail ; Store the score ; Driver code
import java . util . * ; class GFG { static Map < String , Integer > dp = new HashMap < > ( ) ; static int maxScore ( String s , int [ ] a ) { if ( dp . containsKey ( s ) ) return dp . get ( s ) ; int n = s . length ( ) ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int head = 0 ; int mx = - 1 ; while ( head < n ) { int tail = head ; while ( tail < n ) { if ( s . charAt ( tail ) != s . charAt ( head ) ) { head = tail ; break ; } String sub = s . substring ( head , tail + 1 ) ; mx = Math . max ( mx , a [ sub . length ( ) - 1 ] + maxScore ( s . substring ( 0 , head ) + s . substring ( tail + 1 , s . length ( ) ) , a ) ) ; tail += 1 ; } if ( tail == n ) break ; } dp . put ( s , mx ) ; return mx ; } public static void main ( String [ ] args ) { String s = " abb " ; int [ ] a = { 1 , 3 , 1 } ; System . out . println ( ( maxScore ( s , a ) ) ) ; } }
Count all unique outcomes possible by performing S flips on N coins | Java program for the above approach ; Dimensions of the DP table ; Stores the dp states ; Function to recursively count the number of unique outcomes possible by performing S flips on N coins ; Base Case ; If the count for the current state is not calculated , then calculate it recursively ; Otherwise return the already calculated value ; Driver Code
import java . util . * ; class GFG { static int size = 100 ; static int [ ] [ ] ans = new int [ size ] [ size ] ; static void initialize ( ) { for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } static int numberOfUniqueOutcomes ( int n , int s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ans [ n ] [ s ] == 0 ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } public static void main ( String args [ ] ) { initialize ( ) ; int N = 5 , S = 8 ; System . out . println ( numberOfUniqueOutcomes ( N , S ) ) ; } }
Minimize removals to remove another string as a subsequence of a given string | Java program for the above approach ; Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Driver code ; Input ; Function call to get minimum number of character removals
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void printMinimumRemovals ( String str , String X ) { int N = str . length ( ) ; int M = X . length ( ) ; int dp [ ] [ ] = new int [ N ] [ M ] ; for ( int j = 0 ; j < M ; j ++ ) { if ( str . charAt ( 0 ) == X . charAt ( j ) ) { dp [ 0 ] [ j ] = 1 ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( str . charAt ( i ) == X . charAt ( j ) ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } System . out . println ( dp [ N - 1 ] [ M - 1 ] ) ; } public static void main ( String [ ] args ) { String str = " btagd " ; String X = " bad " ; printMinimumRemovals ( str , X ) ; } }
Railway Station | TCS CodeVita 2020 | Java program for the above approach ; Dp table for memoization ; Function to count the number of ways to N - th station ; Base Cases ; If current state is already evaluated ; Count ways in which train 1 can be chosen ; Count ways in which train 2 can be chosen ; Count ways in which train 3 can be chosen ; Store the current state ; Return the number of ways ; Driven Code ; Given Input ; Initialize DP table with - 1 ; Function call to count the number of ways to reach the n - th station
import java . util . * ; class GFG { static int dp [ ] = new int [ 100000 ] ; static int findWays ( int x ) { if ( x < 0 ) return 0 ; if ( x == 0 ) return 1 ; if ( x == 1 ) return 2 ; if ( x == 2 ) return 4 ; if ( dp [ x ] != - 1 ) return dp [ x ] ; int count = findWays ( x - 1 ) ; count += findWays ( x - 2 ) ; count += findWays ( x - 3 ) ; dp [ x ] = count ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 4 ; Arrays . fill ( dp , - 1 ) ; System . out . print ( findWays ( n ) ) ; } }
Maximum sum not exceeding K possible for any rectangle of a Matrix | Java program for the above approach ; Function to find the maximum possible sum of arectangle which is less than K ; Stores the values ( cum_sum - K ) ; Insert 0 into the set sumSet ; Traverse over the rows ; Get cumulative sum from [ 0 to i ] ; Search for upperbound of ( cSum - K ) in the hashmap ; If upper_bound of ( cSum - K ) exists , then update max sum ; Insert cummulative value in the hashmap ; Return the maximum sum which is less than K ; Function to find the maximum sum of rectangle such that its sum is no larger than K ; Stores the number of rows and columns ; Store the required result ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between the current left and right for every row ; Stores the sum of rectangle ; Update the overall maximum sum ; Print the result ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int maxSubarraySum ( int [ ] sum , int k , int row ) { int curSum = 0 , curMax = Integer . MIN_VALUE ; Set < Integer > sumSet = new HashSet < Integer > ( ) ; sumSet . add ( 0 ) ; for ( int r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; ArrayList < Integer > list = new ArrayList < Integer > ( ) ; list . addAll ( sumSet ) ; int it = Integer . MIN_VALUE ; for ( int e : list ) if ( e >= ( curSum - k ) ) { it = e ; break ; } if ( it != Integer . MIN_VALUE ) { curMax = Math . max ( curMax , curSum - it ) ; } sumSet . add ( curSum ) ; } return curMax ; } static void maxSumSubmatrix ( int [ ] [ ] matrix , int k ) { int row = matrix . length ; int col = matrix [ 0 ] . length ; int ret = Integer . MIN_VALUE ; for ( int i = 0 ; i < col ; ++ i ) { int [ ] sum = new int [ row ] ; for ( int j = i ; j < col ; ++ j ) { for ( int r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r ] [ j ] ; } int curMax = maxSubarraySum ( sum , k , row ) ; ret = Math . max ( ret , curMax ) ; } } System . out . print ( ret ) ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 5 , - 4 , - 3 , 4 } , { - 3 , - 4 , 4 , 5 } , { 5 , 1 , 5 , - 4 } } ; int K = 8 ; maxSumSubmatrix ( matrix , K ) ; } }
Count N | Java program for the above approach ; Function to count binary strings of length N having substring "11" ; Initialize dp [ ] of size N + 1 ; Base Cases ; Stores the first N powers of 2 ; Generate ; Iterate over the range [ 2 , N ] ; Print total count of substrings ; Driver Code
import java . util . * ; class GFG { static void binaryStrings ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; int [ ] power = new int [ N + 1 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { power [ i ] = 2 * power [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ] ; } System . out . println ( dp [ N ] ) ; } public static void main ( String [ ] args ) { int N = 12 ; binaryStrings ( N ) ; } }
Maximum sum submatrix | Java program for the above approach ; Function to find maximum continuous maximum sum in the array ; Stores current and maximum sum ; Traverse the array v ; Add the value of the current element ; Update the maximum sum ; Return the maximum sum ; Function to find the maximum submatrix sum ; Store the rows and columns of the matrix ; Create an auxiliary matrix ; Traverse the matrix , prefix and initialize it will all 0 s ; Calculate prefix sum of all rows of matrix A [ ] [ ] and store in matrix prefix [ ] ; Update the prefix [ ] [ ] ; Store the maximum submatrix sum ; Iterate for starting column ; Iterate for last column ; To store current array elements ; Traverse every row ; Store the sum of the kth row ; Update the prefix sum ; Push it in a vector ; Update the maximum overall sum ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static int kadane ( Vector < Integer > v ) { int currSum = 0 ; int maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < ( int ) v . size ( ) ; i ++ ) { currSum += v . get ( i ) ; if ( currSum > maxSum ) { maxSum = currSum ; } if ( currSum < 0 ) { currSum = 0 ; } } return maxSum ; } static void maxSubmatrixSum ( int [ ] [ ] A ) { int r = A . length ; int c = A [ 0 ] . length ; int [ ] [ ] prefix = new int [ r ] [ ] ; for ( int i = 0 ; i < r ; i ++ ) { prefix [ i ] = new int ; for ( int j = 0 ; j < c ; j ++ ) { prefix [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { if ( j == 0 ) prefix [ i ] [ j ] = A [ i ] [ j ] ; else prefix [ i ] [ j ] = A [ i ] [ j ] + prefix [ i ] [ j - 1 ] ; } } int maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < c ; i ++ ) { for ( int j = i ; j < c ; j ++ ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int k = 0 ; k < r ; k ++ ) { int el = 0 ; if ( i == 0 ) el = prefix [ k ] [ j ] ; else el = prefix [ k ] [ j ] - prefix [ k ] [ i - 1 ] ; v . add ( el ) ; } maxSum = Math . max ( maxSum , kadane ( v ) ) ; } } System . out . print ( maxSum + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , - 2 , - 7 , 0 } , { 9 , 2 , - 6 , 2 } , { - 4 , 1 , - 4 , 1 } , { - 1 , 8 , 0 , - 2 } } ; maxSubmatrixSum ( matrix ) ; } }
Minimize cost of painting N houses such that adjacent houses have different colors | Java program for the above approach ; Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color ; Corner Case ; Auxiliary 2D dp array ; Base Case ; If current house is colored with red the take min cost of previous houses colored with ( blue and green ) ; If current house is colored with blue the take min cost of previous houses colored with ( red and green ) ; If current house is colored with green the take min cost of previous houses colored with ( red and blue ) ; Print the min cost of the last painted house ; Driver code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minCost ( int costs [ ] [ ] , int N ) { if ( N == 0 ) return ; int dp [ ] [ ] = new int [ N ] [ 3 ] ; dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] ; dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] ; dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] ; dp [ i ] [ 2 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] ; } System . out . println ( Math . min ( dp [ N - 1 ] [ 0 ] , Math . min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) ; } public static void main ( String [ ] args ) { int costs [ ] [ ] = { { 14 , 2 , 11 } , { 11 , 14 , 5 } , { 14 , 3 , 10 } } ; int N = costs . length ; minCost ( costs , N ) ; } }
Minimize count of flips required to make sum of the given array equal to 0 | Java program for the above approach ; Initialize dp [ ] [ ] ; Function to find the minimum number of operations to make sum of A [ ] 0 ; Initialize answer ; Base case ; Otherwise , return 0 ; Pre - computed subproblem ; Recurrence relation for finding the minimum of the sum of subsets ; Return the result ; Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0 ; Find the sum of array ; Initialise dp [ ] [ ] with - 1 ; No solution exists ; Otherwise ; If sum is odd , no subset is possible ; Driver Code ; Function Call
class GFG { static int [ ] [ ] dp = new int [ 2001 ] [ 2001 ] ; static int solve ( int [ ] A , int i , int sum , int N ) { int res = 2001 ; if ( sum < 0 || ( i == N && sum != 0 ) ) { return 2001 ; } if ( sum == 0 i >= N ) { return dp [ i ] [ sum ] = 0 ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } res = Math . min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) ; return dp [ i ] [ sum ] = res ; } static void minOp ( int [ ] A , int N ) { int sum = 0 ; for ( int it : A ) { sum += it ; } if ( sum % 2 == 0 ) { for ( int i = 0 ; i < 2001 ; i ++ ) { for ( int j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int ans = solve ( A , 0 , sum / 2 , N ) ; if ( ans < 0 ans > N ) { System . out . print ( " - 1" + "NEW_LINE"); } else { System . out . print ( ans + "NEW_LINE"); } } else { System . out . print ( " - 1" + "NEW_LINE"); } } public static void main ( String [ ] args ) { int [ ] A = { 2 , 3 , 1 , 4 } ; int N = A . length ; minOp ( A , N ) ; } }
Count subsequences having average of its elements equal to K | Java program for the above approach ; Stores the dp states ; Function to find the count of subsequences having average K ; Base condition ; Three loops for three states ; Recurrence relation ; Stores the sum of dp [ n ] [ j ] [ K * j ] all possible values of j with average K and sum K * j ; Iterate over the range [ 1 , N ] ; Return the final count ; Driver Code
import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 101 ] [ 101 ] [ 1001 ] ; static int countAverage ( int n , int K , int [ ] arr ) { dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = 0 ; k < n ; k ++ ) { for ( int s = 0 ; s <= 100 ; s ++ ) { dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ] ; dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ] ; } } } int cnt = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cnt += dp [ n ] [ j ] [ K * j ] ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 7 , 8 , 9 } ; int K = 8 ; int N = arr . length ; System . out . print ( countAverage ( N , K , arr ) ) ; } }