dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
MAX = 1005
def shortestSeq ( S : list , T : list ) :
    m = len ( S )
    n = len ( T )
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ]
    for i in range ( m + 1 ) :
        dp [ i ] [ 0 ] = 1
    for i in range ( n + 1 ) :
        dp [ 0 ] [ i ] = MAX
    for i in range ( 1 , m + 1 ) :
        for j in range ( 1 , n + 1 ) :
            ch = S [ i - 1 ]
            k = j - 1
            while k >= 0 :
                if T [ k ] == ch :
                    break
                k -= 1
            if k == - 1 :
                dp [ i ] [ j ] = 1
            else :
                dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ k ] + 1 )
    ans = dp [ m ] [ n ]
    if ans >= MAX :
        ans = - 1
    return ans
if __name__ == " _ _ main _ _ " :
    S = " babab "
    T = " babba "
    print ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : " , shortestSeq ( S , T ) )

Index/key variables: i, j, k, m, n
Iterator variables: i, j
Python program:
def countWaysToJump ( arr , n ) :
    count_jump = [ 0 for i in range ( n ) ]
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( arr [ i ] >= n - i - 1 ) :
            count_jump [ i ] += 1
        j = i + 1
        while ( j < n - 1 and j <= arr [ i ] + i ) :
            if ( count_jump [ j ] != - 1 ) :
                count_jump [ i ] += count_jump [ j ]
            j += 1
        if ( count_jump [ i ] == 0 ) :
            count_jump [ i ] = - 1
    for i in range ( n ) :
        print ( count_jump [ i ] , end = " ▁ " )
arr = [ 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 ]
n = len ( arr )
countWaysToJump ( arr , n )

Index/key variables: i, j
Iterator variables: i
Python program:
def minStepToDeleteString ( str ) :
    N = len ( str )
    dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( N + 1 ) ]
    for l in range ( 1 , N + 1 ) :
        i = 0
        j = l - 1
        while j < N :
            if ( l == 1 ) :
                dp [ i ] [ j ] = 1
            else :
                dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ]
                if ( str [ i ] == str [ i + 1 ] ) :
                    dp [ i ] [ j ] = min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] )
                for K in range ( i + 2 , j + 1 ) :
                    if ( str [ i ] == str [ K ] ) :
                        dp [ i ] [ j ] = min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] )
            i += 1
            j += 1
    return dp [ 0 ] [ N - 1 ]
if __name__ == " _ _ main _ _ " :
    str = "2553432"
    print ( minStepToDeleteString ( str ) )

Index/key variables: K, i, j
Iterator variables: K, l
Python program:
inf = 1000000000
ans = inf
def solve ( i , par , a , n , k , current_ans ) :
    if ( par > k ) :
        return 0
    global ans
    if ( par == k and i == n - 1 ) :
        ans = min ( ans , current_ans )
        return 0
    for j in range ( i + 1 , n ) :
        solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) )
k = 2
a = [ 1 , 5 , 8 , 10 ]
n = len ( a )
solve ( - 1 , 0 , a , n , k , 0 )
print ( ans )

