dhyuti-n/xlcost-variable-roles huggingface dataset:
C++ program:
#include <bits/stdc++.h>
using namespace std ; const int MAX_NODES = 100 ; int in [ MAX_NODES ] ; int out [ MAX_NODES ] ; void dfs1 ( vector < int > v [ ] , int u , int parent ) { in [ u ] = 0 ; for ( int child : v [ u ] ) { if ( child == parent ) continue ; dfs1 ( v , child , u ) ; in [ u ] = max ( in [ u ] , 1 + in [ child ] ) ; } } void dfs2 ( vector < int > v [ ] , int u , int parent ) { int mx1 = -1 , mx2 = -1 ; for ( int child : v [ u ] ) { if ( child == parent ) continue ; if ( in [ child ] >= mx1 ) { mx2 = mx1 ; mx1 = in [ child ] ; } else if ( in [ child ] > mx2 ) mx2 = in [ child ] ; } for ( int child : v [ u ] ) { if ( child == parent ) continue ; int longest = mx1 ; if ( mx1 == in [ child ] ) longest = mx2 ; out [ child ] = 1 + max ( out [ u ] , 1 + longest ) ; dfs2 ( v , child , u ) ; } } void printHeights ( vector < int > v [ ] , int n ) { dfs1 ( v , 1 , 0 ) ; dfs2 ( v , 1 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << " The ▁ maximum ▁ height ▁ when ▁ node ▁ " << i << " ▁ is ▁ considered ▁ as ▁ root " << " ▁ is ▁ " << max ( in [ i ] , out [ i ] ) << " STRNEWLINE " ; } int main ( ) { int n = 11 ; vector < int > v [ n + 1 ] ; v [ 1 ] . push_back ( 2 ) , v [ 2 ] . push_back ( 1 ) ; v [ 1 ] . push_back ( 3 ) , v [ 3 ] . push_back ( 1 ) ; v [ 1 ] . push_back ( 4 ) , v [ 4 ] . push_back ( 1 ) ; v [ 2 ] . push_back ( 5 ) , v [ 5 ] . push_back ( 2 ) ; v [ 2 ] . push_back ( 6 ) , v [ 6 ] . push_back ( 2 ) ; v [ 3 ] . push_back ( 7 ) , v [ 7 ] . push_back ( 3 ) ; v [ 7 ] . push_back ( 10 ) , v [ 10 ] . push_back ( 7 ) ; v [ 7 ] . push_back ( 11 ) , v [ 11 ] . push_back ( 7 ) ; v [ 4 ] . push_back ( 8 ) , v [ 8 ] . push_back ( 4 ) ; v [ 4 ] . push_back ( 9 ) , v [ 9 ] . push_back ( 4 ) ; printHeights ( v , n ) ; return 0 ; }

Index/key variables: MAX_NODES, child, i, u
Accumulator variables: i, stdc
Iterator variables: child, i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int findGolomb ( int n ) { if ( n == 1 ) return 1 ; return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) ) ; } void printGolomb ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << findGolomb ( i ) << " ▁ " ; } int main ( ) { int n = 9 ; printGolomb ( n ) ; return 0 ; }

Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
#include <iostream>
using namespace std ; int max ( int a , int b ) { return ( a > b ) ? a : b ; } void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] ) ; else K [ i ] [ w ] = K [ i - 1 ] [ w ] ; } } int res = K [ n ] [ W ] ; cout << res << endl ; w = W ; for ( i = n ; i > 0 && res > 0 ; i -- ) { if ( res == K [ i - 1 ] [ w ] ) continue ; else { cout << " ▁ " << wt [ i - 1 ] ; res = res - val [ i - 1 ] ; w = w - wt [ i - 1 ] ; } } } int main ( ) { int val [ ] = { 60 , 100 , 120 } ; int wt [ ] = { 10 , 20 , 30 } ; int W = 50 ; int n = sizeof ( val ) / sizeof ( val [ 0 ] ) ; printknapSack ( W , wt , val , n ) ; return 0 ; }

Index/key variables: W, i, n, w
Accumulator variables: i, stdc, w
Iterator variables: i, w
C++ program:
#include <bits/stdc++.h>
using namespace std ; int removals ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = -1 ; int ans = n - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = i ; int j = dp [ i - 1 ] ; while ( j != i && arr [ i ] - arr [ j ] > k ) { j ++ ; } dp [ i ] = min ( dp [ i ] , j ) ; ans = min ( ans , ( n - ( i - j + 1 ) ) ) ; } return ans ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << removals ( a , n , k ) ; return 0 ; }

