dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def maxProfit ( price , n ) :
    buy1 , buy2 = sys . maxsize , sys . maxsize
    profit1 , profit2 = 0 , 0
    for i in range ( n ) :
        buy1 = min ( buy1 , price [ i ] )
        profit1 = max ( profit1 , price [ i ] - buy1 )
        buy2 = min ( buy2 , price [ i ] - profit1 )
        profit2 = max ( profit2 , price [ i ] - buy2 )
    return profit2
price = [ 2 , 30 , 15 , 10 , 8 , 25 , 80 ]
n = len ( price )
print ( " Maximum ▁ Profit ▁ = ▁ " , maxProfit ( price , n ) )

Index/key variables: i
Iterator variables: i
Python program:
from collections import defaultdict
def findMaximumScore ( a , n ) :
    freq = defaultdict ( int )
    for i in range ( n ) :
        freq [ a [ i ] ] += 1
    dp = [ 0 ] * ( max ( a ) + 1 )
    dp [ 0 ] = 0
    dp [ 1 ] = freq [ 1 ]
    for i in range ( 2 , len ( dp ) ) :
        dp [ i ] = max ( dp [ i - 1 ] , dp [ i - 2 ] + freq [ i ] * i )
    return dp [ - 1 ]
if __name__ == " _ _ main _ _ " :
    n = 3
    a = [ 1 , 2 , 3 ]
    print ( findMaximumScore ( a , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def maximizeSum ( arr ) :
    n = len ( arr )
    sum = 0
    for i in range ( 0 , n , 2 ) :
        sum += arr [ i ]
    leftDP = [ 0 ] * ( n )
    rightDP = [ 0 ] * ( n )
    c = 0
    for i in range ( 1 , n , 2 ) :
        leftDiff = arr [ i ] - arr [ i - 1 ]
        if ( c - 1 < 0 ) :
            leftDP [ i ] = leftDiff
        else :
            if ( leftDP [ i ] > 0 ) :
                leftDP [ i ] = ( leftDiff + leftDP [ i - 1 ] )
            else :
                leftDP [ i ] = leftDiff
        rightDiff = 0
        if ( i + 1 >= len ( arr ) ) :
            rightDiff = 0
        else :
            rightDiff = arr [ i ] - arr [ i + 1 ]
        if ( c - 1 < 0 ) :
            rightDP [ i ] = rightDiff
        else :
            if ( rightDP [ i ] > 0 ) :
                rightDP [ i ] = ( rightDiff + rightDP [ i - 1 ] )
            else :
                rightDP [ i ] = rightDiff
        c += 1
    maxm = 0
    for i in range ( n // 2 ) :
        maxm = max ( maxm , max ( leftDP [ i ] , rightDP [ i ] ) )
    return maxm + sum
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 8 , 4 , 5 , 7 , 6 , 8 , 9 , 7 , 3 ]
    ans = maximizeSum ( arr )
    print ( ans )

Index/key variables: i
Accumulator variables: c, sum
Iterator variables: i
Python program:
def validsubsequences ( arr , n ) :
    dp = [ [ 0 for i in range ( 2 ) ] for j in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        if ( arr [ i - 1 ] % 2 ) :
            dp [ i ] [ 1 ] += 1
            dp [ i ] [ 1 ] += dp [ i - 1 ] [ 0 ]
            dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ]
            dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ]
        else :
            dp [ i ] [ 0 ] += 1
            dp [ i ] [ 0 ] += dp [ i - 1 ] [ 1 ]
            dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ]
            dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ]
    return dp [ n ] [ 0 ] + dp [ n ] [ 1 ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 6 , 9 , 7 ]
    n = len ( arr )
    print ( validsubsequences ( arr , n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
def getCount ( n , k ) :
    if n == 1 :
        return 10
    dp = [ [ 0 for x in range ( 11 ) ] for y in range ( n + 1 ) ] ;
    for i in range ( 1 , 10 ) :
        dp [ 1 ] [ i ] = 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( 0 , 10 ) :
            l = max ( 0 , j - k )
            r = min ( 9 , j + k )
            dp [ i ] [ l ] = dp [ i ] [ l ] + dp [ i - 1 ] [ j ]
            dp [ i ] [ r + 1 ] = dp [ i ] [ r + 1 ] - dp [ i - 1 ] [ j ]
        for j in range ( 1 , 10 ) :
            dp [ i ] [ j ] = dp [ i ] [ j ] + dp [ i ] [ j - 1 ]
    count = 0
    for i in range ( 0 , 10 ) :
        count = count + dp [ n ] [ i ]
    return count
n , k = 2 , 1
print ( getCount ( n , k ) )

Index/key variables: i, j, l, n
Iterator variables: i, j
Python program:
X = 6
Z = 2
def existPath ( V , edges , u , v ) :
    mat = [ [ False for i in range ( V ) ] for j in range ( V ) ]
    for i in range ( X ) :
        mat [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = True
    for k in range ( V ) :
        for i in range ( V ) :
            for j in range ( V ) :
                mat [ i ] [ j ] = ( mat [ i ] [ j ] or mat [ i ] [ k ] and mat [ k ] [ j ] )
    if ( u >= V or v >= V ) :
        return False
    if ( mat [ u ] [ v ] ) :
        return True
    return False
V = 4
edges = [ [ 0 , 2 ] , [ 0 , 1 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 2 , 0 ] , [ 3 , 3 ] ]
u , v = 1 , 3
if ( existPath ( V , edges , u , v ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j, k, u, v
Iterator variables: i, j, k
Python program:
def calculate_maximum_splits ( arr , N ) :
    pre = [ 0 , - 1 , - 1 ]
    dp = [ 0 for i in range ( N ) ]
    C = 0
    for i in range ( N ) :
        C = C + arr [ i ]
        C = C % 3
        if pre [ C ] == - 1 :
            dp [ i ] = dp [ i - 1 ]
        else :
            dp [ i ] = max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 )
        pre [ C ] = i
    return dp [ N - 1 ]
arr = [ 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 ]
N = len ( arr )
print ( calculate_maximum_splits ( arr , N ) )

Index/key variables: C, i
Iterator variables: i
Python program:
prime = [ 0 ] * ( 1000001 )
def seiveOfEratosthenes ( ) :
    for i in range ( 2 , 1000001 ) :
        prime [ i ] = 1
    i = 2
    while ( i * i < 1000001 ) :
        if ( prime [ i ] == 1 ) :
            for j in range ( i * i , 1000001 , i ) :
                prime [ j ] = 0
        i += 1
def CountElements ( arr , n , L , R ) :
    seiveOfEratosthenes ( )
    countPrime = [ 0 ] * ( n + 1 )
    for i in range ( 1 , n + 1 ) :
        countPrime [ i ] = ( countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] )
    print ( countPrime [ R ] - countPrime [ L - 1 ] )
    return
arr = [ 2 , 4 , 5 , 8 ]
N = len ( arr )
L = 1
R = 3
CountElements ( arr , N , L , R )

Index/key variables: R, i, j
Iterator variables: i, j
Python program:
import math
def OddDivisorsCount ( n , q , a , Query ) :
    DP = [ 0 for i in range ( n ) ]
    for i in range ( n ) :
        x = int ( math . sqrt ( a [ i ] ) ) ;
        if ( x * x == a [ i ] ) :
            DP [ i ] = 1 ;
    for i in range ( 1 , n ) :
        DP [ i ] = DP [ i - 1 ] + DP [ i ] ;
    l = 0
    r = 0
    for i in range ( q ) :
        l = Query [ i ] [ 0 ] ;
        r = Query [ i ] [ 1 ] ;
        if ( l == 0 ) :
            print ( DP [ r ] )
        else :
            print ( DP [ r ] - DP [ l - 1 ] )
if __name__ == " _ _ main _ _ " :
    N = 5 ;
    Q = 3 ;
    arr = [ 2 , 4 , 5 , 6 , 9 ]
    Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ]
    OddDivisorsCount ( N , Q , arr , Query ) ;

Index/key variables: i, r
Iterator variables: i
Python program:
def countStableNum ( Str , N ) :
    count1 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ]
    count2 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ]
    for i in range ( N ) :
        for j in range ( 10 ) :
            count1 [ i ] [ j ] , count2 [ i ] [ j ] = 0 , 0
    for i in range ( N ) :
        if ( i != 0 ) :
            for j in range ( 10 ) :
                count1 [ i ] [ j ] = ( count1 [ i ] [ j ] + count1 [ i - 1 ] [ j ] )
        count1 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] += 1
    for i in range ( N - 1 , - 1 , - 1 ) :
        if ( i != N - 1 ) :
            for j in range ( 10 ) :
                count2 [ i ] [ j ] += count2 [ i + 1 ] [ j ]
        count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] = count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] + 1
    ans = 0
    for i in range ( 1 , N - 1 ) :
        if ( Str [ i ] == '9' ) :
            continue
        c1 = count1 [ i - 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) ]
        c2 = count2 [ i + 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) + 1 ]
        if ( c2 == 0 ) :
            continue
        ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) // 2 ) ) ) )
    return ans
