dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def maxProductPath ( grid ) :
    n , m = len ( grid ) , len ( grid [ 0 ] )
    maxPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ]
    minPath = [ [ 0 for i in range ( m ) ] for j in range ( n ) ]
    for i in range ( n ) :
        for j in range ( m ) :
            mn = float ( " inf " )
            mx = float ( " - inf " )
            if ( i == 0 and j == 0 ) :
                mx = grid [ i ] [ j ]
                mn = grid [ i ] [ j ]
            if i > 0 :
                tempmx = max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) )
                mx = max ( mx , tempmx )
                tempmn = min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) )
                mn = min ( mn , tempmn )
            if ( j > 0 ) :
                tempmx = max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) )
                mx = max ( mx , tempmx )
                tempmn = min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) )
                mn = min ( mn , tempmn )
            maxPath [ i ] [ j ] = mx
            minPath [ i ] [ j ] = mn
    if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) :
        return - 1
    else :
        return ( maxPath [ n - 1 ] [ m - 1 ] )
mat = [ [ 1 , - 2 , 1 ] , [ 1 , - 2 , 1 ] , [ 3 , - 4 , 1 ] ]
print ( maxProductPath ( mat ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import sys
def removeSmallestSubarray ( arr , S , n , k ) :
    target_remainder = S % k
    map1 = { }
    map1 [ 0 ] = - 1
    curr_remainder = 0
    res = sys . maxsize
    for i in range ( n ) :
        curr_remainder = ( curr_remainder + arr [ i ] + k ) % k
        map1 [ curr_remainder ] = i
        mod = ( curr_remainder - target_remainder + k ) % k
        if ( mod in map1 ) :
            res = min ( res , i - map1 [ mod ] )
    if ( res == sys . maxsize or res == n ) :
        res = - 1
    return res
def smstSubmatDeleted ( mat , N , M , K ) :
    S = 0
    for i in range ( N ) :
        for j in range ( M ) :
            S += mat [ i ] [ j ]
    min_area = N * M
    left = 0
    right = 0
    width = 0
    area = 0
    prefixRowSm = [ 0 ] * N
    for left in range ( M ) :
        prefixRowSum = [ 0 ] * N
        for right in range ( left , M ) :
            for i in range ( N ) :
                prefixRowSum [ i ] += mat [ i ] [ right ]
            width = removeSmallestSubarray ( prefixRowSum , S , N , K )
            if ( width != - 1 ) :
                area = ( right - left + 1 ) * ( width )
                if ( area < min_area ) :
                    min_area = area
    return min_area
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 6 , 2 , 6 ] , [ 3 , 2 , 8 ] , [ 2 , 5 , 3 ] ]
    K = 3
    N = len ( mat )
    M = len ( mat [ 0 ] )
    print ( smstSubmatDeleted ( mat , N , M , K ) )

Index/key variables: curr_remainder, i, j, mod, right
Accumulator variables: S
Iterator variables: i, j, left, right
Python program:
mod = int ( 1e9 + 7 )
def findTotalPath ( X , n , dp ) :
    if ( X == 0 ) :
        return 1
    ans = 0
    if ( dp [ X ] != - 1 ) :
        return dp [ X ]
    for i in range ( 1 , min ( X , n ) + 1 ) :
        ans = ans + findTotalPath ( X - i , n , dp ) % mod ;
        ans %= mod ;
    dp [ X ] = ans
    return ans
if __name__ == ' _ _ main _ _ ' :
    n = 3
    X = 2
    dp = [ - 1 ] * ( X + 1 )
    print ( findTotalPath ( X , n , dp ) )

