dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import numpy as np
maxN = 20
maxM = 64
dp = np . zeros ( ( maxN , maxM ) ) ;
v = np . zeros ( ( maxN , maxM ) ) ;
def findLen ( arr , i , curr , n , m ) :
    if ( i == n ) :
        if ( not curr ) :
            return 0 ;
        else :
            return - 1 ;
    if ( v [ i ] [ curr ] ) :
        return dp [ i ] [ curr ] ;
    v [ i ] [ curr ] = 1 ;
    l = findLen ( arr , i + 1 , curr , n , m ) ;
    r = findLen ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ;
    dp [ i ] [ curr ] = l ;
    if ( r != - 1 ) :
        dp [ i ] [ curr ] = max ( dp [ i ] [ curr ] , r + 1 ) ;
    return dp [ i ] [ curr ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 2 , 2 , 1 ] ;
    n = len ( arr ) ;
    m = 3 ;
    print ( findLen ( arr , 0 , 0 , n , m ) ) ;

Index/key variables: curr, i
Python program:
import numpy as np
maxN = 20
maxSum = 50
minSum = 50
base = 50
dp = np . zeros ( ( maxN , maxSum + minSum ) ) ;
v = np . zeros ( ( maxN , maxSum + minSum ) ) ;
def findCnt ( arr , i , required_sum , n ) :
    if ( i == n ) :
        if ( required_sum == 0 ) :
            return 1 ;
        else :
            return 0 ;
    if ( v [ i ] [ required_sum + base ] ) :
        return dp [ i ] [ required_sum + base ] ;
    v [ i ] [ required_sum + base ] = 1 ;
    dp [ i ] [ required_sum + base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ;
    return dp [ i ] [ required_sum + base ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 3 , 3 , 3 ] ;
    n = len ( arr ) ;
    x = 6 ;
    print ( findCnt ( arr , 0 , x , n ) ) ;

Index/key variables: i
Python program:
import numpy as np
maxN = 20
maxM = 64
dp1 = np . zeros ( ( maxN , maxM ) ) ;
v1 = np . zeros ( ( maxN , maxM ) ) ;
def findCnt ( arr , i , curr , n , m ) :
    if ( i == n ) :
        return ( curr == m ) ;
    if ( v1 [ i ] [ curr ] ) :
        return dp1 [ i ] [ curr ] ;
    v1 [ i ] [ curr ] = 1 ;
    dp1 [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr & arr [ i ] ) , n , m ) ;
    return dp1 [ i ] [ curr ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 0 , 0 , 0 ] ;
    n = len ( arr ) ;
    m = 0 ;
    print ( findCnt ( arr , 0 , ( ( 1 << 6 ) - 1 ) , n , m ) ) ;

Index/key variables: curr, i
Python program:
import numpy as np
MOD = ( int ) ( 1e9 + 7 )
def modulo_13 ( s , n ) :
    dp = np . zeros ( ( n + 1 , 13 ) ) ;
    dp [ 0 ] [ 0 ] = 1 ;
    for i in range ( n ) :
        for j in range ( 10 ) :
            nxt = ord ( s [ i ] ) - ord ( '0' ) ;
            if ( s [ i ] == ' ? ' ) :
                nxt = j ;
            for k in range ( 13 ) :
                rem = ( 10 * k + nxt ) % 13 ;
                dp [ i + 1 ] [ rem ] += dp [ i ] [ k ] ;
                dp [ i + 1 ] [ rem ] %= MOD ;
            if ( s [ i ] != ' ? ' ) :
                break ;
    return int ( dp [ n ] [ 5 ] ) ;
if __name__ == " _ _ main _ _ " :
    s = " ? 44" ;
    n = len ( s ) ;
    print ( modulo_13 ( s , n ) ) ;

Index/key variables: i, k, n, rem
Iterator variables: i, j, k
Python program:
def LISusingLCS ( seq ) :
    n = len ( seq )
    L = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
    sortedseq = sorted ( seq )
    for i in range ( n + 1 ) :
        for j in range ( n + 1 ) :
            if ( i == 0 or j == 0 ) :
                L [ i ] [ j ] = 0
            elif ( seq [ i - 1 ] == sortedseq [ j - 1 ] ) :
                L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1
            else :
                L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] )
    return L [ n ] [ n ]
