dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
n = 0
p = 0
rd = [ 0 ] * 1100
wt = [ 0 ] * 1100
cd = [ 0 ] * 1100
a = [ ]
b = [ ]
c = [ ]
ans = 0
def dfs ( w ) :
    global ans
    if ( cd [ w ] == 0 ) :
        return w
    if ( wt [ w ] < ans ) :
        ans = wt [ w ]
    return dfs ( cd [ w ] )
def solve ( arr ) :
    global ans
    i = 0
    while ( i < p ) :
        q = arr [ i ] [ 0 ]
        h = arr [ i ] [ 1 ]
        t = arr [ i ] [ 2 ]
        cd [ q ] = h
        wt [ q ] = t
        rd [ h ] = q
        i += 1
    a = [ ]
    b = [ ]
    c = [ ]
    for j in range ( 1 , n + 1 ) :
        if ( rd [ j ] == 0 and cd [ j ] ) :
            ans = 1000000000
            w = dfs ( j )
            a . append ( j )
            b . append ( w )
            c . append ( ans )
    print ( len ( a ) )
    for j in range ( len ( a ) ) :
        print ( a [ j ] , b [ j ] , c [ j ] )
n = 9
p = 6
arr = [ [ 7 , 4 , 98 ] , [ 5 , 9 , 72 ] , [ 4 , 6 , 10 ] , [ 2 , 8 , 22 ] , [ 9 , 7 , 17 ] , [ 3 , 1 , 66 ] ]
solve ( arr )

Index/key variables: h, i, j, q, w
Accumulator variables: a, b, c
Iterator variables: j
Python program:
def noAdjacentDup ( s ) :
    n = len ( s )
    for i in range ( 1 , n ) :
        if ( s [ i ] == s [ i - 1 ] ) :
            s [ i ] = " a "
            while ( s [ i ] == s [ i - 1 ] or ( i + 1 < n and s [ i ] == s [ i + 1 ] ) ) :
                s [ i ] += 1
            i += 1
    return s
s = list ( " geeksforgeeks " )
print ( " " . join ( noAdjacentDup ( s ) ) )

Index/key variables: i
Iterator variables: i
Python program:
def minimumSquare ( a , b ) :
    result = 0
    rem = 0
    if ( a < b ) :
        a , b = b , a
    while ( b > 0 ) :
        result += int ( a / b )
        rem = int ( a % b )
        a = b
        b = rem
    return result
n = 13
m = 29
print ( minimumSquare ( n , m ) )

Accumulator variables: result
Python program:
s = " "
dp = [ [ [ [ 0 for _ in range ( 2 ) ] for _ in range ( 2 ) ] for _ in range ( 10 ) ] for _ in range ( 1024 ) ]
def calcCnt ( mask , sz , smaller , started ) :
    if ( sz == len ( s ) ) :
        return ( mask == 0 and started )
    if ( dp [ mask ] [ sz ] [ smaller ] [ started ] != - 1 ) :
        return dp [ mask ] [ sz ] [ smaller ] [ started ]
    mx = 9
    if ( not smaller ) :
        mx = ord ( s [ sz ] ) - ord ( '0' )
    ans = 0
    if ( started ) :
        for i in range ( 0 , mx + 1 ) :
            ans += calcCnt ( mask ^ ( 1 << i ) , sz + 1 , smaller or ( i < ord ( s [ sz ] ) - ord ( '0' ) ) , 1 )
    else :
        ans = calcCnt ( mask , sz + 1 , 1 , 0 )
        for i in range ( 1 , mx + 1 ) :
            ans += calcCnt ( mask ^ ( 1 << i ) , sz + 1 , smaller or ( i < ord ( s [ sz ] ) - ord ( '0' ) ) , 1 )
    dp [ mask ] [ sz ] [ smaller ] [ started ] = ans
    return dp [ mask ] [ sz ] [ smaller ] [ started ]
def countInt ( x ) :
    global dp
    dp = [ [ [ [ - 1 for _ in range ( 2 ) ] for _ in range ( 2 ) ] for _ in range ( 10 ) ] for _ in range ( 1024 ) ]
    global s
    s = str ( x )
    return calcCnt ( 0 , 0 , 0 , 0 )