Index/key variables: i, j, n
Accumulator variables: i, j, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int maximumSegments ( int n , int a , int b , int c ) { int dp [ n + 1 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != -1 ) { dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) ; dp [ i + b ] = max ( dp [ i ] + 1 , dp [ i + b ] ) ; dp [ i + c ] = max ( dp [ i ] + 1 , dp [ i + c ] ) ; } } return dp [ n ] ; } int main ( ) { int n = 7 , a = 5 , b = 2 , c = 5 ; cout << maximumSegments ( n , a , b , c ) ; return 0 ; }

Index/key variables: i, n
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int maximizeSum ( int arr [ ] , int n ) { int mx = -1 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , arr [ i ] ) ; } int freq [ mx + 1 ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int ans = 0 , i = mx ; while ( i > 0 ) { if ( freq [ i ] > 0 ) { ans += i ; freq [ i - 1 ] -- ; freq [ i ] -- ; } else { i -- ; } } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maximizeSum ( a , n ) ; return 0 ; }

Index/key variables: i
Accumulator variables: ans, i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; void sequence ( int n ) { int f [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; cout << f [ 1 ] << " ▁ " << f [ 2 ] << " ▁ " ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; cout << f [ i ] << " ▁ " ; } } int main ( ) { int n = 13 ; sequence ( n ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int lcs [ n + 1 ] [ m + 1 ] ; int cnt [ n + 1 ] [ m + 1 ] ; memset ( lcs , 0 , sizeof ( lcs ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { lcs [ i ] [ j ] = max ( lcs [ i - 1 ] [ j ] , lcs [ i ] [ j - 1 ] ) ; if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1 ; if ( cnt [ i ] [ j ] >= k ) { for ( int a = k ; a <= cnt [ i ] [ j ] ; a ++ ) lcs [ i ] [ j ] = max ( lcs [ i ] [ j ] , lcs [ i - a ] [ j - a ] + a ) ; } } } return lcs [ n ] [ m ] ; } int main ( ) { int k = 4 ; string s1 = " aggasdfa " ; string s2 = " aggajasdfa " ; cout << longestSubsequenceCommonSegment ( k , s1 , s2 ) ; return 0 ; }

Index/key variables: i, j, m, n
Accumulator variables: a, i, j, stdc
Iterator variables: a, i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define row  5
#define col  5
bool isPath ( int arr [ row ] [ col ] ) { int dir [ 4 ] [ 2 ] = { { 0 , 1 } , { 0 , -1 } , { 1 , 0 } , { -1 , 0 } } ; queue < pair < int , int > > q ; q . push ( make_pair ( 0 , 0 ) ) ; while ( q . size ( ) > 0 ) { pair < int , int > p = q . front ( ) ; q . pop ( ) ; arr [ p . first ] [ p . second ] = -1 ; if ( p == make_pair ( row - 1 , col - 1 ) ) return true ; for ( int i = 0 ; i < 4 ; i ++ ) { int a = p . first + dir [ i ] [ 0 ] ; int b = p . second + dir [ i ] [ 1 ] ; if ( arr [ a ] [ b ] != -1 && a >= 0 && b >= 0 && a < row && b < col ) { q . push ( make_pair ( a , b ) ) ; } } } return false ; } int main ( ) { int arr [ row ] [ col ] = { { 0 , 0 , 0 , -1 , 0 } , { -1 , 0 , 0 , -1 , -1 } , { 0 , 0 , 0 , -1 , 0 } , { -1 , 0 , -1 , 0 , -1 } , { 0 , 0 , -1 , 0 , 0 } } ; if ( isPath ( arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }

Index/key variables: a, b, col, i, row
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define SIZE  10
#define SIZE  10
void longSubarrWthSumDivByK ( int arr [ ] , int n , int k , int & start , int & finish ) { unordered_map < int , int > um ; int mod_arr [ n ] ; int curr_sum = 0 , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mod_arr [ i ] == 0 ) { max = i + 1 ; start = 0 ; finish = i ; } else if ( um . find ( mod_arr [ i ] ) == um . end ( ) ) um [ mod_arr [ i ] ] = i ; else if ( max < ( i - um [ mod_arr [ i ] ] ) ) { max = i - um [ mod_arr [ i ] ] ; start = um [ mod_arr [ i ] ] + 1 ; finish = i ; } } } void findLargestSubmatrix ( int mat [ SIZE ] [ SIZE ] , int n , int k ) { int finalLeft , finalRight , finalTop , finalBottom ; int left , right , i , maxArea = 0 ; int temp [ n ] , start , finish ; for ( left = 0 ; left < n ; left ++ ) { memset ( temp , 0 , sizeof ( temp ) ) ; for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] ; longSubarrWthSumDivByK ( temp , n , k , start , finish ) ; if ( maxArea < ( ( right - left + 1 ) * ( finish - start + 1 ) ) ) { finalLeft = left ; finalRight = right ; finalTop = start ; finalBottom = finish ; maxArea = ( right - left + 1 ) * ( finish - start + 1 ) ; } } } cout << " ( Top , ▁ Left ) : ▁ ( " << finalTop << " , ▁ " << finalLeft << " ) STRNEWLINE " ; cout << " ( Bottom , ▁ Right ) : ▁ ( " << finalBottom << " , ▁ " << finalRight << " ) STRNEWLINE " ; cout << " Area : ▁ " << maxArea ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 1 , 2 , -1 , -4 } , { -8 , -3 , 4 , 2 } , { 3 , 8 , 10 , 1 } , { -4 , -1 , 1 , 7 } } ; int n = 4 , k = 5 ; findLargestSubmatrix ( mat , n , k ) ; return 0 ; }