sequence = [ 12 , 34 , 1 , 5 , 40 , 80 ]
print ( LISusingLCS ( sequence ) )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
def count_numbers ( k , n ) :
    dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n + 1 ) ]
    dp [ 1 ] [ 0 ] = 0
    dp [ 1 ] [ 1 ] = k - 1
    for i in range ( 2 , n + 1 ) :
        dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ]
        dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) * ( k - 1 )
    return dp [ n ] [ 0 ] + dp [ n ] [ 1 ]
k = 10
n = 3
print ( count_numbers ( k , n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
def divideArray ( arr , n , k ) :
    dp = [ [ 0 for i in range ( 500 ) ] for i in range ( 500 ) ]
    k -= 1
    for i in range ( n - 1 , - 1 , - 1 ) :
        for j in range ( 0 , k + 1 ) :
            dp [ i ] [ j ] = 10 ** 9
            max_ = - 1
            summ = 0
            for l in range ( i , n ) :
                max_ = max ( max_ , arr [ l ] )
                summ += arr [ l ]
                diff = ( l - i + 1 ) * max_ - summ
                if ( j > 0 ) :
                    dp [ i ] [ j ] = min ( dp [ i ] [ j ] , diff + dp [ l + 1 ] [ j - 1 ] )
                else :
                    dp [ i ] [ j ] = diff
    return dp [ 0 ] [ k ]
arr = [ 2 , 9 , 5 , 4 , 8 , 3 , 6 ]
n = len ( arr )
k = 2
print ( divideArray ( arr , n , k ) )

Index/key variables: i, j, k, l
Accumulator variables: summ
Iterator variables: i, j, l
Python program:
def cntSubsets ( arr , n ) :
    max = pow ( 2 , n )
    result = 0
    for i in range ( max ) :
        counter = i
        if ( counter & ( counter >> 1 ) ) :
            continue
        result += 1
    return result
arr = [ 3 , 5 , 7 ]
n = len ( arr )
print ( cntSubsets ( arr , n ) )

Accumulator variables: result
Iterator variables: i
Python program:
def cntSubsets ( arr , n ) :
    a = [ 0 ] * n
    b = [ 0 ] * n ;
    a [ 0 ] = b [ 0 ] = 1 ;
    for i in range ( 1 , n ) :
        a [ i ] = a [ i - 1 ] + b [ i - 1 ] ;
        b [ i ] = a [ i - 1 ] ;
    result = a [ n - 1 ] + b [ n - 1 ] ;
    return result ;
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 5 , 7 ] ;
    n = len ( arr ) ;
    print ( cntSubsets ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def maxScore ( money , beg , ed ) :
    totalTurns = len ( money )
    turnsTillNow = beg + ( ( totalTurns - 1 ) - ed )
    if ( beg == ed ) :
        if ( turnsTillNow % 2 == 0 ) :
            return [ money [ beg ] , " L " ]
        else :
            return [ 0 , " L " ]
    scoreOne = maxScore ( money , beg + 1 , ed )
    scoreTwo = maxScore ( money , beg , ed - 1 )
    if ( turnsTillNow % 2 == 0 ) :
        if ( money [ beg ] + scoreOne [ 0 ] > money [ ed ] + scoreTwo [ 0 ] ) :
            return [ money [ beg ] + scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ]
        else :
            return [ money [ ed ] + scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ]
    else :
        if ( scoreOne [ 0 ] < scoreTwo [ 0 ] ) :
            return [ scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ]
        else :
            return [ scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ]
ar = [ 10 , 80 , 90 , 30 ]
arraySize = len ( ar )
bags = ar
ans = maxScore ( bags , 0 , arraySize - 1 )
print ( ans [ 0 ] , ans [ 1 ] )

Index/key variables: beg, ed
Python program:
MAX = 1e9
def minJumps ( arr , N ) :
    fib = [ 0 for i in range ( 30 ) ]
    fib [ 0 ] = 0
    fib [ 1 ] = 1
    for i in range ( 2 , 30 ) :
        fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ]
    DP = [ 0 for i in range ( N + 2 ) ]
    DP [ 0 ] = 0
    for i in range ( 1 , N + 2 ) :
        DP [ i ] = MAX
    for i in range ( 1 , N + 2 ) :
        for j in range ( 1 , 30 ) :
            if ( ( arr [ i - 1 ] == 1 or i == N + 1 ) and i - fib [ j ] >= 0 ) :
                DP [ i ] = min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] )
    if ( DP [ N + 1 ] != MAX ) :
        return DP [ N + 1 ]
    else :
        return - 1
