dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def smallestSumSubarr ( arr , n ) :
    for i in range ( n ) :
        arr [ i ] = - arr [ i ]
    sum_here = arr [ 0 ]
    max_sum = arr [ 0 ]
    for i in range ( 1 , n ) :
        sum_here = max ( sum_here + arr [ i ] , arr [ i ] )
        max_sum = max ( max_sum , sum_here )
    return ( - 1 ) * max_sum
arr = [ 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 ]
n = len ( arr )
print ( " Smallest ▁ sum : " , smallestSumSubarr ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def printLargest ( a , n ) :
    max = - 1
    ind = - 1
    for i in range ( 0 , n ) :
        num = a [ i ]
        while ( num ) :
            r = num % 10 ;
            num = num / 10 ;
            if ( num == 0 ) :
                if ( max < r ) :
                    max = r
                    ind = i ;
    for i in range ( ind , n ) :
        print ( a [ i ] , end = ' ' ) ,
    for i in range ( 0 , ind ) :
        print ( a [ i ] , end = ' ' )
if __name__ == " _ _ main _ _ " :
    a = [ 54 , 546 , 548 , 60 ]
    n = len ( a )
    printLargest ( a , n )

Index/key variables: i
Iterator variables: i
Python program:
def MinOperation ( a , n , k ) :
    result = 0
    for i in range ( n ) :
        if ( a [ i ] != 1 and a [ i ] > k ) :
            result = ( result + min ( a [ i ] % k , k - a [ i ] % k ) )
        else :
            result = result + k - a [ i ]
    return result
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 5 , 6 ]
    n = len ( arr )
    k = 5
    print ( MinOperation ( arr , n , k ) )

Index/key variables: i
Iterator variables: i
Python program:
def maxProductSubset ( a , n ) :
    if n == 1 :
        return a [ 0 ]
    max_neg = - 999999999999
    count_neg = 0
    count_zero = 0
    prod = 1
    for i in range ( n ) :
        if a [ i ] == 0 :
            count_zero += 1
            continue
        if a [ i ] < 0 :
            count_neg += 1
            max_neg = max ( max_neg , a [ i ] )
        prod = prod * a [ i ]
    if count_zero == n :
        return 0
    if count_neg & 1 :
        if ( count_neg == 1 and count_zero > 0 and count_zero + count_neg == n ) :
            return 0
        prod = int ( prod / max_neg )
    return prod
if __name__ == ' _ _ main _ _ ' :
    a = [ - 1 , - 1 , - 2 , 4 , 3 ]
    n = len ( a )
    print ( maxProductSubset ( a , n ) )

Index/key variables: i
Accumulator variables: count_neg, count_zero
Iterator variables: i
Python program:
def find ( arr , pos , m , isRunning ) :
    d = [ 0 ] * ( m + 1 )
    for i in range ( m - 1 , pos , - 1 ) :
        if isRunning [ arr [ i ] ] :
            d [ arr [ i ] ] = i
    maxipos = 0
    for ele in d :
        maxipos = max ( ele , maxipos )
    return maxipos
def mincost ( n , m , arr ) :
    freqarr = [ [ ] for i in range ( m ) ]
    newvec = [ 0 ] * ( m + 1 )
    freqarr [ m - 1 ] = newvec [ : ]
    for i in range ( m - 2 , - 1 , - 1 ) :
        nv = freqarr [ i + 1 ] [ : ]
        nv [ arr [ i + 1 ] ] += 1
        freqarr [ i ] = nv [ : ]
    isRunning = [ False ] * ( m + 1 )
    cost = 0
    truecount = 0
    for i in range ( 0 , m ) :
        ele = arr [ i ]
        if isRunning [ ele ] == True :
            continue
        else :
            if truecount < n :
                cost += 1
                truecount += 1
                isRunning [ ele ] = True
            else :
                mini = 100000
                miniind = 0
                for j in range ( 1 , m + 1 ) :
                    if isRunning [ j ] and mini > freqarr [ i ] [ j ] :
                        mini = freqarr [ i ] [ j ]
                        miniind = j
                if mini == 0 :
                    isRunning [ miniind ] = False
                    isRunning [ ele ] = True
                    cost += 1
                else :
                    farpos = find ( arr , i , m , isRunning )
                    isRunning [ arr [ farpos ] ] = False
                    isRunning [ ele ] = True
                    cost += 1
    return cost