Str = "224353"
N = len ( Str )
print ( countStableNum ( Str , N ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def goodInteger ( arr , n , h , l , r ) :
    dp = [ [ - 1 for i in range ( h ) ] for j in range ( n + 1 ) ]
    dp [ 0 ] [ 0 ] = 0
    for i in range ( n ) :
        for j in range ( h ) :
            if ( dp [ i ] [ j ] != - 1 ) :
                h1 = ( j + arr [ i ] ) % h
                h2 = ( j + arr [ i ] - 1 ) % h
                dp [ i + 1 ] [ h1 ] = max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( h1 >= l and h1 <= r ) )
                dp [ i + 1 ] [ h2 ] = max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( h2 >= l and h2 <= r ) )
    ans = 0
    for i in range ( h ) :
        if ( dp [ n ] [ i ] != - 1 ) :
            ans = max ( ans , dp [ n ] [ i ] )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    A = [ 16 , 17 , 14 , 20 , 20 , 11 , 22 ]
    H = 24
    L = 21
    R = 23
    size = len ( A )
    goodInteger ( A , size , H , L , R )

Index/key variables: h1, h2, i, j, n
Iterator variables: i, j
Python program:
def validSubArrLength ( arr , N , K ) :
    dp = [ 0 for i in range ( N + 1 ) ]
    pos = 0
    ans = 0
    mx = 0
    pre = 0
    q = [ ]
    for i in range ( N ) :
        while ( len ( q ) and arr [ len ( q ) - 1 ] < arr [ i ] ) :
            q . remove ( q [ len ( q ) - 1 ] )
        q . append ( i )
        if ( i == 0 ) :
            mx = arr [ i ]
            dp [ i ] = arr [ i ]
        elif ( mx <= arr [ i ] ) :
            dp [ i ] = dp [ i - 1 ] + arr [ i ]
            mx = arr [ i ]
        else :
            dp [ i ] = dp [ i - 1 ] + arr [ i ]
        if ( pre == 0 ) :
            pos = 0
        else :
            pos = pre - 1
        while ( ( i - pre + 1 ) * mx - ( dp [ i ] - dp [ pos ] ) > K and pre < i ) :
            pos = pre
            pre += 1
            while ( len ( q ) and q [ 0 ] < pre and pre < i ) :
                q . remove ( q [ 0 ] )
                mx = arr [ q [ 0 ] ]
        ans = max ( ans , i - pre + 1 )
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 6
    K = 8
    arr = [ 2 , 7 , 1 , 3 , 4 , 5 ]
    print ( validSubArrLength ( arr , N , K ) )