Index/key variables: X
Accumulator variables: ans
Iterator variables: i
Python program:
bin = [ [ 0 for i in range ( 3000 ) ] for i in range ( 3000 ) ]
def countWays ( arr ) :
    mod = 10 ** 9 + 7
    bin [ 0 ] [ 0 ] = 1
    for i in range ( 1 , 3000 ) :
        bin [ i ] [ 0 ] = 1
        for j in range ( 1 , i + 1 ) :
            bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod
    n = 0
    for x in arr :
        n += x
    ways = [ 0 ] * ( n + 1 )
    for i in range ( 1 , n + 1 ) :
        ways [ i ] = 1
        for j in range ( len ( arr ) ) :
            ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod
        for j in range ( 1 , i ) :
            ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod
    ans = 0
    for x in ways :
        ans = ( ans + x ) % mod
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 1 ]
    countWays ( arr )

Index/key variables: i, j
Accumulator variables: n
Iterator variables: i, j, x
Python program:
MAX = 100005
def SieveOfEratosthenes ( ) :
    seive = [ True for i in range ( MAX ) ]
    for p in range ( 2 , MAX ) :
        if p * p > MAX :
            break
        if ( seive [ p ] == True ) :
            for i in range ( p * p , MAX , p ) :
                seive [ i ] = False
    v = [ ]
    for p in range ( 2 , MAX ) :
        if ( seive [ p ] ) :
            v . append ( p )
    return v
def build ( dp , arr , N ) :
    dp [ 0 ] = 0
    dp [ 1 ] = 0
    prime = SieveOfEratosthenes ( )
    pref = [ 0 for i in range ( N + 1 ) ]
    pref [ 0 ] = 0
    for i in range ( 1 , N + 1 ) :
        pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ]
    for i in range ( 2 , N + 1 ) :
        dp [ i ] = dp [ i - 1 ]
        for j in range ( len ( prime ) + 1 ) :
            r = i - 1
            l = r - prime [ j ] + 1
            if ( l < 0 ) :
                break
            temp = 0
            temp = pref [ r + 1 ] - pref [ l ]
            if ( l - 2 >= 0 ) :
                temp += dp [ l - 2 + 1 ]
            dp [ i ] = max ( dp [ i ] , temp )
def maxSumSubseq ( arr , N ) :
    dp = [ 0 for i in range ( N + 1 ) ]
    build ( dp , arr , N )
    print ( dp [ N ] )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 10 , 10 , 7 , 10 , 10 , 10 ]
    N = len ( arr )
    maxSumSubseq ( arr , N )

Index/key variables: N, i, j, l, p
Accumulator variables: temp, v
Iterator variables: i, j, p
Python program:
def maxScore ( arr ) :
    N = len ( arr )
    N += 1
    dp = [ [ - 100000 for i in range ( 2 * N ) ] for i in range ( N + 1 ) ]
    dp [ 0 ] [ 0 ] = 0
    for i in range ( 1 , N + 1 ) :
        for s in range ( 1 , 2 * ( N - 1 ) + 1 ) :
            j = 1
            while j <= N - 1 and j <= s :
                dp [ i ] [ s ] = max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] )
                j += 1
    return dp [ N ] [ 2 * ( N - 1 ) ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 0 ]
    print ( maxScore ( arr ) )

Index/key variables: N, i, s
Accumulator variables: j
Iterator variables: i, s
Python program:
def minimumCost ( cost , n , x ) :
    dp = [ 0 ] * ( n + 2 )
    dp [ 0 ] = cost [ 0 ]
    for i in range ( 1 , n ) :
        if ( i == 1 ) :
            dp [ i ] = cost [ i ] + dp [ i - 1 ]
        if ( i == 2 ) :
            dp [ i ] = cost [ i ] + min ( dp [ i - 1 ] , x + dp [ i - 2 ] )
        if ( i >= 3 ) :
            dp [ i ] = ( cost [ i ] + min ( dp [ i - 1 ] , min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) )
    print ( dp [ n - 1 ] )
if __name__ == ' _ _ main _ _ ' :
    X = 4
    cost = [ 6 , 3 , 9 , 2 , 1 , 3 ]
    N = len ( cost )
    minimumCost ( cost , N , X )