if __name__ == " _ _ main _ _ " :
    n1 , m1 = 3 , 6
    arr1 = [ 1 , 2 , 1 , 3 , 4 , 1 ]
    print ( mincost ( n1 , m1 , arr1 ) )
    n2 , m2 = 2 , 6
    arr2 = [ 1 , 2 , 1 , 3 , 2 , 1 ]
    print ( mincost ( n2 , m2 , arr2 ) )
    n3 , m3 = 3 , 31
    arr3 = [ 7 , 11 , 17 , 10 , 7 , 10 , 2 , 9 , 2 , 18 , 8 , 10 , 20 , 10 , 3 , 20 , 17 , 17 , 17 , 1 , 15 , 10 , 8 , 3 , 3 , 18 , 13 , 2 , 10 , 10 , 11 ]
    print ( mincost ( n3 , m3 , arr3 ) )

Index/key variables: ele, farpos, i, j, miniind
Accumulator variables: cost, truecount
Iterator variables: ele, i, j
Python program:
def calcFiboTerms ( fiboTerms , K ) :
    i = 3
    fiboTerms . append ( 0 )
    fiboTerms . append ( 1 )
    fiboTerms . append ( 1 )
    while True :
        nextTerm = ( fiboTerms [ i - 1 ] + fiboTerms [ i - 2 ] )
        if nextTerm > K :
            return
        fiboTerms . append ( nextTerm )
        i += 1
def findMinTerms ( K ) :
    fiboTerms = [ ]
    calcFiboTerms ( fiboTerms , K )
    count , j = 0 , len ( fiboTerms ) - 1
    while K > 0 :
        count += K // fiboTerms [ j ]
        K %= fiboTerms [ j ]
        j -= 1
    return count
if __name__ == " _ _ main _ _ " :
    K = 17
    print ( findMinTerms ( K ) )

Index/key variables: j
Accumulator variables: K, count, fiboTerms, i
Python program:
import math
def digitsNum ( N ) :
    if ( N == 0 ) :
        print ( "0" , end = " " )
    if ( N % 9 != 0 ) :
        print ( N % 9 , end = " " )
    for i in range ( 1 , int ( N / 9 ) + 1 ) :
        print ( "9" , end = " " )
    for i in range ( 1 , N + 1 ) :
        print ( "0" , end = " " )
    print ( )
N = 5
print ( " The ▁ number ▁ is ▁ : ▁ " , end = " " )
digitsNum ( N )

Iterator variables: i
Python program:
import math
def printVector ( v ) :
    print ( len ( v ) )
    for i in range ( 0 , len ( v ) ) :
        print ( v [ i ] , end = " ▁ " )
    print ( )
def findTwoGroup ( n ) :
    sum = n * ( n + 1 ) / 2
    group1Sum = sum / 2
    group1 = [ ]
    group2 = [ ]
    for i in range ( n , 0 , - 1 ) :
        if ( group1Sum - i >= 0 ) :
            group1 . append ( i )
            group1Sum -= i
        else :
            group2 . append ( i )
    printVector ( group1 )
    printVector ( group2 )
n = 5
findTwoGroup ( n )