Index/key variables: i, pos
Accumulator variables: pre, q
Iterator variables: i
Python program:
MAX = 1e18
v = [ [ 0 , 0 ] ] * ( 151 )
dp = [ [ - 1 for i in range ( 151 ) ] for i in range ( 151 ) ]
def min_operation ( i , j , val , x ) :
    if ( i == n - 1 and j == m - 1 ) :
        if ( val > v [ i ] [ j ] ) :
            dp [ i ] [ j ] = MAX
            return MAX
        else :
            dp [ i ] [ j ] = v [ i ] [ j ] - val
            return dp [ i ] [ j ]
    if ( i == n or j == m ) :
        dp [ i ] [ j ] = MAX
        return MAX
    if ( dp [ i ] [ j ] != - 1 ) :
        return dp [ i ] [ j ]
    if ( val > v [ i ] [ j ] ) :
        dp [ i ] [ j ] = MAX
        return MAX
    tmp = v [ i ] [ j ] - val
    tmp += min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) )
    dp [ i ] [ j ] = tmp
    return tmp
def solve ( x ) :
    ans = 10 ** 19
    for i in range ( n ) :
        for j in range ( m ) :
            val = v [ i ] [ j ] - x * ( i + j )
            for ii in range ( 151 ) :
                for jj in range ( 151 ) :
                    dp [ ii ] [ jj ] = - 1
            val = min_operation ( 0 , 0 , val , x )
            ans = min ( ans , val )
    return ans
