dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
N = 10 ** 5 + 5
v = [ [ ] for i in range ( N ) ]
dp = [ 0 ] * N
leaves = [ 0 ] * ( N )
n = 0
def dfs ( a , par ) :
    leaf = 1
    for i in v [ a ] :
        if ( i [ 0 ] == par ) :
            continue
        leaf = 0
        dfs ( i [ 0 ] , a )
    if ( leaf == 1 ) :
        leaves [ a ] += 1
    else :
        for i in v [ a ] :
            if ( i [ 0 ] == par ) :
                continue
            leaves [ a ] += leaves [ i [ 0 ] ]
            dp [ a ] = ( dp [ a ] + dp [ i [ 0 ] ] + leaves [ i [ 0 ] ] * i [ 1 ] )
def dfs2 ( a , par ) :
    for i in v [ a ] :
        if ( i [ 0 ] == par ) :
            continue
        leafOutside = leaves [ a ] - leaves [ i [ 0 ] ]
        dp [ i [ 0 ] ] += ( dp [ a ] - dp [ i [ 0 ] ] )
        dp [ i [ 0 ] ] += i [ 1 ] * ( leafOutside - leaves [ i [ 0 ] ] )
        leaves [ i [ 0 ] ] += leafOutside
        dfs2 ( i [ 0 ] , a )
def answerQueries ( queries ) :
    dfs ( 1 , 0 )
    dfs2 ( 1 , 0 )
    for i in range ( len ( queries ) ) :
        print ( dp [ queries [ i ] ] )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    v [ 1 ] . append ( [ 4 , 4 ] )
    v [ 4 ] . append ( [ 1 , 4 ] )
    v [ 1 ] . append ( [ 2 , 2 ] )
    v [ 2 ] . append ( [ 1 , 2 ] )
    v [ 2 ] . append ( [ 3 , 3 ] )
    v [ 3 ] . append ( [ 2 , 3 ] )
    v [ 2 ] . append ( [ 5 , 5 ] )
    v [ 5 ] . append ( [ 2 , 5 ] )
    queries = [ 1 , 3 , 5 ]
    answerQueries ( queries )

Index/key variables: a, i
Iterator variables: i
Python program:
def countChanges ( matrix , n , m ) :
    dist = n + m - 1
    Max_element = 0
    for i in range ( n ) :
        for j in range ( m ) :
            Max_element = max ( Max_element , matrix [ i ] [ j ] )
    freq = [ [ 0 for i in range ( Max_element + 1 ) ] for j in range ( dist ) ]
    for i in range ( dist ) :
        for j in range ( Max_element + 1 ) :
            freq [ i ] [ j ] = 0
    for i in range ( n ) :
        for j in range ( m ) :
            freq [ i + j ] [ matrix [ i ] [ j ] ] += 1
    min_changes_sum = 0
    for i in range ( dist // 2 ) :
        maximum = 0
        total_values = 0
        for j in range ( Max_element + 1 ) :
            maximum = max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] )
            total_values += ( freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] )
        min_changes_sum += total_values - maximum
    return min_changes_sum
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 7 , 0 , 3 , 1 , 8 , 1 , 3 ] , [ 0 , 4 , 0 , 1 , 0 , 4 , 0 ] , [ 3 , 1 , 8 , 3 , 1 , 0 , 7 ] ]
    minChanges = countChanges ( mat , 3 , 7 )
    print ( minChanges )

Index/key variables: i, j
Accumulator variables: min_changes_sum, total_values
Iterator variables: i, j
Python program:
dp = [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for i in range ( 10 ) ] for j in range ( 1002 ) ]
def possibleNumber ( pos , previous , tight , start , N , K ) :
    if ( pos == len ( N ) ) :
        return 1
    if ( dp [ pos ] [ previous ] [ tight ] [ start ] != - 1 ) :
        return dp [ pos ] [ previous ] [ tight ] [ start ]
    res = 0
    if ( tight ) :
        upper_limit = ord ( N [ pos ] ) - ord ( '0' )
    else :
        upper_limit = 9
    if ( not start ) :
        res = possibleNumber ( pos + 1 , previous , False , False , N , K )
        for i in range ( 1 , upper_limit + 1 ) :
            if ( tight and i == upper_limit ) :
                new_tight = 1
            else :
                new_tight = 0
            res += possibleNumber ( pos + 1 , i , new_tight , True , N , K )
    else :
        for i in range ( upper_limit + 1 ) :
            if ( tight and i == upper_limit ) :
                new_tight = 1
            else :
                new_tight = 0
            if ( abs ( i - previous ) <= K ) :
                res += possibleNumber ( pos + 1 , i , new_tight , True , N , K )
    dp [ pos ] [ previous ] [ tight ] [ start ] = res
    return dp [ pos ] [ previous ] [ tight ] [ start ]