Index/key variables: j
Iterator variables: j
Python program:
def getMinSteps ( n , memo ) :
    if ( n == 1 ) :
        return 0
    if ( memo [ n ] != - 1 ) :
        return memo [ n ]
    res = getMinSteps ( n - 1 , memo )
    if ( n % 2 == 0 ) :
        res = min ( res , getMinSteps ( n // 2 , memo ) )
    if ( n % 3 == 0 ) :
        res = min ( res , getMinSteps ( n // 3 , memo ) )
    memo [ n ] = 1 + res
    return memo [ n ]
def getsMinSteps ( n ) :
    memo = [ 0 for i in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        memo [ i ] = - 1
    return getMinSteps ( n , memo )
n = 10
print ( getsMinSteps ( n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
MAX = 1000
def numofArray ( n , m ) :
    dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
    di = [ [ ] for i in range ( MAX ) ]
    mu = [ [ ] for i in range ( MAX ) ]
    for i in range ( 1 , m + 1 ) :
        for j in range ( 2 * i , m + 1 , i ) :
            di [ j ] . append ( i )
            mu [ i ] . append ( j )
        di [ i ] . append ( i )
    for i in range ( 1 , m + 1 ) :
        dp [ 1 ] [ i ] = 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( 1 , m + 1 ) :
            dp [ i ] [ j ] = 0
            for x in di [ j ] :
                dp [ i ] [ j ] += dp [ i - 1 ] [ x ]
            for x in mu [ j ] :
                dp [ i ] [ j ] += dp [ i - 1 ] [ x ]
    ans = 0
    for i in range ( 1 , m + 1 ) :
        ans += dp [ n ] [ i ]
        di [ i ] . clear ( )
        mu [ i ] . clear ( )
    return ans
if __name__ == " _ _ main _ _ " :
    n = m = 3
    print ( numofArray ( n , m ) )

Index/key variables: i, j, n, x
Accumulator variables: ans
Iterator variables: i, j, x
Python program:
from typing import List
class Temple :
    def __init__ ( self , l : int , r : int ) :
        self . L = l
        self . R = r
def offeringNumber ( n : int , templeHeight : List [ int ] ) -> int :
    chainSize = [ 0 ] * n
    for i in range ( n ) :
        chainSize [ i ] = Temple ( - 1 , - 1 )
    chainSize [ 0 ] . L = 1
    chainSize [ - 1 ] . R = 1
    for i in range ( 1 , n ) :
        if templeHeight [ i - 1 ] < templeHeight [ i ] :
            chainSize [ i ] . L = chainSize [ i - 1 ] . L + 1
        else :
            chainSize [ i ] . L = 1
    for i in range ( n - 2 , - 1 , - 1 ) :
        if templeHeight [ i + 1 ] < templeHeight [ i ] :
            chainSize [ i ] . R = chainSize [ i + 1 ] . R + 1
        else :
            chainSize [ i ] . R = 1
    sm = 0
    for i in range ( n ) :
        sm += max ( chainSize [ i ] . L , chainSize [ i ] . R )
    return sm
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 1 , 2 , 2 ]
    print ( offeringNumber ( 3 , arr1 ) )
    arr2 = [ 1 , 4 , 3 , 6 , 2 , 1 ]
    print ( offeringNumber ( 6 , arr2 ) )

Index/key variables: i, int
Accumulator variables: sm
Iterator variables: i
Class/struct names: Temple
Python program:
def stringReduction ( str ) :
    n = len ( str )
    count = [ 0 ] * 3
    for i in range ( n ) :
        count [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1
    if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) :
        return n
    if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) :
        return 2
    return 1
if __name__ == " _ _ main _ _ " :
    str = " abcbbaacb "
    print ( stringReduction ( str ) )

Index/key variables: i
Iterator variables: i
Python program:
MAX = 100 ;
dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
def largestZigZagSumRec ( mat , i , j , n ) :
    if ( dp [ i ] [ j ] != - 1 ) :
        return dp [ i ] [ j ] ;
    if ( i == n - 1 ) :
        dp [ i ] [ j ] = mat [ i ] [ j ] ;
        return ( dp [ i ] [ j ] ) ;
    zzs = 0 ;
    for k in range ( n ) :
        if ( k != j ) :
            zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ;
    dp [ i ] [ j ] = ( zzs + mat [ i ] [ j ] ) ;
    return ( dp [ i ] [ j ] ) ;
def largestZigZag ( mat , n ) :
    for i in range ( MAX ) :
        for k in range ( MAX ) :
            dp [ i ] [ k ] = - 1 ;
    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, k
Iterator variables: i, j, k
Python program:
def maxRevenue ( m , x , revenue , n , t ) :
    maxRev = [ 0 ] * ( m + 1 )
    nxtbb = 0 ;
    for i in range ( 1 , m + 1 ) :
        if ( nxtbb < n ) :
            if ( x [ nxtbb ] != i ) :
                maxRev [ i ] = maxRev [ i - 1 ]
            else :
                if ( i <= t ) :
                    maxRev [ i ] = max ( maxRev [ i - 1 ] , revenue [ nxtbb ] )
                else :
                    maxRev [ i ] = max ( maxRev [ i - t - 1 ] + revenue [ nxtbb ] , maxRev [ i - 1 ] ) ;
                nxtbb += 1
        else :
            maxRev [ i ] = maxRev [ i - 1 ]
    return maxRev [ m ]
if __name__ == " _ _ main _ _ " :
    m = 20
    x = [ 6 , 7 , 12 , 13 , 14 ]
    revenue = [ 5 , 6 , 5 , 3 , 1 ]
    n = len ( x )
    t = 5
    print ( maxRevenue ( m , x , revenue , n , t ) )

Index/key variables: i, m, nxtbb
Iterator variables: i
Python program:
Row = 6
Col = 6
def largestKSubmatrix ( a ) :
    dp = [ [ 0 for x in range ( Row ) ] for y in range ( Col ) ]
    result = 0
    for i in range ( Row ) :
        for j in range ( Col ) :
            if ( i == 0 or j == 0 ) :
                dp [ i ] [ j ] = 1
            else :
                if ( a [ i ] [ j ] == a [ i - 1 ] [ j ] and a [ i ] [ j ] == a [ i ] [ j - 1 ] and a [ i ] [ j ] == a [ i - 1 ] [ j - 1 ] ) :
                    dp [ i ] [ j ] = min ( min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) , dp [ i - 1 ] [ j - 1 ] ) + 1
                else :
                    dp [ i ] [ j ] = 1
            result = max ( result , dp [ i ] [ j ] )
    return result
a = [ [ 2 , 2 , 3 , 3 , 4 , 4 ] , [ 5 , 5 , 7 , 7 , 7 , 4 ] , [ 1 , 2 , 7 , 7 , 7 , 4 ] , [ 4 , 4 , 7 , 7 , 7 , 4 ] , [ 5 , 5 , 5 , 1 , 2 , 7 ] , [ 8 , 7 , 9 , 4 , 4 , 4 ] ] ;
print ( largestKSubmatrix ( a ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def countDivisibleSubseq ( str , n ) :
    l = len ( str )
    dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ]
    dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1
    for i in range ( 1 , l ) :
        dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1
        for j in range ( n ) :
            dp [ i ] [ j ] += dp [ i - 1 ] [ j ]
            dp [ i ] [ ( j * 10 + ( ord ( str [ i ] ) - ord ( '0' ) ) ) % n ] += dp [ i - 1 ] [ j ]
    return dp [ l - 1 ] [ 0 ]
if __name__ == " _ _ main _ _ " :
    str = "1234"
    n = 4
    print ( countDivisibleSubseq ( str , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
from typing import List
def findLIS ( arr : List [ int ] , n : int ) -> List [ int ] :
    L = [ 0 ] * n
    for i in range ( n ) :
        L [ i ] = [ ]
    L [ 0 ] . append ( arr [ 0 ] )
    for i in range ( 1 , n ) :
        for j in range ( i ) :
            if ( arr [ i ] > arr [ j ] and ( len ( L [ i ] ) < len ( L [ j ] ) ) ) :
                L [ i ] = L [ j ] . copy ( )
        L [ i ] . append ( arr [ i ] )
    maxSize = 1
    lis : List [ int ] = [ ]
    for x in L :
        if ( len ( x ) > maxSize ) :
            lis = x . copy ( )
            maxSize = len ( x )
    return lis
def minimize ( input : List [ int ] , n : int ) -> None :
    arr = input . copy ( )
    while len ( arr ) :
        lis = findLIS ( arr , len ( arr ) )
        if ( len ( lis ) < 2 ) :
            break
        i = 0
        while i < len ( arr ) and len ( lis ) > 0 :
            if ( arr [ i ] == lis [ 0 ] ) :
                arr . remove ( arr [ i ] )
                i -= 1
                lis . remove ( lis [ 0 ] )
            i += 1
    i = 0
    while i < len ( arr ) :
        print ( arr [ i ] , end = " ▁ " )
        i += 1
    if ( i == 0 ) :
        print ( " - 1" )
if __name__ == " _ _ main _ _ " :
    input = [ 3 , 2 , 6 , 4 , 5 , 1 ]
    n = len ( input )
    minimize ( input , n )

Index/key variables: i, int, j
Iterator variables: i, j, x
Python program:
MAX = 21
fact = [ 0 ] * MAX
def probability ( k , n ) :
    ans = 0
    for i in range ( k , n + 1 ) :
        ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] )
    ans = ans / ( 1 << n )
    return ans
def precompute ( ) :
    fact [ 0 ] = 1
    fact [ 1 ] = 1
    for i in range ( 2 , 20 ) :
        fact [ i ] = fact [ i - 1 ] * i
if __name__ == ' _ _ main _ _ ' :
    precompute ( )
    print ( probability ( 2 , 3 ) )
    print ( probability ( 3 , 6 ) )
    print ( probability ( 12 , 18 ) )

Index/key variables: i, n
Accumulator variables: ans
Iterator variables: i
Python program:
def countStrings ( n , k ) :
    dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ]
    dp [ 1 ] [ 0 ] [ 0 ] = 1
    dp [ 1 ] [ 0 ] [ 1 ] = 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( k + 1 ) :
            dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ 1 ] )
            dp [ i ] [ j ] [ 1 ] = dp [ i - 1 ] [ j ] [ 0 ]
            if j >= 1 :
                dp [ i ] [ j ] [ 1 ] += dp [ i - 1 ] [ j - 1 ] [ 1 ]
    return dp [ n ] [ k ] [ 0 ] + dp [ n ] [ k ] [ 1 ]
if __name__ == ' _ _ main _ _ ' :
    n = 5
    k = 2
    print ( countStrings ( n , k ) )

Index/key variables: i, j, k, n
Iterator variables: i, j
Python program:
def canVote ( a , n , x ) :
    total_sum = 0
    for i in range ( len ( a ) ) :
        total_sum += a [ i ]
    if ( total_sum <= x ) :
        return True
    a . sort ( )
    presum = [ 0 for i in range ( len ( a ) ) ]
    presum [ 0 ] = a [ 0 ]
    for i in range ( 1 , len ( presum ) ) :
        presum [ i ] = presum [ i - 1 ] + a [ i ]
    for i in range ( 0 , len ( presum ) ) :
        for j in range ( i + 1 , len ( presum ) ) :
            arr1_sum = ( presum [ i ] + ( total_sum - presum [ j ] ) )
            if ( ( arr1_sum <= x ) and ( total_sum - arr1_sum ) <= x ) :
                return True
    return False
n = 3
x = 4
a = [ 2 , 4 , 2 ]
if ( canVote ( a , n , x ) ) :
    print ( " YES " )
else :
    print ( " NO " )

Index/key variables: i, j
Accumulator variables: total_sum
Iterator variables: i, j
Python program:
dp = [ - 1 ] * 1000
def countFriendsPairings ( n ) :
    global dp
    if ( dp [ n ] != - 1 ) :
        return dp [ n ]
    if ( n > 2 ) :
        dp [ n ] = ( countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) )
        return dp [ n ]
    else :
        dp [ n ] = n
        return dp [ n ]
n = 4
print ( countFriendsPairings ( n ) )

Index/key variables: n
Python program:
fact = [ 1 ]
def preComputeFact ( n ) :
    for i in range ( 1 , n + 1 ) :
        fact . append ( ( fact [ i - 1 ] * i ) )
def countFriendsPairings ( n ) :
    ones = n
    twos = 1
    ans = 0
    while ( ones >= 0 ) :
        ans = ans + ( fact [ n ] // ( twos * fact [ ones ] * fact [ ( n - ones ) // 2 ] ) )
        ones = ones - 2
        twos = twos * 2
    return ( ans )
preComputeFact ( 1000 )
n = 4
print ( countFriendsPairings ( n ) )

Index/key variables: n, ones
Accumulator variables: fact
Iterator variables: i
Python program:
N = 3
def maxPathSum ( tri , m , n ) :
    for i in range ( m - 1 , - 1 , - 1 ) :
        for j in range ( i + 1 ) :
            if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) :
                tri [ i ] [ j ] += tri [ i + 1 ] [ j ]
            else :
                tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ]
    return tri [ 0 ] [ 0 ]