Index/key variables: i
Iterator variables: i
Python program:
def checkStr ( str , P , Q ) :
    N = len ( str )
    prev = str [ 0 ]
    cnt = 0
    for i in range ( N ) :
        if ( str [ i ] == prev ) :
            cnt += 1
        else :
            if ( prev == '1' and cnt >= Q ) :
                return False
            if ( prev == '0' and cnt >= P ) :
                return False
            cnt = 1
        prev = str [ i ]
    if ( prev == '1' and cnt >= Q ) :
        return False
    if ( prev == '0' and cnt >= P ) :
        return False
    return True
def cntBinStr ( str , N , P , Q ) :
    lenn = len ( str )
    if ( lenn == N ) :
        if ( checkStr ( str , P , Q ) ) :
            return 1
        return 0
    X = cntBinStr ( str + '0' , N , P , Q )
    Y = cntBinStr ( str + '1' , N , P , Q )
    return X + Y
if __name__ == ' _ _ main _ _ ' :
    N = 5
    P = 2
    Q = 3
    print ( cntBinStr ( " " , N , P , Q ) )

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i
Python program:
def binCoff ( N , R ) :
    res = 1
    if ( R > ( N - R ) ) :
        R = ( N - R )
    for i in range ( R ) :
        res *= ( N - i )
        res //= ( i + 1 )
    return res