if __name__ == ' _ _ main _ _ ' :
    N = "20"
    K = 2
    print ( possibleNumber ( 0 , 0 , True , False , N , K ) )

Index/key variables: pos, previous, start, tight
Accumulator variables: res
Iterator variables: i
Python program:
import sys
def mergeTwoNumbers ( numbers ) :
    n = len ( numbers )
    if ( len ( numbers ) == 0 ) :
        return 0
    prefixSum = [ 0 ] * ( n + 1 )
    for i in range ( 1 , n + 1 ) :
        prefixSum [ i ] = ( prefixSum [ i - 1 ] + numbers [ i - 1 ] )
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        dp [ i ] [ i ] = 0
    for p in range ( 2 , n + 1 ) :
        for i in range ( 1 , n - p + 2 ) :
            j = i + p - 1
            sum = prefixSum [ j ] - prefixSum [ i - 1 ]
            dp [ i ] [ j ] = sys . maxsize
            for k in range ( i , j ) :
                dp [ i ] [ j ] = min ( dp [ i ] [ j ] , ( dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) )
    return dp [ 1 ] [ n ]
arr1 = [ 6 , 4 , 4 , 6 ]
print ( mergeTwoNumbers ( arr1 ) )

Index/key variables: i, j, k, n
Iterator variables: i, k, p
Python program:
dp1 = [ 0 for i in range ( 100005 ) ]
dp2 = [ 0 for i in range ( 100005 ) ]
def dfs1 ( u , par , g , weight ) :
    dp1 [ u ] = weight [ u ]
    for c in g [ u ] :
        if ( c != par ) :
            dfs1 ( c , u , g , weight )
            dp1 [ u ] += max ( 0 , dp1 )
def dfs2 ( u , par , g , weight ) :
    if ( par != 0 ) :
        maxSumAncestors = dp2 [ par ] - max ( 0 , dp1 [ u ] )
        dp2 [ u ] = dp1 [ u ] + max ( 0 , maxSumAncestors )
    for c in g [ u ] :
        if ( c != par ) :
            dfs2 ( c , u , g , weight )
def addEdge ( u , v , g ) :
    g [ u ] . append ( v )
    g [ v ] . append ( u )
def maxSumSegments ( g , weight , n ) :
    dfs1 ( 1 , 0 , g , weight )
    for i in range ( 1 , n + 1 ) :
        dp2 [ i ] = dp1 [ i ]
    dfs2 ( 1 , 0 , g , weight )
def printAns ( n ) :
    for i in range ( 1 , n ) :
        print ( dp2 [ i ] , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    n = 7
    u = 0
    v = 0
    g = [ [ ] for i in range ( 100005 ) ]
    addEdge ( 1 , 2 , g )
    addEdge ( 1 , 3 , g )
    addEdge ( 2 , 4 , g )
    addEdge ( 2 , 5 , g )
    addEdge ( 3 , 6 , g )
    addEdge ( 4 , 7 , g )
    weight = [ 0 for i in range ( n + 1 ) ]
    weight [ 1 ] = - 8
    weight [ 2 ] = 9
    weight [ 3 ] = 7
    weight [ 4 ] = - 4
    weight [ 5 ] = 5
    weight [ 6 ] = - 10
    weight [ 7 ] = - 6
    maxSumSegments ( g , weight , n )
    printAns ( n )

Index/key variables: i, par, u, v
Iterator variables: c, i
Python program:
def countPairs ( arr , N ) :
    maxm = max ( arr )
    i = 0
    k = 0
    bitscount = [ 0 for i in range ( maxm + 1 ) ]
    i = 1
    while i <= maxm :
        bitscount [ i ] = 1
        i *= 2
    for i in range ( 1 , maxm + 1 ) :
        if ( bitscount [ i ] == 1 ) :
            k = i
        if ( bitscount [ i ] == 0 ) :
            bitscount [ i ] = ( bitscount [ k ] + bitscount [ i - k ] )
    setbits = dict ( )
    for i in range ( N ) :
        if bitscount [ arr [ i ] ] in setbits :
            setbits [ bitscount [ arr [ i ] ] ] += 1
        else :
            setbits [ bitscount [ arr [ i ] ] ] = 1
    ans = 0
    for it in setbits . values ( ) :
        ans += it * ( it - 1 ) // 2
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 12
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]
    print ( countPairs ( arr , N ) )

