dhyuti-n/xlcost-variable-roles huggingface dataset:
Java program:
import java . util . * ; class GFG { static int N = 100000 ; static int canTake ; static int best ; static int [ ] dp = new int [ N ] ; static boolean [ ] vis = new boolean [ N ] ; static boolean dfs ( Vector < Integer > [ ] g , int [ ] cost , int u , int pre ) { vis [ u ] = true ; dp [ u ] = cost [ u ] ; boolean check = true ; int cur = cost [ u ] ; for ( int x : g [ u ] ) { if ( vis [ x ] && x != pre ) { check = false ; } else if ( ! vis [ x ] ) { check = dfs ( g , cost , x , u ) ? false : true ; cur = Math . max ( cur , cost [ u ] + dp [ x ] ) ; } } dp [ u ] = cur ; if ( ! check ) { canTake += cost [ u ] ; } else { best = Math . max ( best , dp [ u ] ) ; } return check ; } static void FindMaxCost ( Vector < Integer > [ ] g , int [ ] cost , int source ) { dfs ( g , cost , source , - 1 ) ; System . out . print ( canTake + best ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int cost [ ] = { 2 , 2 , 8 , 6 , 9 } ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] g = new Vector [ n ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; g [ 0 ] . add ( 1 ) ; g [ 1 ] . add ( 0 ) ; g [ 0 ] . add ( 2 ) ; g [ 2 ] . add ( 0 ) ; g [ 0 ] . add ( 3 ) ; g [ 3 ] . add ( 0 ) ; g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; g [ 1 ] . add ( 4 ) ; g [ 4 ] . add ( 1 ) ; int source = 1 ; FindMaxCost ( g , cost , source ) ; } }

Index/key variables: N, i, n, u, x
Accumulator variables: canTake, i
Iterator variables: i, x
Boolean variables: check
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static final int N = 5 ; static int after_rev ( Vector < Integer > v ) { int mini = 0 , count = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { count += v . get ( i ) ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } static void print ( int arr [ ] ) { int sum = 0 ; for ( int i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; Vector < Integer > v1 , v2 ; v1 = new Vector < Integer > ( ) ; v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i + 1 < N ; i += 2 ) v1 . add ( arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 1 ; i + 1 < N ; i += 2 ) v2 . add ( arr [ i ] - arr [ i + 1 ] ) ; int change = Math . min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 4 , 3 , 0 } ; print ( arr ) ; } }

Index/key variables: i
Accumulator variables: count, i, sum
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { HashMap < Integer , Integer > mapA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mapB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . containsKey ( A [ i ] ) ) { mapA . put ( A [ i ] , mapA . get ( A [ i ] ) + 1 ) ; } else { mapA . put ( A [ i ] , 1 ) ; } if ( mapB . containsKey ( B [ i ] ) ) { mapB . put ( B [ i ] , mapB . get ( B [ i ] ) + 1 ) ; } else { mapB . put ( B [ i ] , 1 ) ; } } HashSet < Integer > possibleValues = new HashSet < Integer > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Integer . MAX_VALUE ; for ( int it : possibleValues ) { boolean possible = true ; for ( Map . Entry < Integer , Integer > it2 : mapA . entrySet ( ) ) { if ( it2 . getValue ( ) != mapB . get ( ( it2 . getKey ( ) + it ) % m ) ) { possible = false ; break ; } } if ( possible ) { ans = Math . min ( ans , it ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; System . out . print ( moduloEquality ( A , B , n , m ) + "NEW_LINE"); } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i, it
Boolean variables: possible
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int countCyclicShifts ( String S , int n ) { int [ ] aux = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( S . charAt ( i ) == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String S = " ) ( ) ( " ; int N = S . length ( ) ; System . out . print ( countCyclicShifts ( S , N ) ) ; } }

Index/key variables: i, n
Accumulator variables: count
Iterator variables: i
Class/struct names: GFG
Java program:
class GFG { static int maxProfit ( int [ ] prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } public static void main ( String [ ] args ) { int prices [ ] = { 2 , 3 , 5 } ; int N = prices . length ; System . out . print ( maxProfit ( prices , N ) ) ; } }

Index/key variables: currentDay, day
Accumulator variables: day, profit
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; boolean lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < Character > s = new Stack < Character > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . add ( str . charAt ( i ) ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . peek ( ) == str . charAt ( i ) ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }

Accumulator variables: c, i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str . charAt ( left ) != str . charAt ( right ) ) return false ; left += 2 ; right -= 2 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }

Accumulator variables: left, right
Class/struct names: GFG
Java program:
class GFG { static void min_remove ( String str ) { int N = str . length ( ) ; int [ ] prefix_a = new int [ N + 1 ] ; int [ ] prefix_b = new int [ N + 1 ] ; int [ ] prefix_c = new int [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' a ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' b ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' c ' ) ? 1 : 0 ) ; } int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = Math . max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } System . out . println ( ( N - maxi ) ) ; } public static void main ( String [ ] args ) { String str = " aaaabaaxccac " ; min_remove ( str ) ; } }

Index/key variables: N, i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
class GFG { static void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { System . out . print ( 2 + " ▁ " ) ; s -= 2 ; } System . out . println ( s ) ; System . out . println ( 1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; } }

Accumulator variables: i, s
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int maximum_partition ( String str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( ! m . containsKey ( str . charAt ( i ) ) ) { m . put ( str . charAt ( i ) , i ) ; } } i = 0 ; k = m . get ( str . charAt ( i ) ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } else { r = r + 1 ; c = 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } public static void main ( String [ ] args ) { String str = " ababcbacadefegdehijhklij " ; System . out . print ( maximum_partition ( str ) ) ; } }

Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int findAns ( int [ ] [ ] mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += Math . abs ( x - i ) + Math . abs ( y - j ) ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; System . out . print ( findAns ( mat , x , y , mat . length , mat [ 0 ] . length ) + "NEW_LINE"); } }

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void countReversebitonic ( int arr [ ] , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < arr [ k ] && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } System . out . print ( c + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 4 } ; int N = arr . length ; countReversebitonic ( arr , N ) ; } }

Index/key variables: i, k
Accumulator variables: c, i, j, k
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int longestSequence ( int n , int arr [ ] ) { int l = 0 , r = n - 1 ; int prev = Integer . MIN_VALUE ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 1 , 4 , 2 } ; int n = arr . length ; System . out . print ( longestSequence ( n , arr ) ) ; } }

Index/key variables: l, r
Accumulator variables: ans, l, r
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int solve ( int n , int m , int x , int y , int d ) { int top = Math . min ( d , x - 1 ) ; int down = Math . min ( d , n - x ) ; int left = Math . min ( d , y - 1 ) ; int right = Math . min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } public static void main ( String [ ] args ) { int n = 10 , m = 10 ; int x = 7 , y = 8 ; int d = 4 ; d -- ; System . out . println ( solve ( n , m , x , y , d ) ) ; } }

Accumulator variables: d
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { Vector < Integer > req = new Vector < Integer > ( ) ; req . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . add ( arr [ i ] ) ; } req . add ( arr [ n - 1 ] ) ; System . out . print ( req . size ( ) + "NEW_LINE"); for ( int x : req ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . length ; getSubsequence ( arr , n ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i, x
Class/struct names: GFG
Java program:
import java . io . * ; import java . util . * ; class GFG { static final int M = 3 ; static final int N = 3 ; static int minchanges ( int [ ] [ ] mat ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } else if ( i + j == right ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } } } int changes = 0 ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) changes = Math . max ( changes , itr . getValue ( ) ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; System . out . println ( minchanges ( mat ) ) ; } }

Index/key variables: i, j
Accumulator variables: count, i, j, left, right, totalsize
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static int CountLongestSubarrays ( int arr [ ] , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 2 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( CountLongestSubarrays ( arr , n , k ) ) ; } }