arr = [ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ]
n = len ( arr )
print ( minJumps ( arr , n - 1 ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
from math import gcd as __gcd
MAX = 100
def recur ( ind , cnt , last , a , n , k , dp ) :
    if ( cnt == k ) :
        return 0
    if ( ind == n ) :
        return - 10 ** 9
    if ( dp [ ind ] [ cnt ] != - 1 ) :
        return dp [ ind ] [ cnt ]
    ans = 0
    for i in range ( ind , n ) :
        if ( cnt % 2 == 0 ) :
            ans = max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) )
        else :
            ans = max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) )
    dp [ ind ] [ cnt ] = ans
    return ans
a = [ 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 ]
n = len ( a )
k = 4 ;
dp = [ [ - 1 for i in range ( MAX ) ] for i in range ( n ) ]
print ( recur ( 0 , 0 , 0 , a , n , k , dp ) )

Index/key variables: cnt, i, ind, last
Iterator variables: i
Python program:
def find ( s ) :
    if ( len ( s ) == 0 ) :
        return 0
    c = 1
    d = " "
    for i in range ( len ( s ) ) :
        d += s [ i ]
        s2 = s [ i + 1 : i + 1 + len ( d ) ]
        if ( s2 == d ) :
            c = 1 + find ( s [ i + 1 : ] )
            break
    return c
s = " abababab "
print ( find ( s ) )

Index/key variables: i
Accumulator variables: d
Iterator variables: i
Python program:
edges = [ [ ] for i in range ( 100 ) ]
tree = [ [ ] for i in range ( 100 ) ]
def addEdge ( x , y ) :
    edges . append ( [ x , y ] )
    tree [ x ] . append ( y )
    tree [ y ] . append ( x )
def dfs ( node , parent , dp ) :
    dp [ node ] = 1
    for it in tree [ node ] :
        if ( it != parent ) :
            dfs ( it , node , dp )
            dp [ node ] += dp [ it ]
def maximizeSum ( a , n ) :
    dp = [ 0 for i in range ( n + 1 ) ]
    dfs ( 1 , 0 , dp )
    a = sorted ( a [ : - 1 ] )
    ans = [ ]
    for it in edges :
        if len ( it ) > 0 :
            x = it [ 0 ]
            y = it [ 1 ]
            if ( dp [ x ] < dp [ y ] ) :
                fi = dp [ x ]
                sec = n - dp [ x ]
                ans . append ( fi * sec )
            else :
                fi = dp [ y ]
                sec = n - dp [ y ]
                ans . append ( fi * sec )
    ans = sorted ( ans )
    res = 0
    for i in range ( n - 1 ) :
        res += ans [ i ] * a [ i ]
    return res
n = 5
addEdge ( 1 , 2 )
addEdge ( 1 , 3 )
addEdge ( 3 , 4 )
addEdge ( 3 , 5 )
a = [ 6 , 3 , 1 , 9 , 3 ]
print ( maximizeSum ( a , n ) )

Index/key variables: i, it, node, x, y
Accumulator variables: ans, res
Iterator variables: i, it
Python program:
def solveUtil ( start , bucket , str1 , K , dp ) :
    N = len ( str1 )
    if ( start == N ) :
        if ( bucket == K ) :
            return 0
        return 10 ** 9
    if ( bucket == K ) :
        return 10 ** 9
    if ( dp [ start ] [ bucket ] != - 1 ) :
        return dp [ start ] [ bucket ]
    zeroes = 0
    ones = 0
    ans = 10 ** 9
    for i in range ( start , N ) :
        if ( str1 [ i ] == '1' ) :
            ones += 1
        else :
            zeroes += 1
        if ( ones * zeroes > ans ) :
            break
        temp = solveUtil ( i + 1 , bucket + 1 , str1 , K , dp )
        if ( temp != 10 ** 9 ) :
            ans = min ( ans , temp + ( ones * zeroes ) )
    dp [ start ] [ bucket ] = ans
    return ans
def solve ( str1 , K ) :
    N = len ( str1 )
    dp = [ [ - 1 for i in range ( K ) ] for i in range ( N ) ]
    ans = solveUtil ( 0 , 0 , str1 , K , dp )
    if ans == 10 ** 9 :
        return - 1
    else :
        return ans
