dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def check ( i , val ) :
    if i - val < 0 :
        return False
    return True
def MinimumPrimes ( n ) :
    dp = [ 10 ** 9 ] * ( n + 1 )
    dp [ 0 ] = dp [ 2 ] = dp [ 3 ] = dp [ 5 ] = dp [ 7 ] = 1
    for i in range ( 1 , n + 1 ) :
        if check ( i , 2 ) :
            dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 2 ] )
        if check ( i , 3 ) :
            dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 3 ] )
        if check ( i , 5 ) :
            dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 5 ] )
        if check ( i , 7 ) :
            dp [ i ] = min ( dp [ i ] , 1 + dp [ i - 7 ] )
    if dp [ n ] == 10 ** 9 :
        return - 1
    else :
        return dp [ n ]
if __name__ == " _ _ main _ _ " :
    n = 12
    minimal = MinimumPrimes ( n )
    if minimal != - 1 :
        print ( " Minimum ▁ number ▁ of ▁ single ▁ digit ▁ primes ▁ required ▁ : ▁ " , minimal )
    else :
        print ( " Not ▁ possible " )

Index/key variables: i, n
Iterator variables: i
Python program:
def subsetSum ( arr , n , maxSum ) :
    dp = [ False for i in range ( maxSum + 1 ) ]
    dp [ arr [ 0 ] ] = True
    for i in range ( 1 , n , 1 ) :
        j = maxSum
        while ( j >= 1 ) :
            if ( arr [ i ] <= j ) :
                if ( arr [ i ] == j or dp [ j ] or dp [ ( j - arr [ i ] ) ] ) :
                    dp [ j ] = True
                else :
                    dp [ j ] = False
            j -= 1
    print ( 0 , end = " ▁ " )
    for j in range ( maxSum + 1 ) :
        if ( dp [ j ] == True ) :
            print ( j , end = " ▁ " )
    print ( "21" )
def printDistinct ( a , n ) :
    maxSum = 0
    for i in range ( n ) :
        maxSum += a [ i ]
    subsetSum ( a , n , maxSum )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 5 , 6 ]
    n = len ( arr )
    printDistinct ( arr , n )

Index/key variables: i, j
Accumulator variables: maxSum
Iterator variables: i, j
Python program:
size = 1000
def maxCost ( a : list , m : int , n : int ) -> int :
    if n < 0 or m < 0 :
        return int ( - 1e9 )
    elif m == 0 and n == 0 :
        return 0
    else :
        num = m + n
        if ( num & ( num - 1 ) ) == 0 :
            return a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 )
        else :
            return a [ m ] [ n ] + max ( maxCost ( a , m - 1 , n ) , maxCost ( a , m , n - 1 ) )
def answer ( a : list , n : int ) -> int :
    return maxCost ( a , n - 1 , n - 1 )
if __name__ == " _ _ main _ _ " :
    a = [ [ 1 , 2 , 3 , 1 ] , [ 4 , 5 , 6 , 1 ] , [ 7 , 8 , 9 , 1 ] , [ 1 , 1 , 1 , 1 ] ]
    n = 4
    print ( answer ( a , n ) )

Index/key variables: m, n
Python program:
def dfs ( n , m , stepNum ) :
    if ( stepNum <= m and stepNum >= n ) :
        print ( stepNum , end = " ▁ " )
    if ( stepNum == 0 or stepNum > m ) :
        return
    lastDigit = stepNum % 10
    stepNumA = stepNum * 10 + ( lastDigit - 1 )
    stepNumB = stepNum * 10 + ( lastDigit + 1 )
    if ( lastDigit == 0 ) :
        dfs ( n , m , stepNumB )
    elif ( lastDigit == 9 ) :
        dfs ( n , m , stepNumA )
    else :
        dfs ( n , m , stepNumA )
        dfs ( n , m , stepNumB )
def displaySteppingNumbers ( n , m ) :
    for i in range ( 10 ) :
        dfs ( n , m , i )
n , m = 0 , 21
displaySteppingNumbers ( n , m )

Iterator variables: i
Python program:
def numberOfBST ( n ) :
    dp = [ 0 ] * ( n + 1 )
    dp [ 0 ] , dp [ 1 ] = 1 , 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( 1 , i + 1 ) :
            dp [ i ] = dp [ i ] + ( dp [ i - j ] * dp [ j - 1 ] )
    return dp [ n ]
if __name__ == " _ _ main _ _ " :
    n = 3
    print ( " Number ▁ of ▁ structurally ▁ Unique ▁ BST ▁ with " , n , " keys ▁ are ▁ : " , numberOfBST ( n ) )

