dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
N = 1005 ;
K = 20 ;
n = 0
k = 0
dp = [ [ [ - 1 for k in range ( K + 1 ) ] for j in range ( N + 1 ) ] for i in range ( N + 1 ) ]
def minCost ( i , prev , cnt , ele , cost ) :
    if ( cnt == k + 1 ) :
        return 0 ;
    if ( i == n + 1 ) :
        return 100000 ;
    ans = dp [ i ] [ prev ] [ cnt ] ;
    if ( ans != - 1 ) :
        return ans ;
    ans = minCost ( i + 1 , prev , cnt , ele , cost ) ;
    if ( ele [ i ] > ele [ prev ] ) :
        ans = min ( ans , cost [ i ] + minCost ( i + 1 , i , cnt + 1 , ele , cost ) ) ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    n = 4 ;
    k = 2 ;
    ele = [ 0 , 2 , 6 , 4 , 8 ]
    cost = [ 0 , 40 , 20 , 30 , 10 ]
    ans = minCost ( 1 , 0 , 1 , ele , cost ) ;
    if ( ans == 100000 ) :
        ans = - 1 ;
    print ( ans )

Index/key variables: cnt, i, prev
Python program:
N = 5
def func ( idx , cur , a , dp , n , x ) :
    if ( idx == n ) :
        return 0
    if ( dp [ idx ] [ cur ] != - 1 ) :
        return dp [ idx ] [ cur ]
    ans = 0
    if ( cur == 0 ) :
        ans = max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) )
        ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) )
    elif ( cur == 1 ) :
        ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) )
        ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) )
    else :
        ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) )
    dp [ idx ] [ cur ] = ans
    return dp [ idx ] [ cur ]
def getMaximumSum ( a , n , x ) :
    dp = [ [ - 1 for i in range ( 3 ) ] for j in range ( n ) ]
    maxi = 0
    for i in range ( 0 , n ) :
        maxi = max ( maxi , func ( i , 0 , a , dp , n , x ) )
    return maxi
a = [ - 3 , 8 , - 2 , 1 , - 6 ]
n = len ( a )
x = - 1
print ( getMaximumSum ( a , n , x ) )

Index/key variables: cur, idx
Iterator variables: i
Python program:
N = 100
def pre_process ( dp , s ) :
    n = len ( s )
    for i in range ( n ) :
        for j in range ( n ) :
            dp [ i ] [ j ] = False
    for j in range ( 1 , n + 1 ) :
        for i in range ( n - j + 1 ) :
            if ( j <= 2 ) :
                if ( s [ i ] == s [ i + j - 1 ] ) :
                    dp [ i ] [ i + j - 1 ] = True
            elif ( s [ i ] == s [ i + j - 1 ] ) :
                dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ]
def countPairs ( s ) :
    dp = [ [ False for i in range ( N ) ] for j in range ( N ) ]
    pre_process ( dp , s )
    n = len ( s )
    left = [ 0 for i in range ( n ) ]
    right = [ 0 for i in range ( n ) ]
    left [ 0 ] = 1
    for i in range ( 1 , n ) :
        for j in range ( i + 1 ) :
            if ( dp [ j ] [ i ] == 1 ) :
                left [ i ] += 1
    right [ n - 1 ] = 1
    for i in range ( n - 2 , - 1 , - 1 ) :
        right [ i ] = right [ i + 1 ]
        for j in range ( n - 1 , i - 1 , - 1 ) :
            if ( dp [ i ] [ j ] == 1 ) :
                right [ i ] += 1
    ans = 0
    for i in range ( n - 1 ) :
        ans += left [ i ] * right [ i + 1 ]
    return ans
