dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def makeDivisble ( arr , k ) :
    n = len ( arr )
    b1 = [ ]
    b2 = [ ]
    for i in range ( n ) :
        b1 . append ( arr [ i ] % k )
    for j in range ( n ) :
        if ( ( arr [ j ] % k ) != 0 ) :
            b2 . append ( k - ( arr [ j ] % k ) )
        else :
            b2 . append ( 0 )
    c = 0
    mini = float ( ' inf ' )
    suml = 0
    sumr = 0
    index = - 1
    for c in range ( 0 , n + 1 , 1 ) :
        suml = sum ( b1 [ : c + 1 ] )
        sumr = sum ( b2 )
        if suml >= sumr :
            rem = suml - sumr
            if rem < mini :
                mini = rem
                index = c
    return index , mini
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 14 , 4 , 41 , 1 ]
    k = 7
    index , diff = makeDivisble ( arr , k )
    print ( index , diff )

Index/key variables: i, j
Accumulator variables: b1, b2
Iterator variables: c, i, j
Python program:
import math
Map = { }
def findPrimeFactors ( N ) :
    Map . clear ( )
    while ( N % 2 == 0 ) :
        if 2 in Map :
            Map [ 2 ] += 1
        else :
            Map [ 2 ] = 1
        N = N // 2
    for i in range ( 3 , int ( math . sqrt ( N ) ) + 1 , 2 ) :
        while ( N % i == 0 ) :
            if i in Map :
                Map [ i ] += 1
            else :
                Map [ i ] = 1
            N = N // i
    if ( N > 2 ) :
        if N in Map :
            Map [ N ] += 1
        else :
            Map [ N ] = 1