Index/key variables: i, n
Iterator variables: i, j
Python program:
MAX = 1000
memo = [ [ 0.0 for i in range ( MAX ) ] for i in range ( MAX ) ]
def score ( n , A , k ) :
    if ( memo [ n ] [ k ] > 0 ) :
        return memo [ n ] [ k ]
    sum = 0
    i = n - 1
    while ( i > 0 ) :
        sum += A [ i ]
        memo [ n ] [ k ] = max ( memo [ n ] [ k ] , score ( i , A , k - 1 ) + int ( sum / ( n - i ) ) )
        i -= 1
    return memo [ n ] [ k ]
def largestSumOfAverages ( A , K ) :
    n = len ( A )
    sum = 0
    for i in range ( n ) :
        sum += A [ i ]
        memo [ i + 1 ] [ 1 ] = int ( sum / ( i + 1 ) )
    return score ( n , A , K )
if __name__ == ' _ _ main _ _ ' :
    A = [ 9 , 1 , 2 , 3 , 9 ]
    K = 3
    print ( largestSumOfAverages ( A , K ) )

Index/key variables: i, k, n
Accumulator variables: sum
Iterator variables: i
Python program:
def minMaxValues ( arr , n , m ) :
    sum = 0
    INF = 1000000000
    MAX = 50
    for i in range ( 0 , ( n + m ) ) :
        sum += arr [ i ]
        arr [ i ] += 50
    dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ]
    dp [ 0 ] [ 0 ] = 1
    for i in range ( 0 , ( n + m ) ) :
        for k in range ( min ( n , i + 1 ) , 0 , - 1 ) :
            for j in range ( 0 , MAX * MAX + 1 ) :
                if ( dp [ k - 1 ] [ j ] ) :
                    dp [ k ] [ j + arr [ i ] ] = 1
    max_value = - 1 * INF
    min_value = INF
    for i in range ( 0 , MAX * MAX + 1 ) :
        if ( dp [ n ] [ i ] ) :
            temp = i - 50 * n
            max_value = max ( max_value , temp * ( sum - temp ) )
            min_value = min ( min_value , temp * ( sum - temp ) )
    print ( " Maximum Value : { } Minimum Value : { } " . format ( max_value , min_value ) )
n = 2
m = 2
arr = [ 1 , 2 , 3 , 4 ]
minMaxValues ( arr , n , m )

Index/key variables: i, j, k, n
Accumulator variables: sum
Iterator variables: i, j, k
Python program:
inn = [ 0 ] * 100
out = [ 0 ] * 100
def dfs1 ( v , u , parent ) :
    global inn , out
    inn [ u ] = 0
    for child in v [ u ] :
        if ( child == parent ) :
            continue
        dfs1 ( v , child , u )
        inn [ u ] = max ( inn [ u ] , 1 + inn [ child ] )
def dfs2 ( v , u , parent ) :
    global inn , out
    mx1 , mx2 = - 1 , - 1
    for child in v [ u ] :
        if ( child == parent ) :
            continue
        if ( inn [ child ] >= mx1 ) :
            mx2 = mx1
            mx1 = inn [ child ]
        elif ( inn [ child ] > mx2 ) :
            mx2 = inn [ child ]
    for child in v [ u ] :
        if ( child == parent ) :
            continue
        longest = mx1
        if ( mx1 == inn [ child ] ) :
            longest = mx2
        out [ child ] = 1 + max ( out [ u ] , 1 + longest )
        dfs2 ( v , child , u )
def printHeights ( v , n ) :
    global inn , out
    dfs1 ( v , 1 , 0 )
    dfs2 ( v , 1 , 0 )
    for i in range ( 1 , n + 1 ) :
        print ( " The ▁ maximum ▁ height ▁ when ▁ node " , i , " is ▁ considered ▁ as ▁ root ▁ is " , max ( inn [ i ] , out [ i ] ) )