def countIntInRange ( L , R ) :
    return countInt ( R ) - countInt ( L - 1 )
if __name__ == " _ _ main _ _ " :
    L = 32
    R = 1010
    print ( countIntInRange ( L , R ) )

Index/key variables: mask, smaller, started, sz
Accumulator variables: ans
Iterator variables: i
Python program:
MAX = 5000 ;
def minDifference ( arr , n , q , m ) :
    for i in range ( m ) :
        L = q [ i ] [ 0 ] ; R = q [ i ] [ 1 ] ;
        ans = MAX ;
        for i in range ( L , R ) :
            ans = min ( ans , arr [ i ] ) ;
        print ( ans ) ;
def minimumDifference ( arr , q , N , m ) :
    diff = [ 0 ] * N ;
    for i in range ( N - 1 ) :
        diff [ i ] = abs ( arr [ i ] - arr [ i + 1 ] ) ;
    minDifference ( diff , N - 1 , q , m ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 6 , 1 , 8 , 3 , 4 ] ;
    N = len ( arr ) ;
    Q = [ [ 0 , 3 ] , [ 1 , 5 ] , [ 4 , 5 ] ] ;
    M = len ( Q ) ;
    minimumDifference ( arr , Q , N , M ) ;

Index/key variables: i
Iterator variables: i
Python program:
def countMinSteps ( A , B , N ) :
    dp = [ 0 ] * ( N + 1 )
    for i in range ( 1 , N + 1 ) :
        if ( A [ i - 1 ] == B [ i - 1 ] ) :
            dp [ i ] = dp [ i - 1 ]
        else :
            dp [ i ] = dp [ i - 1 ] + 1
        if ( i >= 2 and A [ i - 2 ] == B [ i - 1 ] and A [ i - 1 ] == B [ i - 2 ] ) :
            dp [ i ] = min ( dp [ i ] , dp [ i - 2 ] + 1 )
    return dp [ N ]
A = "0101"
B = "0011"
N = len ( A )
print ( countMinSteps ( A , B , N ) )

Index/key variables: N, i
Iterator variables: i
Python program:
dp = [ [ [ - 1 for i in range ( 2 ) ] for i in range ( 1 << 10 ) ] for i in range ( 50 ) ]
def countOfNumbers ( digit , mask , repeated , n ) :
    global dp
    if ( digit == n + 1 ) :
        if ( repeated == True ) :
            return 1
        return 0
    if ( repeated == True ) :
        return pow ( 10 , n - digit + 1 )
    val = dp [ digit ] [ mask ] [ repeated ]
    if ( val != - 1 ) :
        return val
    val = 4
    if ( digit == 1 ) :
        for i in range ( ( 0 if ( n == 1 ) else 1 ) , 10 ) :
            if ( mask & ( 1 << i ) ) :
                val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n )
        else :
            val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n )
    else :
        for i in range ( 10 ) :
            if ( mask & ( 1 << i ) ) :
                val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n )
        else :
            val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n )
    dp [ digit ] [ mask ] [ repeated ] = val
    return dp [ digit ] [ mask ] [ repeated ]
def countNDigitNumber ( N ) :
    print ( countOfNumbers ( 1 , 0 , 0 , N ) )
if __name__ == ' _ _ main _ _ ' :
    N = 2
    countNDigitNumber ( N )

Index/key variables: digit, mask, repeated
Accumulator variables: val
Iterator variables: i
Python program:
def getMaximum ( S , T ) :
    if ( S . count ( "0" ) > 0 ) :
        return T ;
    if ( T . count ( "0" ) > 0 ) :
        return S ;
    return S if len ( S ) > len ( T ) else T ;