s = "0101"
S = [ i for i in s ]
K = 2
print ( solve ( S , K ) )

Index/key variables: bucket, i, start
Accumulator variables: ones, zeroes
Iterator variables: i
Python program:
import numpy as np
def Probability ( p , n ) :
    dp = np . zeros ( ( n + 1 , n + 1 ) ) ;
    for i in range ( n + 1 ) :
        for j in range ( n + 1 ) :
            dp [ i ] [ j ] = 0.0
    dp [ 0 ] [ 0 ] = 1.0 ;
    for i in range ( 1 , n + 1 ) :
        for j in range ( i + 1 ) :
            if ( j == 0 ) :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ;
            else :
                dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ) ;
    ans = 0.0 ;
    for i in range ( ( n + 1 ) // 2 , n + 1 ) :
        ans += dp [ n ] [ i ] ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    p = [ 0.0 , 0.3 , 0.4 , 0.7 ] ;
    n = len ( p ) - 1 ;
    print ( Probability ( p , n ) ) ;

Index/key variables: i, j, n
Accumulator variables: ans
Iterator variables: i, j
Python program:
import numpy as np
mod = 1000000007 ;
dp = np . zeros ( ( 55 , 55 ) ) ;
def NoofWays ( face , throws , sum ) :
    if ( sum == 0 and throws == 0 ) :
        return 1 ;
    if ( sum < 0 or throws == 0 ) :
        return 0 ;
    if ( dp [ throws ] [ sum ] != - 1 ) :
        return dp [ throws ] [ sum ] ;
    ans = 0 ;
    for i in range ( 1 , face + 1 ) :
        ans += NoofWays ( face , throws - 1 , sum - i ) ;
    dp [ throws ] [ sum ] = ans ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    faces = 6 ; throws = 3 ; sum = 12 ;
    for i in range ( 55 ) :
        for j in range ( 55 ) :
            dp [ i ] [ j ] = - 1
    print ( NoofWays ( faces , throws , sum ) ) ;

Index/key variables: i, j, sum, throws
Accumulator variables: ans
Iterator variables: i, j
Python program:
import numpy as np
def MaxHappiness ( A , N , v ) :
    val = [ 0 ] * N ; wt = [ 0 ] * N ; c = 0 ;
    for i in range ( N ) :
        string = v [ i ] ;
        c = 0 ;
        for j in range ( len ( string ) ) :
            if ( string [ j ] == ' c ' ) :
                c += 4 ;
            elif ( string [ j ] == ' w ' ) :
                c += 3 ;
            elif ( string [ j ] == ' m ' ) :
                c += 2 ;
            else :
                c += 1 ;
        c *= len ( string ) ;
        val [ i ] = c ;
        wt [ i ] = len ( string ) ;
    k = np . zeros ( ( N + 1 , A + 1 ) )
    for i in range ( N + 1 ) :
        for w in range ( A + 1 ) :
            if ( i == 0 or w == 0 ) :
                k [ i ] [ w ] = 0 ;
            elif ( wt [ i - 1 ] <= w ) :
                k [ i ] [ w ] = max ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] , k [ i - 1 ] [ w ] ) ;
            else :
                k [ i ] [ w ] = k [ i - 1 ] [ w ] ;
    return k [ N ] [ A ] ;
if __name__ == " _ _ main _ _ " :
    A = 5 ;
    v = [ " mmo " , " oo " , " cmw " , " cc " , " c " ] ;
    N = len ( v ) ;
    print ( MaxHappiness ( A , N , v ) ) ;

Index/key variables: A, N, i, j, w
Accumulator variables: c
Iterator variables: i, j, w
Python program:
import numpy as np
import sys
C_MAX = 30
max_arr_len = 10
dp = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ;
v = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ;
INT_MIN = - ( sys . maxsize ) + 1
def findMax ( i , r , w , n ) :
    if ( r < 0 ) :
        return INT_MIN ;
    if ( i == n ) :
        return 0 ;
    if ( v [ i ] [ r ] ) :
        return dp [ i ] [ r ] ;
    v [ i ] [ r ] = 1 ;
    dp [ i ] [ r ] = max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ;
    return dp [ i ] [ r ] ;
def preCompute ( w , n ) :
    for i in range ( C_MAX , - 1 , - 1 ) :
        findMax ( 0 , i , w , n ) ;