if __name__ == ' _ _ main _ _ ' :
    n = 11
    v = [ [ ] for i in range ( n + 1 ) ]
    v [ 1 ] . append ( 2 )
    v [ 2 ] . append ( 1 )
    v [ 1 ] . append ( 3 )
    v [ 3 ] . append ( 1 )
    v [ 1 ] . append ( 4 )
    v [ 4 ] . append ( 1 )
    v [ 2 ] . append ( 5 )
    v [ 5 ] . append ( 2 )
    v [ 2 ] . append ( 6 )
    v [ 6 ] . append ( 2 )
    v [ 3 ] . append ( 7 )
    v [ 7 ] . append ( 3 )
    v [ 7 ] . append ( 10 )
    v [ 10 ] . append ( 7 )
    v [ 7 ] . append ( 11 )
    v [ 11 ] . append ( 7 )
    v [ 4 ] . append ( 8 )
    v [ 8 ] . append ( 4 )
    v [ 4 ] . append ( 9 )
    v [ 9 ] . append ( 4 )
    printHeights ( v , n )

Index/key variables: child, i, u
Iterator variables: child, i
Python program:
def findGolomb ( n ) :
    if ( n == 1 ) :
        return 1
    return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) )
def printGolomb ( n ) :
    for i in range ( 1 , n + 1 ) :
        print ( findGolomb ( i ) , end = " ▁ " )
n = 9
printGolomb ( n )

Iterator variables: i
Python program:
def printknapSack ( W , wt , val , n ) :
    K = [ [ 0 for w in range ( W + 1 ) ] for i in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        for w in range ( W + 1 ) :
            if i == 0 or w == 0 :
                K [ i ] [ w ] = 0
            elif 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 ]
    res = K [ n ] [ W ]
    print ( res )
    w = W
    for i in range ( n , 0 , - 1 ) :
        if res <= 0 :
            break
        if res == K [ i - 1 ] [ w ] :
            continue
        else :
            print ( wt [ i - 1 ] )
            res = res - val [ i - 1 ]
            w = w - wt [ i - 1 ]
val = [ 60 , 100 , 120 ]
wt = [ 10 , 20 , 30 ]
W = 50
n = len ( val )
printknapSack ( W , wt , val , n )

Index/key variables: W, i, n, w
Iterator variables: i, w
Python program:
def maximizeSum ( a , n ) :
    maximum = max ( a )
    ans = dict . fromkeys ( range ( 0 , n + 1 ) , 0 )
    for i in range ( n ) :
        ans [ a [ i ] ] += 1
    result = 0
    i = maximum
    while i > 0 :
        if ans [ i ] > 0 :
            result += i ;
            ans [ i - 1 ] -= 1 ;
            ans [ i ] -= 1 ;
        else :
            i -= 1 ;
    return result ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 2 , 3 ]
    n = len ( a )
    print ( maximizeSum ( a , n ) )

Index/key variables: i
Accumulator variables: result
Iterator variables: i
Python program:
def sequence ( n ) :
    f = [ 0 , 1 , 1 ]
    print ( f [ 1 ] , end = " ▁ " ) ,
    print ( f [ 2 ] , end = " ▁ " ) ,
    for i in range ( 3 , n + 1 ) :
        f . append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] )
        print ( f [ i ] , end = " ▁ " ) ,
n = 13
sequence ( n )

Index/key variables: i
Accumulator variables: f
Iterator variables: i
Python program:
def longestSubsequenceCommonSegment ( k , s1 , s2 ) :
    n = len ( s1 )
    m = len ( s2 )
    lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
    cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , m + 1 ) :
            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 a in range ( k , cnt [ i ] [ j ] + 1 ) :
                    lcs [ i ] [ j ] = max ( lcs [ i ] [ j ] , lcs [ i - a ] [ j - a ] + a )
    return lcs [ n ] [ m ]
k = 4
s1 = " aggasdfa "
s2 = " aggajasdfa "
print ( longestSubsequenceCommonSegment ( k , s1 , s2 ) )

Index/key variables: i, j, m, n
Iterator variables: a, i, j
Python program:
row = 5
col = 5
def isPath ( arr ) :
    Dir = [ [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] ]
    q = [ ]
    q . append ( ( 0 , 0 ) )
    while ( len ( q ) > 0 ) :
        p = q [ 0 ]
        q . pop ( 0 )
        arr [ p [ 0 ] ] [ p [ 1 ] ] = - 1
        if ( p == ( row - 1 , col - 1 ) ) :
            return True
        for i in range ( 4 ) :
            a = p [ 0 ] + Dir [ i ] [ 0 ]
            b = p [ 1 ] + Dir [ i ] [ 1 ]
            if ( a >= 0 and b >= 0 and a < row and b < col and arr [ a ] [ b ] != - 1 ) :
                q . append ( ( a , b ) )
    return False
arr = [ [ 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 ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: a, b, i
Accumulator variables: q
Iterator variables: i
Python program:
def zigzag ( n , k ) :
    dp = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ]
    dp [ 0 ] [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        dp [ i ] [ 0 ] = 0
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , k + 1 ) :
            dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i - j ] )
    return dp [ n ] [ k ]