Index/key variables: i, k
Accumulator variables: ans
Iterator variables: i, it
Python program:
import sys
def depthFirst ( v , graph , visited , storeChain ) :
    visited [ v ] = True ;
    storeChain . append ( v ) ;
    for i in graph [ v ] :
        if ( visited [ i ] == False ) :
            depthFirst ( i , graph , visited , storeChain ) ;
def subarraySum ( arr , n ) :
    maxSubarraySum = arr [ 0 ] ;
    currentMax = arr [ 0 ] ;
    for i in range ( 1 , n ) :
        currentMax = max ( arr [ i ] , arr [ i ] + currentMax )
        maxSubarraySum = max ( maxSubarraySum , currentMax ) ;
    return maxSubarraySum ;
def maxSubarraySum ( graph , vertices , values ) :
    visited = [ False for i in range ( 1001 ) ]
    maxSum = - sys . maxsize ;
    for i in range ( 1 , vertices + 1 ) :
        if ( visited [ i ] == False ) :
            sizeChain = 0
            tempSum = 0 ;
            storeChain = [ ] ;
            depthFirst ( i , graph , visited , storeChain ) ;
            sizeChain = len ( storeChain )
            chainValues = [ 0 for i in range ( sizeChain + 1 ) ] ;
            for i in range ( sizeChain ) :
                temp = values [ storeChain [ i ] - 1 ] ;
                chainValues [ i ] = temp ;
            tempSum = subarraySum ( chainValues , sizeChain ) ;
            if ( tempSum > maxSum ) :
                maxSum = tempSum ;
    print ( " Maximum ▁ subarray ▁ sum ▁ among ▁ all ▁ " , end = ' ' ) ;
    print ( " connected ▁ components ▁ = ▁ " , end = ' ' )
    print ( maxSum )
if __name__ == " _ _ main _ _ " :
    graph = [ [ ] for i in range ( 1001 ) ]
    E = 4 ;
    V = 7 ;
    values = [ ] ;
    values . append ( 3 ) ;
    values . append ( 2 ) ;
    values . append ( 4 ) ;
    values . append ( - 2 ) ;
    values . append ( 0 ) ;
    values . append ( - 1 ) ;
    values . append ( - 5 ) ;
    graph [ 1 ] . append ( 2 ) ;
    graph [ 2 ] . append ( 1 ) ;
    graph [ 3 ] . append ( 4 ) ;
    graph [ 4 ] . append ( 3 ) ;
    graph [ 4 ] . append ( 5 ) ;
    graph [ 5 ] . append ( 4 ) ;
    graph [ 6 ] . append ( 7 ) ;
    graph [ 7 ] . append ( 6 ) ;
    maxSubarraySum ( graph , V , values ) ;

Index/key variables: i, v
Iterator variables: i
Python program:
rows , cols = ( 1000 , 1000 )
dp = [ [ 0 for i in range ( cols ) ] for j in range ( rows ) ]
def solve ( n , k , m ) :
    if n == 0 and k == 0 :
        return 1
    if n == 0 and k != 0 :
        return 0
    if n != 0 and k == 0 :
        return 0
    if n < 0 or k < 0 :
        return 0
    if dp [ n ] [ k ] :
        return dp [ n ] [ k ]
    ans = 0
    for j in range ( 1 , m + 1 ) :
        ans = ans + solve ( n - j , k - 1 , m )
    dp [ n ] [ k ] = ans
    return dp [ n ] [ k ]