def cntPermutation ( N ) :
    C_2N_N = binCoff ( 2 * N , N )
    cntPerm = C_2N_N // ( N + 1 )
    return cntPerm
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 ]
    N = len ( arr )
    print ( cntPermutation ( N // 2 ) )

Accumulator variables: res
Iterator variables: i
Python program:
def smallestPosition ( m , k ) :
    n = len ( m )
    mm = len ( m [ 1 ] )
    xor = [ [ 0 for i in range ( mm ) ] for j in range ( n ) ]
    minHeap = [ ]
    Map = { }
    for i in range ( n ) :
        for j in range ( mm ) :
            if i - 1 >= 0 :
                a = xor [ i - 1 ] [ j ]
            else :
                a = 0
            if j - 1 >= 0 :
                b = xor [ i ] [ j - 1 ]
            else :
                b = 0
            if i - 1 >= 0 and j - 1 >= 0 :
                c = xor [ i - 1 ] [ j - 1 ]
            else :
                c = 0
            xor [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c
            minHeap . append ( xor [ i ] [ j ] )
            minHeap . sort ( )
            if ( len ( minHeap ) > k ) :
                del minHeap [ 0 ]
            if xor [ i ] [ j ] not in Map :
                Map [ xor [ i ] [ j ] ] = [ i , j ]
    minHeap . sort ( )
    kth_max_e = minHeap [ 0 ]
    print ( ( Map [ kth_max_e ] [ 0 ] + 1 ) , ( Map [ kth_max_e ] [ 1 ] + 1 ) )
m = [ [ 1 , 2 , 3 ] , [ 2 , 2 , 1 ] , [ 2 , 4 , 2 ] ]
k = 1
smallestPosition ( m , k )

Index/key variables: i, j, kth_max_e
Accumulator variables: minHeap
Iterator variables: i, j
Python program:
def longestPalSubstr ( str ) :
    n = len ( str )
    maxLength = 1
    start = 0
    for i in range ( len ( str ) ) :
        for j in range ( i , len ( str ) , 1 ) :
            flag = 1
            for k in range ( ( j - i + 1 ) // 2 ) :
                if ( str [ i + k ] != str [ j - k ] ) :
                    flag = 0
            if ( flag != 0 and ( j - i + 1 ) > maxLength ) :
                start = i
                maxLength = j - i + 1
    return maxLength
str = " forgeeksskeegfor "
print ( longestPalSubstr ( str ) )

Iterator variables: i, j, k
Python program:
inf = 1000000008
def minSteps ( m , n ) :
    if ( n == m ) :
        return 0
    if ( m > n ) :
        return inf
    min_cost = inf
    for i in range ( 2 , m , 2 ) :
        if ( m % i == 0 ) :
            min_cost = min ( min_cost , m / i + minSteps ( m + i , n ) )
    return min_cost
if __name__ == ' _ _ main _ _ ' :
    M = 6
    N = 24
    minimum_cost = minSteps ( M , N )
    if minimum_cost == inf :
        minimum_cost = - 1
    print ( minimum_cost )

Iterator variables: i
Python program:
def manipulation ( matrix , q ) :
    x1 , y1 , x2 , y2 = q
    for i in range ( x1 - 1 , x2 ) :
        for j in range ( y1 - 1 , y2 ) :
            if matrix [ i ] [ j ] :
                matrix [ i ] [ j ] = 0
            else :
                matrix [ i ] [ j ] = 1
def queries_fxn ( matrix , queries ) :
    for q in queries :
        manipulation ( matrix , q )
matrix = [ [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ]
queries = [ [ 1 , 1 , 2 , 3 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 3 ] ]
queries_fxn ( matrix , queries )
print ( matrix )

Index/key variables: i, j
Iterator variables: i, j, q
Python program:
INF = int ( 1e7 ) ;
def min_op ( cur , M ) :
    if ( cur > M ) :
        return INF ;
    if ( cur == M ) :
        return 0 ;
    op = int ( INF ) ;
    for i in range ( 2 , int ( cur ** 1 / 2 ) + 1 ) :
        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 ) ) ;
    return op ;
def min_operations ( N , M ) :
    op = min_op ( N , M ) ;
    if ( op >= INF ) :
        print ( " - 1" ) ;
    else :
        print ( op ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 6 ;
    M = 24 ;
    min_operations ( N , M ) ;

Iterator variables: i
Python program:
def findPrimeFactors ( n ) :
    primeFactors = [ 0 for i in range ( 9 ) ]
    j = 0
    if ( n % 2 == 0 ) :
        primeFactors [ j ] = 2
        j += 1
        while ( n % 2 == 0 ) :
            n >>= 1
    i = 3
    while ( i * i <= n ) :
        if ( n % i == 0 ) :
            primeFactors [ j ] = i
            j += 1
            while ( n % i == 0 ) :
                n //= i
        i += 2
    if ( n > 2 ) :
        primeFactors [ j ] = n
        j += 1
    for i in range ( 0 , j + 1 ) :
        primeFactors [ i ] = 0
    return primeFactors
def findShortestSubsequence ( dp , a , index , primeFactors ) :
    n = len ( a )
    for j in range ( index , n ) :
        bitmask = 0
        for p in range ( len ( primeFactors ) ) :
            if ( primeFactors [ p ] != 0 and a [ j ] % primeFactors [ p ] == 0 ) :
                bitmask ^= ( 1 << p )
        for i in range ( len ( dp ) ) :
            if ( dp [ i ] == n + 1 ) :
                continue
            dp [ bitmask & i ] = min ( dp [ bitmask & i ] , dp [ i ] + 1 )
def printMinimumLength ( a ) :
    mn = len ( a ) + 1
    for i in range ( len ( a ) - 1 ) :
        primeFactors = findPrimeFactors ( a [ i ] )
        n = len ( primeFactors )
        dp = [ 0 for i in range ( 1 << n ) ]
        dp = [ len ( a ) + 1 for i in range ( len ( dp ) ) ]
        setBits = ( 1 << n ) - 1
        dp [ setBits ] = 1
        findShortestSubsequence ( dp , a , i + 1 , primeFactors )
        mn = min ( dp [ 0 ] , mn )
    if ( mn == len ( a ) + 1 ) :
        print ( - 1 )
    else :
        print ( mn )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 6 , 12 , 3 ]
    printMinimumLength ( arr )

Index/key variables: i, j, p, setBits
Accumulator variables: bitmask, n
Iterator variables: i, j, p
Python program:
def xorSum ( a , n , mask , dp ) :
    if ( dp [ mask ] != - 1 ) :
        return dp [ mask ]
    max_value = 0
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( i != j and ( mask & ( 1 << i ) ) == 0 and ( mask & ( 1 << j ) ) == 0 ) :
                max_value = max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) )
    dp [ mask ] = max_value
    return dp [ mask ]
n = 4
arr = [ 1 , 2 , 3 , 4 ]
dp = [ - 1 ] * ( ( 1 << n ) + 5 )
print ( xorSum ( arr , n , 0 , dp ) )

Index/key variables: i, j, mask
Iterator variables: i, j
Python program:
fib = [ 0 ] * 101
dp1 = [ 0 ] * 101
dp2 = [ 0 ] * 101
v = [ 0 ] * 101
def fibonacci ( ) :
    fib [ 1 ] = 1
    fib [ 2 ] = 2
    for i in range ( 3 , 87 + 1 ) :
        fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ]