Index/key variables: i
Accumulator variables: group1, group1Sum, group2
Iterator variables: i
Python program:
v = [ ]
def solve ( n , d , a , b , arr ) :
    first , second = 0 , 1
    for i in range ( n ) :
        m = arr [ i ] [ 0 ]
        t = arr [ i ] [ 1 ]
        v . append ( [ a * m + b * t , i + 1 ] )
    v . sort ( )
    ans = [ ]
    for i in range ( n ) :
        if v [ i ] [ first ] <= d :
            ans . append ( v [ i ] [ second ] )
            d -= v [ i ] [ first ]
    print ( len ( ans ) )
    for i in range ( len ( ans ) ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    d = 5
    a = 1
    b = 1
    arr = [ [ 2 , 0 ] , [ 3 , 2 ] , [ 4 , 4 ] , [ 10 , 0 ] , [ 0 , 1 ] ]
    solve ( n , d , a , b , arr )

Index/key variables: first, i, second
Accumulator variables: ans, d, v
Iterator variables: i
Python program:
def maxDifference ( arr , N , k ) :
    S = 0
    S1 = 0
    max_difference = 0
    for i in range ( N ) :
        S += arr [ i ]
    arr . sort ( reverse = True )
    M = max ( k , N - k )
    for i in range ( M ) :
        S1 += arr [ i ]
    max_difference = S1 - ( S - S1 )
    return max_difference
arr = [ 8 , 4 , 5 , 2 , 10 ]
N = len ( arr )
k = 2
print ( maxDifference ( arr , N , k ) )

Index/key variables: i
Accumulator variables: S, S1
Iterator variables: i
Python program:
def minproduct ( a , b , n , k ) :
    diff = 0
    res = 0
    for i in range ( n ) :
        pro = a [ i ] * b [ i ]
        res = res + pro
        if ( pro < 0 and b [ i ] < 0 ) :
            temp = ( a [ i ] + 2 * k ) * b [ i ]
        elif ( pro < 0 and a [ i ] < 0 ) :
            temp = ( a [ i ] - 2 * k ) * b [ i ]
        elif ( pro > 0 and a [ i ] < 0 ) :
            temp = ( a [ i ] + 2 * k ) * b [ i ]
        elif ( pro > 0 and a [ i ] > 0 ) :
            temp = ( a [ i ] - 2 * k ) * b [ i ]
        d = abs ( pro - temp )
        if ( d > diff ) :
            diff = d
    return res - diff
a = [ 2 , 3 , 4 , 5 , 4 ]
b = [ 3 , 4 , 2 , 3 , 2 ]
n = 5
k = 3
print ( minproduct ( a , b , n , k ) )

Index/key variables: i
Iterator variables: i
Python program:
def policeThief ( arr , n , k ) :
    i = 0
    l = 0
    r = 0
    res = 0
    thi = [ ]
    pol = [ ]
    while i < n :
        if arr [ i ] == ' P ' :
            pol . append ( i )
        elif arr [ i ] == ' T ' :
            thi . append ( i )
        i += 1
    while l < len ( thi ) and r < len ( pol ) :
        if ( abs ( thi [ l ] - pol [ r ] ) <= k ) :
            res += 1
            l += 1
            r += 1
        elif thi [ l ] < pol [ r ] :
            l += 1
        else :
            r += 1
    return res
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ ' P ' , ' T ' , ' T ' , ' P ' , ' T ' ]
    k = 2
    n = len ( arr1 )
    print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr1 , n , k ) ) ) )
    arr2 = [ ' T ' , ' T ' , ' P ' , ' P ' , ' T ' , ' P ' ]
    k = 2
    n = len ( arr2 )
    print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr2 , n , k ) ) ) )
    arr3 = [ ' P ' , ' T ' , ' P ' , ' T ' , ' T ' , ' P ' ]
    k = 3
    n = len ( arr3 )
    print ( ( " Maximum ▁ thieves ▁ caught : ▁ { } " . format ( policeThief ( arr3 , n , k ) ) ) )

Index/key variables: i, l, r
Accumulator variables: pol, res, thi
Python program:
def minRotation ( input , unlock_code ) :
    rotation = 0 ;
    while ( input > 0 or unlock_code > 0 ) :
        input_digit = input % 10 ;
        code_digit = unlock_code % 10 ;
        rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ;
        input = int ( input / 10 ) ;
        unlock_code = int ( unlock_code / 10 ) ;
    return rotation ;
input = 28756 ;
unlock_code = 98234 ;
print ( " Minimum ▁ Rotation ▁ = " , minRotation ( input , unlock_code ) ) ;