Index/key variables: SIZE, i, n, right
Accumulator variables: curr_sum, i, left, right, stdc
Iterator variables: i, left, right
C++ program:
#include <bits/stdc++.h>
using namespace std ; int zigzag ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i - j ] ; return dp [ n ] [ k ] ; } int main ( ) { int n = 4 , k = 3 ; cout << zigzag ( n , k ) << endl ; return 0 ; }

Index/key variables: i, j, k, n
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
#define MAXN  109
using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int lobb ( int n , int m ) { return ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) ; } int main ( ) { int n = 5 , m = 3 ; cout << lobb ( n , m ) << endl ; return 0 ; }

Index/key variables: i, j, k, n
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
#define MAXN  109
using namespace std ; int countarray ( int n , int k , int x ) { int dp [ MAXN ] = { 0 } ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } int main ( ) { int n = 4 , k = 3 , x = 2 ; cout << countarray ( n , k , x ) << endl ; return 0 ; }

Index/key variables: MAXN, i
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
#define MAX  100
#define MAX_CHAR  26
using namespace std ; void precompute ( string s , int n , int l [ ] [ MAX ] , int r [ ] [ MAX ] ) { l [ s [ 0 ] - ' a ' ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) l [ j ] [ i ] += l [ j ] [ i - 1 ] ; l [ s [ i ] - ' a ' ] [ i ] ++ ; } r [ s [ n - 1 ] - ' a ' ] [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) r [ j ] [ i ] += r [ j ] [ i + 1 ] ; r [ s [ i ] - ' a ' ] [ i ] ++ ; } } int countPalindromes ( int k , int n , int l [ ] [ MAX ] , int r [ ] [ MAX ] ) { int ans = 0 ; if ( k == 1 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i ] [ n - 1 ] ; return ans ; } if ( k == 2 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( int i = 1 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ] ; return ans ; } int main ( ) { string s = " aabab " ; int k = 2 ; int n = s . length ( ) ; int l [ MAX_CHAR ] [ MAX ] = { 0 } , r [ MAX_CHAR ] [ MAX ] = { 0 } ; precompute ( s , n , l , r ) ; cout << countPalindromes ( k , n , l , r ) << endl ; return 0 ; }

Index/key variables: MAX, MAX_CHAR, i, j
Accumulator variables: ans, i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; const int N = 30 ; int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) { if ( ! k ) return 0 ; if ( l < 0 r < 0 ) return 1e9 ; if ( dp [ l ] [ r ] [ k ] != -1 ) return dp [ l ] [ r ] [ k ] ; int cost = ( X [ l ] - ' a ' ) ^ ( Y [ r ] - ' a ' ) ; return dp [ l ] [ r ] [ k ] = min ( { cost + solve ( X , Y , l - 1 , r - 1 , k - 1 , dp ) , solve ( X , Y , l - 1 , r , k , dp ) , solve ( X , Y , l , r - 1 , k , dp ) } ) ; } int main ( ) { char X [ ] = " abble " ; char Y [ ] = " pie " ; int n = strlen ( X ) ; int m = strlen ( Y ) ; int k = 2 ; int dp [ N ] [ N ] [ N ] ; memset ( dp , -1 , sizeof dp ) ; int ans = solve ( X , Y , n - 1 , m - 1 , k , dp ) ; cout << ( ans == 1e9 ? -1 : ans ) << endl ; return 0 ; }