def recursion ( arr , idx , N , K , dp ) :
    if ( K == 0 ) :
        return " " ;
    if ( K < 0 or idx == N ) :
        return "0" ;
    if ( dp [ idx ] [ K ] != " - 1" ) :
        return dp [ idx ] [ K ] ;
    include = str ( idx + 1 ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ;
    exclude = recursion ( arr , idx + 1 , N , K , dp ) ;
    dp [ idx ] [ K ] = getMaximum ( include , exclude )
    return ( dp [ idx ] [ K ] )
def largestNumber ( arr , N , K ) :
    dp = [ [ " - 1" for i in range ( K + 1 ) ] for i in range ( N + 1 ) ]
    ans = recursion ( arr , 0 , N , K , dp ) ;
    return "0" if ans == " " else ans ;
arr = [ 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 ] ;
K = 14 ;
N = len ( arr ) ;
print ( largestNumber ( arr , N , K ) ) ;

Index/key variables: K, idx
Python program:
dp = [ [ - 1 for i in range ( 10 ) ] for j in range ( 100 ) ]
val = 0
def countOfNumbers ( digit , prev , n ) :
    global val
    global dp
    if ( digit == n + 1 ) :
        return 1
    val = dp [ digit ] [ prev ]
    if ( val != - 1 ) :
        return val
    val = 0
    if ( digit == 1 ) :
        i = 0 if n == 1 else 1
        while ( i <= 9 ) :
            val += countOfNumbers ( digit + 1 , i , n )
            i += 1
    else :
        for i in range ( 10 ) :
            if ( ( i & prev ) == 0 ) :
                val += countOfNumbers ( digit + 1 , i , n )
    return val
if __name__ == ' _ _ main _ _ ' :
    N = 3
    print ( countOfNumbers ( 1 , 0 , N ) )

Index/key variables: digit, prev
Accumulator variables: val
Iterator variables: i
Python program:
dp = [ [ 0 for i in range ( 1005 ) ] for j in range ( 1005 ) ]
def KvisibleFromLeft ( N , K ) :
    if ( dp [ N ] [ K ] != - 1 ) :
        return dp [ N ] [ K ]
    if ( N == K ) :
        dp [ N ] [ K ] = 1
        return dp [ N ] [ K ]
    if ( K == 1 ) :
        ans = 1
        for i in range ( 1 , N ) :
            ans *= i
        dp [ N ] [ K ] = ans
        return dp [ N ] [ K ]
    dp [ N ] [ K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K )
    return dp [ N ] [ K ]
N , K = 5 , 2
for i in range ( 1005 ) :
    for j in range ( 1005 ) :
        dp [ i ] [ j ] = - 1
print ( KvisibleFromLeft ( N , K ) )

Index/key variables: K, N, i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def power ( x , y , p ) :
    res = 1
    x = x % p
    if ( x == 0 ) :
        return 0
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % p
        y = y >> 1
        x = ( x * x ) % p
    return res
def kvowelwords ( N , K ) :
    i , j = 0 , 0
    MOD = 1000000007
    dp = [ [ 0 for i in range ( K + 1 ) ] for i in range ( N + 1 ) ]
    sum = 1
    for i in range ( 1 , N + 1 ) :
        dp [ i ] [ 0 ] = sum * 21
        dp [ i ] [ 0 ] %= MOD
        sum = dp [ i ] [ 0 ]
        for j in range ( 1 , K + 1 ) :
            if ( j > i ) :
                dp [ i ] [ j ] = 0
            elif ( j == i ) :
                dp [ i ] [ j ] = power ( 5 , i , MOD )
            else :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5
            dp [ i ] [ j ] %= MOD
            sum += dp [ i ] [ j ]
            sum %= MOD
    return sum
if __name__ == ' _ _ main _ _ ' :
    N = 3
    K = 3
    print ( kvowelwords ( N , K ) )

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def maxBoundary ( N , V ) :
    dp = [ [ 0 for i in range ( 2 ) ] for j in range ( N ) ]
    dp [ 0 ] [ 0 ] = V [ 0 ] [ 0 ]
    dp [ 0 ] [ 1 ] = V [ 0 ] [ 1 ]
    for i in range ( 1 , N , 1 ) :
        dp [ i ] [ 0 ] = V [ i ] [ 0 ]
        height1 = abs ( V [ i - 1 ] [ 1 ] - V [ i ] [ 1 ] )
        height2 = abs ( V [ i - 1 ] [ 0 ] - V [ i ] [ 1 ] )
        dp [ i ] [ 0 ] += max ( height1 + dp [ i - 1 ] [ 0 ] , height2 + dp [ i - 1 ] [ 1 ] )
        dp [ i ] [ 1 ] = V [ i ] [ 1 ]
        vertical1 = abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 1 ] ) ;
        vertical2 = abs ( V [ i ] [ 0 ] - V [ i - 1 ] [ 1 ] ) ;
        dp [ i ] [ 1 ] += max ( vertical1 + dp [ i - 1 ] [ 0 ] , vertical2 + dp [ i - 1 ] [ 1 ] )
    print ( max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) - 1 )