Accumulator variables: rotation
Python program:
def checkJobs ( startin , endin , n ) :
    a = [ ]
    for i in range ( 0 , n ) :
        a . append ( [ startin [ i ] , endin [ i ] ] )
    a . sort ( )
    tv1 = a [ 0 ] [ 1 ]
    tv2 = a [ 1 ] [ 1 ]
    for i in range ( 2 , n ) :
        if ( a [ i ] [ 0 ] >= tv1 ) :
            tv1 = tv2
            tv2 = a [ i ] [ 1 ]
        elif ( a [ i ] [ 0 ] >= tv2 ) :
            tv2 = a [ i ] [ 1 ]
        else :
            return 0
    return 1
if __name__ == ' _ _ main _ _ ' :
    n = 3
    print ( checkJobs ( startin , endin , n ) )

Index/key variables: i
Accumulator variables: a
Iterator variables: i
Python program:
import math as mt
def preprocess ( coin , n ) :
    coin . sort ( )
    for i in range ( 1 , n ) :
        coin [ i ] += coin [ i - 1 ]
def minCost ( coin , n , k ) :
    coins_needed = mt . ceil ( 1.0 * n / ( k + 1 ) )
    return coin [ coins_needed - 1 ]
coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ]
n = len ( coin )
preprocess ( coin , n )
k = 3
print ( minCost ( coin , n , k ) )
k = 7
print ( minCost ( coin , n , k ) )

Index/key variables: i
Iterator variables: i
Python program:
def worstFit ( blockSize , m , processSize , n ) :
    allocation = [ - 1 ] * n
    for i in range ( n ) :
        wstIdx = - 1
        for j in range ( m ) :
            if blockSize [ j ] >= processSize [ i ] :
                if wstIdx == - 1 :
                    wstIdx = j
                elif blockSize [ wstIdx ] < blockSize [ j ] :
                    wstIdx = j
        if wstIdx != - 1 :
            allocation [ i ] = wstIdx
            blockSize [ wstIdx ] -= processSize [ i ]
    print ( " Process ▁ No . ▁ Process ▁ Size ▁ Block ▁ no . " )
    for i in range ( n ) :
        print ( i + 1 , " ▁ " , processSize [ i ] , end = " ▁ " )
        if allocation [ i ] != - 1 :
            print ( allocation [ i ] + 1 )
        else :
            print ( " Not ▁ Allocated " )
if __name__ == ' _ _ main _ _ ' :
    blockSize = [ 100 , 500 , 200 , 300 , 600 ]
    processSize = [ 212 , 417 , 112 , 426 ]
    m = len ( blockSize )
    n = len ( processSize )
    worstFit ( blockSize , m , processSize , n )

Index/key variables: i, j, wstIdx
Iterator variables: i, j
Python program:
def maximumSum ( arr , n , k ) :
    for i in range ( 1 , k + 1 ) :
        min = + 2147483647
        index = - 1
        for j in range ( n ) :
            if ( arr [ j ] < min ) :
                min = arr [ j ]
                index = j
        if ( min == 0 ) :
            break
        arr [ index ] = - arr [ index ]
    sum = 0
    for i in range ( n ) :
        sum += arr [ i ]
    return sum
arr = [ - 2 , 0 , 5 , - 1 , 2 ]
k = 4
n = len ( arr )
print ( maximumSum ( arr , n , k ) )

Index/key variables: i, index, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def sol ( arr , k ) :
    arr . sort ( )
    Sum = 0
    i = 0
    while ( k > 0 ) :
        if ( arr [ i ] >= 0 ) :
            k = 0
        else :
            arr [ i ] = ( - 1 ) * arr [ i ]
            k = k - 1
        i += 1
    for j in range ( len ( arr ) ) :
        Sum += arr [ j ]
    return Sum
arr = [ - 2 , 0 , 5 , - 1 , 2 ]
print ( sol ( arr , 4 ) )

Index/key variables: i, j
Accumulator variables: Sum
Iterator variables: j
Python program:
def sumArray ( arr , n ) :
    sum = 0
    for i in range ( n ) :
        sum += arr [ i ]
    return sum