def ansQuery ( w ) :
    return dp [ 0 ] [ w ] ;
if __name__ == " _ _ main _ _ " :
    w = [ 3 , 8 , 9 ] ;
    n = len ( w )
    preCompute ( w , n ) ;
    queries = [ 11 , 10 , 4 ] ;
    q = len ( queries )
    for i in range ( q ) :
        print ( ansQuery ( queries [ i ] ) ) ;

Index/key variables: i, r, w
Iterator variables: i
Python program:
maxLen = 10
dp = [ 0 ] * maxLen ;
v = [ 0 ] * maxLen ;
prefix_sum = [ 0 ] * maxLen ;
def findPrefixSum ( arr , n ) :
    prefix_sum [ 0 ] = arr [ 0 ] ;
    for i in range ( n ) :
        prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ;
def maxSum ( arr , i , n , k ) :
    if ( i + k > n ) :
        return 0 ;
    if ( v [ i ] ) :
        return dp [ i ] ;
    v [ i ] = 1 ;
    if ( i == 0 ) :
        x = prefix_sum [ k - 1 ] ;
    else :
        x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ;
    dp [ i ] = max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ;
    return dp [ i ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 7 , 6 ] ;
    n = len ( arr ) ;
    k = 1 ;
    findPrefixSum ( arr , n ) ;
    print ( maxSum ( arr , 0 , n , k ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
import numpy as np
N = 3
M = 3
def findMaxSum ( arr ) :
    sum = 0 ;
    b = np . zeros ( ( N , M ) ) ;
    for i in range ( N ) :
        b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ;
    for i in range ( N ) :
        for j in range ( M - 2 , - 1 , - 1 ) :
            b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ;
    for i in range ( 1 , N ) :
        for j in range ( M ) :
            sum = max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ;
            b [ i ] [ j ] = max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    arr = [ [ 3 , 7 , 4 ] , [ 1 , 9 , 6 ] , [ 1 , 7 , 7 ] ] ;
    print ( findMaxSum ( arr ) ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
import numpy as np
max_size = 20
max_k = 20
dp = np . zeros ( ( max_size , max_k ) ) ;
v = np . zeros ( ( max_size , max_k ) ) ;
sum = 0 ;
def findSum ( arr , n ) :
    global sum
    for i in range ( n ) :
        sum += arr [ i ] ;
def cntWays ( arr , i , ck , k , n , curr_sum ) :
    if ( sum % k != 0 ) :
        return 0 ;
    if ( i != n and ck == k + 1 ) :
        return 0 ;
    if ( i == n ) :
        if ( ck == k + 1 ) :
            return 1 ;
        else :
            return 0 ;
    if ( v [ i ] [ ck ] ) :
        return dp [ i ] [ ck ] ;
    curr_sum += arr [ i ] ;
    v [ i ] [ ck ] = 1 ;
    dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ;
    if ( curr_sum == ( sum / k ) * ck ) :
        dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ;
    return dp [ i ] [ ck ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ;
    n = len ( arr ) ;
    k = 2 ;
    findSum ( arr , n ) ;
    print ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ;

Index/key variables: ck, i
Accumulator variables: sum
Iterator variables: i
Python program:
arrSize = 51
dp = [ 0 for i in range ( arrSize ) ]
v = [ False for i in range ( arrSize ) ]
def sumMax ( i , arr , n ) :
    if ( i >= n - 1 ) :
        return 0
    if ( v [ i ] ) :
        return dp [ i ]
    v [ i ] = True
    dp [ i ] = max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) )
    return dp [ i ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 1 , 1 , 1 ]
    n = len ( arr )
    print ( sumMax ( 0 , arr , n ) )

Index/key variables: i
Python program:
def MaxIncreasingSub ( arr , n , k ) :
    dp = [ - 1 ] * n
    ans = - 1
    for i in range ( n ) :
        dp [ i ] = [ - 1 ] * ( k + 1 )
    for i in range ( n ) :
        dp [ i ] [ 1 ] = arr [ i ]
    for i in range ( 1 , n ) :
        for j in range ( i ) :
            if arr [ j ] < arr [ i ] :
                for l in range ( 1 , k ) :
                    if dp [ j ] [ l ] != - 1 :
                        dp [ i ] [ l + 1 ] = max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] )
    for i in range ( n ) :
        if ans < dp [ i ] [ k ] :
            ans = dp [ i ] [ k ]
    return ( 0 if ans == - 1 else ans )