N = 7
K = 4
M = 3
print ( solve ( N , K , M ) )

Index/key variables: k, n
Iterator variables: j
Python program:
def printMatrix ( A ) :
    for i in range ( len ( A ) ) :
        for j in range ( len ( A [ 0 ] ) ) :
            print ( A [ i ] [ j ] , end = ' ▁ ' )
        print ( )
def getMaxNeighbour ( A , K ) :
    ans = A ;
    for q in range ( 1 , K + 1 ) :
        for i in range ( len ( A ) ) :
            for j in range ( len ( A [ 0 ] ) ) :
                maxi = ans [ i ] [ j ] ;
                if ( i > 0 ) :
                    maxi = max ( maxi , ans [ i - 1 ] [ j ] ) ;
                if ( j > 0 ) :
                    maxi = max ( maxi , ans [ i ] [ j - 1 ] ) ;
                if ( i < len ( A ) - 1 ) :
                    maxi = max ( maxi , ans [ i + 1 ] [ j ] ) ;
                if ( j < len ( A [ 0 ] ) - 1 ) :
                    maxi = max ( maxi , ans [ i ] [ j + 1 ] ) ;
                A [ i ] [ j ] = maxi ;
        ans = A ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    B = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
    printMatrix ( getMaxNeighbour ( B , 2 ) ) ;

Index/key variables: i, j
Iterator variables: i, j, q
Python program:
prime = [ ]
isprime = [ True ] * 1000
dp = [ [ [ '0' for col in range ( 200 ) ] for col in range ( 20 ) ] for row in range ( 1000 ) ]
def sieve ( ) :
    for i in range ( 2 , 1000 ) :
        if ( isprime [ i ] ) :
            for j in range ( i * i , 1000 , i ) :
                isprime [ j ] = False
    for i in range ( 2 , 1000 ) :
        if ( isprime [ i ] ) :
            prime . append ( i )
def CountWays ( i , j , sums , n , k ) :
    if ( i >= len ( prime ) or sums > n ) :
        return 0
    if ( sums == n ) :
        if ( j == k ) :
            return 1
        return 0
    if j == k :
        return 0
    if dp [ i ] [ j ] [ sums ] == 0 :
        return dp [ i ] [ j ] [ sums ]
    inc = 0
    exc = 0
    inc = CountWays ( i + 1 , j + 1 , sums + prime [ i ] , n , k )
    exc = CountWays ( i + 1 , j , sums , n , k )
    dp [ i ] [ j ] [ sums ] = inc + exc
    return dp [ i ] [ j ] [ sums ]
if __name__ == " _ _ main _ _ " :
    sieve ( )
    N = 100
    K = 5
    print ( CountWays ( 0 , 0 , 0 , N , K ) )

Index/key variables: i, j, sums
Accumulator variables: prime
Iterator variables: i, j
Python program:
factors = [ [ ] for i in range ( 100005 ) ] ;
dp = [ 1000000000 for i in range ( 100005 ) ] ;
def precompute ( ) :
    for i in range ( 1 , 100001 ) :
        for j in range ( i , 100001 , i ) :
            factors [ j ] . append ( i ) ;
def solve ( arr , n ) :
    dp [ 0 ] = 0 ;
    for i in range ( n ) :
        for j in factors [ arr [ i ] ] :
            if ( i + j < n ) :
                dp [ i + j ] = min ( dp [ i + j ] , 1 + dp [ i ] ) ;
    return dp [ n - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    precompute ( ) ;
    arr = [ 2 , 8 , 16 , 55 , 99 , 100 ]
    n = len ( arr )
    print ( solve ( arr , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
dp = [ [ - 1 ] * ( ( 1 << 10 ) + 5 ) ] * 5000
def getmask ( val ) :
    mask = 0
    prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 ]
    for i in range ( 1 , 15 ) :
        if val % prime [ i ] == 0 :
            mask = mask | ( 1 << i )
    return mask
def calculate ( pos , mask , a , n ) :
    if ( ( pos == n ) or ( mask == ( 1 << n - 1 ) ) ) :
        return 0
    if dp [ pos ] [ mask ] != - 1 :
        return dp [ pos ] [ mask ]
    size = 0
    size = max ( size , calculate ( pos + 1 , mask , a , n ) )
    if ( getmask ( a [ pos ] ) & mask ) == 0 :
        new_mask = ( mask | ( getmask ( a [ pos ] ) ) )
        size = max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) )
    dp [ pos ] [ mask ] = size
    return dp [ pos ] [ mask ]