n = 4
k = 3
print ( zigzag ( n , k ) )

Index/key variables: i, j, k, n
Iterator variables: i, j
Python program:
def binomialCoeff ( n , k ) :
    C = [ [ 0 for j in range ( k + 1 ) ] for i in range ( n + 1 ) ]
    for i in range ( 0 , n + 1 ) :
        for j in range ( 0 , min ( i , k ) + 1 ) :
            if ( j == 0 or j == i ) :
                C [ i ] [ j ] = 1
            else :
                C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] )
    return C [ n ] [ k ]
def lobb ( n , m ) :
    return ( ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) )
n = 5
m = 3
print ( int ( lobb ( n , m ) ) )

Index/key variables: i, j, k, n
Iterator variables: i, j
Python program:
def countarray ( n , k , x ) :
    dp = list ( )
    dp . append ( 0 )
    dp . append ( 1 )
    i = 2
    while i < n :
        dp . append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] )
        i = i + 1
    return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
n = 4
k = 3
x = 2
print ( countarray ( n , k , x ) )

Accumulator variables: dp
Python program:
MAX = 100
MAX_CHAR = 26
def precompute ( s , n , l , r ) :
    l [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] [ 0 ] = 1
    for i in range ( 1 , n ) :
        for j in range ( MAX_CHAR ) :
            l [ j ] [ i ] += l [ j ] [ i - 1 ]
        l [ ord ( s [ i ] ) - ord ( ' a ' ) ] [ i ] += 1
    r [ ord ( s [ n - 1 ] ) - ord ( ' a ' ) ] [ n - 1 ] = 1
    k = n - 2
    while ( k >= 0 ) :
        for j in range ( MAX_CHAR ) :
            r [ j ] [ k ] += r [ j ] [ k + 1 ]
        r [ ord ( s [ k ] ) - ord ( ' a ' ) ] [ k ] += 1
        k -= 1
def countPalindromes ( k , n , l , r ) :
    ans = 0
    if ( k == 1 ) :
        for i in range ( MAX_CHAR ) :
            ans += l [ i ] [ n - 1 ]
        return ans
    if ( k == 2 ) :
        for i in range ( MAX_CHAR ) :
            ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 )
        return ans
    for i in range ( 1 , n - 1 ) :
        for j in range ( MAX_CHAR ) :
            ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ]
    return ans
s = " aabab "
k = 2
n = len ( s )
l = [ [ 0 for x in range ( MAX ) ] for y in range ( MAX_CHAR ) ]
r = [ [ 0 for x in range ( MAX ) ] for y in range ( MAX_CHAR ) ]
precompute ( s , n , l , r )
print ( countPalindromes ( k , n , l , r ) )

Index/key variables: i, j, k
Accumulator variables: ans
Iterator variables: i, j
Python program:
SIZE = 10
INT_MIN = - 10000000
def maxSum ( mat , n ) :
    if n == 1 :
        return mat [ 0 ] [ 0 ]
    dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
    maxSum = INT_MIN
    for j in range ( n ) :
        dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ]
    for i in range ( n - 2 , - 1 , - 1 ) :
        for j in range ( n ) :
            maxi = INT_MIN
            if ( ( ( ( j - 1 ) >= 0 ) and ( maxi < dp [ i + 1 ] [ j - 1 ] ) ) ) :
                maxi = dp [ i + 1 ] [ j - 1 ]
            if ( ( ( ( j + 1 ) < n ) and ( maxi < dp [ i + 1 ] [ j + 1 ] ) ) ) :
                maxi = dp [ i + 1 ] [ j + 1 ]
            dp [ i ] [ j ] = mat [ i ] [ j ] + maxi
    for j in range ( n ) :
        if ( maxSum < dp [ 0 ] [ j ] ) :
            maxSum = dp [ 0 ] [ j ]
    return maxSum
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 5 , 6 , 1 , 7 ] , [ - 2 , 10 , 8 , - 1 ] , [ 3 , - 7 , - 9 , 11 ] , [ 12 , - 4 , 2 , 6 ] ]
    n = 4
    print ( " Maximum ▁ Sum = " , maxSum ( mat , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def longestRepeatedSubSeq ( str ) :
    n = len ( str )
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , n + 1 ) :
            if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
                dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
            else :
                dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] )
    res = ' '
    i = n
    j = n
    while ( i > 0 and j > 0 ) :
        if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) :
            res += str [ i - 1 ]
            i -= 1
            j -= 1
        elif ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) :
            i -= 1
        else :
            j -= 1
    res = ' ' . join ( reversed ( res ) )
    return res