s = " abacaba "
print ( countPairs ( s ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
N = 100
def pre_process ( dp , s ) :
    n = len ( s )
    for i in range ( n ) :
        for j in range ( n ) :
            dp [ i ] [ j ] = False
    for j in range ( 1 , n + 1 ) :
        for i in range ( n - j + 1 ) :
            if ( j <= 2 ) :
                if ( s [ i ] == s [ i + j - 1 ] ) :
                    dp [ i ] [ i + j - 1 ] = True
            elif ( s [ i ] == s [ i + j - 1 ] ) :
                dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ]
def answerQuery ( l , r , dp ) :
    if ( dp [ l ] [ r ] ) :
        print ( " Yes " )
    else :
        print ( " No " )
s = " abaaab "
dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ]
pre_process ( dp , s )
queries = [ [ 0 , 1 ] , [ 1 , 5 ] ]
q = len ( queries )
for i in range ( q ) :
    answerQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , dp )

Index/key variables: i, j, l, r
Iterator variables: i, j
Python program:
N = 100005
def LIS ( a , n ) :
    dp = [ 0 for i in range ( N ) ]
    d = [ 0 for i in range ( N ) ]
    ans = 0
    for i in range ( n ) :
        dp [ a [ i ] ] = 1
        for j in range ( 2 , a [ i ] ) :
            if j * j > a [ i ] :
                break
            if ( a [ i ] % j == 0 ) :
                dp [ a [ i ] ] = max ( dp [ a [ i ] ] , dp [ d [ j ] ] + 1 )
                dp [ a [ i ] ] = max ( dp [ a [ i ] ] , dp [ d [ a [ i ] // j ] ] + 1 )
                d [ j ] = a [ i ]
                d [ a [ i ] // j ] = a [ i ]
        ans = max ( ans , dp [ a [ i ] ] )
        d [ a [ i ] ] = a [ i ]
    return ans
a = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len ( a )
print ( LIS ( a , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def check ( s ) :
    n = len ( s ) ;
    for i in range ( 2 , n ) :
        if ( s [ i ] == '1' and s [ i - 1 ] == '1' and s [ i - 2 ] == '1' ) :
            return 1 ;
def countStr ( i , s ) :
    if ( i < 0 ) :
        if ( check ( s ) ) :
            return 1 ;
        return 0 ;
    s [ i ] = '0' ;
    ans = countStr ( i - 1 , s ) ;
    s [ i ] = '1' ;
    ans += countStr ( i - 1 , s ) ;
    s [ i ] = '0' ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    N = 4 ;
    s = list ( '0' * N ) ;
    print ( countStr ( N - 1 , s ) ) ;

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

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

Index/key variables: i, n
Iterator variables: i
Python program:
def dfs ( node , parent , dp1 , dp2 , adj , tree ) :
    sum1 = 0
    sum2 = 0
    for i in adj [ node ] :
        if ( i == parent ) :
            continue ;
        dfs ( i , node , dp1 , dp2 , adj , tree ) ;
        sum1 += dp2 [ i ] ;
        sum2 += max ( dp1 [ i ] , dp2 [ i ] ) ;
    dp1 [ node ] = tree [ node ] + sum1 ;
    dp2 [ node ] = sum2 ;
if __name__ == " _ _ main _ _ " :
    n = 5 ;
    adj = [ [ ] for i in range ( n + 1 ) ]
    adj [ 1 ] . append ( 2 ) ;
    adj [ 2 ] . append ( 1 ) ;
    adj [ 1 ] . append ( 3 ) ;
    adj [ 3 ] . append ( 1 ) ;
    adj [ 2 ] . append ( 4 ) ;
    adj [ 4 ] . append ( 2 ) ;
    adj [ 2 ] . append ( 5 ) ;
    adj [ 5 ] . append ( 2 ) ;
    tree = [ 0 for i in range ( n + 1 ) ] ;
    tree [ 1 ] = 10 ;
    tree [ 2 ] = 5 ;
    tree [ 3 ] = 11 ;
    tree [ 4 ] = 6 ;
    tree [ 5 ] = 8 ;
    dp1 = [ 0 for i in range ( n + 1 ) ] ;
    dp2 = [ 0 for i in range ( n + 1 ) ] ;
    dfs ( 1 , 1 , dp1 , dp2 , adj , tree ) ;
    print ( " Maximum ▁ sum : " , max ( dp1 [ 1 ] , dp2 [ 1 ] ) )

Index/key variables: i, node
Accumulator variables: sum1, sum2
Iterator variables: i
Python program:
n = 3
MAX = 60
dp = [ [ [ 0 for i in range ( 30 ) ] for i in range ( 30 ) ] for i in range ( MAX + 1 ) ]
v = [ [ [ 0 for i in range ( 30 ) ] for i in range ( 30 ) ] for i in range ( MAX + 1 ) ]
def findCount ( mat , i , j , m ) :
    if ( i == 0 and j == 0 ) :
        if ( m == mat [ 0 ] [ 0 ] ) :
            return 1
        else :
            return 0
    if ( m < 0 ) :
        return 0
    if ( i < 0 or j < 0 ) :
        return 0
    if ( v [ i ] [ j ] [ m ] > 0 ) :
        return dp [ i ] [ j ] [ m ]
    v [ i ] [ j ] [ m ] = True
    dp [ i ] [ j ] [ m ] = ( findCount ( mat , i - 1 , j , m - mat [ i ] [ j ] ) + findCount ( mat , i , j - 1 , m - mat [ i ] [ j ] ) )
    return dp [ i ] [ j ] [ m ]
mat = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ]
m = 5
print ( findCount ( mat , n - 1 , n - 1 , m ) )

Index/key variables: i, j, m
Python program:
mod = 1000000007
RUNMAX = 300
BALLMAX = 50
WICKETMAX = 10
def CountWays ( r , b , l , R , B , W , dp ) :
    if ( l > W ) :
        return 0 ;
    if ( r > R ) :
        return 0 ;
    if ( b == B and r == R ) :
        return 1 ;
    if ( b == B ) :
        return 0 ;
    if ( dp [ r ] [ b ] [ l ] != - 1 ) :
        return dp [ r ] [ b ] [ l ]
    ans = 0 ;
    ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ;
    ans = ans % mod ;
    ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ;
    ans = ans % mod ;
    dp [ r ] [ b ] [ l ] = ans
    return ans ;
if __name__ == " _ _ main _ _ " :
    R = 40
    B = 10
    W = 40
    dp = [ [ [ - 1 for k in range ( WICKETMAX ) ] for j in range ( BALLMAX ) ] for i in range ( RUNMAX ) ]
    print ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) )

Index/key variables: b, l, r
Python program:
def findMinimumDeletion ( l , r , dp , s ) :
    if l > r :
        return 0
    if l == r :
        return 1
    if dp [ l ] [ r ] != - 1 :
        return dp [ l ] [ r ]
    res = 1 + findMinimumDeletion ( l + 1 , r , dp , s )
    for i in range ( l + 1 , r + 1 ) :
        if s [ l ] == s [ i ] :
            res = min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) )
    dp [ l ] [ r ] = res
    return res
if __name__ == " _ _ main _ _ " :
    s = " abcddcba "
    n = len ( s )
    N = 10
    dp = [ [ - 1 for i in range ( N ) ] for j in range ( N ) ]
    print ( findMinimumDeletion ( 0 , n - 1 , dp , s ) )

Index/key variables: i, l, r
Iterator variables: i
Python program:
import math
mod = 1000000007 ;
MAX = 100005 ;
def minimumProductSubsequence ( arr , n , k ) :
    s = [ ]
    dp = [ 0 for i in range ( MAX ) ] ;
    p = [ 0.0 for i in range ( MAX ) ] ;
    dp [ 0 ] = arr [ 0 ] ;
    p [ 0 ] = math . log ( arr [ 0 ] ) ;
    s . append ( [ p [ 0 ] , dp [ 0 ] ] ) ;
    s . sort ( )
    for i in range ( 1 , k ) :
        l = s [ 0 ] [ 0 ]
        min = s [ 0 ] [ 1 ]
        p [ i ] = math . log ( arr [ i ] ) + l ;
        dp [ i ] = ( arr [ i ] * min ) % mod ;
        s . append ( [ p [ i ] , dp [ i ] ] ) ;
        s . sort ( )
    for i in range ( k , n ) :
        l = s [ 0 ] [ 0 ]
        min = s [ 0 ] [ 1 ]
        p [ i ] = math . log ( arr [ i ] ) + l ;
        dp [ i ] = ( arr [ i ] * min ) % mod ;
        if [ p [ i - k ] , dp [ i - k ] ] in s :
            s . pop ( s . index ( [ p [ i - k ] , dp [ i - k ] ] ) )
        s . append ( [ p [ i ] , dp [ i ] ] ) ;
        s . sort ( )
    return dp [ n - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 ]
    n = len ( arr )
    k = 2 ;
    print ( minimumProductSubsequence ( arr , n , k ) )

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
C = [ [ 0 for i in range ( 1000 ) ] for i in range ( 1000 ) ]
def binomialCoeff ( n ) :
    i , j = 0 , 0
    for i in range ( n + 1 ) :
        for j in range ( i + 1 ) :
            if ( j == 0 or j == i ) :
                C [ i ] [ j ] = 1
            else :
                C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ]