def PowInFactN ( N , P ) :
    ans = 0
    temp = P
    while ( temp <= N ) :
        ans = ans + ( N // temp )
        temp = temp * P
    return ans
def findPowers ( N , P ) :
    findPrimeFactors ( P )
    Powers = [ ]
    for it1 , it2 in Map . items ( ) :
        primeFac = it1
        facPow = it2
        p = PowInFactN ( N , primeFac )
        p = p // facPow
        Powers . append ( p )
    return min ( Powers )
N , P = 24 , 4
if __name__ == " _ _ main _ _ " :
    print ( findPowers ( N , P ) )

Index/key variables: N, i
Accumulator variables: Powers
Iterator variables: i, it1, it2
Python program:
def FindMaxLength ( A , B ) :
    n = len ( A )
    m = len ( B )
    dp = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ]
    for i in range ( n - 1 , - 1 , - 1 ) :
        for j in range ( m - 1 , - 1 , - 1 ) :
            if A [ i ] == B [ j ] :
                dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1
    maxm = 0
    for i in dp :
        for j in i :
            maxm = max ( maxm , j )
    return maxm
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 2 , 8 , 2 , 1 ]
    B = [ 8 , 2 , 1 , 4 , 7 ]
    print ( FindMaxLength ( A , B ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
k = 16
arr = [ 43 , 1 , 17 , 26 , 15 ]
n = len ( arr )
dp = [ [ 0 for i in range ( k ) ] for j in range ( n + 2 ) ]
for i in range ( 1 , n + 1 ) :
    for j in range ( k ) :
        dp [ i ] [ j ] = dp [ i - 1 ] [ j ]
    dp [ i ] [ arr [ i - 1 ] % k ] = max ( dp [ i ] [ arr [ i - 1 ] % k ] , arr [ i - 1 ] )
    for j in range ( k ) :
        m = ( j + arr [ i - 1 ] ) % k
        if dp [ i - 1 ] [ j ] != 0 :
            dp [ i ] [ m ] = max ( dp [ i ] [ m ] , arr [ i - 1 ] + dp [ i - 1 ] [ j ] )
print ( dp [ n ] [ 0 ] )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
def maxLength ( a , n ) :
    maximum = 1 ;
    left = [ 0 ] * n ;
    right = [ 0 ] * n ;
    for i in range ( n ) :
        left [ i ] = 1 ;
        right [ i ] = 1 ;
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( a [ i ] > a [ i + 1 ] ) :
            right [ i ] = right [ i + 1 ] + 1 ;
        maximum = max ( maximum , right [ i ] ) ;
    for i in range ( 1 , n ) :
        if ( a [ i ] < a [ i - 1 ] ) :
            left [ i ] = left [ i - 1 ] + 1 ;
    if ( n > 2 ) :
        for i in range ( 1 , n - 1 ) :
            if ( a [ i - 1 ] > a [ i + 1 ] ) :
                maximum = max ( maximum , left [ i - 1 ] + right [ i + 1 ] ) ;
    return maximum ;
if __name__ == " _ _ main _ _ " :
    arr = [ 8 , 7 , 3 , 5 , 2 , 9 ] ;
    n = len ( arr ) ;
    print ( maxLength ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
import numpy as np
LAS = np . zeros ( ( 1000 , 2 ) )
for i in range ( 1000 ) :
    for j in range ( 2 ) :
        LAS [ i ] [ j ] = False
def solve ( arr , n , i , pos ) :
    if ( i == n ) :
        return 0 ;
    if ( LAS [ i ] [ pos ] ) :
        return LAS [ i ] [ pos ] ;
    inc = 0 ; exc = 0 ;
    if ( arr [ i ] > 0 and pos == True ) :
        pos = False ;
        inc = 1 + solve ( arr , n , i + 1 , pos ) ;
    elif ( arr [ i ] < 0 and pos == False ) :
        pos = True ;
        inc = 1 + solve ( arr , n , i + 1 , pos ) ;
    exc = solve ( arr , n , i + 1 , pos ) ;
    LAS [ i ] [ pos ] = max ( inc , exc ) ;
    return LAS [ i ] [ pos ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 ] ;
    n = len ( arr ) ;
    print ( max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ;

Index/key variables: i, j, pos
Iterator variables: i, j
Boolean variables: pos
Python program:
import math
def cntSubSeq ( arr , n ) :
    pos_count = 0 ;
    neg_count = 0
    for i in range ( n ) :
        if ( arr [ i ] > 0 ) :
            pos_count += 1
        if ( arr [ i ] < 0 ) :
            neg_count += 1
    result = int ( math . pow ( 2 , pos_count ) )
    if ( neg_count > 0 ) :
        result *= int ( math . pow ( 2 , neg_count - 1 ) )
    else :
        result = 0
    return result
arr = [ 2 , - 3 , - 1 , 4 ]
n = len ( arr ) ;
print ( cntSubSeq ( arr , n ) )

Index/key variables: i
Accumulator variables: neg_count, pos_count
Iterator variables: i
Python program:
import sys
INT_MAX = sys . maxsize ;
def minSteps ( string , n , k ) :
    if ( string [ n - 1 ] == '0' ) :
        return - 1 ;
    if ( n == 1 ) :
        return 0 ;
    if ( n < 4 ) :
        return 1 ;
    dp = [ 0 ] * n ;
    dp [ n - 1 ] = 0 ;
    dp [ n - 2 ] = 1 ;
    dp [ n - 3 ] = 1 ;
    for i in range ( n - 4 , - 1 , - 1 ) :
        if ( string [ i ] == '0' ) :
            continue ;
        steps = INT_MAX ;
        if ( i + k < n and string [ i + k ] == '1' ) :
            steps = min ( steps , dp [ i + k ] ) ;
        if ( string [ i + 1 ] == '1' ) :
            steps = min ( steps , dp [ i + 1 ] ) ;
        if ( string [ i + 2 ] == '1' ) :
            steps = min ( steps , dp [ i + 2 ] ) ;
        dp [ i ] = steps if ( steps == INT_MAX ) else ( 1 + steps ) ;
    if ( dp [ 0 ] == INT_MAX ) :
        return - 1 ;
    return dp [ 0 ] ;
if __name__ == " _ _ main _ _ " :
    string = "101000011" ;
    n = len ( string ) ;
    k = 5 ;
    print ( minSteps ( string , n , k ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
dp = { }
def solve ( s , c ) :
    if ( len ( s ) == 1 ) :
        return 0
    if ( len ( s ) == 2 ) :
        if ( s [ 0 ] == s [ 1 ] and s [ 0 ] == c ) :
            return 1
        else :
            return 0
    if ( s + " ▁ " + c ) in dp :
        return dp [ s + " ▁ " + c ]
    ans = 0
    if ( s [ 0 ] == s [ len ( s ) - 1 ] and s [ 0 ] == c ) :
        for c1 in range ( 97 , 123 ) :
            if ( chr ( c1 ) != c ) :
                ans = max ( ans , 1 + solve ( s [ 1 : len ( s ) - 1 ] , chr ( c1 ) ) )
    else :
        for i in range ( len ( s ) ) :
            if ( s [ i ] == c ) :
                for j in range ( len ( s ) - 1 , i , - 1 ) :
                    if ( s [ j ] == c ) :
                        if ( j == i ) :
                            break
                        ans = solve ( s [ i : j - i + 2 ] , c )
                        break
                break
    dp [ s + " ▁ " + c ] = ans
    return dp [ s + " ▁ " + c ]
if __name__ == " _ _ main _ _ " :
    s = " abscrcdba "
    ma = 0
    for c1 in range ( 97 , 123 ) :
        ma = max ( ma , solve ( s , chr ( c1 ) ) * 2 )
    print ( ma )

Index/key variables: i, j
Iterator variables: c1, i, j
Python program:
def eggDrop ( n , k ) :
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ]
    x = 0 ;
    while ( dp [ x ] [ n ] < k ) :
        x += 1 ;
        for i in range ( 1 , n + 1 ) :
            dp [ x ] [ i ] = dp [ x - 1 ] [ i - 1 ] + dp [ x - 1 ] [ i ] + 1 ;
    return x ;
if __name__ == ' _ _ main _ _ ' :
    n = 2 ; k = 36 ;
    print ( eggDrop ( n , k ) ) ;

Index/key variables: i, n, x
Iterator variables: i
Python program:
def maxIncSubarr ( a , n ) :
    pre = [ 0 ] * n ;
    pos = [ 0 ] * n ;
    pre [ 0 ] = 1 ;
    pos [ n - 1 ] = 1 ;
    l = 0 ;
    for i in range ( 1 , n ) :
        if ( a [ i ] > a [ i - 1 ] ) :
            pre [ i ] = pre [ i - 1 ] + 1 ;
        else :
            pre [ i ] = 1 ;
    l = 1 ;
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( a [ i ] < a [ i + 1 ] ) :
            pos [ i ] = pos [ i + 1 ] + 1 ;
        else :
            pos [ i ] = 1 ;
    ans = 0 ;
    l = 1 ;
    for i in range ( 1 , n ) :
        if ( a [ i ] > a [ i - 1 ] ) :
            l += 1 ;
        else :
            l = 1 ;
        ans = max ( ans , l ) ;
    for i in range ( 1 , n - 1 ) :
        if ( a [ i - 1 ] < a [ i + 1 ] ) :
            ans = max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 ] ;
    n = len ( arr ) ;
    print ( maxIncSubarr ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: l
Iterator variables: i
Python program:
n = 3
m = 3
def countSquareMatrices ( a , N , M ) :
    count = 0
    for i in range ( 1 , N ) :
        for j in range ( 1 , M ) :
            if ( a [ i ] [ j ] == 0 ) :
                continue
            a [ i ] [ j ] = min ( [ a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] , a [ i - 1 ] [ j - 1 ] ] ) + 1
    for i in range ( N ) :
        for j in range ( M ) :
            count += a [ i ] [ j ]
    return count
arr = [ [ 1 , 0 , 1 ] , [ 1 , 1 , 0 ] , [ 1 , 1 , 0 ] ]
print ( countSquareMatrices ( arr , n , m ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
N = 10000
seg = [ 0 ] * ( 3 * N )
def update ( In , l , r , up_In , val ) :
    if ( r < up_In or l > up_In ) :
        return seg [ In ]
    if ( l == up_In and r == up_In ) :
        seg [ In ] = val
        return val
    m = ( l + r ) // 2
    seg [ In ] = update ( 2 * In + 1 , l , m , up_In , val ) + update ( 2 * In + 2 , m + 1 , r , up_In , val )
    return seg [ In ]
def query ( In , l , r , l1 , r1 ) :
    if ( l > r ) :
        return 0
    if ( r < l1 or l > r1 ) :
        return 0
    if ( l1 <= l and r <= r1 ) :
        return seg [ In ]
    m = ( l + r ) // 2
    return query ( 2 * In + 1 , l , m , l1 , r1 ) + query ( 2 * In + 2 , m + 1 , r , l1 , r1 )
def fIndCnt ( arr , n ) :
    brr = [ 0 ] * n
    for i in range ( n ) :
        brr [ i ] = arr [ i ]
    brr = sorted ( brr )
    r = dict ( )
    for i in range ( n ) :
        r [ brr [ i ] ] = i + 1
    dp = [ 0 ] * n
    ans = 0
    for i in range ( n - 1 , - 1 , - 1 ) :
        rank = r [ arr [ i ] ]
        dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 )
        ans += dp [ i ]
        update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) )
    return ans
arr = [ 1 , 2 , 10 , 9 ]
n = len ( arr )
print ( fIndCnt ( arr , n ) )

Index/key variables: In, i
Accumulator variables: ans
Iterator variables: i
Python program:
def minCost ( n , x , y , z ) :
    dp = [ 0 ] * ( n + 1 )
    dp [ 0 ] = dp [ 1 ] = 0
    for i in range ( 2 , n + 1 ) :
        if ( i % 2 == 1 ) :
            dp [ i ] = min ( dp [ ( i + 1 ) // 2 ] + x + z , dp [ i - 1 ] + y )
        else :
            dp [ i ] = min ( dp [ i // 2 ] + x , dp [ i - 1 ] + y )
    return dp [ n ]
n = 5
x = 2
y = 1
z = 3
print ( minCost ( n , x , y , z ) )

Index/key variables: i, n
Iterator variables: i
Python program:
N = 1000005
def maximum_set ( a , n ) :
    dp = [ 0 for i in range ( N ) ]
    for i in a :
        dp [ i ] = 1
    ans = 1
    for i in range ( N - 1 , 0 , - 1 ) :
        if ( dp [ i ] != 0 ) :
            for j in range ( 2 * i , N , i ) :
                dp [ i ] = max ( dp [ i ] , 1 + dp [ j ] )
                ans = max ( ans , dp [ i ] )
    return ans
arr = [ 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 ]
n = len ( arr )
print ( maximum_set ( arr , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import numpy as np
N = 100
dp = np . zeros ( ( N , 3 ) ) ;
v = np . zeros ( ( N , 3 ) ) ;
def findCnt ( s , i , r ) :
    if ( i == len ( s ) ) :
        if ( r == 0 ) :
            return 1 ;
        else :
            return 0 ;
    if ( v [ i ] [ r ] ) :
        return dp [ i ] [ r ] ;
    v [ i ] [ r ] = 1 ;
    dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( ord ( s [ i ] ) - ord ( '0' ) ) ) % 3 ) + findCnt ( s , i + 1 , r ) ;
    return dp [ i ] [ r ] ;
if __name__ == " _ _ main _ _ " :
    s = "11" ;
    print ( findCnt ( s , 0 , 0 ) - 1 ) ;

Index/key variables: i, r
Python program:
def minCount ( arr , n , k ) :
    ans = 0 ;
    sum = 0 ;
    for i in range ( n ) :
        sum += arr [ i ] ;
    sum -= k ;
    l = 0 ;
    r = 0 ;
    tot = 0 ;
    while ( l < n ) :
        if ( tot <= sum ) :
            ans = max ( ans , r - l ) ;
            if ( r == n ) :
                break ;
            tot += arr [ r ] ;
            r += 1
        else :
            tot -= arr [ l ] ;
            l += 1
    return ( n - ans ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 11 , 5 , 5 ] ;
    n = len ( arr ) ;
    k = 11 ;
    print ( minCount ( arr , n , k ) ) ;

Index/key variables: i, l, r
Accumulator variables: sum, tot
Iterator variables: i
Python program:
import sys
N = 401 ;
dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ;
v = [ [ False for i in range ( N ) ] for j in range ( N ) ] ;
def minMergeCost ( i , j , arr ) :
    if ( i == j ) :
        return 0 ;
    if ( v [ i ] [ j ] ) :
        return dp [ i ] [ j ] ;
    v [ i ] [ j ] = True ;
    x = dp [ i ] [ j ] ;
    x = sys . maxsize ;
    tot = 0 ;
    for k in range ( i , j + 1 ) :
        tot += arr [ k ] ;
    for k in range ( i + 1 , j + 1 ) :
        x = min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ;
    return x ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 7 ] ;
    n = len ( arr ) ;
    print ( minMergeCost ( 0 , n - 1 , arr ) ) ;

Index/key variables: i, j, k
Accumulator variables: tot
Iterator variables: k
Python program:
maxSize = 50
dp = [ 0 ] * maxSize
v = [ 0 ] * maxSize
def expectedSteps ( x ) :
    if ( x == 0 ) :
        return 0
    if ( x <= 5 ) :
        return 6
    if ( v [ x ] ) :
        return dp [ x ]
    v [ x ] = 1
    dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6
    return dp [ x ]
n = 10
print ( round ( expectedSteps ( n - 1 ) , 5 ) )

Index/key variables: x
Python program:
def countSubSeq ( strr , lenn ) :
    ans = 0
    mul = 1
    for i in range ( lenn ) :
        if ( strr [ i ] == '0' ) :
            ans += mul
        mul *= 2
    return ans
strr = "10010"
lenn = len ( strr )
print ( countSubSeq ( strr , lenn ) )

Index/key variables: i
Accumulator variables: ans, mul
Iterator variables: i
Python program:
V_SUM_MAX = 1000
N_MAX = 100
W_MAX = 10000000
dp = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ]
v = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ]
def solveDp ( r , i , w , val , n ) :
    if ( r <= 0 ) :
        return 0
    if ( i == n ) :
        return W_MAX
    if ( v [ r ] [ i ] ) :
        return dp [ r ] [ i ]
    v [ r ] [ i ] = 1
    dp [ r ] [ i ] = min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) )
    return dp [ r ] [ i ]
def maxWeight ( w , val , n , c ) :
    for i in range ( V_SUM_MAX , - 1 , - 1 ) :
        if ( solveDp ( i , 0 , w , val , n ) <= c ) :
            return i
    return 0
if __name__ == ' _ _ main _ _ ' :
    w = [ 3 , 4 , 5 ]
    val = [ 30 , 50 , 60 ]
    n = len ( w )
    C = 8
    print ( maxWeight ( w , val , n , C ) )

Index/key variables: i, r
Iterator variables: i
Python program:
sz = 10 ** 5
n = 5
an = 0
tree = [ [ ] for i in range ( sz ) ]
subtree_size = [ 0 ] * sz
vis = [ 0 ] * sz
def addEdge ( a , b ) :
    tree [ a ] . append ( b )
    tree [ b ] . append ( a )
def dfs ( node ) :
    leaf = True
    vis [ node ] = 1
    for child in tree [ node ] :
        if ( vis [ child ] == 0 ) :
            leaf = False
            dfs ( child )
            subtree_size [ node ] += subtree_size [ child ]
    if leaf :
        subtree_size [ node ] = 1
def contribution ( node , ans ) :
    global an
    vis [ node ] = 1
    for child in tree [ node ] :
        if ( vis [ child ] == 0 ) :
            an += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) )
            contribution ( child , ans )