def largestSubset ( A , n ) :
    return calculate ( 0 , 0 , A , n ) ;
A = [ 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 ]
N = len ( A )
print ( largestSubset ( A , N ) )

Index/key variables: i, mask, pos
Iterator variables: i
Python program:
def MaximumSum ( a , n ) :
    dp = [ [ 0 for x in range ( 2 ) ] for y in range ( n + 1 ) ]
    dp [ 2 ] [ 1 ] = a [ 1 ]
    dp [ 2 ] [ 0 ] = a [ 0 ]
    for i in range ( 3 , n + 1 ) :
        if ( i & 1 ) :
            temp = max ( [ dp [ i - 3 ] [ 1 ] , dp [ i - 3 ] [ 0 ] , dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] ] )
            dp [ i ] [ 1 ] = a [ i - 1 ] + temp
            dp [ i ] [ 0 ] = max ( [ a [ i - 2 ] + dp [ i - 2 ] [ 0 ] , a [ i - 2 ] + dp [ i - 3 ] [ 1 ] , a [ i - 2 ] + dp [ i - 3 ] [ 0 ] , a [ i - 3 ] + dp [ i - 3 ] [ 0 ] ] )
        else :
            dp [ i ] [ 1 ] = ( a [ i - 1 ] + max ( [ dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] , dp [ i - 1 ] [ 0 ] ] ) )
            dp [ i ] [ 0 ] = a [ i - 2 ] + dp [ i - 2 ] [ 0 ]
    return max ( dp [ n ] [ 1 ] , dp [ n ] [ 0 ] )
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 2 , 3 , 4 , 5 , 6 ]
    N = len ( A )
    print ( MaximumSum ( A , N ) )

Index/key variables: i, n
Iterator variables: i
Python program:
from math import sqrt
def divisorGame ( N , A , dp ) :
    if ( N == 1 or N == 3 ) :
        return False
    if ( N == 2 ) :
        return True
    if ( dp [ N ] [ A ] != - 1 ) :
        return dp [ N ] [ A ]
    if ( A == 1 ) :
        ans = 0
    else :
        ans = 1
    for i in range ( 1 , int ( sqrt ( N ) ) + 1 , 1 ) :
        if ( N % i == 0 ) :
            if ( A ) :
                ans |= divisorGame ( N - i , 0 , dp )
            else :
                ans &= divisorGame ( N - i , 1 , dp )
    dp [ N ] [ A ] = ans
    return dp [ N ] [ A ]
if __name__ == ' _ _ main _ _ ' :
    N = 3
    dp = [ [ - 1 for i in range ( 2 ) ] for j in range ( N + 1 ) ]
    if ( divisorGame ( N , 1 , dp ) == True ) :
        print ( " Player ▁ A ▁ wins " )
    else :
        print ( " Player ▁ B ▁ wins " )

Index/key variables: A, N
Accumulator variables: ans
Iterator variables: i
Python program:
MAX = 100
def countMountains ( a , n ) :
    A = [ [ 0 for i in range ( n + 2 ) ] for i in range ( n + 2 ) ]
    count = 0
    for i in range ( n + 2 ) :
        for j in range ( n + 2 ) :
            if ( ( i == 0 ) or ( j == 0 ) or ( i == n + 1 ) or ( j == n + 1 ) ) :
                A [ i ] [ j ] = float ( ' - inf ' )
            else :
                A [ i ] [ j ] = a [ i - 1 ] [ j - 1 ]
    for i in range ( n + 1 ) :
        for j in range ( n + 1 ) :
            if ( ( A [ i ] [ j ] > A [ i - 1 ] [ j ] ) and ( A [ i ] [ j ] > A [ i + 1 ] [ j ] ) and ( A [ i ] [ j ] > A [ i ] [ j - 1 ] ) and ( A [ i ] [ j ] > A [ i ] [ j + 1 ] ) and ( A [ i ] [ j ] > A [ i - 1 ] [ j - 1 ] ) and ( A [ i ] [ j ] > A [ i + 1 ] [ j + 1 ] ) and ( A [ i ] [ j ] > A [ i - 1 ] [ j + 1 ] ) and ( A [ i ] [ j ] > A [ i + 1 ] [ j - 1 ] ) ) :
                count = count + 1
    return count
