dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def find ( i ) :
    while parent [ i ] != i :
        i = parent [ i ]
    return i
def union ( i , j ) :
    a = find ( i )
    b = find ( j )
    parent [ a ] = b
def kruskalMST ( cost ) :
    for i in range ( V ) :
        parent [ i ] = i
    edge_count = 0
    while edge_count < V - 1 :
        min = INF
        a = - 1
        b = - 1
        for i in range ( V ) :
            for j in range ( V ) :
                if find ( i ) != find ( j ) and cost [ i ] [ j ] < min :
                    min = cost [ i ] [ j ]
                    a = i
                    b = j
        union ( a , b )
        print ( ' Edge ▁ { } : ( { } , ▁ { } ) ▁ cost : { } ' . format ( edge_count , a , b , min ) )
        edge_count += 1
        mincost += min
    print ( " Minimum ▁ cost = ▁ { } " . format ( mincost ) )
V = 5
parent = [ i for i in range ( V ) ]
INF = float ( ' inf ' )
cost = [ [ INF , 2 , INF , 6 , INF ] , [ 2 , INF , 3 , 8 , 5 ] , [ INF , 3 , INF , INF , 7 ] , [ 6 , 8 , INF , INF , 9 ] , [ INF , 5 , 7 , 9 , INF ] ]
kruskalMST ( cost )

Index/key variables: a, i, j
Accumulator variables: edge_count, mincost
Iterator variables: i, j
Python program:
import bisect
def longestSubsequence ( a , n , q , m ) :
    a . sort ( )
    Sum = 0
    b = [ None ] * n
    for i in range ( 0 , n ) :
        Sum += a [ i ]
        av = Sum // ( i + 1 )
        b [ i ] = av + 1
    b . sort ( )
    for i in range ( 0 , m ) :
        k = q [ i ]
        longest = bisect . bisect_right ( b , k )
        print ( " Answer ▁ to ▁ Query " , i + 1 , " : " , longest )
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 3 , 2 , 5 , 4 ]
    n = len ( a )
    q = [ 4 , 2 , 1 , 5 ]
    m = len ( q )
    longestSubsequence ( a , n , q , m )

Index/key variables: i
Accumulator variables: Sum
Iterator variables: i
Python program:
from queue import PriorityQueue
def DecreasingArray ( a , n ) :
    ss , dif = ( 0 , 0 )
    pq = PriorityQueue ( )
    for i in range ( n ) :
        tmp = 0
        if not pq . empty ( ) :
            tmp = pq . get ( )
            pq . put ( tmp )
        if not pq . empty ( ) and tmp < a [ i ] :
            dif = a [ i ] - tmp
            ss += dif
            pq . get ( )
        pq . put ( a [ i ] )
    return ss
if __name__ == " _ _ main _ _ " :
    a = [ 3 , 1 , 2 , 1 ]
    n = len ( a )
    print ( DecreasingArray ( a , n ) )

Index/key variables: i
Accumulator variables: ss
Iterator variables: i
Python program:
def solve ( a , b , n ) :
    s = 0
    for i in range ( 0 , n ) :
        s += a [ i ] + b [ i ]
    if n == 1 :
        return a [ 0 ] + b [ 0 ]
    if s % n != 0 :
        return - 1
    x = s // n
    for i in range ( 0 , n ) :
        if a [ i ] > x :
            return - 1
        if i > 0 :
            a [ i ] += b [ i - 1 ]
            b [ i - 1 ] = 0
        if a [ i ] == x :
            continue
        y = a [ i ] + b [ i ]
        if i + 1 < n :
            y += b [ i + 1 ]
        if y == x :
            a [ i ] = y
            b [ i ] = 0
            if i + 1 < n : b [ i + 1 ] = 0
            continue
        if a [ i ] + b [ i ] == x :
            a [ i ] += b [ i ]
            b [ i ] = 0
            continue
        if i + 1 < n and a [ i ] + b [ i + 1 ] == x :
            a [ i ] += b [ i + 1 ]
            b [ i + 1 ] = 0
            continue
        return - 1
    for i in range ( 0 , n ) :
        if b [ i ] != 0 :
            return - 1
    return x
if __name__ == " _ _ main _ _ " :
    a = [ 6 , 14 , 21 , 1 ]
    b = [ 15 , 7 , 10 , 10 ]
    n = len ( a )
    print ( solve ( a , b , n ) )