def getSum ( ) :
    for i in range ( sz ) :
        vis [ i ] = 0
    dfs ( 0 )
    ans = 0
    for i in range ( sz ) :
        vis [ i ] = 0
    contribution ( 0 , ans )
    return an
n = 5
addEdge ( 0 , 1 )
addEdge ( 0 , 2 )
addEdge ( 1 , 3 )
addEdge ( 1 , 4 )
print ( getSum ( ) )

Index/key variables: a, b, child, i, node
Accumulator variables: an
Iterator variables: child, i
Boolean variables: leaf
Python program:
N = 3
def numberOfCells ( mat ) :
    row = [ [ False for i in range ( N ) ] for i in range ( N ) ]
    col = [ [ False for i in range ( N ) ] for i in range ( N ) ]
    for i in range ( N ) :
        for j in range ( N - 1 , - 1 , - 1 ) :
            if ( mat [ i ] [ j ] == 1 ) :
                if j + 1 < N :
                    row [ i ] [ j ] = row [ i ] [ j + 1 ]
                else :
                    row [ i ] [ j ] = True
            else :
                row [ i ] [ j ] = False
    for j in range ( N ) :
        for i in range ( N - 1 , - 1 , - 1 ) :
            if ( mat [ i ] [ j ] == 1 ) :
                if i + 1 < N :
                    col [ i ] [ j ] = col [ i + 1 ] [ j ]
                else :
                    col [ i ] [ j ] = True
            else :
                col [ i ] [ j ] = False
    cnt = 0
    for i in range ( N - 1 ) :
        for j in range ( N - 1 ) :
            if ( row [ i ] [ j ] and col [ i ] [ j ] ) :
                cnt += 1
    for i in range ( N ) :
        if ( col [ i ] [ N - 1 ] ) :
            cnt += 1
    for j in range ( N - 1 ) :
        if ( row [ N - 1 ] [ j ] ) :
            cnt += 1
    return cnt