def find ( num ) :
    cnt = 0
    for i in range ( 87 , 0 , - 1 ) :
        if ( num >= fib [ i ] ) :
            v [ cnt ] = i
            cnt += 1
            num -= fib [ i ]
    v [ : : - 1 ]
    dp1 [ 0 ] = 1
    dp2 [ 0 ] = ( v [ 0 ] - 1 ) // 2
    for i in range ( 1 , cnt ) :
        dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ]
        dp2 [ i ] = ( ( ( v [ i ] - v [ i - 1 ] ) // 2 ) * dp2 [ i - 1 ] + ( ( v [ i ] - v [ i - 1 ] - 1 ) // 2 ) * dp1 [ i - 1 ] )
    return dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ]
fibonacci ( )
num = 13
print ( find ( num ) )

Index/key variables: cnt, i
Accumulator variables: num
Iterator variables: i
Python program:
def countNums ( N ) :
    l = pow ( 10 , N - 1 )
    r = pow ( 10 , N ) - 1
    count = 0
    for i in range ( l , r + 1 ) :
        xorr = 0
        temp = i
        while ( temp > 0 ) :
            xorr = xorr ^ ( temp % 10 )
            temp //= 10
        if ( xorr <= 9 ) :
            count += 1
    print ( count )
N = 2
countNums ( N )

Accumulator variables: count, temp
Iterator variables: i
Python program:
import sys
def main ( arr , n ) :
    dp = [ [ 0 for x in range ( n ) ] for y in range ( 4 ) ]
    if arr [ 0 ] == 0 :
        dp [ 0 ] [ 0 ] = 1
    else :
        dp [ 1 ] [ 0 ] = 1
    for i in range ( 1 , n ) :
        if arr [ i ] == 0 :
            dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1
        else :
            dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ]
    for i in range ( 1 , n ) :
        if arr [ i ] == 1 :
            dp [ 1 ] [ i ] = max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 )
        else :
            dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ]
    for i in range ( 1 , n ) :
        if arr [ i ] == 0 :
            dp [ 2 ] [ i ] = max ( [ dp [ 2 ] [ i - 1 ] + 1 , dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ] )
        else :
            dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ]
    for i in range ( 1 , n ) :
        if arr [ i ] == 1 :
            dp [ 3 ] [ i ] = max ( [ dp [ 3 ] [ i - 1 ] + 1 , dp [ 2 ] [ i - 1 ] + 1 , dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ] )
        else :
            dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ]
    ans = max ( [ dp [ 2 ] [ n - 1 ] , dp [ 1 ] [ n - 1 ] , dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ] )
    print ( ans )
if __name__ == " _ _ main _ _ " :
    n = 4
    arr = [ 0 , 1 , 0 , 1 ]
    main ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