if __name__ == ' _ _ main _ _ ' :
    n = 2
    m = 2
    x = 3
    v [ 0 ] = [ 15 , 153 ]
    v [ 1 ] = [ 135 , 17 ]
    print ( solve ( x ) )

Index/key variables: i, ii, j, jj
Iterator variables: i, ii, j, jj
Python program:
def Count ( S , m , n ) :
    table = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        table [ 0 ] [ i ] = 10 ** 9 - 1
    for i in range ( 1 , m + 1 ) :
        for j in range ( 1 , n + 1 ) :
            if ( S [ i - 1 ] > j ) :
                table [ i ] [ j ] = table [ i - 1 ] [ j ]
            else :
                table [ i ] [ j ] = min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 )
    return table [ m ] [ n ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 9 , 6 , 5 , 1 ]
    m = len ( arr )
    print ( Count ( arr , m , 11 ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
INF = 10000000
def maximum ( A , B , C , D ) :
    return max ( max ( A , B ) , max ( C , D ) )
def maxProductUtil ( X , Y , A , B , dp ) :
    if ( X < 0 or Y < 0 ) :
        return - INF
    if ( dp [ X ] [ Y ] != - 1 ) :
        return dp [ X ] [ Y ]
    dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) )
    return dp [ X ] [ Y ]
def maxProduct ( A , N , B , M ) :
    dp = [ [ - 1 for i in range ( m ) ] for i in range ( n ) ]
    return maxProductUtil ( N - 1 , M - 1 , A , B , dp )
a = [ - 2 , 6 , - 2 , - 5 ]
b = [ - 3 , 4 , - 2 , 8 ]
n = len ( a )
m = len ( b )
print ( maxProduct ( a , n , b , m ) )

Index/key variables: X, Y
Python program:
def twoStringsEquality ( s , t ) :
    n = len ( s )
    dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n ) ]
    if ( s [ n - 1 ] == t [ 0 ] ) :
        dp [ n - 1 ] [ 1 ] = 1
    if ( s [ n - 1 ] == t [ n - 1 ] ) :
        dp [ n - 1 ] [ 0 ] = 1
    for i in range ( n - 1 , - 1 , - 1 ) :
        for j in range ( n - i + 1 ) :
            if ( dp [ i ] [ j ] ) :
                if ( s [ i - 1 ] == t [ j ] ) :
                    dp [ i - 1 ] [ j + 1 ] = 1
                if ( s [ i - 1 ] == t [ i + j - 1 ] ) :
                    dp [ i - 1 ] [ j ] = 1
    ans = False
    for i in range ( n + 1 ) :
        if ( dp [ 0 ] [ i ] == 1 ) :
            ans = True
            break
    if ( ans == True ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    S = " abab "
    T = " baab "
    twoStringsEquality ( S , T )

Index/key variables: i, j
Iterator variables: i, j
Boolean variables: ans
Python program:
def findSum ( arr , n ) :
    totalSum = 0
    for i in range ( n ) :
        totalSum += arr [ i ]
    dp = [ [ 0 ] * n for i in range ( 2 ) ]
    for i in range ( n ) :
        dp [ 0 ] [ i ] = 1
        dp [ 1 ] [ i ] = arr [ i ]
    for i in range ( 1 , n ) :
        for j in range ( i ) :
            if ( arr [ i ] > arr [ j ] ) :
                if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) :
                    dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1
                    dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ]
                elif ( dp [ 0 ] [ i ] == dp [ 0 ] [ j ] + 1 ) :
                    dp [ 1 ] [ i ] = min ( dp [ 1 ] [ i ] , dp [ 1 ] [ j ] + arr [ i ] )
    maxm = 0
    subtractSum = 0
    for i in range ( n ) :
        if ( dp [ 0 ] [ i ] > maxm ) :
            maxm = dp [ 0 ] [ i ]
            subtractSum = dp [ 1 ] [ i ]
        elif ( dp [ 0 ] [ i ] == maxm ) :
            subtractSum = min ( subtractSum , dp [ 1 ] [ i ] )
    return totalSum - subtractSum