Index/key variables: i
Accumulator variables: s, y
Iterator variables: i
Python program:
def bestpermutation ( arr , k , n ) :
    h = { }
    for i in range ( n ) :
        h [ arr [ i ] ] = i
    if ( n <= k ) :
        arr . sort ( )
        arr . reverse ( )
    else :
        for j in range ( n , 0 , - 1 ) :
            if ( k > 0 ) :
                initial_index = h [ j ]
                best_index = n - j
                if ( initial_index != best_index ) :
                    h [ j ] = best_index
                    element = arr [ best_index ]
                    h [ element ] = initial_index
                    arr [ best_index ] , arr [ initial_index ] = ( arr [ initial_index ] , arr [ best_index ] )
                    k -= 1
arr = [ 3 , 1 , 4 , 2 , 5 ]
k = 10
n = len ( arr )
bestpermutation ( arr , k , n )
print ( " Largest ▁ possible ▁ permutation ▁ after " , k , " swaps ▁ is " , end = " ▁ " )
for i in range ( n ) :
    print ( arr [ i ] , end = " ▁ " )

Index/key variables: best_index, element, i, initial_index, j
Accumulator variables: k
Iterator variables: i, j
Python program:
def bestFit ( blockSize , m , processSize , n ) :
    allocation = [ - 1 ] * n
    for i in range ( n ) :
        bestIdx = - 1
        for j in range ( m ) :
            if blockSize [ j ] >= processSize [ i ] :
                if bestIdx == - 1 :
                    bestIdx = j
                elif blockSize [ bestIdx ] > blockSize [ j ] :
                    bestIdx = j
        if bestIdx != - 1 :
            allocation [ i ] = bestIdx
            blockSize [ bestIdx ] -= processSize [ i ]
    print ( " Process ▁ No . ▁ Process ▁ Size ▁ Block ▁ no . " )
    for i in range ( n ) :
        print ( i + 1 , " ▁ " , processSize [ i ] , end = " ▁ " )
        if allocation [ i ] != - 1 :
            print ( allocation [ i ] + 1 )
        else :
            print ( " Not ▁ Allocated " )
if __name__ == ' _ _ main _ _ ' :
    blockSize = [ 100 , 500 , 200 , 300 , 600 ]
    processSize = [ 212 , 417 , 112 , 426 ]
    m = len ( blockSize )
    n = len ( processSize )
    bestFit ( blockSize , m , processSize , n )

Index/key variables: bestIdx, i, j
Iterator variables: i, j
Python program:
def firstFit ( weight , n , c ) :
    res = 0 ;
    bin_rem = [ 0 ] * n ;
    for i in range ( n ) :
        j = 0 ;
        min = c + 1 ;
        bi = 0 ;
        for j in range ( res ) :
            if ( bin_rem [ j ] >= weight [ i ] and bin_rem [ j ] - weight [ i ] < min ) :
                bi = j ;
                min = bin_rem [ j ] - weight [ i ] ;
        if ( min == c + 1 ) :
            bin_rem [ res ] = c - weight [ i ] ;
            res += 1 ;
            bin_rem [ bi ] -= weight [ i ] ;
    return res ;
if __name__ == ' _ _ main _ _ ' :
    weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ;
    c = 10 ;
    n = len ( weight ) ;
    print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " , firstFit ( weight , n , c ) ) ;