def SubarrayWithMaxSum ( nums ) :
    currMax = nums [ 0 ]
    globalMax = nums [ 0 ]
    for i in range ( 1 , len ( nums ) ) :
        currMax = max ( nums [ i ] , nums [ i ] + currMax )
        if ( currMax > globalMax ) :
            globalMax = currMax
            endIndex = i
    startIndex = endIndex
    while ( startIndex >= 0 ) :
        globalMax -= nums [ startIndex ]
        if ( globalMax == 0 ) :
            break
        startIndex -= 1
    for i in range ( startIndex , endIndex + 1 ) :
        print ( nums [ i ] , end = " ▁ " )
arr = [ - 2 , - 5 , 6 , - 2 , - 3 , 1 , 5 , - 6 ]
SubarrayWithMaxSum ( arr )

Index/key variables: i, startIndex
Accumulator variables: globalMax
Iterator variables: i
Python program:
M = 100005
primes = [ ]
def sieve ( ) :
    mark = [ False ] * M
    for i in range ( 2 , M ) :
        mark [ i ] = True
    i = 2
    while i * i < M :
        if ( mark [ i ] ) :
            j = i * i
            while j < M :
                mark [ j ] = False
                j += i
        i += 1
    for i in range ( 2 , M ) :
        if ( mark [ i ] ) :
            primes . append ( i )
def countPrimonacci ( l , r ) :
    dp = [ ]
    dp . append ( 1 )
    dp . append ( 1 )
    i = 2
    s = set ( )
    while ( True ) :
        x = 0
        for j in range ( len ( primes ) ) :
            p = primes [ j ]
            if ( p >= i ) :
                break
            x += dp [ i - p ]
        if ( x >= l and x <= r ) :
            s . add ( x )
        if ( x > r ) :
            break
        dp . append ( x )
        i += 1
    print ( len ( s ) )
if __name__ == ' _ _ main _ _ ' :
    sieve ( )
    L , R = 1 , 10
    countPrimonacci ( L , R )

Index/key variables: i, j
Accumulator variables: dp, primes, s, x
Iterator variables: i, j
Python program:
N = int ( 1e5 + 5 )
MOD = 1000000007
dp = [ 0 ] * N
def countStrings ( K , Q ) :
    dp [ 0 ] = 1
    for i in range ( 1 , N ) :
        dp [ i ] = dp [ i - 1 ]
        if ( i >= K ) :
            dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD
    for i in range ( 1 , N ) :
        dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD
    for i in range ( len ( Q ) ) :
        ans = dp [ Q [ i ] [ 1 ] ] - dp [ Q [ i ] [ 0 ] - 1 ]
        if ( ans < 0 ) :
            ans += MOD
        print ( ans )
K = 3
Q = [ [ 1 , 4 ] , [ 3 , 7 ] ]
countStrings ( K , Q )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
import sys ;
def minSum ( n , num , price ) :
    dp = [ 0 for i in range ( n ) ]
    for i in range ( n ) :
        dp [ i ] = sys . maxsize
    ans = sys . maxsize
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( num [ j ] > num [ i ] ) :
                dp [ j ] = min ( dp [ j ] , price [ i ] + price [ j ] )
                ans = min ( ans , dp [ i ] + price [ j ] )
    if ans is not sys . maxsize :
        return ans
    else :
        return - 1