arr = [ 4 , 6 , 1 , 2 , 3 , 8 ]
n = len ( arr )
print ( findSum ( arr , n ) )

Index/key variables: i, j
Accumulator variables: totalSum
Iterator variables: i, j
Python program:
def count ( n ) :
    if ( n == 2 ) :
        return 1 ;
    elif ( n == 4 ) :
        return 2 ;
    elif ( n == 6 ) :
        return 4 ;
    table = [ 0 ] * ( n + 1 ) ;
    for i in range ( n + 1 ) :
        table [ i ] = 0 ;
    table [ 0 ] = 0 ;
    table [ 2 ] = 1 ;
    table [ 4 ] = 2 ;
    table [ 6 ] = 4 ;
    for i in range ( 8 , n + 1 , 2 ) :
        table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ;
    return table [ n ] ;
if __name__ == " _ _ main _ _ " :
    n = 8 ;
    print ( count ( n ) ) ;

Index/key variables: i, n
Iterator variables: i
Python program:
def squareOfZeroes ( ) :
    global matrix , cache
    lastIdx = len ( matrix ) - 1
    return hasSquareOfZeroes ( 0 , 0 , lastIdx , lastIdx )
def hasSquareOfZeroes ( r1 , c1 , r2 , c2 ) :
    global matrix , cache
    if ( r1 >= r2 or c1 >= c2 ) :
        return False
    key = ( str ( r1 ) + ' - ' + str ( c1 ) + ' - ' + str ( r2 ) + ' - ' + str ( c2 ) )
    if ( key in cache ) :
        return cache [ key ]
    cache [ key ] = ( isSquareOfZeroes ( r1 , c1 , r2 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 ) )
    cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 , c1 + 1 , r2 - 1 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 , r2 , c2 - 1 ) )
    cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 , c2 ) or hasSquareOfZeroes ( r1 , c1 , r2 - 1 , c2 - 1 ) )
    return cache [ key ]
def isSquareOfZeroes ( r1 , c1 , r2 , c2 ) :
    global matrix
    for row in range ( r1 , r2 + 1 ) :
        if ( matrix [ row ] [ c1 ] != 0 or matrix [ row ] [ c2 ] != 0 ) :
            return False
    for col in range ( c1 , c2 + 1 ) :
        if ( matrix [ r1 ] [ col ] != 0 or matrix [ r2 ] [ col ] != 0 ) :
            return False
    return True
if __name__ == ' _ _ main _ _ ' :
    cache = { }
    matrix = [ [ 1 , 1 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] ]
    ans = squareOfZeroes ( )
    if ( ans == 1 ) :
        print ( " True " )
    else :
        print ( " False " )

Index/key variables: c1, c2, col, key, r1, r2, row
Iterator variables: col, row
Python program:
def CountWays ( a , b , c ) :
    x = b + c + 1 ;
    arr = [ 0 ] * x ;
    for i in range ( a , b + 1 ) :
        for j in range ( b , c + 1 ) :
            arr [ i + j ] += 1 ;
    for i in range ( 1 , x ) :
        print ( arr [ i ] , end = " ▁ " ) ;