a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
n = 3
print ( countMountains ( a , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import numpy as np
def minimalLength ( a , n ) :
    dp = np . ones ( ( n + 1 , n + 1 ) ) * - 1 ;
    dp1 = [ 0 ] * n ;
    for size in range ( 1 , n + 1 ) :
        for i in range ( n - size + 1 ) :
            j = i + size - 1 ;
            if ( i == j ) :
                dp [ i ] [ j ] = a [ i ] ;
            else :
                for k in range ( i , j ) :
                    if ( dp [ i ] [ k ] != - 1 and dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) :
                        dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ;
    for i in range ( n ) :
        dp1 [ i ] = int ( 1e7 ) ;
    for i in range ( n ) :
        for j in range ( i + 1 ) :
            if ( dp [ j ] [ i ] != - 1 ) :
                if ( j == 0 ) :
                    dp1 [ i ] = 1 ;
                else :
                    dp1 [ i ] = min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ;
    return dp1 [ n - 1 ] ;
if __name__ == " _ _ main _ _ " :
    n = 7 ;
    a = [ 3 , 3 , 4 , 4 , 4 , 3 , 3 ] ;
    print ( minimalLength ( a , n ) ) ;

Index/key variables: i, j, k
Iterator variables: i, j, k, size
Python program:
dp = [ [ [ - 1 for x in range ( 100 ) ] for y in range ( 100 ) ] for z in range ( 100 ) ]
def MaximumScoreDP ( l , r , prefix_sum , num ) :
    if ( l > r ) :
        return 0
    if ( dp [ l ] [ r ] [ num ] != - 1 ) :
        return dp [ l ] [ r ] [ num ]
    current_sum = prefix_sum [ r ]
    if ( l - 1 >= 0 ) :
        current_sum -= prefix_sum [ l - 1 ]
    if ( num % 2 == 0 ) :
        current_sum *= - 1
    dp [ l ] [ r ] [ num ] = ( current_sum + max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) )
    return dp [ l ] [ r ] [ num ]
def findMaxScore ( a , n ) :
    prefix_sum = [ 0 ] * n
    prefix_sum [ 0 ] = a [ 0 ]
    for i in range ( 1 , n ) :
        prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ]
    global dp
    return MaximumScoreDP ( 0 , n - 1 , prefix_sum , 1 )
if __name__ == " _ _ main _ _ " :
    n = 6
    A = [ 1 , 2 , 3 , 4 , 2 , 6 ]
    print ( findMaxScore ( A , n ) )

Index/key variables: i, l, num, r
Iterator variables: i
Python program:
memo = [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for k in range ( 32 ) ]
def dp ( pos , fl , pr , bin ) :
    if ( pos == len ( bin ) ) :
        return 1 ;
    if ( memo [ pos ] [ fl ] [ pr ] != - 1 ) :
        return memo [ pos ] [ fl ] [ pr ] ;
    val = 0
    if ( bin [ pos ] == '0' ) :
        val = val + dp ( pos + 1 , fl , 0 , bin )
    elif ( bin [ pos ] == '1' ) :
        val = val + dp ( pos + 1 , 1 , 0 , bin )
    if ( pr == 0 ) :
        if ( fl == 1 ) :
            val += dp ( pos + 1 , fl , 1 , bin )
        elif ( bin [ pos ] == '1' ) :
            val += dp ( pos + 1 , fl , 1 , bin )
    memo [ pos ] [ fl ] [ pr ] = val
    return val