def maximizeSum ( arr , n , k ) :
    arr . sort ( )
    i = 0
    for i in range ( n ) :
        if ( k and arr [ i ] < 0 ) :
            arr [ i ] *= - 1
            k -= 1
            continue
        break
    if ( i == n ) :
        i -= 1
    if ( k == 0 or k % 2 == 0 ) :
        return sumArray ( arr , n )
    if ( i != 0 and abs ( arr [ i ] ) >= abs ( arr [ i - 1 ] ) ) :
        i -= 1
    arr [ i ] *= - 1
    return sumArray ( arr , n )
n = 5
k = 4
arr = [ - 3 , - 2 , - 1 , 5 , 6 ]
print ( maximizeSum ( arr , n , k ) )

Index/key variables: i
Accumulator variables: k, sum
Iterator variables: i
Python program:
def firstFit ( weight , n , c ) :
    res = 0
    bin_rem = [ 0 ] * n
    for i in range ( n ) :
        j = 0
        while ( j < res ) :
            if ( bin_rem [ j ] >= weight [ i ] ) :
                bin_rem [ j ] = bin_rem [ j ] - weight [ i ]
                break
            j += 1
        if ( j == res ) :
            bin_rem [ res ] = c - weight [ i ]
            res = res + 1
    return res
weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ]
c = 10
n = len ( weight )
print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " , firstFit ( weight , n , c ) )

Index/key variables: i, j, res
Iterator variables: i
Python program:
import math as Math
def getMinOperations ( A , B , prevA , prevB , dp ) :
    if ( A == prevA and B == prevB ) :
        return 10 ** 9 ;
    if ( A == 0 and B == 0 ) :
        return 0 ;
    if ( dp [ A ] [ B ] != - 1 ) :
        return dp [ A ] [ B ] ;
    ans1 = getMinOperations ( A // 2 , B , A , B , dp ) ;
    if ( ans1 != 10 ** 9 ) :
        ans1 += 1 ;
    ans2 = getMinOperations ( A , B // 2 , A , B , dp ) ;
    if ( ans2 != 10 ** 9 ) :
        ans2 += 1 ;
    ans3 = getMinOperations ( Math . floor ( Math . sqrt ( A * B ) ) , Math . floor ( Math . sqrt ( A * B ) ) , A , B , dp ) ;
    if ( ans3 != 10 ** 9 ) :
        ans3 += 2 ;
    dp [ A ] [ B ] = min ( ans1 , min ( ans2 , ans3 ) )
    return dp [ A ] [ B ] ;
A = 53
B = 16
mx = max ( A , B ) ;
dp = [ [ - 1 for i in range ( mx + 1 ) ] for i in range ( mx + 1 ) ]
print ( getMinOperations ( A , B , - 1 , - 1 , dp ) ) ;

Index/key variables: A, B
Python program:
def maxScoreSum ( a , b , row , mask , dp ) :
    if ( row >= len ( a ) ) :
        return 0
    if ( dp [ mask ] != - 1 ) :
        return dp [ mask ]
    ans = 0
    for i in range ( len ( a ) ) :
        if ( mask & ( 1 << i ) ) :
            newMask = mask ^ ( 1 << i )
            curSum = 0
            for j in range ( len ( a [ i ] ) ) :
                if ( a [ row ] [ j ] == b [ i ] [ j ] ) :
                    curSum += 1
            ans = max ( ans , curSum + maxScoreSum ( a , b , row + 1 , newMask , dp ) )
    dp [ mask ] = ans
    return dp [ mask ]
def maxScoreSumUtil ( a , b , N , M ) :
    row = 0
    mask = pow ( 2 , M ) - 1
    dp = [ - 1 ] * ( mask + 1 )
    return maxScoreSum ( a , b , row , mask , dp )
if __name__ == " _ _ main _ _ " :
    N = 3
    M = 3
    a = [ [ 1 , 1 , 0 ] , [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] ]
    b = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 1 , 1 , 0 ] ]
    print ( maxScoreSumUtil ( a , b , N , M ) )