def numberOfWays ( x , y , z ) :
    binomialCoeff ( max ( x , max ( y , z ) ) )
    sum = 0
    for i in range ( 1 , z + 1 ) :
        sum = ( sum + C [ z ] [ i ] )
    sum1 = 0
    for i in range ( 1 , y + 1 ) :
        for j in range ( i + 1 , x + 1 ) :
            sum1 = ( sum1 + ( C [ y ] [ i ] * C [ x ] [ j ] ) )
    sum1 = ( sum * sum1 )
    return sum1
x = 3
y = 2
z = 1
print ( numberOfWays ( x , y , z ) )

Index/key variables: i, j, x, y, z
Iterator variables: i, j
Python program:
def findSubsequence ( arr , n ) :
    Len = 1
    tmp = 0
    i , j , d = 0 , 0 , 0
    dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n ) ]
    cnt = [ 0 for i in range ( 10 ) ]
    locMax = 0
    tmp = arr [ 0 ]
    while ( tmp > 0 ) :
        dp [ 0 ] [ tmp % 10 ] = 1
        tmp //= 10
    for i in range ( 1 , n ) :
        tmp = arr [ i ]
        locMax = 1
        cnt = [ 0 for i in range ( 10 ) ]
        while ( tmp > 0 ) :
            cnt [ tmp % 10 ] = 1
            tmp //= 10
        for d in range ( 10 ) :
            if ( cnt [ d ] ) :
                dp [ i ] [ d ] = 1
                for j in range ( i ) :
                    dp [ i ] [ d ] = max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 )
                    locMax = max ( dp [ i ] [ d ] , locMax )
        for d in range ( 10 ) :
            if ( cnt [ d ] ) :
                dp [ i ] [ d ] = locMax
        Len = max ( Len , locMax )
    return Len
arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ]
n = len ( arr )
print ( findSubsequence ( arr , n ) )

Index/key variables: d, i, j
Accumulator variables: tmp
Iterator variables: d, i, j
Python program:
M = 18
prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 ]
def count ( pos , even , odd , tight , num ) :
    if pos == len ( num ) :
        if len ( num ) & 1 :
            odd , even = even , odd
        d = even - odd
        for i in range ( 24 ) :
            if d == prime [ i ] :
                return 1
        return 0
    if dp [ pos ] [ even ] [ odd ] [ tight ] != - 1 :
        return dp [ pos ] [ even ] [ odd ] [ tight ]
    ans = 0
    limit = 9 if tight else num [ pos ]
    for d in range ( limit + 1 ) :
        currF = tight
        currEven = even
        currOdd = odd
        if d < num [ pos ] :
            currF = 1
        if pos & 1 :
            currOdd += d
        else :
            currEven += d
        ans += count ( pos + 1 , currEven , currOdd , currF , num )
    dp [ pos ] [ even ] [ odd ] [ tight ] = ans
    return dp [ pos ] [ even ] [ odd ] [ tight ]
def solve ( x ) :
    global dp
    num = [ ]
    while x :
        num . append ( x % 10 )
        x //= 10
    num . reverse ( )
    dp = [ [ [ [ - 1 , - 1 ] for i in range ( 90 ) ] for j in range ( 90 ) ] for k in range ( M ) ]
    return count ( 0 , 0 , 0 , 0 , num )