if __name__ == ' _ _ main _ _ ' :
    a = 1 ;
    b = 2 ;
    c = 2 ;
    CountWays ( a , b , c ) ;

Index/key variables: i
Iterator variables: i, j
Python program:
def differentStrings ( s ) :
    n = len ( s )
    dp = [ 0 ] * ( n + 1 )
    dp [ 0 ] = dp [ 1 ] = 1
    for i in range ( 1 , n ) :
        if ( s [ i ] == s [ i - 1 ] and ( s [ i ] == '1' or s [ i ] == '2' ) ) :
            dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ]
        else :
            dp [ i + 1 ] = dp [ i ]
    print ( dp [ n ] )
if __name__ == " _ _ main _ _ " :
    S = "0111022110"
    differentStrings ( S )

Index/key variables: i, n
Iterator variables: i
Python program:
Col = 3 ;
def nCr ( n , r ) :
    return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ;
def fact ( n ) :
    res = 1 ;
    for i in range ( 2 , n + 1 ) :
        res = res * i ;
    return res ;
def sumPathCost ( grid , m , n ) :
    sum = 0 ;
    count = 0 ;
    for i in range ( 0 , m + 1 ) :
        for j in range ( 0 , n + 1 ) :
            count = ( nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ) ;
            sum += count * grid [ i ] [ j ] ;
    return sum ;
if __name__ == ' _ _ main _ _ ' :
    m = 2 ;
    n = 2 ;
    grid = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ;
    print ( int ( sumPathCost ( grid , m , n ) ) ) ;

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
K = 0
N = 0
v = [ ]
dp = [ [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 2 ) ] for k in range ( 10 ) ] for l in range ( 1000 ) ] for m in range ( 20 ) ]
def init ( x ) :
    global K
    global N
    global v
    v = [ ]
    while ( x > 0 ) :
        v . append ( x % 10 )
        x //= 10
    v = v [ : : - 1 ]
    N = len ( v )
def fun ( pos , sum , st , check , f ) :
    global N
    global v
    if ( pos == N ) :
        return ( sum == 0 and check == 1 )
    if ( dp [ pos ] [ sum ] [ st ] [ check ] [ f ] != - 1 ) :
        return dp [ pos ] [ sum ] [ st ] [ check ] [ f ]
    lmt = 9
    if ( f == False ) :
        lmt = v [ pos ]
    ans = 0
    for digit in range ( lmt + 1 ) :
        nf = f
        new_sum = ( sum + digit ) % K
        new_check = check
        new_st = st
        if ( f == 0 and digit < lmt ) :
            nf = 1
        if ( check == 0 and digit != 0 ) :
            new_st = digit
            new_check = 1
        if ( pos == N - 1 and new_st == digit ) :
            continue
        ans += fun ( pos + 1 , new_sum , new_st , new_check , nf )
        dp [ pos ] [ sum ] [ st ] [ check ] [ f ] = ans
    return ans
def findCount ( L , R , K ) :
    init ( R )
    r_ans = fun ( 0 , 0 , 0 , 0 , 0 )
    init ( L - 1 )
    r_ans = 0
    l_ans = fun ( 0 , 0 , 0 , 0 , 0 )
    print ( l_ans - r_ans )
if __name__ == ' _ _ main _ _ ' :
    L = 10
    R = 20
    K = 2
    findCount ( L , R , K )