Index/key variables: i, j, mask, row
Accumulator variables: curSum
Iterator variables: i, j
Python program:
def maxDiffSum ( arr , n ) :
    dp = [ 0 ] * n
    for i in range ( n ) :
        maxVal = arr [ i ]
        minVal = arr [ i ]
        for j in range ( i , - 1 , - 1 ) :
            minVal = min ( minVal , arr [ j ] )
            maxVal = max ( maxVal , arr [ j ] )
            if ( j - 1 >= 0 ) :
                dp [ i ] = max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] )
            else :
                dp [ i ] = max ( dp [ i ] , maxVal - minVal )
    return dp [ n - 1 ]
arr = [ 8 , 1 , 7 , 9 , 2 ]
N = len ( arr )
print ( maxDiffSum ( arr , N ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
MOD = 1000000007
from math import sqrt
def modPow ( x , y ) :
    r = 1
    a = x
    while ( y > 0 ) :
        if ( ( y & 1 ) == 1 ) :
            r = ( r * a ) % MOD
        a = ( a * a ) % MOD
        y /= 2
    return r
def modInverse ( x ) :
    return modPow ( x , MOD - 2 )
def modDivision ( p , q ) :
    return ( p * modInverse ( q ) ) % MOD
def C ( n , k ) :
    if ( k > n ) :
        return 0
    p = 1
    q = 1
    for i in range ( 1 , k + 1 , 1 ) :
        q = ( q * i ) % MOD
        p = ( p * ( n - i + 1 ) ) % MOD
    return modDivision ( p , q )
def countArrays ( N , K ) :
    res = 1
    for p in range ( 2 , int ( sqrt ( K ) ) , 1 ) :
        c = 0
        while ( K % p == 0 ) :
            K /= p
            c += 1
        res = ( res * C ( N - 1 + c , c ) ) % MOD
    if ( N > 1 ) :
        res = ( res * N ) % MOD
    return res
if __name__ == ' _ _ main _ _ ' :
    N = 3
    K = 5
    print ( countArrays ( N , K ) )

Accumulator variables: K, c, y
Iterator variables: i, p
Python program:
import sys
def minSessions ( codeTime , dp , ones , n , mask , currTime , WorkingSessionTime ) :
    if ( currTime > WorkingSessionTime ) :
        return sys . maxsize
    if ( mask == ones ) :
        return 1
    if ( dp [ mask ] [ currTime ] != - 1 ) :
        return dp [ mask ] [ currTime ]
    ans = sys . maxsize
    for i in range ( n ) :
        if ( ( mask & ( 1 << i ) ) == 0 ) :
            inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime )
            inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime )
            ans = min ( [ ans , inc , inc_next ] )
    dp [ mask ] [ currTime ] = ans
    return ans
def solve ( codeTime , n , WorkingSessionTime ) :
    dp = [ [ - 1 for i in range ( 15 ) ] for j in range ( 1 << 14 ) ]
    ones = ( 1 << n ) - 1
    ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime )
    if ( WorkingSessionTime < 6 ) :
        if ( ans % 2 == 0 ) :
            ans = ans // 2
        else :
            ans = ( ans / 2 ) + 1
    return int ( ans )
if __name__ == ' _ _ main _ _ ' :
    codeTime = [ 1 , 2 , 3 , 1 , 1 , 3 ]
    n = len ( codeTime )
    WorkingSessionTime = 4
    print ( solve ( codeTime , n , WorkingSessionTime ) )

Index/key variables: currTime, i, mask
Iterator variables: i
Python program:
def combinationsPossible ( N , idx , prev , val , K , dp ) :
    if ( val > K ) :
        return 0
    if ( dp [ val ] [ idx ] [ prev ] != - 1 ) :
        return dp [ val ] [ idx ] [ prev ]
    if ( idx == N - 1 ) :
        if ( val == K ) :
            return 1
        return 0
    ans = 0
    if ( prev == 1 ) :
        ans += combinationsPossible ( N , idx + 1 , 1 , val + 1 , K , dp )
        ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp )
    else :
        ans += combinationsPossible ( N , idx + 1 , 1 , val , K , dp )
        ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp )
    dp [ val ] [ idx ] [ prev ] = ans
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 5
    K = 3
    dp = [ [ [ - 1 for i in range ( 2 ) ] for j in range ( N + 1 ) ] for k in range ( K + 1 ) ]
    print ( combinationsPossible ( N , 0 , 0 , 0 , K , dp ) + combinationsPossible ( N , 0 , 1 , 0 , K , dp ) )