if __name__ == " _ _ main _ _ " :
    dp = [ ]
    L = 1
    R = 50
    print ( solve ( R ) - solve ( L - 1 ) )
    L = 50
    R = 100
    print ( solve ( R ) - solve ( L - 1 ) )

Index/key variables: even, i, odd, pos, tight
Accumulator variables: ans, currEven, currOdd, num, x
Iterator variables: d, i
Python program:
N = 5005
n , k = 0 , 0
gr = [ [ ] for i in range ( N ) ]
d = [ [ 0 for i in range ( 505 ) ] for i in range ( N ) ]
ans = 0
def Add_edge ( x , y ) :
    gr [ x ] . append ( y )
    gr [ y ] . append ( x )
def dfs ( v , par ) :
    global ans
    d [ v ] [ 0 ] = 1
    for i in gr [ v ] :
        if ( i != par ) :
            dfs ( i , v )
            for j in range ( 1 , k + 1 ) :
                ans += d [ i ] [ j - 1 ] * d [ v ] [ k - j ]
            for j in range ( 1 , k + 1 ) :
                d [ v ] [ j ] += d [ i ] [ j - 1 ]
n = 5
k = 2
Add_edge ( 1 , 2 )
Add_edge ( 2 , 3 )
Add_edge ( 3 , 4 )
Add_edge ( 2 , 5 )
dfs ( 1 , 0 )
print ( ans )

Index/key variables: i, j, v, x, y
Accumulator variables: ans
Iterator variables: i, j
Python program:
def findXorSum ( arr , n ) :
    Sum = 0
    mul = 1
    for i in range ( 30 ) :
        c_odd = 0
        odd = 0
        for j in range ( n ) :
            if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) :
                odd = ( ~ odd )
            if ( odd ) :
                c_odd += 1
        for j in range ( n ) :
            Sum += ( mul * c_odd )
            if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) :
                c_odd = ( n - j - c_odd )
        mul *= 2
    return Sum
arr = [ 3 , 8 , 13 ]
n = len ( arr )
print ( findXorSum ( arr , n ) )

Index/key variables: j
Accumulator variables: Sum, c_odd, mul
Iterator variables: i, j
Python program:
import sys
def charVal ( s , i ) :
    if ( s [ i ] == '0' ) :
        return 0
    elif ( s [ i ] == '1' ) :
        return 1
    else :
        return 2
def countMinimalReplacements ( s , i , prev , dp , n ) :
    if ( i == n ) :
        return 0
    if ( dp [ i ] [ prev ] != - 1 ) :
        return dp [ i ] [ prev ]
    val = charVal ( s , i )
    ans = sys . maxsize
    if ( val == prev ) :
        val = 0
        for cur in range ( 3 ) :
            if ( cur == prev ) :
                continue
            val = 1 + countMinimalReplacements ( s , i + 1 , cur , dp , n )
            ans = min ( ans , val )
    else :
        ans = countMinimalReplacements ( s , i + 1 , val , dp , n )
    dp [ i ] [ val ] = ans
    return dp [ i ] [ val ]
if __name__ == ' _ _ main _ _ ' :
    s = "201220211"
    n = len ( s )
    dp = [ [ - 1 for i in range ( 3 ) ] for i in range ( n ) ]
    val = charVal ( s , 0 )
    print ( countMinimalReplacements ( s , 1 , val , dp , n ) )