def findIntegers ( num ) :
    bin = " "
    while ( num > 0 ) :
        if ( num % 2 ) :
            bin += "1"
        else :
            bin += "0"
        num //= 2
    bin = bin [ : : - 1 ] ;
    return dp ( 0 , 0 , 0 , bin )
if __name__ == " _ _ main _ _ " :
    N = 12
    print ( findIntegers ( N ) )

Index/key variables: fl, pos, pr
Accumulator variables: bin, num
Python program:
def TotalWays ( n , s , k ) :
    dp = [ 0 ] * n
    dp [ s - 1 ] = 1
    for i in range ( s , n ) :
        idx = max ( s - 1 , i - k )
        for j in range ( idx , i ) :
            dp [ i ] += dp [ j ]
    return dp [ n - 1 ]
if __name__ == " _ _ main _ _ " :
    n = 5
    k = 2
    s = 2
    print ( " Total ▁ Ways ▁ = ▁ " , TotalWays ( n , s , k ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def max_sum ( arr , n , k ) :
    dp = [ 0 ] * n ;
    dp [ 0 ] = max ( 0 , arr [ 0 ] ) ;
    i = 1 ;
    while ( i < k ) :
        dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] ) ;
        i += 1 ;
    i = k ;
    while ( i < n ) :
        dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ;
        i += 1 ;
    return dp [ n - 1 ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , - 2 , 4 , 3 , 1 ] ;
    n = len ( arr )
    k = 4 ;
    print ( max_sum ( arr , n , k ) ) ;

Index/key variables: i
Python program:
MOD = 1000000007
def countStr ( N ) :
    dp = [ [ 0 for i in range ( 3 ) ] for i in range ( N + 1 ) ]
    dp [ 1 ] [ 0 ] = 1
    dp [ 1 ] [ 1 ] = 1
    dp [ 1 ] [ 2 ] = 0
    for i in range ( 2 , N + 1 ) :
        dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD
        dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD
        dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD
    ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 8
    print ( countStr ( N ) )

Index/key variables: N, i
Iterator variables: i
Python program:
def CountOfOddSum ( a , n ) :
    dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ]
    cnt = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ]
    for i in range ( n ) :
        for j in range ( 2 ) :
            if ( a [ i ] [ j ] % 2 == 0 ) :
                cnt [ i ] [ 0 ] += 1
            else :
                cnt [ i ] [ 1 ] += 1
    dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ]
    dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ]
    for i in range ( 1 , n ) :
        dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] )
        dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] )
    return dp [ n - 1 ] [ 1 ]
a = [ [ 1 , 2 ] , [ 3 , 6 ] ]
n = len ( a )
ans = CountOfOddSum ( a , n )
print ( ans )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def LongestConsSeq ( arr , N ) :
    maxval = 1
    for i in range ( N ) :
        maxval = max ( maxval , arr [ i ] )
    dp = [ [ 0 for i in range ( 2 ) ] for i in range ( maxval + 1 ) ]
    ans = 1
    for i in range ( N ) :
        dp [ arr [ i ] ] [ 0 ] = 1 + dp [ arr [ i ] - 1 ] [ 0 ]
        if ( arr [ i ] >= 2 ) :
            dp [ arr [ i ] ] [ 1 ] = max ( 1 + dp [ arr [ i ] - 1 ] [ 1 ] , 2 + dp [ arr [ i ] - 2 ] [ 0 ] )
        else :
            dp [ arr [ i ] ] [ 1 ] = 1
        ans = max ( ans , dp [ arr [ i ] ] [ 1 ] )
    return ans
arr = [ 2 , 1 , 4 , 5 ]
N = len ( arr )
print ( LongestConsSeq ( arr , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def isStepNum ( n ) :
    prevDigit = - 1
    while ( n ) :
        curDigit = n % 10
        if ( prevDigit == - 1 ) :
            prevDigit = curDigit
        else :
            if ( abs ( prevDigit - curDigit ) != 1 ) :
                return False
        prevDigit = curDigit
        n //= 10
    return True
def displaySteppingNumbers ( n , m ) :
    for i in range ( n , m + 1 ) :
        if ( isStepNum ( i ) ) :
            print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n , m = 0 , 21
    displaySteppingNumbers ( n , m )

Accumulator variables: n
Iterator variables: i