Index/key variables: idx, prev, val
Python program:
import numpy as np
def countOfWays ( arr , N ) :
    pre = [ 0 ] * ( N + 1 ) ;
    for i in range ( N ) :
        pre [ i + 1 ] = pre [ i ] + arr [ i ] ;
    dp = np . zeros ( ( N + 2 , N + 2 ) ) ;
    dp [ 1 ] [ 0 ] += 1 ;
    ans = 0 ;
    for i in range ( N ) :
        for j in range ( N , 0 , - 1 ) :
            dp [ j + 1 ] [ pre [ i + 1 ] % ( j + 1 ) ] += dp [ j ] [ pre [ i + 1 ] % j ] ;
            if ( i == N - 1 ) :
                ans += dp [ j ] [ pre [ i + 1 ] % j ] ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 ] ;
    N = len ( arr ) ;
    print ( countOfWays ( arr , N ) ) ;

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
MOD = 1e9 + 7
def numTilings ( N ) :
    if ( N < 3 ) :
        return N
    dp = [ [ 0 ] * 3 for i in range ( N + 1 ) ]
    dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1
    dp [ 1 ] [ 1 ] = dp [ 1 ] [ 2 ] = 1
    for i in range ( 2 , N + 1 ) :
        dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 2 ] [ 0 ] + dp [ i - 2 ] [ 1 ] + dp [ i - 2 ] [ 2 ] ) % MOD
        dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 2 ] ) % MOD
        dp [ i ] [ 2 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) % MOD
    return int ( dp [ N ] [ 0 ] )
N = 3
print ( numTilings ( N ) )

Index/key variables: N, i
Iterator variables: i
Python program:
dp = { }
def countStrings ( N ) :
    if N == 1 :
        return 0
    if dp . get ( N , - 1 ) != - 1 :
        return dp [ N ]
    ret = 0
    for div in range ( 1 , int ( N ** .5 ) + 1 ) :
        if N % div == 0 :
            ret += ( 1 << div ) - countStrings ( div )
            div2 = N // div
            if div2 != div and div != 1 :
                ret += ( 1 << div2 ) - countStrings ( div2 )
    dp [ N ] = ret
    return ret
N = 6
print ( countStrings ( N ) )

Index/key variables: N
Accumulator variables: ret
Iterator variables: div
Python program:
mod = 1000000000 + 7
def countOfNumbers ( N ) :
    ans = 1
    for index in range ( 1 , N + 1 ) :
        choices = 0
        for digit in range ( 1 , 10 ) :
            if ( index % digit == 0 ) :
                choices += 1
        ans = ( ans * choices ) % mod
    print ( ans )
N = 5
countOfNumbers ( N )

Accumulator variables: choices
Iterator variables: digit, index
Python program:
def dfs ( arr , left , K ) :
    q = 0
    if ( left == 0 ) :
        for i in range ( 1 , K , 1 ) :
            if ( arr [ i ] > 0 ) :
                arr [ i ] -= 1
                q = max ( q , 1 + dfs ( arr , K - i , K ) )
                arr [ i ] += 1
    else :
        for i in range ( 1 , K , 1 ) :
            if ( arr [ i ] > 0 ) :
                arr [ i ] -= 1
                nleft = left - i if i <= left else K + left - i
                q = max ( q , dfs ( arr , nleft , K ) )
                arr [ i ] += 1
    return q
def maxGroups ( K , arr , n ) :
    V = [ 0 for i in range ( K ) ]
    for x in range ( n ) :
        V [ arr [ x ] % K ] += 1
    ans = V [ 0 ] + dfs ( V , 0 , K )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
    n = len ( arr )
    K = 3
    print ( maxGroups ( K , arr , n ) )

Index/key variables: i, x
Iterator variables: i, x