Index/key variables: check, f, pos, st, sum
Accumulator variables: ans, v, x
Iterator variables: digit
Python program:
import sys
def minSumProd ( S , K ) :
    Len = len ( S ) ;
    if ( K > Len ) :
        return - 1 ;
    if ( K == Len ) :
        return 0 ;
    dp = [ 0 ] * Len ;
    count_zero = 0 ;
    count_one = 0 ;
    for j in range ( 0 , Len , 1 ) :
        if ( S [ j ] == '0' ) :
            count_zero += 1 ;
        else :
            count_one += 1 ;
        dp [ j ] = count_zero * count_one ;
    for i in range ( 1 , K ) :
        for j in range ( Len - 1 , i - 1 , - 1 ) :
            count_zero = 0 ;
            count_one = 0 ;
            dp [ j ] = sys . maxsize ;
            for k in range ( j , i - 1 , - 1 ) :
                if ( S [ k ] == '0' ) :
                    count_zero += 1 ;
                else :
                    count_one += 1 ;
                dp [ j ] = min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ;
    return dp [ Len - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    S = "1011000110110100" ;
    K = 5 ;
    print ( minSumProd ( S , K ) ) ;

Index/key variables: j, k
Accumulator variables: count_one, count_zero
Iterator variables: i, j, k
Python program:
def minPrizes ( arr , n ) :
    dpLeft = [ 0 ] * n
    dpLeft [ 0 ] = 1
    for i in range ( n ) :
        if arr [ i ] > arr [ i - 1 ] :
            dpLeft [ i ] = dpLeft [ i - 1 ] + 1
        else :
            dpLeft [ i ] = 1
    dpRight = [ 0 ] * n
    dpRight [ - 1 ] = 1
    for i in range ( n - 2 , - 1 , - 1 ) :
        if arr [ i ] > arr [ i + 1 ] :
            dpRight [ i ] = dpRight [ i + 1 ] + 1
        else :
            dpRight [ i ] = 1
    totalPrizes = 0
    for i in range ( n ) :
        totalPrizes += max ( dpLeft [ i ] , dpRight [ i ] )
    print ( totalPrizes )
arr = [ 1 , 2 , 2 , 3 ]
n = len ( arr )
minPrizes ( arr , n )

Index/key variables: i
Accumulator variables: totalPrizes
Iterator variables: i
Python program:
from collections import defaultdict
def get_max_sum ( arr , n ) :
    freq = defaultdict ( lambda : 0 )
    for i in range ( n ) :
        freq [ arr [ i ] ] += 1
    dp = [ 0 ] * 100001
    dp [ 0 ] = 0
    dp [ 1 ] = freq [ 0 ]
    for i in range ( 2 , 100000 + 1 ) :
        dp [ i ] = max ( dp [ i - 1 ] , dp [ i - 2 ] + i * freq [ i ] )
    return dp [ 100000 ]
N = 3
arr = [ 2 , 2 , 3 ]
print ( get_max_sum ( arr , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def minFlip ( a ) :
    n = len ( a )
    dp = [ [ 0 , 0 ] for i in range ( n ) ]
    dp [ 0 ] [ 0 ] = int ( a [ 0 ] == '1' )
    dp [ 0 ] [ 1 ] = int ( a [ 0 ] == '0' )
    for i in range ( 1 , n ) :
        dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + int ( a [ i ] == '1' )
        dp [ i ] [ 1 ] = min ( dp [ i - 1 ] ) + int ( a [ i ] == '0' )
    answer = 10 ** 18
    for i in range ( n ) :
        answer = min ( answer , dp [ i ] [ 1 ] + dp [ n - 1 ] [ 0 ] - dp [ i ] [ 0 ] )
    return min ( answer , dp [ n - 1 ] [ 0 ] )
s = "1100111000101"
print ( minFlip ( s ) )

Index/key variables: i
Iterator variables: i
Python program:
def LIIDS ( arr , N ) :
    dp = [ ]
    for i in range ( 1 , N + 1 ) :
        dp . append ( 1 )
    ans = 0
    for i in range ( 1 , N + 1 ) :
        j = i + i
        while j <= N :
            if j < N and i < N and arr [ j ] > arr [ i ] :
                dp [ j ] = max ( dp [ j ] , dp [ i ] + 1 )
            j += i
        if i < N :
            ans = max ( ans , dp [ i ] )
    return ans
arr = [ 1 , 2 , 3 , 7 , 9 , 10 ]
N = len ( arr )
print ( LIIDS ( arr , N ) )

Index/key variables: i, j
Accumulator variables: dp
Iterator variables: i