Index/key variables: N, k, l, r
Accumulator variables: stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define SIZE  10
int maxSum ( int mat [ SIZE ] [ SIZE ] , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ n ] [ n ] ; int maxSum = INT_MIN , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = INT_MIN ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 ] [ j - 1 ] ) ) max = dp [ i + 1 ] [ j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 ] [ j + 1 ] ) ) max = dp [ i + 1 ] [ j + 1 ] ; dp [ i ] [ j ] = mat [ i ] [ j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 ] [ j ] ) maxSum = dp [ 0 ] [ j ] ; return maxSum ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 5 , 6 , 1 , 7 } , { -2 , 10 , 8 , -1 } , { 3 , -7 , -9 , 11 } , { 12 , -4 , 2 , 6 } } ; int n = 4 ; cout << " Maximum ▁ Sum ▁ = ▁ " << maxSum ( mat , n ) ; return 0 ; }

Index/key variables: SIZE, i, j, n
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; string longestRepeatedSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; string res = " " ; int i = n , j = n ; while ( i > 0 && j > 0 ) { if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) { res = res + str [ i - 1 ] ; i -- ; j -- ; } else if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) i -- ; else j -- ; } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string str = " AABEBCDD " ; cout << longestRepeatedSubSeq ( str ) ; return 0 ; }

Index/key variables: i, j
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; void dfs ( int node , int parent , vector < int > tree [ ] , int colour [ ] , int answer [ ] ) { answer [ node ] = colour [ node ] ; for ( auto u : tree [ node ] ) { if ( u == parent ) continue ; dfs ( u , node , tree , colour , answer ) ; answer [ node ] += max ( answer [ u ] , 0 ) ; } } int maxDiff ( vector < int > tree [ ] , int colour [ ] , int N ) { int answer [ N + 1 ] ; memset ( answer , 0 , sizeof ( answer ) ) ; dfs ( 1 , 0 , tree , colour , answer ) ; int high = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { high = max ( high , answer [ i ] ) ; answer [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( colour [ i ] == -1 ) colour [ i ] = 1 ; else colour [ i ] = -1 ; } dfs ( 1 , 0 , tree , colour , answer ) ; for ( int i = 1 ; i < N ; i ++ ) high = max ( high , answer [ i ] ) ; return high ; } int main ( ) { int N = 5 ; vector < int > tree [ N + 1 ] ; tree [ 1 ] . push_back ( 2 ) ; tree [ 2 ] . push_back ( 1 ) ; tree [ 1 ] . push_back ( 3 ) ; tree [ 3 ] . push_back ( 1 ) ; tree [ 2 ] . push_back ( 4 ) ; tree [ 4 ] . push_back ( 2 ) ; tree [ 3 ] . push_back ( 5 ) ; tree [ 5 ] . push_back ( 3 ) ; int colour [ ] = { 0 , 1 , 1 , -1 , -1 , 1 } ; cout << maxDiff ( tree , colour , N ) ; return 0 ; }

Index/key variables: i, node, u
Accumulator variables: i, stdc
Iterator variables: i, u
C++ program:
#include <bits/stdc++.h>
#define MAX  50
using namespace std ; int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) return a [ low ] * turn ; if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ; dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int dp [ MAX ] [ MAX ] ; memset ( dp , 0 , sizeof ( dp ) ) ; cout << solve ( dp , arr , 0 , n - 1 , 1 ) << endl ; return 0 ; }

Index/key variables: MAX, high, low
Accumulator variables: stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; int maxSumBitonicSubArr ( int arr [ ] , int n ) { int msis [ n ] , msds [ n ] ; int max_sum = INT_MIN ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ] ; else msds [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ] ; return max_sum ; } int main ( ) { int arr [ ] = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ Sum ▁ = ▁ " << maxSumBitonicSubArr ( arr , n ) ; return 0 ; }

Index/key variables: i, n
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } int index = dp [ m ] [ n ] ; string res ( index + 1 , ' \0' ) ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( a [ i - 1 ] == b [ j - 1 ] ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( dp [ i - 1 ] [ j ] < dp [ i ] [ j - 1 ] ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; index -- ; } else { res [ index - 1 ] = b [ j - 1 ] ; j -- ; index -- ; } } while ( i > 0 ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; index -- ; } while ( j > 0 ) { res [ index - 1 ] = b [ j - 1 ] ; j -- ; index -- ; } cout << res ; } int main ( ) { string a = " algorithm " , b = " rhythm " ; printSuperSeq ( a , b ) ; return 0 ; }