tri = [ [ 1 , 0 , 0 ] , [ 4 , 8 , 0 ] , [ 1 , 5 , 3 ] ]
print ( maxPathSum ( tri , 2 , 2 ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def MaxDotProduct ( A , B , m , n ) :
    dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 , 1 ) :
        for j in range ( i , m + 1 , 1 ) :
            dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] )
    return dp [ n ] [ m ]
if __name__ == ' _ _ main _ _ ' :
    A = [ 2 , 3 , 1 , 7 , 8 ]
    B = [ 3 , 6 , 7 ]
    m = len ( A )
    n = len ( B )
    print ( MaxDotProduct ( A , B , m , n ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
X = " AGGT12"
Y = "12TXAYB "
Z = "12XBA "
dp = [ [ [ - 1 for i in range ( 100 ) ] for j in range ( 100 ) ] for k in range ( 100 ) ]
def lcsOf3 ( i , j , k ) :
    if ( i == - 1 or j == - 1 or k == - 1 ) :
        return 0
    if ( dp [ i ] [ j ] [ k ] != - 1 ) :
        return dp [ i ] [ j ] [ k ]
    if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) :
        dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 )
        return dp [ i ] [ j ] [ k ]
    else :
        dp [ i ] [ j ] [ k ] = max ( max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) )
        return dp [ i ] [ j ] [ k ]