if __name__ == ' _ _ main _ _ ' :
    N = 5
    V = [ [ 2 , 5 ] , [ 3 , 8 ] , [ 1 , 10 ] , [ 7 , 14 ] , [ 2 , 5 ] ]
    maxBoundary ( N , V )

Index/key variables: i
Iterator variables: i
Python program:
def NumberOfways ( N , K ) :
    dp = [ 0 ] * ( N + 1 )
    dp [ 0 ] = 1
    for row in range ( 1 , K + 1 ) :
        for col in range ( 1 , N + 1 ) :
            if ( col >= row ) :
                dp [ col ] = dp [ col ] + dp [ col - row ]
    return ( dp [ N ] )
N = 8
K = 2
print ( NumberOfways ( N , K ) )

Index/key variables: N, col
Iterator variables: col, row
Python program:
def findAPSequence ( arr , N , Q , M ) :
    dp = [ 0 ] * ( N + 5 )
    i = 0
    while i + 1 < N :
        j = i + 1
        while ( j + 1 < N and arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) :
            j += 1
        for k in range ( i , j ) :
            dp [ k ] = j - k
        i = j
    for i in range ( M ) :
        if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) :
            print ( " Yes " )
        else :
            print ( " No " )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 ]
    Q = [ [ 0 , 3 ] , [ 3 , 4 ] , [ 2 , 4 ] ]
    N = len ( arr )
    M = len ( Q )
    findAPSequence ( arr , N , Q , M )

Index/key variables: i, j, k
Iterator variables: i, k
Python program:
import sys
dp = [ [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ] for i in range ( 100 ) ]
def minSumDifference ( arr , n , k1 , k2 , sum1 , sum2 ) :
    global dp
    if ( n < 0 ) :
        if ( k1 == 0 and k2 == 0 ) :
            return abs ( sum1 - sum2 )
        else :
            return sys . maxsize + 1
    if ( dp [ n ] [ sum1 ] [ sum2 ] != - 1 ) :
        return dp [ n ] [ sum1 ] [ sum2 ]
    op1 = sys . maxsize + 1
    op2 = sys . maxsize + 1
    op3 = sys . maxsize + 1
    if ( k1 > 0 ) :
        op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 )
    if ( k2 > 0 ) :
        op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] )
    op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 )
    dp [ n ] [ sum1 ] [ sum2 ] = min ( op1 , min ( op2 , op3 ) )
    return dp [ n ] [ sum1 ] [ sum2 ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 12 , 3 , 5 , 6 , 7 , 17 ]
    K = 2
    N = len ( arr )
    print ( minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) )

Index/key variables: n, sum1, sum2
Python program:
def find ( A , B , C , N ) :
    arr = [ 0 ] * N
    arr [ 0 ] = A
    arr [ 1 ] = B
    arr [ 2 ] = C
    for i in range ( 3 , N ) :
        arr [ i ] = ( arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] )
    return arr [ N - 1 ]