str = ' AABEBCDD '
print ( longestRepeatedSubSeq ( str ) )

Index/key variables: i, j
Accumulator variables: res
Iterator variables: i, j
Python program:
def dfs ( node , parent , tree , colour , answer ) :
    answer [ node ] = colour [ node ]
    for u in tree [ node ] :
        if ( u == parent ) :
            continue
        dfs ( u , node , tree , colour , answer )
        answer [ node ] += max ( answer [ u ] , 0 )
def maxDiff ( tree , colour , N ) :
    answer = [ 0 for _ in range ( N + 1 ) ]
    dfs ( 1 , 0 , tree , colour , answer )
    high = 0
    for i in range ( 1 , N + 1 ) :
        high = max ( high , answer [ i ] )
        answer [ i ] = 0
    for i in range ( 1 , N + 1 ) :
        if colour [ i ] == - 1 :
            colour [ i ] = 1
        else :
            colour [ i ] = - 1
    dfs ( 1 , 0 , tree , colour , answer )
    for i in range ( 1 , N ) :
        high = max ( high , answer [ i ] )
    return high
N = 5
tree = [ list ( ) for _ in range ( N + 1 ) ]
tree [ 1 ] . append ( 2 )
tree [ 2 ] . append ( 1 )
tree [ 1 ] . append ( 3 )
tree [ 3 ] . append ( 1 )
tree [ 2 ] . append ( 4 )
tree [ 4 ] . append ( 2 )
tree [ 3 ] . append ( 5 )
tree [ 5 ] . append ( 3 )
colour = [ 0 , 1 , 1 , - 1 , - 1 , 1 ]
print ( maxDiff ( tree , colour , N ) )

Index/key variables: i, node, u
Iterator variables: i, u
Python program:
MAX = 50
def solve ( dp , a , low , high , 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 ]
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 1 , 5 , 2 ]
    n = len ( arr )
    dp = [ [ 0 for x in range ( MAX ) ] for y in range ( MAX ) ]
    print ( solve ( dp , arr , 0 , n - 1 , 1 ) )

Index/key variables: high, low
Python program:
def maxSumBitonicSubArr ( arr , n ) :
    msis = [ None ] * n
    msds = [ None ] * n
    max_sum = 0
    msis [ 0 ] = arr [ 0 ]
    for i in range ( 1 , n ) :
        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 i in range ( n - 2 , - 1 , - 1 ) :
        if ( arr [ i ] > arr [ i + 1 ] ) :
            msds [ i ] = msds [ i + 1 ] + arr [ i ]
        else :
            msds [ i ] = arr [ i ]
    for i in range ( n ) :
        if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) :
            max_sum = ( msis [ i ] + msds [ i ] - arr [ i ] )
    return max_sum
arr = [ 5 , 3 , 9 , 2 , 7 , 6 , 4 ] ;
n = len ( arr )
print ( " Maximum ▁ Sum ▁ = ▁ " + str ( maxSumBitonicSubArr ( arr , n ) ) )

Index/key variables: i
Iterator variables: i
Python program:
def minTimeForWritingChars ( N , insert , remov , cpy ) :
    if N == 0 :
        return 0
    if N == 1 :
        return insert
    dp = [ 0 ] * ( N + 1 )
    dp [ 1 ] = insert
    for i in range ( 2 , N + 1 ) :
        if i % 2 == 0 :
            dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i // 2 ] + cpy )
        else :
            dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ ( i + 1 ) // 2 ] + cpy + remov )
    return dp [ N ]
if __name__ == " _ _ main _ _ " :
    N = 9
    insert = 1
    remov = 2
    cpy = 1
    print ( minTimeForWritingChars ( N , insert , remov , cpy ) )

Index/key variables: N, i
Iterator variables: i
Python program:
def countWays ( n , m ) :
    count = [ ]
    for i in range ( n + 2 ) :
        count . append ( 0 )
    count [ 0 ] = 0
    for i in range ( 1 , n + 1 ) :
        if ( i > m ) :
            count [ i ] = count [ i - 1 ] + count [ i - m ]
        elif ( i < m or i == 1 ) :
            count [ i ] = 1
        else :
            count [ i ] = 2
    return count [ n ]
n = 7
m = 4
print ( " Number ▁ of ▁ ways ▁ = ▁ " , countWays ( n , m ) )