if __name__ == " _ _ main _ _ " :
    m = len ( X )
    n = len ( Y )
    o = len ( Z )
    print ( " Length ▁ of ▁ LCS ▁ is " , lcsOf3 ( m - 1 , n - 1 , o - 1 ) )

Index/key variables: i, j, k
Python program:
MAX = 102
def countMin ( arr , dp , n , dec , inc , i ) :
    if dp [ dec ] [ inc ] [ i ] != - 1 :
        return dp [ dec ] [ inc ] [ i ]
    if i == n :
        return 0
    if arr [ i ] < arr [ dec ] :
        dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , i , inc , i + 1 )
    if arr [ i ] > arr [ inc ] :
        if dp [ dec ] [ inc ] [ i ] == - 1 :
            dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , dec , i , i + 1 )
        else :
            dp [ dec ] [ inc ] [ i ] = min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec ] [ inc ] [ i ] )
    if dp [ dec ] [ inc ] [ i ] == - 1 :
        dp [ dec ] [ inc ] [ i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 )
    else :
        dp [ dec ] [ inc ] [ i ] = min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec ] [ inc ] [ i ] )
    return dp [ dec ] [ inc ] [ i ]
def wrapper ( arr , n ) :
    arr [ MAX - 2 ] = 1000000000
    arr [ MAX - 1 ] = - 1000000000
    dp = [ [ [ - 1 for i in range ( MAX ) ] for i in range ( MAX ) ] for i in range ( MAX ) ]
    return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 )