A = 1
B = 3
C = 2
N = 4
print ( find ( A , B , C , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def daysToCure ( arr , N , P ) :
    risk = 0
    normal_risk = 0
    for i in range ( N ) :
        if ( arr [ i ] >= 60 or arr [ i ] <= 10 ) :
            risk += 1
        else :
            normal_risk += 1
    days = ( risk // P ) + ( risk % P > 0 ) + ( normal_risk // P ) + ( normal_risk % P > 0 )
    print ( days )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 9 , 80 , 27 , 72 , 79 ]
    N = len ( arr )
    P = 2
    daysToCure ( arr , N , P )

Index/key variables: i
Accumulator variables: normal_risk, risk
Iterator variables: i
Python program:
def countVowelPermutation ( n ) :
    MOD = 1e9 + 7
    dp = [ [ 0 for i in range ( 5 ) ] for j in range ( n + 1 ) ]
    for i in range ( 5 ) :
        dp [ 1 ] [ i ] = 1
    relation = [ [ 1 ] , [ 0 , 2 ] , [ 0 , 1 , 3 , 4 ] , [ 2 , 4 ] , [ 0 ] ]
    for i in range ( 1 , n , 1 ) :
        for u in range ( 5 ) :
            dp [ i + 1 ] [ u ] = 0
            for v in relation [ u ] :
                dp [ i + 1 ] [ u ] += dp [ i ] [ v ] % MOD
    ans = 0
    for i in range ( 5 ) :
        ans = ( ans + dp [ n ] [ i ] ) % MOD
    return int ( ans )
if __name__ == ' _ _ main _ _ ' :
    N = 2
    print ( countVowelPermutation ( N ) )

Index/key variables: i, n, u, v
Iterator variables: i, u, v
Python program:
import math ;
def max ( a , b ) :
    if ( a > b ) :
        return a ;
    else :
        return b ;
def maximumsum ( arr , K , N , M ) :
    sum = 0 ;
    maxSum = 0 ;
    dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( K + 1 ) ]
    for i in range ( 0 , N + 1 ) :
        dp [ 0 ] [ i ] = 0 ;
    for i in range ( 0 , K + 1 ) :
        dp [ i ] [ 0 ] = 0 ;
    for i in range ( 1 , K + 1 ) :
        for j in range ( 0 , N ) :
            sum = 0 ;
            maxSum = dp [ i ] [ j ] ;
            for k in range ( 1 , i + 1 ) :
                if ( k > M ) :
                    break ;
                sum += arr [ j ] [ k - 1 ] ;
                maxSum = max ( maxSum , sum + dp [ i - k ] [ j ] ) ;
            dp [ i ] [ j + 1 ] = maxSum ;
    return dp [ K ] [ N ] ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 10 , 10 , 100 , 30 ] , [ 80 , 50 , 10 , 50 ] ] ;
    N = 2 ;
    M = 4 ;
    K = 5 ;
    print ( maximumsum ( arr , K , N , M ) ) ;

Index/key variables: K, N, i, j
Accumulator variables: sum
Iterator variables: i, j, k
Python program:
def waysToArrangeKLengthStrings ( N , K ) :
    column_sum = [ 0 for i in range ( N + 1 ) ]
    i = 0
    j = 0
    dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( K + 1 ) ]
    for i in range ( N + 1 ) :
        dp [ 0 ] [ i ] = 1
        column_sum [ i ] = 1
    for i in range ( 1 , K + 1 ) :
        for j in range ( 1 , N + 1 ) :
            dp [ i ] [ j ] += column_sum [ j - 1 ]
            column_sum [ j ] += dp [ i ] [ j ]
    print ( dp [ K ] [ N ] )
if __name__ == ' _ _ main _ _ ' :
    N = 5
    K = 2
    waysToArrangeKLengthStrings ( N , K )

Index/key variables: K, N, i, j
Iterator variables: i, j
Python program:
def findNumberOfStrings ( n ) :
    DP = [ [ 0 for i in range ( 6 ) ] for i in range ( n + 1 ) ]
    DP [ 1 ] [ 1 ] = 1
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , 6 ) :
            if ( i == 1 ) :
                DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1
            else :
                DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ]
    return DP [ n ] [ 5 ]
if __name__ == ' _ _ main _ _ ' :
    N = 2
    print ( findNumberOfStrings ( N ) )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
V = 3
M = 2
def dfs ( v , p ) :
    isLeaf = 1
    ans [ v ] = - 10 ** 9
    for u in adj [ v ] :
        if ( u == p ) :
            continue
        isLeaf = 0
        dfs ( u , v )
        ans [ v ] = max ( ans [ u ] + vals [ v ] , max ( ans [ u ] , vals [ u ] ) )
    if ( isLeaf ) :
        ans [ v ] = vals [ v ]