if __name__ == ' _ _ main _ _ ' :
    num = [ 2 , 4 , 6 , 7 , 8 ]
    price = [ 10 , 20 , 100 , 20 , 40 ]
    n = len ( price )
    print ( minSum ( n , num , price ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def binomialCoeff ( n , k ) :
    res = 1
    if ( k > n - k ) :
        k = n - k
    for i in range ( k ) :
        res *= ( n - i )
        res //= ( i + 1 )
    return res
def countStrings ( N ) :
    if ( N % 2 == 1 ) :
        return 0
    else :
        N //= 2
        c = binomialCoeff ( 2 * N , N )
        return c // ( N + 1 )
if __name__ == ' _ _ main _ _ ' :
    N = 6
    print ( countStrings ( N ) )

Accumulator variables: res
Iterator variables: i
Python program:
def solve ( n , k , mod , dp ) :
    if ( n < 0 ) :
        return 0
    if ( n == 0 ) :
        return 1
    if ( dp [ n ] != - 1 ) :
        return dp [ n ]
    cnt = 0
    for i in range ( 2 , k + 1 , 2 ) :
        cnt = ( ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod )
    dp [ n ] = cnt
    return int ( cnt )
if __name__ == ' _ _ main _ _ ' :
    mod = 1e9 + 7
    n = 4
    k = 2
    dp = [ - 1 ] * ( n + 1 )
    ans = solve ( n , k , mod , dp )
    print ( ans )

Index/key variables: n
Iterator variables: i
Python program:
f = [ 0 ] * 12
def fact ( ) :
    f [ 0 ] = f [ 1 ] = 1
    for i in range ( 2 , 11 ) :
        f [ i ] = i * 1 * f [ i - 1 ]
def solve ( arr , n , k ) :
    fact ( )
    even = 0
    for i in range ( n ) :
        if ( arr [ i ] % 2 == 0 ) :
            even += 1
    if ( k > even ) :
        print ( 0 )
    else :
        print ( f [ even ] // ( f [ k ] * f [ even - k ] ) )
arr = [ 1 , 2 , 3 , 4 ]
n = len ( arr )
k = 1
solve ( arr , n , k )

Index/key variables: even, i, k
Iterator variables: i
Python program:
MOD = 1000000007
def nCrModp ( n , r ) :
    if ( n % 2 == 1 ) :
        return - 1
    C = [ 0 ] * ( r + 1 )
    C [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        for j in range ( min ( i , r ) , 0 , - 1 ) :
            C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD
    return C [ r ]
N = 6
print ( nCrModp ( N , N // 2 ) )

Index/key variables: j, r
Iterator variables: i, j
Python program:
def printMinOperations ( N ) :
    dp = [ N ] * ( N + 1 )
    dp [ 1 ] = 0
    for i in range ( 2 , N + 1 ) :
        if ( i % 2 == 0 and dp [ i ] > dp [ i // 2 ] + 1 ) :
            dp [ i ] = dp [ i // 2 ] + 1
        if ( i % 3 == 0 and dp [ i ] > dp [ i // 3 ] + 1 ) :
            dp [ i ] = dp [ i // 3 ] + 1
        if ( dp [ i ] > dp [ i - 1 ] + 1 ) :
            dp [ i ] = dp [ i - 1 ] + 1
    print ( dp [ N ] , end = " " )
    seq = [ ]
    while ( N > 1 ) :
        seq . append ( N )
        if dp [ N - 1 ] == dp [ N ] - 1 :
            N = N - 1
        elif N % 2 == 0 and dp [ N // 2 ] == dp [ N ] - 1 :
            N = N // 2
        else :
            N = N // 3
    seq . append ( 1 )
    for i in reversed ( seq ) :
        print ( i , end = " ▁ " )
X = 96234
printMinOperations ( X )

Index/key variables: N, i
Accumulator variables: seq
Iterator variables: i
Python program:
from math import *
def maxLength ( arr , n ) :
    dp = [ 1 ] * n
    for i in range ( n - 1 , 1 , - 1 ) :
        for j in range ( 1 , int ( sqrt ( i ) ) + 1 ) :
            if ( i % j == 0 ) :
                s = i // j
                if ( s == j ) :
                    if ( arr [ i ] > arr [ s ] ) :
                        dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] )
                else :
                    if ( s != i and arr [ i ] > arr [ s ] ) :
                        dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] )
                    if ( arr [ i ] > arr [ j ] ) :
                        dp [ j ] = max ( dp [ i ] + 1 , dp [ j ] )
    Max = 0
    for i in range ( 1 , n ) :
        if ( dp [ i ] > Max ) :
            Max = dp [ i ]
    print ( Max )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ]
    size = len ( arr )
    maxLength ( arr , size )

Index/key variables: i, j, s
Iterator variables: i, j