Index/key variables: bi, i, j, res
Iterator variables: i, j
Python program:
def LongestOddEvenSubarray ( A , N ) :
    dp = [ 0 for i in range ( N ) ]
    dp [ 0 ] = 1
    ans = 1
    for i in range ( 1 , N , 1 ) :
        if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) :
            dp [ i ] = dp [ i - 1 ] + 1
        else :
            dp [ i ] = 1
    for i in range ( N ) :
        ans = max ( ans , dp [ i ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ]
    N = len ( A )
    print ( LongestOddEvenSubarray ( A , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def totalSubarrays ( arr , n , k ) :
    ans = 0
    i = 0
    while ( i < n ) :
        if ( arr [ i ] > k ) :
            i += 1
            continue
        count = 0
        while ( i < n and arr [ i ] <= k ) :
            i += 1
            count += 1
        ans += ( ( count * ( count + 1 ) ) // 2 )
    return ans
def countSubarrays ( arr , n , k ) :
    count1 = totalSubarrays ( arr , n , k - 1 )
    count2 = totalSubarrays ( arr , n , k )
    ans = count2 - count1
    return ans
if __name__ == ' _ _ main _ _ ' :
    n = 4
    k = 3
    arr = [ 2 , 1 , 3 , 4 ]
    print ( countSubarrays ( arr , n , k ) )

Index/key variables: i
Accumulator variables: ans, count
Python program:
def maxSumWO3Consec ( A , N ) :
    if ( N == 1 ) :
        return A [ 0 ]
    if ( N == 2 ) :
        return A [ 0 ] + A [ 1 ]
    third = A [ 0 ]
    second = third + A [ 1 ]
    first = max ( second , A [ 1 ] + A [ 2 ] )
    sum = max ( max ( third , second ) , first )
    for i in range ( 3 , N , 1 ) :
        sum = max ( max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] )
        third = second
        second = first
        first = sum
    return sum
if __name__ == ' _ _ main _ _ ' :
    A = [ 3000 , 2000 , 1000 , 3 , 10 ]
    N = len ( A )
    print ( maxSumWO3Consec ( A , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def findMinOperations ( n ) :
    dp = [ 0 for i in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        dp [ i ] = 999999
    dp [ 2 ] = 0
    for i in range ( 2 , n + 1 ) :
        if ( i * 5 <= n ) :
            dp [ i * 5 ] = min ( dp [ i * 5 ] , dp [ i ] + 1 )
        if ( i + 3 <= n ) :
            dp [ i + 3 ] = min ( dp [ i + 3 ] , dp [ i ] + 1 )
    return dp [ n ]
if __name__ == ' _ _ main _ _ ' :
    n = 28
    m = findMinOperations ( n )
    if ( m != 9999 ) :
        print ( m )
    else :
        print ( - 1 )

Index/key variables: i, n
Iterator variables: i
Python program:
def getValue ( arr , N ) :
    dp = [ 0 for i in range ( N ) ]
    for i in range ( 1 , N ) :
        minn = arr [ i ]
        maxx = arr [ i ]
        j = i
        while ( j >= 0 ) :
            minn = min ( arr [ j ] , minn )
            maxx = max ( arr [ j ] , maxx )
            dp [ i ] = max ( dp [ i ] , maxx - minn + ( dp [ j - 1 ] if ( j >= 1 ) else 0 ) )
            j -= 1
    return dp [ N - 1 ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 8 , 1 , 7 , 9 , 2 ]
    N = len ( arr )
    print ( getValue ( arr , N ) )

Index/key variables: i, j
Iterator variables: i
Python program:
def isUnique ( s ) :
    return True if len ( set ( s ) ) == 1 else False
def maxScore ( s , a ) :
    n = len ( s )
    if n == 0 :
        return 0
    if n == 1 :
        return a [ 0 ]
    mx = - 1
    for i in range ( n ) :
        for j in range ( i , n ) :
            sub = s [ i : j + 1 ]
            if isUnique ( sub ) :
                mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : i ] + s [ j + 1 : ] , a ) )
    return mx
if __name__ == " _ _ main _ _ " :
    s = "011"
    a = [ 1 , 3 , 1 ]
    print ( maxScore ( s , a ) )

Iterator variables: i, j
Python program:
dp = dict ( )
def maxScore ( s , a ) :
    if s in dp :
        return dp [ s ]
    n = len ( s )
    if n == 0 :
        return 0
    if n == 1 :
        return a [ 0 ]
    head = 0
    mx = - 1
    while head < n :
        tail = head
        while tail < n :
            if s [ tail ] != s [ head ] :
                head = tail
                break
            sub = s [ head : tail + 1 ]
            mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : head ] + s [ tail + 1 : ] , a ) )
            tail += 1
        if tail == n :
            break
    dp [ s ] = mx
    return mx
if __name__ == " _ _ main _ _ " :
    s = " abb "
    a = [ 1 , 3 , 1 ]
    print ( maxScore ( s , a ) )

Index/key variables: head, s, tail
Python program:
size = 100
ans = [ [ 0 for i in range ( size ) ] for j in range ( size ) ]
def numberOfUniqueOutcomes ( n , s ) :
    if ( s < n ) :
        ans [ n ] [ s ] = 0 ;
    elif ( n == 1 or n == s ) :
        ans [ n ] [ s ] = 1 ;
    elif ( ans [ n ] [ s ] == 0 ) :
        ans [ n ] [ s ] = ( numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) )
    return ans [ n ] [ s ] ;