def printAnswer ( n , edges , vals ) :
    for i in range ( n - 1 ) :
        u = edges [ i ] [ 0 ] - 1
        v = edges [ i ] [ 1 ] - 1
        adj [ u ] . append ( v )
        adj [ v ] . append ( u )
    dfs ( 0 , - 1 )
    for x in ans :
        print ( x , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 3 , 4 ] ]
    adj = [ [ ] for i in range ( N ) ]
    ans = [ 0 for i in range ( N ) ]
    vals = [ 1 , - 1 , 0 , 1 ]
    printAnswer ( N , edges , vals )

Index/key variables: i, u, v
Iterator variables: i, u, x
Python program:
def noOfChars ( s , queries ) :
    n = len ( s )
    dp = [ [ 0 for i in range ( 26 ) ] for i in range ( n + 1 ) ]
    for i in range ( n ) :
        dp [ i + 1 ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1
        for j in range ( 26 ) :
            dp [ i + 1 ] [ j ] += dp [ i ] [ j ]
    q = len ( queries )
    for i in range ( q ) :
        l = queries [ i ] [ 0 ]
        r = queries [ i ] [ 1 ]
        c = ord ( queries [ i ] [ 2 ] ) - ord ( ' a ' )
        print ( dp [ r ] - dp [ l - 1 ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    S = " aabv "
    queries = [ [ 1 , 2 , ' a ' ] , [ 2 , 3 , ' b ' ] ]
    noOfChars ( S , queries )

Index/key variables: i, j, r
Iterator variables: i, j
Python program:
def LowerBound ( a , k , x ) :
    l = - 1
    r = k
    while ( l + 1 < r ) :
        m = ( l + r ) >> 1
        if ( a [ m ] >= x ) :
            r = m
        else :
            l = m
    return r
def findLCS ( nums , N ) :
    k = 0
    for i in range ( N ) :
        pos = LowerBound ( nums , k , nums [ i ] )
        nums [ pos ] = nums [ i ]
        if ( k == pos ) :
            k = pos + 1
    return k
def minimumCost ( A , B , M , N , C ) :
    nums = [ 0 ] * 100000
    index = [ - 1 ] * 100000
    for i in range ( N ) :
        index [ B [ i ] ] = i
    k = 0
    for i in range ( M ) :
        if ( index [ A [ i ] ] != - 1 ) :
            k += 1
            nums [ k ] = index [ A [ i ] ]
    lcs_length = findLCS ( nums , k )
    elements_to_be_added = N - lcs_length
    min_cost = elements_to_be_added * C
    print ( min_cost )
A = [ 1 , 6 , 3 , 5 , 10 ]
B = [ 3 , 1 , 5 ]
C = 2
M = len ( A )
N = len ( B )
minimumCost ( A , B , M , N , C )

Index/key variables: i, k, m, pos
Iterator variables: i
Python program:
def pathSum ( mat , N , M ) :
    dp = [ [ 0 for x in range ( M ) ] for y in range ( N ) ]
    dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ]
    for i in range ( 1 , N ) :
        dp [ i ] [ 0 ] = ( mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] )
    for j in range ( 1 , M ) :
        dp [ 0 ] [ j ] = ( mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] )
    for i in range ( 1 , N ) :
        for j in range ( 1 , M ) :
            dp [ i ] [ j ] = ( mat [ i ] [ j ] + max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) )
    for i in range ( N ) :
        for j in range ( M ) :
            print ( dp [ i ] [ j ] , end = " ▁ " )
        print ( )
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 3 , 2 , 1 ] , [ 6 , 5 , 4 ] , [ 7 , 8 , 9 ] ]
    N = 3
    M = 3
    pathSum ( mat , N , M )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def longestPalSubstr ( str ) :
    n = len ( str ) ;
    table = [ [ False for i in range ( n ) ] for j in range ( n ) ] ;
    maxLength = 1 ;
    for i in range ( n ) :
        table [ i ] [ i ] = True ;
    start = 0 ;
    for i in range ( n - 1 ) :
        if ( str [ i ] == str [ i + 1 ] ) :
            table [ i ] [ i + 1 ] = True ;
            start = i ;
            maxLength = 2 ;
    for k in range ( 3 , n + 1 ) :
        for i in range ( n - k + 1 ) :
            j = i + k - 1 ;
            if ( table [ i + 1 ] [ j - 1 ] and str [ i ] == str [ j ] ) :
                table [ i ] [ j ] = True ;
                if ( k > maxLength ) :
                    start = i ;
                    maxLength = k ;
    return maxLength ;