Index/key variables: i, prev, val
Iterator variables: cur
Python program:
n = 4
m = 4
def calcProbability ( M , k ) :
    dp = [ [ 0 for i in range ( n ) ] for i in range ( m ) ]
    Sum = [ 0 for i in range ( n ) ]
    for j in range ( n ) :
        dp [ 0 ] [ j ] = M [ 0 ] [ j ]
        Sum [ 0 ] += dp [ 0 ] [ j ]
    for i in range ( 1 , m ) :
        for j in range ( n ) :
            dp [ i ] [ j ] += ( dp [ i - 1 ] [ j ] / Sum [ i - 1 ] + M [ i ] [ j ] )
            Sum [ i ] += dp [ i ] [ j ]
    return dp [ n - 1 ] [ k - 1 ] / Sum [ n - 1 ]
M = [ [ 1 , 1 , 0 , 3 ] , [ 2 , 3 , 2 , 3 ] , [ 9 , 3 , 0 , 2 ] , [ 2 , 3 , 2 , 2 ] ]
k = 3
print ( calcProbability ( M , k ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import math as mt
def MaximumNumbers ( string ) :
    n = len ( string )
    remIndex = [ - 1 for i in range ( 3 ) ]
    remIndex [ 0 ] = 0
    res = [ - 1 for i in range ( n + 1 ) ]
    r = 0
    for i in range ( n + 1 ) :
        r = ( r + ord ( string [ i - 1 ] ) - ord ( '0' ) ) % 3
        res [ i ] = res [ i - 1 ]
        if ( remIndex [ r ] != - 1 ) :
            res [ i ] = max ( res [ i ] , res [ remIndex [ r ] ] + 1 )
        remIndex [ r ] = i + 1
    return res [ n ]
s = "12345"
print ( MaximumNumbers ( s ) )

Index/key variables: i, n, r
Iterator variables: i
Python program:
INF = 99999
size = 10
def findMinimumSteps ( mat , x , y , n ) :
    dist = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
    i , j , k = 0 , 0 , 0
    for i in range ( n ) :
        for j in range ( n ) :
            if ( mat [ i ] [ j ] == 0 ) :
                dist [ i ] [ j ] = INF
            else :
                dist [ i ] [ j ] = 1
            if ( i == j ) :
                dist [ i ] [ j ] = 1
    for k in range ( n ) :
        for i in range ( n ) :
            for j in range ( n ) :
                if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) :
                    dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ]
    if ( dist [ x ] [ y ] < INF ) :
        return dist [ x ] [ y ]
    else :
        return - 1
mat = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ]
x , y = 2 , 3
print ( findMinimumSteps ( mat , x , y , size ) )

Index/key variables: i, j, k, x, y
Iterator variables: i, j, k
Python program:
Max = 4
c = 2
def countPaths ( Sum , get , m , n , dp ) :
    if ( Sum < 0 ) :
        return 0
    if ( Sum == 0 ) :
        return get
    if ( dp [ Sum ] [ get ] != - 1 ) :
        return dp [ Sum ] [ get ]
    res = 0
    for i in range ( 1 , n + 1 ) :
        if ( i == m ) :
            res += countPaths ( Sum - i , 1 , m , n , dp )
            res += countPaths ( Sum - i , get , m , n , dp )
    dp [ Sum ] [ get ] = res
    return dp [ Sum ] [ get ]
n = 3
m = 2
x = 3
dp = [ [ - 1 for i in range ( 2 ) ] for i in range ( Max + 1 ) ]
for i in range ( Max + 1 ) :
    for j in range ( 2 ) :
        dp [ i ] [ j ] = - 1
print ( countPaths ( x , 0 , m , n , dp ) )

Index/key variables: Sum, get, i, j
Accumulator variables: res
Iterator variables: i, j
Python program:
def addEdge ( adj , u , v ) :
    adj [ u ] . append ( v )
    adj [ v ] . append ( u )