N = 5
S = 8
print ( numberOfUniqueOutcomes ( N , S ) )

Index/key variables: n, s
Python program:
def printMinimumRemovals ( s , X ) :
    N = len ( s )
    M = len ( X )
    dp = [ [ 0 for x in range ( M ) ] for y in range ( N ) ]
    for j in range ( M ) :
        if ( s [ 0 ] == X [ j ] ) :
            dp [ 0 ] [ j ] = 1
    for i in range ( 1 , N ) :
        for j in range ( M ) :
            if ( s [ i ] == X [ j ] ) :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1
                if ( j != 0 ) :
                    dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] )
            else :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j ]
    print ( dp [ N - 1 ] [ M - 1 ] )
if __name__ == " _ _ main _ _ " :
    s = " btagd "
    X = " bad "
    printMinimumRemovals ( s , X )

Index/key variables: i, j
Iterator variables: i, j
Python program:
dp = [ - 1 for i in range ( 100000 ) ]
def findWays ( x ) :
    if ( x < 0 ) :
        return 0
    if ( x == 0 ) :
        return 1
    if ( x == 1 ) :
        return 2
    if ( x == 2 ) :
        return 4
    if ( dp [ x ] != - 1 ) :
        return dp [ x ]
    count = findWays ( x - 1 )
    count += findWays ( x - 2 )
    count += findWays ( x - 3 )
    dp [ x ] = count
    return dp [ x ]
if __name__ == ' _ _ main _ _ ' :
    n = 4
    print ( findWays ( n ) )

Index/key variables: x
Python program:
from bisect import bisect_left , bisect_right
import sys
def maxSubarraySum ( sum , k , row ) :
    curSum , curMax = 0 , - sys . maxsize - 1
    sumSet = { }
    sumSet [ 0 ] = 1
    for r in range ( row ) :
        curSum += sum [ r ]
        arr = list ( sumSet . keys ( ) )
        it = bisect_left ( arr , curSum - k )
        if ( it != len ( arr ) ) :
            curMax = max ( curMax , curSum - it )
        sumSet [ curSum ] = 1
    return curMax
def maxSumSubmatrix ( matrix , k ) :
    row = len ( matrix )
    col = len ( matrix [ 0 ] )
    ret = - sys . maxsize - 1
    for i in range ( col ) :
        sum = [ 0 ] * ( row )
        for j in range ( i , col ) :
            for r in range ( row ) :
                sum [ r ] += matrix [ r ] [ j ]
            curMax = maxSubarraySum ( sum , k , row )
            ret = max ( ret , curMax )
    print ( ret )
if __name__ == ' _ _ main _ _ ' :
    matrix = [ [ 1 , 0 , 1 ] , [ 0 , - 2 , 3 ] ]
    K = 2
    maxSumSubmatrix ( matrix , K )

Index/key variables: curSum, j, r
Iterator variables: i, j, r
Python program:
def binaryStrings ( N ) :
    dp = [ 0 ] * ( N + 1 )
    dp [ 0 ] = 0
    dp [ 1 ] = 0
    power = [ 0 ] * ( N + 1 )
    power [ 0 ] = 1
    for i in range ( 1 , N + 1 ) :
        power [ i ] = 2 * power [ i - 1 ]
    for i in range ( 2 , N + 1 ) :
        dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ]
    print ( dp [ N ] )
if __name__ == ' _ _ main _ _ ' :
    N = 12
    binaryStrings ( N )

Index/key variables: N, i
Iterator variables: i
Python program:
import sys
def kadane ( v ) :
    currSum = 0
    maxSum = - sys . maxsize - 1
    for i in range ( len ( v ) ) :
        currSum += v [ i ]
        if ( currSum > maxSum ) :
            maxSum = currSum
        if ( currSum < 0 ) :
            currSum = 0
    return maxSum
def maxSubmatrixSum ( A ) :
    r = len ( A )
    c = len ( A [ 0 ] )
    prefix = [ [ 0 for i in range ( c ) ] for j in range ( r ) ]
    for i in range ( r ) :
        for j in range ( c ) :
            if ( j == 0 ) :
                prefix [ i ] [ j ] = A [ i ] [ j ]
            else :
                prefix [ i ] [ j ] = A [ i ] [ j ] + prefix [ i ] [ j - 1 ]
    maxSum = - sys . maxsize - 1
    for i in range ( c ) :
        for j in range ( i , c ) :
            v = [ ]
            for k in range ( r ) :
                el = 0
                if ( i == 0 ) :
                    el = prefix [ k ] [ j ]
                else :
                    el = prefix [ k ] [ j ] - prefix [ k ] [ i - 1 ]
                v . append ( el )
            maxSum = max ( maxSum , kadane ( v ) )
    print ( maxSum )
matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ]
maxSubmatrixSum ( matrix )

Index/key variables: i, j, k
Accumulator variables: currSum, v
Iterator variables: i, j, k
Python program:
def minCost ( costs , N ) :
    if ( N == 0 ) :
        return 0
    dp = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ]
    dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ]
    dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ]
    dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ]
    for i in range ( 1 , N , 1 ) :
        dp [ i ] [ 0 ] = min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ]
        dp [ i ] [ 1 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ]
        dp [ i ] [ 2 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ]
    print ( min ( dp [ N - 1 ] [ 0 ] , min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) )
if __name__ == ' _ _ main _ _ ' :
    costs = [ [ 14 , 2 , 11 ] , [ 11 , 14 , 5 ] , [ 14 , 3 , 10 ] ]
    N = len ( costs )
    minCost ( costs , N )

Index/key variables: i
Iterator variables: i
Python program:
dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ]
def solve ( A , i , sum , N ) :
    res = 2001
    if ( sum < 0 or ( i == N and sum != 0 ) ) :
        return 2001
    if ( sum == 0 or i >= N ) :
        dp [ i ] [ sum ] = 0
        return 0
    if ( dp [ i ] [ sum ] != - 1 ) :
        return dp [ i ] [ sum ]
    res = min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) )
    dp [ i ] [ sum ] = res
    return res
def minOp ( A , N ) :
    sum = 0
    for it in A :
        sum += it
    if ( sum % 2 == 0 ) :
        dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ]
        ans = solve ( A , 0 , sum // 2 , N )
        if ( ans < 0 or ans > N ) :
            print ( " - 1" )
        else :
            print ( ans )
    else :
        print ( - 1 )
A = [ 2 , 3 , 1 , 4 ]
N = len ( A )
minOp ( A , N )

Index/key variables: i, sum
Iterator variables: it
Python program:
dp = [ [ [ 0 for i in range ( 1001 ) ] for i in range ( 101 ) ] for i in range ( 101 ) ]
def countAverage ( n , K , arr ) :
    global dp
    dp [ 0 ] [ 0 ] [ 0 ] = 1
    for i in range ( n ) :
        for k in range ( n ) :
            for s in range ( 100 ) :
                dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ]
                dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ]
    cnt = 0
    for j in range ( 1 , n + 1 ) :
        cnt += dp [ n ] [ j ] [ K * j ]
    return cnt
if __name__ == ' _ _ main _ _ ' :
    arr = [ 9 , 7 , 8 , 9 ]
    K = 8
    N = len ( arr )
    print ( countAverage ( N , K , arr ) )

Index/key variables: i, j, k, n, s
Accumulator variables: cnt
Iterator variables: i, j, k, s
Python program:
def numberofpairs ( v , k ) :
    n = len ( v )
    if ( n % 2 == 1 ) :
        print ( 0 )
        return
    ans = 0
    dp = [ [ 0 for i in range ( 2 ) ] for j in range ( k ) ]
    for i in range ( k ) :
        for j in range ( 2 ) :
            dp [ i ] [ j ] = 0
    for i in range ( n ) :
        for j in range ( n ) :
            if ( ( i + j ) % 2 == 0 ) :
                dp [ v [ i ] [ j ] - 1 ] [ 0 ] += 1
            else :
                dp [ v [ i ] [ j ] - 1 ] [ 1 ] += 1
    for i in range ( k ) :
        for j in range ( i + 1 , k ) :
            ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ]
            ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ]
    print ( ans )
mat = [ [ 1 , 2 ] , [ 3 , 4 ] ]
K = 4
numberofpairs ( mat , K )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
import sys
dp = [ [ [ [ - 1 for i in range ( 50 ) ] for j in range ( 50 ) ] for k in range ( 50 ) ] for l in range ( 50 ) ]
def FindMaxS ( X , Y , Z , n , A , B , C ) :
    if ( X < 0 or Y < 0 or Z < 0 ) :
        return - sys . maxsize - 1
    if ( n < 0 ) :
        return 0
    if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) :
        return dp [ n ] [ X ] [ Y ] [ Z ]
    ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C )
    ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C )
    co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C )
    no = FindMaxS ( X , Y , Z , n - 1 , A , B , C )
    maximum = max ( ch , max ( ca , max ( co , no ) ) )
    dp [ n ] [ X ] [ Y ] [ Z ] = maximum
    return dp [ n ] [ X ] [ Y ] [ Z ]
