codeparrot/xlcost-text-to-code huggingface dataset:
Reverse a subarray to maximize sum of even | Java Program to implement the above approach ; Function to return maximized sum at even indices ; Stores difference with element on the left ; Stores difference with element on the right ; Compute and store left difference ; For first index ; If previous difference is positive ; Otherwise ; For the last index ; Otherwise ; For first index ; If the previous difference is positive ; Driver Code
import java . io . * ; class GFG { public static int maximizeSum ( int [ ] arr ) { int n = arr . length ; int sum = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) sum += arr [ i ] ; int leftDP [ ] = new int [ n / 2 ] ; int rightDP [ ] = new int [ n / 2 ] ; int c = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { int leftDiff = arr [ i ] - arr [ i - 1 ] ; if ( c - 1 < 0 ) leftDP = leftDiff ; else { if ( leftDP > 0 ) leftDP = leftDiff + leftDP ; else leftDP [ i ] = leftDiff ; } int rightDiff ; if ( i + 1 >= arr . length ) rightDiff = 0 ; else rightDiff = arr [ i ] - arr [ i + 1 ] ; if ( c - 1 < 0 ) rightDP = rightDiff ; else { if ( rightDP > 0 ) rightDP = rightDiff + rightDP ; else rightDP = rightDiff ; } c ++ ; } int max = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { max = Math . max ( max , Math . max ( leftDP [ i ] , rightDP [ i ] ) ) ; } return max + sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 4 , 5 , 7 , 6 , 8 , 9 , 7 , 3 } ; int ans = maximizeSum ( arr ) ; System . out . println ( ans ) ; } }
Count of all subsequences having adjacent elements with different parity | Java Program implementation of the approach ; Function to find required subsequences ; dp [ i ] [ 0 ] : Stores the number of subsequences till i - th index ending with even element dp [ i ] [ 1 ] : Stores the number of subsequences till i - th index ending with odd element ; Initialise the dp [ ] [ ] with 0. ; If odd element is encountered ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with even element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with odd element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Count of all valid subsequences ; Driver code
import java . util . * ; import java . io . * ; class GFG { static int validsubsequences ( int arr [ ] , int n ) { long dp [ ] [ ] = new long [ n + 1 ] [ 2 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] [ 0 ] = 0 ; dp [ i ] [ 1 ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 != 0 ) { dp [ i ] [ 1 ] += 1 ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] ; } else { dp [ i ] [ 0 ] += 1 ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] ; } } return ( int ) ( dp [ n ] [ 0 ] + dp [ n ] [ 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 9 , 7 } ; int n = arr . length ; System . out . print ( validsubsequences ( arr , n ) ) ; } }
Count of N | Java Program to implement the above approach ; Function to return count of such numbers ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ i ] [ j ] stores the number of such i - digit numbers ending in j ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual values of i - digit numbers ending in j ; Stores the final answer ; Driver Code
import java . util . * ; class GFG { public static long getCount ( int n , int k ) { if ( n == 1 ) return 10 ; long dp [ ] [ ] = new long [ n + 1 ] [ 11 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp [ 1 ] [ i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { int l = Math . max ( 0 , j - k ) ; int r = Math . min ( 9 , j + k ) ; dp [ i ] [ l ] += dp [ i - 1 ] [ j ] ; dp [ i ] [ r + 1 ] -= dp [ i - 1 ] [ j ] ; } for ( int j = 1 ; j <= 9 ; j ++ ) dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } long count = 0 ; for ( int i = 0 ; i <= 9 ; i ++ ) count += dp [ n ] [ i ] ; return count ; } public static void main ( String [ ] args ) { int n = 2 , k = 1 ; System . out . println ( getCount ( n , k ) ) ; } }
Find if there is a path between two vertices in a directed graph | Set 2 | Java program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; mat matrix ; set mat [ i ] [ j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code
import java . util . * ; class GFG { static final int X = 6 ; static final int Z = 2 ; static boolean existPath ( int V , int edges [ ] [ ] , int u , int v ) { boolean [ ] [ ] mat = new boolean [ V ] [ V ] ; for ( int i = 0 ; i < X ; i ++ ) mat [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = true ; for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { mat [ i ] [ j ] = mat [ i ] [ j ] || mat [ i ] [ k ] && mat [ k ] [ j ] ; } } } if ( u >= V v >= V ) { return false ; } if ( mat [ u ] [ v ] ) return true ; return false ; } public static void main ( String [ ] args ) { int V = 4 ; int edges [ ] [ ] = { { 0 , 2 } , { 0 , 1 } , { 1 , 2 } , { 2 , 3 } , { 2 , 0 } , { 3 , 3 } } ; int u = 1 , v = 3 ; if ( existPath ( V , edges , u , v ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
Maximize the numbers of splits in an Array having sum divisible by 3 | Java implementation of the above approach ; Prefix array storing right most index with prefix sums 0 , 1 , 2 ; dp array ; Current prefix sum ; Calculating the prefix sum modulo 3 ; We dont have a left pointer with prefix sum C ; We cannot consider i as a right pointer of any segment ; We have a left pointer pre [ C ] with prefix sum C ; i is the rightmost index of prefix sum C ; Driver code
import java . util . * ; class GFG { static int calculate_maximum_splits ( int arr [ ] , int N ) { int pre [ ] = { 0 , - 1 , - 1 } ; int [ ] dp = new int [ N ] ; Arrays . fill ( dp , 0 ) ; int C = 0 ; for ( int i = 0 ; i < N ; i ++ ) { C = C + arr [ i ] ; C = C % 3 ; if ( pre [ C ] == - 1 ) { if ( 1 <= i ) dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 ) ; } pre [ C ] = i ; } return dp [ N - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 } ; int N = arr . length ; System . out . println ( calculate_maximum_splits ( arr , N ) ) ; } }
Count of elements having Euler 's Totient value one less than itself | Java program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Driver Code ; Given array ; Size of the array ; Function Call
import java . util . * ; class GFG { static int prime [ ] = new int [ 1000001 ] ; static void seiveOfEratosthenes ( ) { for ( int i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } static void CountElements ( int arr [ ] , int n , int L , int R ) { seiveOfEratosthenes ( ) ; int countPrime [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } System . out . print ( countPrime [ R ] - countPrime [ L - 1 ] + "NEW_LINE"); return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 8 } ; int N = arr . length ; int L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ; } }
Count of elements having odd number of divisors in index range [ L , R ] for Q queries | Java program for the above approach ; Function count the number of elements having odd number of divisors ; Initialise dp [ ] array ; Precomputation ; Find the Prefix Sum ; Iterate for each query ; Find the answer for each query ; Driver Code ; Given array arr [ ] ; Given Query ; Function Call
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void OddDivisorsCount ( int n , int q , int a [ ] , pair [ ] Query ) { int DP [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) Math . sqrt ( a [ i ] ) ; if ( x * x == a [ i ] ) DP [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } int l , r ; for ( int i = 0 ; i < q ; i ++ ) { l = Query [ i ] . first ; r = Query [ i ] . second ; if ( l == 0 ) { System . out . print ( DP [ r ] + "NEW_LINE"); } else { System . out . print ( DP [ r ] - DP [ l - 1 ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int N = 5 ; int Q = 3 ; int arr [ ] = { 2 , 4 , 5 , 6 , 9 } ; pair [ ] Query = { new pair ( 0 , 2 ) , new pair ( 1 , 3 ) , new pair ( 1 , 4 ) } ; OddDivisorsCount ( N , Q , arr , Query ) ; } }
Number of ways to make exactly C components in a 2 * N matrix | Java implementation to find the number of ways to make exactly C components in a 2 * N matrix ; row1 and row2 are one when both are same colored ; Function to find the number of ways to make exactly C components in a 2 * N matrix ; if No of components at any stage exceeds the given number then base case ; Condition to check if already visited ; if not visited previously ; At the first column ; color { white , white } or { black , black } ; Color { white , black } or { black , white } ; If previous both rows have same color ; Fill with { same , same } and { white , black } and { black , white } ; Fill with same without increase in component as it has been counted previously ; When previous rows had { white , black } ; When previous rows had { black , white } ; Memoization ; Driver Code ; Initially at first column with 0 components
class GFG { static int n , k ; static int [ ] [ ] [ ] [ ] dp = new int [ 1024 ] [ 2048 ] [ 2 ] [ 2 ] ; static int Ways ( int col , int comp , int row1 , int row2 ) { if ( comp > k ) return 0 ; if ( col > n ) { if ( comp == k ) return 1 ; else return 0 ; } if ( dp [ col ] [ comp ] [ row1 ] [ row2 ] != - 1 ) return dp [ col ] [ comp ] [ row1 ] [ row2 ] ; else { int ans = 0 ; if ( col == 1 ) { ans = ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) + Ways ( col + 1 , comp + 1 , 1 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } else { if ( ( row1 > 0 && row2 > 0 ) || ( row1 == 0 && row2 == 0 ) ) { ans = ( ( ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) ) + Ways ( col + 1 , comp + 1 , 1 , 0 ) ) + Ways ( col + 1 , comp + 1 , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp , 1 , 1 ) ) ; } if ( row1 > 0 && row2 == 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 1 , 0 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) ) ; } if ( row1 == 0 && row2 > 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } } return dp [ col ] [ comp ] [ row1 ] [ row2 ] = ans ; } } public static void main ( String [ ] args ) { n = 2 ; k = 1 ; for ( int i = 0 ; i < 1024 ; i ++ ) for ( int j = 0 ; j < 2048 ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; System . out . print ( Ways ( 1 , 0 , 0 , 0 ) ) ; } }
Find all possible ways to Split the given string into Primes | Java program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static boolean [ ] primes = new boolean [ 1000001 ] ; static int maxn = 1000000 ; static void sieve ( ) { Arrays . fill ( primes , true ) ; primes [ 0 ] = false ; primes [ 1 ] = false ; for ( int i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( int j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } static String toBinary ( int n ) { String r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? "0" : "1" ) + r ; n /= 2 ; } return ( r == " " ) ? "0" : r ; } static void PrimeSplit ( String str ) { String temp ; int cnt = 0 ; ArrayList < String > ans = new ArrayList < > ( ) ; int bt = 1 << ( str . length ( ) - 1 ) ; int n = str . length ( ) ; for ( int i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + "0" ; int j = 0 , x = n - temp . length ( ) , y ; while ( j < x ) { temp = "0" + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; String sp = " " , tp = " " ; boolean flag = false ; while ( j < n ) { sp += str . charAt ( j ) ; if ( temp . charAt ( j ) == '1' ) { tp += sp + ' , ' ; y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) { flag = true ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) flag = true ; } if ( ! flag ) ans . add ( tp ) ; } if ( ans . size ( ) == 0 ) { System . out . println ( - 1 ) ; } for ( String i : ans ) { System . out . println ( i ) ; } } public static void main ( String [ ] args ) { String str = "11373" ; sieve ( ) ; PrimeSplit ( str ) ; } }
Count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) | Set 2 | Java program for the above approach ; Function to count the numbers ; Array that stores the digits from left to right ; Array that stores the digits from right to left ; Initially both array store zero ; Fill the table for count1 array ; Update the count of current character ; Fill the table for count2 array ; Update the count of cuuent character ; Variable that stores the count of the numbers ; Traverse Input string and get the count of digits from count1 and count2 array such that difference b / w digit is 1 & store it int c1 & c2 . And store it in variable c1 and c2 ; Update the ans ; Return the final count ; Driver code ; Given String ; Function call
import java . io . * ; class GFG { static int countStableNum ( String str , int N ) { int count1 [ ] [ ] = new int [ N ] [ 10 ] ; int count2 [ ] [ ] = new int [ N ] [ 10 ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < 10 ; j ++ ) count1 [ i ] [ j ] = count2 [ i ] [ j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i != 0 ) { for ( int j = 0 ; j < 10 ; j ++ ) { count1 [ i ] [ j ] += count1 [ i - 1 ] [ j ] ; } } count1 [ i ] [ str . charAt ( i ) - '0' ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i != N - 1 ) { for ( int j = 0 ; j < 10 ; j ++ ) { count2 [ i ] [ j ] += count2 [ i + 1 ] [ j ] ; } } count2 [ i ] [ str . charAt ( i ) - '0' ] ++ ; } int ans = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( str . charAt ( i ) == '9' ) continue ; int c1 = count1 [ i - 1 ] [ str . charAt ( i ) - '0' ] ; int c2 = count2 [ i + 1 ] [ str . charAt ( i ) - '0' + 1 ] ; if ( c2 == 0 ) continue ; ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) / 2 ) ) ) ) ; } return ans ; } public static void main ( String [ ] args ) { String str = "224353" ; int N = str . length ( ) ; System . out . println ( countStableNum ( str , N ) ) ; } }
Count of subsequences in an array with sum less than or equal to X | Java program to count number of subsequences in an array with sum less than or equal to X ; Utility function to return the count of subsequence in an array with sum less than or equal to X ; Base condition ; Return if the sub - problem is already calculated ; Check if the current element is less than or equal to sum ; Count subsequences excluding the current element ; Count subsequences including the current element ; Exclude current element ; Return the result ; Function to return the count of subsequence in an array with sum less than or equal to X ; Initialize a DP array ; Return the result ; Driver Code
class GFG { static int countSubsequenceUtil ( int ind , int sum , int [ ] A , int N , int [ ] [ ] dp ) { if ( ind == N ) return 1 ; if ( dp [ ind ] [ sum ] != - 1 ) return dp [ ind ] [ sum ] ; if ( A [ ind ] <= sum ) { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) + countSubsequenceUtil ( ind + 1 , sum - A [ ind ] , A , N , dp ) ; } else { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) ; } return dp [ ind ] [ sum ] ; } static int countSubsequence ( int [ ] A , int N , int X ) { int [ ] [ ] dp = new int [ N ] [ X + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < X + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return countSubsequenceUtil ( 0 , X , A , N , dp ) - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 13 , 40 } , X = 50 ; int N = arr . length ; System . out . print ( countSubsequence ( arr , N , X ) ) ; } }
Count possible decodings of a given Digit Sequence | Set 2 | Java implementation to count the possible decodings of the given digit sequence ; Function to count the number of ways to decode the given digit sequence ; Array to store the dp states ; Case of empty String ; Condition to check if the first character of String is 0 ; Base case for single length String ; Bottom - up dp for the String ; Previous character ; Current character ; Case to include the Current digit as a single digit for decoding the String ; Case to include the current character as two - digit for decoding the String ; Condition to check if the current character is " * " ; Condition to check if the current character is less than or equal to 26 ; Condition to check if the previous digit is equal to " * " ; Driver Code ; Function Call
class GFG { static int waysToDecode2 ( char [ ] s ) { int n = s . length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; if ( s [ 0 ] == '0' ) return 0 ; dp [ 1 ] = ( ( s [ 0 ] == ' * ' ) ? 9 : 1 ) ; for ( int i = 2 ; i <= n ; i ++ ) { char first = s [ i - 2 ] ; char second = s [ i - 1 ] ; if ( second == ' * ' ) { dp [ i ] += 9 * dp [ i - 1 ] ; } else if ( second > '0' ) dp [ i ] += dp [ i - 1 ] ; if ( first == '1' first == '2' ) { if ( second == ' * ' ) { if ( first == '1' ) dp [ i ] += 9 * dp [ i - 2 ] ; else if ( first == '2' ) dp [ i ] += 6 * dp [ i - 2 ] ; } else if ( ( ( first - '0' ) * 10 + ( second - '0' ) ) <= 26 ) { dp [ i ] += dp [ i - 2 ] ; } } else if ( first == ' * ' ) { if ( second == ' * ' ) { dp [ i ] += 15 * dp [ i - 2 ] ; } else if ( second <= '6' ) { dp [ i ] += 2 * dp [ i - 2 ] ; } else { dp [ i ] += dp [ i - 2 ] ; } } } return dp [ n ] ; } public static void main ( String [ ] args ) { String str = "12*3" ; System . out . print ( waysToDecode2 ( str . toCharArray ( ) ) + "NEW_LINE"); } }
Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | Java implementation of the above approach ; Function that prints the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code
class GFG { static void goodInteger ( int arr [ ] , int n , int h , int l , int r ) { int [ ] [ ] dp = new int [ n + 1 ] [ h ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < h ; j ++ ) dp [ i ] [ j ] = - 1 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < h ; j ++ ) { if ( dp [ i ] [ j ] != - 1 ) { int h1 = ( j + arr [ i ] ) % h ; int h2 = ( j + arr [ i ] - 1 ) % h ; dp [ i + 1 ] [ h1 ] = Math . max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 ] [ h2 ] = Math . max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } int ans = 0 ; for ( int i = 0 ; i < h ; i ++ ) { if ( dp [ n ] [ i ] != - 1 ) ans = Math . max ( ans , dp [ n ] [ i ] ) ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int A [ ] = { 16 , 17 , 14 , 20 , 20 , 11 , 22 } ; int H = 24 ; int L = 21 ; int R = 23 ; int size = A . length ; goodInteger ( A , size , H , L , R ) ; } }
Range queries for alternatively addition and subtraction on given Array | Java program for the above approach ; Structure to represent a query ; This function fills the Prefix Array ; Initialise the prefix array ; Iterate all the element of arr [ ] and update the prefix array ; If n is even then , add the previous value of prefix array with the current value of arr ; if n is odd , then subtract the previous value of prefix Array from current value ; Function to find the result of alternatively adding and subtracting elements in the range [ L < R ] ; Case 1 : when L is zero ; Case 2 : When L is non zero ; If L is odd means range starts from odd position multiply result by - 1 ; Return the final result ; Function to find the sum of all alternative add and subtract between ranges [ L , R ] ; Declare prefix array ; Function Call to fill prefix arr [ ] ; Iterate for each query ; Driver Code ; Given array ; Given Queries ; Function Call
import java . util . * ; class GFG { static class Query { int L , R ; public Query ( int l , int r ) { super ( ) ; L = l ; R = r ; } } ; static void fillPrefixArray ( int arr [ ] , int n , int prefixArray [ ] ) { prefixArray [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { prefixArray [ i ] = prefixArray [ i - 1 ] + arr [ i ] ; } else { prefixArray [ i ] = prefixArray [ i - 1 ] - arr [ i ] ; } } } static int findResultUtil ( int prefixArray [ ] , int L , int R ) { int result ; if ( L == 0 ) { result = prefixArray [ R ] ; } else { result = prefixArray [ R ] - prefixArray [ L - 1 ] ; } if ( L % 2 == 1 ) { result = result * ( - 1 ) ; } return result ; } static void findResult ( int arr [ ] , int n , Query q [ ] , int m ) { int [ ] prefixArray = new int [ n ] ; fillPrefixArray ( arr , n , prefixArray ) ; for ( int i = 0 ; i < m ; i ++ ) { System . out . print ( findResultUtil ( prefixArray , q [ i ] . L , q [ i ] . R ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 } ; int n = arr . length ; Query q [ ] = { new Query ( 2 , 5 ) , new Query ( 6 , 8 ) , new Query ( 1 , 7 ) , new Query ( 4 , 8 ) , new Query ( 0 , 5 ) } ; int m = q . length ; findResult ( arr , n , q , m ) ; } }