mat = [ [ 0 , 1 , 1 ] , [ 0 , 1 , 1 ] , [ 0 , 1 , 1 ] ]
print ( numberOfCells ( mat ) )

Index/key variables: i, j
Accumulator variables: cnt
Iterator variables: i, j
Python program:
from math import gcd as __gcd
def MaxGCD ( a , n ) :
    Prefix = [ 0 ] * ( n + 2 ) ;
    Suffix = [ 0 ] * ( n + 2 ) ;
    Prefix [ 1 ] = a [ 0 ] ;
    for i in range ( 2 , n + 1 ) :
        Prefix [ i ] = __gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ;
    Suffix [ n ] = a [ n - 1 ] ;
    for i in range ( n - 1 , 0 , - 1 ) :
        Suffix [ i ] = __gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ;
    ans = max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ;
    for i in range ( 2 , n ) :
        ans = max ( ans , __gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    a = [ 6 , 7 , 8 ] ;
    n = len ( a ) ;
    print ( MaxGCD ( a , n ) ) ;

Index/key variables: i, n
Iterator variables: i
Python program:
def sumOfProduct ( arr , n , k ) :
    dp = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ]
    cur_sum = 0
    for i in range ( 1 , n + 1 ) :
        dp [ 1 ] [ i ] = arr [ i - 1 ]
        cur_sum += arr [ i - 1 ]
    for i in range ( 2 , k + 1 ) :
        temp_sum = 0
        for j in range ( 1 , n + 1 ) :
            cur_sum -= dp [ i - 1 ] [ j ]
            dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum
            temp_sum += dp [ i ] [ j ]
        cur_sum = temp_sum
    return cur_sum
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 ]
    n = len ( arr )
    k = 2
    print ( sumOfProduct ( arr , n , k ) )