if __name__ == ' _ _ main _ _ ' :
    n = 12
    arr = [ 7 , 8 , 1 , 2 , 4 , 6 , 3 , 5 , 2 , 1 , 8 , 7 ]
    for i in range ( MAX ) :
        arr . append ( 0 )
    print ( wrapper ( arr , n ) )

Index/key variables: dec, i, inc
Accumulator variables: arr
Iterator variables: i
Python program:
def distSumRec ( arr , n , sum , currindex , s ) :
    if ( currindex > n ) :
        return
    if ( currindex == n ) :
        s . add ( sum )
        return
    distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s )
    distSumRec ( arr , n , sum , currindex + 1 , s )
def printDistSum ( arr , n ) :
    s = set ( )
    distSumRec ( arr , n , 0 , 0 , s )
    for i in s :
        print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 5 , 6 ]
    n = len ( arr )
    printDistSum ( arr , n )

Index/key variables: currindex
Iterator variables: i
Python program:
def printDistSum ( arr , n ) :
    Sum = sum ( arr )
    dp = [ [ False for i in range ( Sum + 1 ) ] for i in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        dp [ i ] [ 0 ] = True
    for i in range ( 1 , n + 1 ) :
        dp [ i ] [ arr [ i - 1 ] ] = True
        for j in range ( 1 , Sum + 1 ) :
            if ( dp [ i - 1 ] [ j ] == True ) :
                dp [ i ] [ j ] = True
                dp [ i ] [ j + arr [ i - 1 ] ] = True
    for j in range ( Sum + 1 ) :
        if ( dp [ n ] [ j ] == True ) :
            print ( j , end = " ▁ " )
arr = [ 2 , 3 , 4 , 5 , 6 ]
n = len ( arr )
printDistSum ( arr , n )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
def ugly ( a , size , n ) :
    if ( n <= 0 ) :
        return - 1
    if ( n == 1 ) :
        return 1
    pq = [ ]
    for i in range ( size ) :
        pq . append ( a [ i ] )
    count = 1
    no = 0
    pq = sorted ( pq )
    while ( count < n ) :
        no = pq [ 0 ]
        del pq [ 0 ]
        if ( no != pq [ 0 ] ) :
            count += 1
            for i in range ( size ) :
                pq . append ( no * a [ i ] )
        pq = sorted ( pq )
    return no
if __name__ == ' _ _ main _ _ ' :
    a = [ 2 , 3 , 5 ]
    size = len ( a )
    print ( ugly ( a , size , 1000 ) )

Index/key variables: i
Accumulator variables: count, pq
Iterator variables: i
Python program:
R = 4
C = 4
def countPaths ( maze ) :
    if ( maze [ 0 ] [ 0 ] == - 1 ) :
        return 0
    for i in range ( R ) :
        if ( maze [ i ] [ 0 ] == 0 ) :
            maze [ i ] [ 0 ] = 1
        else :
            break
    for i in range ( 1 , C , 1 ) :
        if ( maze [ 0 ] [ i ] == 0 ) :
            maze [ 0 ] [ i ] = 1
        else :
            break
    for i in range ( 1 , R , 1 ) :
        for j in range ( 1 , C , 1 ) :
            if ( maze [ i ] [ j ] == - 1 ) :
                continue
            if ( maze [ i - 1 ] [ j ] > 0 ) :
                maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] )
            if ( maze [ i ] [ j - 1 ] > 0 ) :
                maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] )
    if ( maze [ R - 1 ] [ C - 1 ] > 0 ) :
        return maze [ R - 1 ] [ C - 1 ]
    else :
        return 0