Index/key variables: i, j, m, n
Accumulator variables: i, index, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; int minTimeForWritingChars ( int N , int insert , int remove , int copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; int dp [ N + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] = insert ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ; else dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ ( i + 1 ) / 2 ] + copy + remove ) ; } return dp [ N ] ; } int main ( ) { int N = 9 ; int insert = 1 , remove = 2 , copy = 1 ; cout << minTimeForWritingChars ( N , insert , remove , copy ) ; return 0 ; }

Index/key variables: N, i
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int countWays ( int n , int m ) { int count [ n + 1 ] ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m i == 1 ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } int main ( ) { int n = 7 , m = 4 ; cout << " Number ▁ of ▁ ways ▁ = ▁ " << countWays ( n , m ) ; return 0 ; }

Index/key variables: i, n
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int LAS ( int arr [ ] , int n ) { int inc = 1 ; int dec = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { inc = dec + 1 ; } else if ( arr [ i ] < arr [ i - 1 ] ) { dec = inc + 1 ; } } return max ( inc , dec ) ; } int main ( ) { int arr [ ] = { 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LAS ( arr , n ) << endl ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <iostream>
using namespace std ; int min ( int x , int y ) { return ( x < y ) ? x : y ; } int utility_fun_for_del ( string str , int i , int j ) { if ( i >= j ) return 0 ; if ( str [ i ] == str [ j ] ) { return utility_fun_for_del ( str , i + 1 , j - 1 ) ; } return 1 + min ( utility_fun_for_del ( str , i + 1 , j ) , utility_fun_for_del ( str , i , j - 1 ) ) ; } int min_ele_del ( string str ) { return utility_fun_for_del ( str , 0 , str . length ( ) - 1 ) ; } int main ( ) { string str = " abefbac " ; cout << " Minimum ▁ element ▁ of ▁ deletions ▁ = ▁ " << min_ele_del ( str ) << endl ; return 0 ; }

Index/key variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; int dp [ 2000 ] [ 2000 ] ; int transformation ( string s1 , string s2 , int i , int j ) { if ( i >= ( s1 . size ( ) ) || j >= ( s2 . size ( ) ) ) return 0 ; if ( s1 [ i ] == s2 [ j ] ) { dp [ i ] [ j ] = 1 + transformation ( s1 , s2 , i + 1 , j + 1 ) ; } if ( dp [ i ] [ j ] != -1 ) { return dp [ i ] [ j ] ; } else dp [ i ] [ j ] = max ( transformation ( s1 , s2 , i , j + i ) , transformation ( s1 , s2 , i + 1 , j ) ) ; return dp [ s1 . size ( ) - 1 ] [ s2 . size ( ) - 1 ] ; } int main ( ) { string s1 = " geeksforgeeks " ; string s2 = " geeks " ; int i = 0 ; int j = 0 ; memset ( dp , -1 , sizeof dp ) ; cout << " MINIMUM ▁ NUMBER ▁ OF ▁ DELETIONS : ▁ " << ( s1 . size ( ) ) - transformation ( s1 , s2 , 0 , 0 ) << endl ; cout << " MINIMUM ▁ NUMBER ▁ OF ▁ INSERTIONS : ▁ " << ( s2 . size ( ) ) - transformation ( s1 , s2 , 0 , 0 ) << endl ; cout << ( " LCS ▁ LENGTH : ▁ " ) << transformation ( s1 , s2 , 0 , 0 ) ; }

Index/key variables: i, j
Accumulator variables: stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; const int MAX = 100 ; int largestZigZagSumRec ( int mat [ ] [ MAX ] , int i , int j , int n ) { if ( i == n - 1 ) return mat [ i ] [ j ] ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return zzs + mat [ i ] [ j ] ; } int largestZigZag ( int mat [ ] [ MAX ] , int n ) { int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } int main ( ) { int n = 3 ; int mat [ ] [ MAX ] = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; cout << " Largest ▁ zigzag ▁ sum : ▁ " << largestZigZag ( mat , n ) ; return 0 ; }

Index/key variables: MAX, i, j
Accumulator variables: j, k, stdc
Iterator variables: j, k