if __name__ == ' _ _ main _ _ ' :
    str = " forgeeksskeegfor " ;
    print ( longestPalSubstr ( str ) ) ;

Index/key variables: i, j
Iterator variables: i, k
Python program:
import sys
def maxSumFlip ( a , n ) :
    total_sum = 0
    for i in range ( n ) :
        total_sum += a [ i ]
    max_sum = - sys . maxsize - 1
    for i in range ( n ) :
        sum = 0
        flip_sum = 0
        for j in range ( i , n ) :
            sum += a [ j ]
            max_sum = max ( max_sum , total_sum - 2 * sum )
    return max ( max_sum , total_sum )
arr = [ - 2 , 3 , - 1 , - 4 , - 2 ]
N = len ( arr )
print ( maxSumFlip ( arr , N ) )

Index/key variables: i, j
Accumulator variables: sum, total_sum
Iterator variables: i, j
Python program:
INF = 10000007 ;
max_size = 100007 ;
dp = [ 0 for i in range ( max_size ) ] ;
def min_op ( cur , M ) :
    if ( cur > M ) :
        return INF ;
    if ( cur == M ) :
        return 0 ;
    if ( dp [ cur ] != - 1 ) :
        return dp [ cur ] ;
    op = INF ;
    i = 2
    while ( i * i <= cur ) :
        if ( cur % i == 0 ) :
            if ( i % 2 == 0 ) :
                op = min ( op , 1 + min_op ( cur + i , M ) ) ;
            if ( ( cur // i ) != i and ( cur // i ) % 2 == 0 ) :
                op = min ( op , 1 + min_op ( cur + ( cur // i ) , M ) )
        i += 1
    dp [ cur ] = op ;
    return op
def min_operations ( N , M ) :
    for i in range ( N , M + 1 ) :
        dp [ i ] = - 1 ;
    return min_op ( N , M ) ;
if __name__ == " _ _ main _ _ " :
    N = 6
    M = 24
    op = min_operations ( N , M ) ;
    if ( op >= INF ) :
        print ( - 1 )
    else :
        print ( op )

Index/key variables: cur, i
Iterator variables: i
Python program:
def maxValue ( a , n , pos , moves , left , dp ) :
    if ( moves == 0 or ( pos > n - 1 or pos < 0 ) ) :
        return 0
    if ( dp [ pos ] [ left ] != - 1 ) :
        return dp [ pos ] [ left ]
    value = 0
    if ( left > 0 and pos >= 1 ) :
        value = max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) )
    if ( pos <= n - 1 ) :
        value = max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) )
    dp [ pos ] [ left ] = value
    return dp [ pos ] [ left ]
n = 5
a = [ 1 , 5 , 4 , 3 , 2 ]
k = 1
m = 4
dp = [ [ - 1 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ]
print ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) )

Index/key variables: left, pos
Python program:
def maximumSum ( a , count , index , n , dp ) :
    if ( index == n ) :
        return 0
    if ( dp [ index ] [ count ] != - 1 ) :
        return dp [ index ] [ count ]
    take_element = ( a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) )
    dont_take = maximumSum ( a , count , index + 1 , n , dp )
    dp [ index ] [ count ] = max ( take_element , dont_take )
    return dp [ index ] [ count ]
n = 5
a = [ - 1 , - 9 , 0 , 5 , - 7 ]
dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ]
print ( maximumSum ( a , 1 , 0 , n , dp ) )

Index/key variables: count, index