def printGraph ( adj , V ) :
    for v in range ( 0 , V ) :
        print ( " Adjacency ▁ list ▁ of ▁ vertex " , v )
        print ( " head " , end = " ▁ " )
        for x in adj [ v ] :
            print ( " - > " , x , end = " " )
        print ( )
def findMinOperationUtil ( dp , adj , A , src , parent ) :
    dp [ src ] [ 0 ] = dp [ src ] [ 1 ] = 0
    for V in adj [ src ] :
        if V == parent :
            continue
        findMinOperationUtil ( dp , adj , A , V , src )
        dp [ src ] [ 0 ] = max ( dp [ src ] [ 0 ] , dp [ V ] [ 0 ] )
        dp [ src ] [ 1 ] = max ( dp [ src ] [ 1 ] , dp [ V ] [ 1 ] )
    A [ src - 1 ] += dp [ src ] [ 0 ] - dp [ src ] [ 1 ]
    if A [ src - 1 ] > 0 :
        dp [ src ] [ 1 ] += A [ src - 1 ]
    else :
        dp [ src ] [ 0 ] += abs ( A [ src - 1 ] )
def findMinOperation ( adj , A , V ) :
    dp = [ [ 0 , 0 ] for i in range ( V + 1 ) ]
    findMinOperationUtil ( dp , adj , A , 1 , 0 )
    minOperations = dp [ 1 ] [ 0 ] + dp [ 1 ] [ 1 ]
    return minOperations
if __name__ == " _ _ main _ _ " :
    V = 5
    adj = [ [ ] for i in range ( V + 1 ) ]
    addEdge ( adj , 1 , 2 )
    addEdge ( adj , 1 , 3 )
    A = [ 1 , - 1 , 1 ]
    minOperations = findMinOperation ( adj , A , V )
    print ( minOperations )

Index/key variables: V, src, u, v
Iterator variables: V, v, x
Python program:
MAX_K = 15
fac = [ 1 for i in range ( MAX_K ) ]
def factorial ( k ) :
    fac [ 0 ] = 1
    for i in range ( 1 , k + 2 ) :
        fac [ i ] = ( i * fac [ i - 1 ] )
def bin ( a , b ) :
    ans = fac [ a ] // ( fac [ a - b ] * fac [ b ] )
    return ans
def sumofn ( n , k ) :
    p = 0
    num1 , temp = 1 , 1
    arr = [ 1 for i in range ( 1000 ) ]
    for j in range ( 1 , k + 1 ) :
        if j == 1 :
            num1 = ( n * ( n + 1 ) ) // 2
            arr [ p ] = num1
            p += 1
        else :
            temp = pow ( n + 1 , j + 1 ) - 1 - n
            for s in range ( 1 , j ) :
                temp = temp - ( arr [ j - s - 1 ] * bin ( j + 1 , s + 1 ) )
            temp = temp // ( j + 1 )
            arr [ p ] = temp
            p += 1
    temp = arr [ p - 1 ]
    return temp
n , k = 5 , 2
factorial ( k )
print ( sumofn ( n , k ) )

Index/key variables: a, b, i, p
Iterator variables: i, j, s
Python program:
max = 4
def countWays ( index , cnt , dp , n , m , k ) :
    if ( index == n ) :
        if ( cnt == k ) :
            return 1
        else :
            return 0
    if ( dp [ index ] [ cnt ] != - 1 ) :
        return dp [ index ] [ cnt ]
    ans = 0
    ans += countWays ( index + 1 , cnt , dp , n , m , k )
    ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k )
    dp [ index ] [ cnt ] = ans
    return dp [ index ] [ cnt ]
if __name__ == " _ _ main _ _ " :
    n = 3
    m = 3
    k = 2
    dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( max ) ]
    print ( m * countWays ( 1 , 0 , dp , n , m , k ) )

Index/key variables: cnt, index