if __name__ == ' _ _ main _ _ ' :
    X = 1
    Y = 1
    Z = 1
    A = [ 10 , 0 , 5 ]
    B = [ 5 , 10 , 0 ]
    C = [ 0 , 5 , 10 ]
    n = len ( B )
    print ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) )

Index/key variables: X, Y, Z, n
Python program:
MOD = 10 ** 9 + 7
MEM = [ 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 ]
def mod_pow2 ( n ) :
    while n >= len ( MEM ) :
        MEM . append ( ( MEM [ - 1 ] * 2 ) % MOD )
    return MEM [ n ]
def inversions ( bstr ) :
    total , zeros , questions = ( 0 , ) * 3
    for x in reversed ( bstr ) :
        if x == '1' :
            z = zeros * mod_pow2 ( questions )
            if questions == 0 :
                q = 0
            else :
                q = questions * mod_pow2 ( questions - 1 )
            total = ( total + z + q ) % MOD
        elif x == '0' :
            zeros += 1
        else :
            total *= 2
            z = zeros * mod_pow2 ( questions )
            if questions == 0 :
                q = 0
            else :
                q = questions * mod_pow2 ( questions - 1 )
            total = ( total + z + q ) % MOD
            questions += 1
    return total
def main ( ) :
    S = " ? 0 ? "
    print ( inversions ( S ) )
if __name__ == " _ _ main _ _ " :
    main ( )

Index/key variables: n
Accumulator variables: MEM, questions, total, zeros
Iterator variables: x
Python program:
def minRemovalsUtil ( arr ) :
    result = 0
    if ( len ( arr ) < 3 ) :
        return - 1
    leftIncreasing = [ 0 ] * len ( arr )
    rightIncreasing = [ 0 ] * len ( arr )
    for i in range ( 1 , len ( arr ) ) :
        for j in range ( i ) :
            if ( arr [ j ] < arr [ i ] ) :
                leftIncreasing [ i ] = max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ;
    for i in range ( len ( arr ) - 2 , - 1 , - 1 ) :
        j = len ( arr ) - 1
        while j > i :
            if ( arr [ j ] < arr [ i ] ) :
                rightIncreasing [ i ] = max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 )
            j -= 1
    for i in range ( len ( arr ) ) :
        if ( leftIncreasing [ i ] != 0 and rightIncreasing [ i ] != 0 ) :
            result = max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ;
    return len ( arr ) - ( result + 1 )
def minRemovals ( arr ) :
    ans = minRemovalsUtil ( arr )
    print ( ans )
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 ]
    minRemovals ( arr )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def bfs ( n , a , invGr , ans , parity ) :
    q = [ ]
    vis = [ 0 for i in range ( n + 1 ) ]
    dist = [ 0 for i in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        if ( ( a [ i ] + parity ) & 1 ) :
            q . append ( i )
            vis [ i ] = 1
    while ( len ( q ) != 0 ) :
        v = q [ 0 ]
        q . pop ( 0 )
        for u in invGr [ v ] :
            if ( not vis [ u ] ) :
                dist [ u ] = dist [ v ] + 1
                vis [ u ] = 1
                if ( ( a [ u ] + parity ) % 2 == 0 ) :
                    if ( ans [ u ] == - 1 ) :
                        ans [ u ] = dist [ u ]
                q . append ( u )
def minJumps ( a , jump , n ) :
    invGr = [ [ ] for i in range ( n + 1 ) ]
    ans = [ - 1 for i in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        for ind in [ i + jump [ i ] , i - jump [ i ] ] :
            if ( ind >= 1 and ind <= n ) :
                invGr [ ind ] . append ( i )
    bfs ( n , a , invGr , ans , 0 )
    bfs ( n , a , invGr , ans , 1 )
    for i in range ( 1 , n + 1 ) :
        print ( str ( ans [ i ] ) , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 4 , 2 , 5 , 2 , 1 ]
    jump = [ 0 , 1 , 2 , 3 , 1 , 2 ]
    N = len ( arr )
    minJumps ( arr , jump , N - 1 )

Index/key variables: i, ind, u, v
Accumulator variables: q
Iterator variables: i, ind, u