Index/key variables: fin, i, ini
Accumulator variables: count, i, s, sum
Iterator variables: i
Class/struct names: GFG
Java program:
class GFG { static int MAXN = 1000001 ; static int freq [ ] = new int [ MAXN ] ; static int mul [ ] = new int [ MAXN ] ; static int cnt [ ] = new int [ MAXN ] ; static int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } static void count_triplet ( int [ ] arr , int N , int K ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( i = 1 ; i <= 1000000 ; i ++ ) { for ( j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( i = 1000000 ; i >= 1 ; i -- ) { for ( j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } System . out . print ( " Number ▁ of ▁ triplets ▁ " + " with ▁ GCD ▁ " + K ) ; System . out . print ( " ▁ are ▁ " + cnt [ K ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; } }

Index/key variables: K, MAXN, i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
class GFG { static boolean isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }

Index/key variables: i
Accumulator variables: even, i, odd
Iterator variables: i
Class/struct names: GFG
Java program:
import java . io . * ; class GFG { static void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { System . out . println ( " - 1" ) ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = Math . min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } System . out . print ( pos + " ▁ " ) ; ; coin -= tmp ; jump -= 1 ; } } } public static void main ( String [ ] args ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; } }

Accumulator variables: coin, jump, pos
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static final int INF = 10000009 ; static void findMinHike ( Vector < Integer > arr , int n ) { arr . add ( 0 , INF ) ; arr . add ( INF ) ; int [ ] hike = new int [ n + 2 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = Math . max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( hike [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < > ( ) ; arr . add ( 5 ) ; arr . add ( 3 ) ; arr . add ( 4 ) ; arr . add ( 2 ) ; arr . add ( 1 ) ; arr . add ( 6 ) ; findMinHike ( arr , arr . size ( ) ) ; } }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void minimumCost ( int arr [ ] , int cost [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ 3 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = ( int ) 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } int ans = ( int ) 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 4 } ; int cost [ ] = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = arr . length ; minimumCost ( arr , cost , N ) ; } }

Index/key variables: N, i, j
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; class GFG { static void maximumSumSubarray ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 ) mp . put ( arr [ i ] , 1 ) ; } int first = 0 ; int last = 0 ; int ans = 0 ; int INF = ( int ) 1e6 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int mx = i . getKey ( ) ; int curr = 0 ; int curr_start = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; int val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } System . out . print ( ( first + 1 ) + " ▁ " + ( last + 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 2 , 10 , - 1 , 4 } ; int size = arr . length ; maximumSumSubarray ( arr , size ) ; } }

Index/key variables: i, j
Accumulator variables: curr, i, j
Iterator variables: i, j
Class/struct names: GFG
Java program:
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } public static void main ( String [ ] args ) { int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; System . out . println ( maxProfit ( profit , weight , n , max_weight , max_element ) ) ; } }

Index/key variables: i, j, k, max_E, max_W, n
Accumulator variables: i, j, k
Iterator variables: i, j, k
Class/struct names: GFG
Java program:
import java . util . ArrayList ; import java . util . Arrays ; import java . util . PriorityQueue ; class GFG { static class Pair implements Comparable < Pair > { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int compareTo ( GFG . Pair o ) { if ( this . first == o . first ) { return this . second - o . second ; } return this . first - o . first ; } } static final int MAXX = 10000 , INF = ( int ) 1e9 ; @ SuppressWarnings ( " unchecked " ) static ArrayList < Pair > [ ] adj = new ArrayList [ MAXX * 10 + 3 ] ; static int [ ] dist = new int [ MAXX * 10 + 3 ] ; static int even ( int x ) { return x * 10 + 2 ; } static int odd ( int x ) { return x * 10 + 1 ; } static void addEdge ( int a , int b , int cost ) { adj [ even ( a ) ] . add ( new Pair ( odd ( b ) , cost ) ) ; adj [ odd ( a ) ] . add ( new Pair ( even ( b ) , cost ) ) ; adj [ odd ( b ) ] . add ( new Pair ( even ( a ) , cost ) ) ; adj [ even ( b ) ] . add ( new Pair ( odd ( a ) , cost ) ) ; } static int dijkstra ( int source , int destination ) { PriorityQueue < Pair > pq = new PriorityQueue < > ( ) ; pq . add ( new Pair ( 0 , even ( source ) ) ) ; dist [ even ( source ) ] = 0 ; while ( ! pq . isEmpty ( ) ) { int u = pq . poll ( ) . second ; for ( Pair p : adj [ u ] ) { int v = p . first ; int c = p . second ; if ( dist [ u ] + c < dist [ v ] ) { dist [ v ] = dist [ u ] + c ; pq . add ( new Pair ( dist [ v ] , v ) ) ; } } } return dist [ even ( destination ) ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAXX * 10 + 3 ; i ++ ) { adj [ i ] = new ArrayList < Pair > ( ) ; } Arrays . fill ( dist , INF ) ; int n = 5 , m = 6 ; addEdge ( 1 , 2 , 1 ) ; addEdge ( 2 , 3 , 2 ) ; addEdge ( 2 , 5 , 15 ) ; addEdge ( 3 , 5 , 1 ) ; addEdge ( 3 , 4 , 4 ) ; addEdge ( 5 , 4 , 3 ) ; int source = 1 ; int destination = n ; int ans = dijkstra ( source , destination ) ; if ( ans == INF ) System . out . println ( " - 1" ) ; else System . out . println ( ans ) ; } }

Index/key variables: i, u, v
Accumulator variables: i
Iterator variables: i, p
Class/struct names: GFG, Pair