if __name__ == " _ _ main _ _ " :
    n , k = 8 , 3
    arr = [ 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 ]
    ans = MaxIncreasingSub ( arr , n , k )
    print ( ans )

Index/key variables: i, j, k, l
Iterator variables: i, j, l
Python program:
bitscount = 32
n = 3
prefix_count = [ [ [ 0 for i in range ( n ) ] for j in range ( n ) ] for k in range ( bitscount ) ]
def findPrefixCount ( arr ) :
    for i in range ( bitscount ) :
        for j in range ( n ) :
            prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 )
            for k in range ( 1 , n ) :
                prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 )
                prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ]
    for i in range ( bitscount ) :
        for j in range ( 1 , n ) :
            for k in range ( n ) :
                prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ]
def rangeOr ( x1 , y1 , x2 , y2 ) :
    ans = 0
    for i in range ( bitscount ) :
        if ( x1 == 0 and y1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ]
        elif ( x1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ]
        elif ( y1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ]
        else :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ;
        if ( p != 0 ) :
            ans = ( ans | ( 1 << i ) )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
    findPrefixCount ( arr )
    queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ]
    q = len ( queries )
    for i in range ( q ) :
        print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) )

Index/key variables: i, j, k, x2, y2
Iterator variables: i, j, k
Python program:
bitscount = 32
n = 3
prefix_count = [ [ [ 0 for i in range ( n ) ] for j in range ( n ) ] for k in range ( bitscount ) ]
def findPrefixCount ( arr ) :
    for i in range ( bitscount ) :
        for j in range ( n ) :
            prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 )
            for k in range ( 1 , n ) :
                prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 )
                prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ]
    for i in range ( bitscount ) :
        for j in range ( 1 , n ) :
            for k in range ( n ) :
                prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ]
def rangeOr ( x1 , y1 , x2 , y2 ) :
    ans = 0
    for i in range ( bitscount ) :
        if ( x1 == 0 and y1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ]
        elif ( x1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ]
        elif ( y1 == 0 ) :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ]
        else :
            p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ;
        if ( p == ( x2 - x1 + 1 ) * ( y2 - y1 + 1 ) ) :
            ans = ( ans | ( 1 << i ) )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
    findPrefixCount ( arr )
    queries = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 ] ]
    q = len ( queries )
    for i in range ( q ) :
        print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) )

Index/key variables: i, j, k, x2, y2
Iterator variables: i, j, k
Python program:
maxLen = 10
dp = [ 0 for i in range ( maxLen ) ]
v = [ 0 for i in range ( maxLen ) ]
def maxSum ( arr , i , n ) :
    if ( i >= n ) :
        return 0
    if ( v [ i ] ) :
        return dp [ i ]
    v [ i ] = 1
    dp [ i ] = max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) )
    return dp [ i ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 12 , 9 , 7 , 33 ]
    n = len ( arr )
    print ( maxSum ( arr , 0 , n ) )

Index/key variables: i
Python program:
MAX = 100
memo = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
def oSRec ( arr , i , j , Sum ) :
    if ( j == i + 1 ) :
        return max ( arr [ i ] , arr [ j ] )
    if ( memo [ i ] [ j ] != - 1 ) :
        return memo [ i ] [ j ]
    memo [ i ] [ j ] = max ( ( Sum - oSRec ( arr , i + 1 , j , Sum - arr [ i ] ) ) , ( Sum - oSRec ( arr , i , j - 1 , Sum - arr [ j ] ) ) )
    return memo [ i ] [ j ]
def optimalStrategyOfGame ( arr , n ) :
    Sum = 0
    Sum = sum ( arr )
    for j in range ( MAX ) :
        for k in range ( MAX ) :
            memo [ j ] [ k ] = - 1
    return oSRec ( arr , 0 , n - 1 , Sum )
arr1 = [ 8 , 15 , 3 , 7 ]
n = len ( arr1 )
print ( optimalStrategyOfGame ( arr1 , n ) )
arr2 = [ 2 , 2 , 2 , 2 ]
n = len ( arr2 )
print ( optimalStrategyOfGame ( arr2 , n ) )
arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ]
n = len ( arr3 )
print ( optimalStrategyOfGame ( arr3 , n ) )

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