if __name__ == ' _ _ main _ _ ' :
    maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
    print ( countPaths ( maze ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def minSum ( arr , n ) :
    dp = [ 0 ] * n
    if ( n == 1 ) :
        return arr [ 0 ]
    if ( n == 2 ) :
        return min ( arr [ 0 ] , arr [ 1 ] )
    if ( n == 3 ) :
        return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) )
    if ( n == 4 ) :
        return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( arr [ 2 ] , arr [ 3 ] ) )
    dp [ 0 ] = arr [ 0 ]
    dp [ 1 ] = arr [ 1 ]
    dp [ 2 ] = arr [ 2 ]
    dp [ 3 ] = arr [ 3 ]
    for i in range ( 4 , n ) :
        dp [ i ] = arr [ i ] + min ( min ( dp [ i - 1 ] , dp [ i - 2 ] ) , min ( dp [ i - 3 ] , dp [ i - 4 ] ) )
    return min ( min ( dp [ n - 1 ] , dp [ n - 2 ] ) , min ( dp [ n - 4 ] , dp [ n - 3 ] ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 6 , 1 ]
    n = len ( arr )
    print ( minSum ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
N = 4
def maxDecimalValue ( mat , i , j , p ) :
    if i >= N or j >= N :
        return 0
    result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) )
    if mat [ i ] [ j ] == 1 :
        return pow ( 2 , p ) + result
    else :
        return result
mat = [ [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 0 ] , [ 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 ] ]
print ( maxDecimalValue ( mat , 0 , 0 , 0 ) )

Index/key variables: i, j
Python program:
def CountPS ( str , n ) :
    dp = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
    P = [ [ False for x in range ( n ) ] for y in range ( n ) ]
    for i in range ( n ) :
        P [ i ] [ i ] = True
    for i in range ( n - 1 ) :
        if ( str [ i ] == str [ i + 1 ] ) :
            P [ i ] [ i + 1 ] = True
            dp [ i ] [ i + 1 ] = 1
    for gap in range ( 2 , n ) :
        for i in range ( n - gap ) :
            j = gap + i
            if ( str [ i ] == str [ j ] and P [ i + 1 ] [ j - 1 ] ) :
                P [ i ] [ j ] = True
            if ( P [ i ] [ j ] == True ) :
                dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] )
            else :
                dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] )
    return dp [ 0 ] [ n - 1 ]
if __name__ == " _ _ main _ _ " :
    str = " abaab "
    n = len ( str )
    print ( CountPS ( str , n ) )

Index/key variables: i, j
Iterator variables: gap, i
Python program:
def maxSumPairWithDifferenceLessThanK ( arr , N , K ) :
    arr . sort ( )
    dp = [ 0 ] * N
    dp [ 0 ] = 0
    for i in range ( 1 , N ) :
        dp [ i ] = dp [ i - 1 ]
        if ( arr [ i ] - arr [ i - 1 ] < K ) :
            if ( i >= 2 ) :
                dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + arr [ i - 1 ] ) ;
            else :
                dp [ i ] = max ( dp [ i ] , arr [ i ] + arr [ i - 1 ] ) ;
    return dp [ N - 1 ]
arr = [ 3 , 5 , 10 , 15 , 17 , 12 , 9 ]
N = len ( arr )
K = 4
print ( maxSumPairWithDifferenceLessThanK ( arr , N , K ) )

Index/key variables: i
Iterator variables: i