Index/key variables: i, j
Accumulator variables: cur_sum, temp_sum
Iterator variables: i, j
Python program:
import numpy as np
mod = int ( 1e9 + 7 )
def subsequence ( S , T , n , m ) :
    dp = np . zeros ( ( n + 1 , m + 1 ) ) ;
    for i in range ( n + 1 ) :
        dp [ i ] [ 0 ] = 1 ;
    for j in range ( m + 1 ) :
        dp [ 0 ] [ j ] = 1 ;
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , m + 1 ) :
            dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ;
            if ( S [ i - 1 ] == T [ j - 1 ] ) :
                dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ;
            dp [ i ] [ j ] += mod ;
            dp [ i ] [ j ] %= mod ;
    return dp [ n ] [ m ] ;
if __name__ == " _ _ main _ _ " :
    S = [ 1 , 1 ] ;
    n = len ( S ) ;
    T = [ 1 , 1 ] ;
    m = len ( T ) ;
    print ( subsequence ( S , T , n , m ) ) ;

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
def findMax ( arr , n ) :
    res = 0
    for i in range ( 1 , n ) :
        count = 0
        for j in range ( 0 , i ) :
            if arr [ j ] % arr [ i ] == 0 :
                count += 1
        res = max ( count , res )
    return res
arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ]
n = len ( arr )
print ( findMax ( arr , n ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def findMax ( arr , n ) :
    divisible = [ False ] * n ;
    res = 0 ;
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( divisible [ i ] ) :
            continue ;
        cnt = 0 ;
        for j in range ( i ) :
            if ( ( arr [ j ] % arr [ i ] ) == 0 ) :
                divisible [ j ] = True ;
                cnt += 1 ;
        res = max ( res , cnt ) ;
    return res ;
if __name__ == " _ _ main _ _ " :
    arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ;
    n = len ( arr ) ;
    print ( findMax ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: cnt
Iterator variables: i, j
Python program:
maxN = 20
maxM = 10
dp = [ [ 0 for i in range ( maxN ) ] for i in range ( maxM ) ]
v = [ [ 0 for i in range ( maxN ) ] for i in range ( maxM ) ]
def findCnt ( arr , i , curr , n , m ) :
    if ( i == n ) :
        if ( curr == 0 ) :
            return 1
        else :
            return 0
    if ( v [ i ] [ curr ] ) :
        return dp [ i ] [ curr ]
    v [ i ] [ curr ] = 1
    dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m )
    return dp [ i ] [ curr ]
arr = [ 3 , 3 , 3 , 3 ]
n = len ( arr )
m = 6
print ( findCnt ( arr , 0 , 0 , n , m ) - 1 )

Index/key variables: curr, i
Python program:
import numpy as np
maxN = 20
maxM = 256
dp = np . zeros ( ( maxN , maxM ) ) ;
v = np . zeros ( ( maxN , maxM ) ) ;
def findLen ( arr , i , curr , n , m ) :
    if ( i == n ) :
        if ( curr == m ) :
            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 ] , 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 , 7 , 2 , 3 ] ;
    n = len ( arr ) ;
    m = 3 ;
    ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ;
    if ( ans == - 1 ) :
        print ( 0 ) ;
    else :
        print ( ans ) ;

Index/key variables: curr, i