Index/key variables: i, n
Accumulator variables: count
Iterator variables: i
Python program:
def LAS ( arr , n ) :
    inc = 1
    dec = 1
    for i in range ( 1 , n ) :
        if ( arr [ i ] > arr [ i - 1 ] ) :
            inc = dec + 1
        elif ( arr [ i ] < arr [ i - 1 ] ) :
            dec = inc + 1
    return max ( inc , dec )
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 ]
    n = len ( arr )
    print ( LAS ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def utility_fun_for_del ( Str , i , 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 ) ) )
def min_ele_del ( Str ) :
    return utility_fun_for_del ( Str , 0 , len ( Str ) - 1 )
Str = " abefbac "
print ( " Minimum ▁ element ▁ of ▁ deletions ▁ = " , min_ele_del ( Str ) )

Index/key variables: i, j
Python program:
MAX = 100
def largestZigZagSumRec ( mat , i , j , n ) :
    if ( i == n - 1 ) :
        return mat [ i ] [ j ]
    zzs = 0
    for k in range ( n ) :
        if ( k != j ) :
            zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) )
    return zzs + mat [ i ] [ j ]
def largestZigZag ( mat , n ) :
    res = 0
    for j in range ( n ) :
        res = max ( res , largestZigZagSumRec ( mat , 0 , j , n ) )
    return res
if __name__ == " _ _ main _ _ " :
    n = 3
    mat = [ [ 4 , 2 , 1 ] , [ 3 , 9 , 6 ] , [ 11 , 3 , 15 ] ]
    print ( " Largest ▁ zigzag ▁ sum : ▁ " , largestZigZag ( mat , n ) )

Index/key variables: i, j
Iterator variables: j, k
Python program:
M = 26
def waysToIncreaseLCSBy1 ( str1 , str2 ) :
    m = len ( str1 )
    n = len ( str2 )
    position = [ [ ] for i in range ( M ) ]
    for i in range ( 1 , n + 1 , 1 ) :
        position [ ord ( str2 [ i - 1 ] ) - 97 ] . append ( i )
    lcsl = [ [ 0 for i in range ( n + 2 ) ] for j in range ( m + 2 ) ]
    lcsr = [ [ 0 for i in range ( n + 2 ) ] for j in range ( m + 2 ) ]
    for i in range ( 1 , m + 1 , 1 ) :
        for j in range ( 1 , n + 1 , 1 ) :
            if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) :
                lcsl [ i ] [ j ] = 1 + lcsl [ i - 1 ] [ j - 1 ]
            else :
                lcsl [ i ] [ j ] = max ( lcsl [ i - 1 ] [ j ] , lcsl [ i ] [ j - 1 ] )
    for i in range ( m , 0 , - 1 ) :
        for j in range ( n , 0 , - 1 ) :
            if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) :
                lcsr [ i ] [ j ] = 1 + lcsr [ i + 1 ] [ j + 1 ]
            else :
                lcsr [ i ] [ j ] = max ( lcsr [ i + 1 ] [ j ] , lcsr [ i ] [ j + 1 ] )
    ways = 0
    for i in range ( 0 , m + 1 , 1 ) :
        for C in range ( 0 , 26 , 1 ) :
            for j in range ( 0 , len ( position [ C ] ) , 1 ) :
                p = position [ C ] [ j ]
                if ( lcsl [ i ] [ p - 1 ] + lcsr [ i + 1 ] [ p + 1 ] == lcsl [ m ] [ n ] ) :
                    ways += 1
    return ways
str1 = " abcabc "
str2 = " abcd "
print ( waysToIncreaseLCSBy1 ( str1 , str2 ) )

Index/key variables: C, i, j, m, n
Accumulator variables: ways
Iterator variables: C, i, j
Python program:
from math import log2
MAX = 100001
dp = [ 0 ] * MAX
def probability ( k , n ) :
    ans = 0
    for i in range ( k , n + 1 ) :
        res = dp [ n ] - dp [ i ] - dp [ n - i ] - n
        ans = ans + pow ( 2.0 , res )
    return ans
def precompute ( ) :
    for i in range ( 2 , MAX ) :
        dp [ i ] = log2 ( i ) + dp [ i - 1 ]
if __name__ == ' _ _ main _ _ ' :
    precompute ( )
    print ( probability ( 2 , 3 ) )
    print ( probability ( 3 , 6 ) )
    print ( probability ( 500 , 1000 ) )

Index/key variables: i, n
Iterator variables: i