dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from math import sqrt , pow
def isPerfectPower ( n ) :
    if n == 1 :
        return True
    for x in range ( 2 , int ( sqrt ( n ) ) + 1 ) :
        y = 2
        p = pow ( x , y )
        while p <= n and p > 0 :
            if p == n :
                return True
            y += 1
            p = pow ( x , y )
    return False
def isStrongNumber ( n ) :
    count = { i : 0 for i in range ( n ) }
    while n % 2 == 0 :
        n = n // 2
        count [ 2 ] += 1
    for i in range ( 3 , int ( sqrt ( n ) ) + 1 , 2 ) :
        while n % i == 0 :
            n = n // i
            count [ i ] += 1
    if n > 2 :
        count [ n ] += 1
    flag = 0
    for key , value in count . items ( ) :
        if value == 1 :
            flag = 1
            break
    if flag == 1 :
        return False
    return True
def isTrojan ( n ) :
    return isPerfectPower ( n ) == False and isStrongNumber ( n )
if __name__ == ' _ _ main _ _ ' :
    n = 108
    if ( isTrojan ( n ) ) :
        print ( " YES " )
    else :
        print ( " NO " )

Index/key variables: i, n
Accumulator variables: y
Iterator variables: i, key, value, x
Python program:
import sys
def CalculateDifference ( arr , n ) :
    max_val = - 1
    for i in range ( 0 , n ) :
        if ( arr [ i ] > max_val ) :
            max_val = arr [ i ]
    prime = [ True for i in range ( max_val + 1 ) ]
    prime [ 0 ] = False
    prime [ 1 ] = False
    p = 2
    while ( p * p <= max_val ) :
        if prime [ p ] == True :
            for i in range ( p * 2 , max_val + 1 , p ) :
                prime [ i ] = False
        p += 1
    S1 = 0
    S2 = 0
    for i in range ( 0 , n ) :
        if prime [ arr [ i ] ] :
            S1 += arr [ i ]
        elif arr [ i ] != 1 :
            S2 += arr [ i ]
    return abs ( S2 - S1 )
arr = [ 1 , 3 , 5 , 10 , 15 , 7 ]
n = len ( arr )
print ( CalculateDifference ( arr , n ) )

Index/key variables: i, p
Accumulator variables: S1, S2
Iterator variables: i
Python program:
def sum ( x , n ) :
    total = 1.0 ;
    previous = 1.0 ;
    for i in range ( 1 , n + 1 ) :
        previous = ( previous * x ) / ( i + 1 ) ;
        total = total + previous ;
    return total ;
if __name__ == ' _ _ main _ _ ' :
    x = 5 ;
    n = 4 ;
    print ( " Sum ▁ is : ▁ " , sum ( x , n ) ) ;

Iterator variables: i
Python program:
def numberOfDivisors ( num ) :
    c = 0
    for i in range ( 1 , num + 1 ) :
        if ( num % i == 0 ) :
            c += 1
    return c
def countNumbers ( n ) :
    c = 0
    for i in range ( 1 , n + 1 ) :
        if ( numberOfDivisors ( i ) == 9 ) :
            c += 1
    return c
if __name__ == " _ _ main _ _ " :
    n = 1000
    print ( countNumbers ( n ) )

Accumulator variables: c
Iterator variables: i
Python program:
import math
def numberOfDistinct ( n ) :
    ans = 0
    for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) :
        if ( n % i == 0 ) :
            ans += 1
            if ( ( n // i ) != i ) :
                ans += 1
    return ans
if __name__ == " _ _ main _ _ " :
    n = 3
    print ( numberOfDistinct ( n ) )

Accumulator variables: ans
Iterator variables: i
Python program:
MAX = 1000
arr = [ ]
def ulam ( ) :
    arr . append ( 1 ) ;
    arr . append ( 2 ) ;
    for i in range ( 3 , MAX ) :
        count = 0 ;
        for j in range ( len ( arr ) - 1 ) :
            for k in range ( j + 1 , len ( arr ) ) :
                if ( arr [ j ] + arr [ k ] == i ) :
                    count += 1
                if ( count > 1 ) :
                    break ;
            if ( count > 1 ) :
                break ;
        if ( count == 1 ) :
            arr . append ( i ) ;
if __name__ == ' _ _ main _ _ ' :
    ulam ( ) ;
    n = 9 ;
    print ( arr [ n - 1 ] )

Index/key variables: j, k
Accumulator variables: arr, count
Iterator variables: i, j, k
Python program:
def numberOfSolutions ( n ) :
    c = 0
    for x in range ( n + 1 ) :
        if ( n == ( x + ( n ^ x ) ) ) :
            c += 1
    return c
if __name__ == " _ _ main _ _ " :
    n = 3
    print ( numberOfSolutions ( n ) )

Accumulator variables: c
Iterator variables: x
Python program:
from math import sqrt
MAX = 10000
prefix = [ 0 for i in range ( MAX + 1 ) ]
def buildPrefix ( ) :
    prime = [ True for i in range ( MAX + 1 ) ]
    for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 , 1 ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * 2 , MAX + 1 , p ) :
                prime [ i ] = False
    prefix [ 0 ] = 0
    prefix [ 1 ] = 0
    for p in range ( 2 , MAX + 1 , 1 ) :
        prefix [ p ] = prefix [ p - 1 ]
        if ( prime [ p ] ) :
            prefix [ p ] += p
def sumPrimeRange ( L , R ) :
    buildPrefix ( )
    return prefix [ R ] - prefix [ L - 1 ]
if __name__ == ' _ _ main _ _ ' :
    L = 10
    R = 20
    print ( sumPrimeRange ( L , R ) )

Index/key variables: R, i, p
Iterator variables: i, p
Python program:
def repeatedSum ( n ) :
    if ( n == 0 ) :
        return 0
    return 9 if ( n % 9 == 0 ) else ( n % 9 )
def repeatedProduct ( n ) :
    prod = 1
    while ( n > 0 or prod > 9 ) :
        if ( n == 0 ) :
            n = prod
            prod = 1
        prod *= n % 10
        n //= 10
    return prod
def maxSumProduct ( N ) :
    if ( N < 10 ) :
        return N
    return max ( repeatedSum ( N ) , repeatedProduct ( N ) )
if __name__ == " _ _ main _ _ " :
    n = 631
    print ( maxSumProduct ( n ) )

Accumulator variables: n, prod
Python program:
digits = [ ]
vis = [ False for i in range ( 170 ) ]
dp = [ [ [ [ 0 for l in range ( 170 ) ] for k in range ( 2 ) ] for j in range ( 19 ) ] for i in range ( 19 ) ]
def ConvertIntoDigit ( n ) :
    while ( n > 0 ) :
        dig = n % 10 ;
        digits . append ( dig ) ;
        n //= 10 ;
    digits . reverse ( )
def solve ( idx , k , tight , sum ) :
    if ( idx == len ( digits ) and k == 0 and sum % 2 == 1 ) :
        if ( not vis [ sum ] ) :
            vis [ sum ] = True ;
            return 1 ;
        return 0 ;
    if ( idx > len ( digits ) ) :
        return 0 ;
    if ( dp [ idx ] [ k ] [ tight ] [ sum ] ) :
        return dp [ idx ] [ k ] [ tight ] [ sum ] ;
    j = 0 ;
    if ( idx < len ( digits ) and tight == 0 ) :
        j = digits [ idx ] ;
    else :
        j = 9 ;
    cnt = 0 ;
    for i in range ( 0 if k else 1 , j + 1 ) :
        newtight = tight ;
        if ( i < j ) :
            newtight = 1 ;
        if ( i == 0 ) :
            cnt += solve ( idx + 1 , k - 1 , newtight , sum ) ;
        else :
            cnt += solve ( idx + 1 , k , newtight , sum + i ) ;
    dp [ idx ] [ k ] [ tight ] [ sum ] = cnt
    return cnt ;
if __name__ == ' _ _ main _ _ ' :
    N = 169
    k = 2 ;
    ConvertIntoDigit ( N ) ;
    k = len ( digits ) - k ;
    print ( solve ( 0 , k , 0 , 0 ) )

Index/key variables: idx, k, sum, tight
Accumulator variables: cnt, digits, n
Iterator variables: i
Python program:
def countSubsets ( N ) :
    if ( N <= 2 ) :
        return N
    if ( N == 3 ) :
        return 2
    DP = [ 0 ] * ( N + 1 )
    DP [ 0 ] = 0
    DP [ 1 ] = 1
    DP [ 2 ] = 2
    DP [ 3 ] = 2
    for i in range ( 4 , N + 1 ) :
        DP [ i ] = DP [ i - 2 ] + DP [ i - 3 ]
    return DP [ N ]
if __name__ == ' _ _ main _ _ ' :
    N = 20
    print ( countSubsets ( N ) )

Index/key variables: N, i
Iterator variables: i
Python program:
dp = [ [ - 1 for j in range ( 500 ) ] for i in range ( 500 ) ]
def CountSets ( x , pos ) :
    if ( x <= 0 ) :
        if ( pos == 0 ) :
            return 1
        else :
            return 0
    if ( pos == 0 ) :
        return 1
    if ( dp [ x ] [ pos ] != - 1 ) :
        return dp [ x ] [ pos ]
    answer = ( CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) )
    dp [ x ] [ pos ] = answer
    return answer
def CountOrderedSets ( n ) :
    factorial = [ 0 for i in range ( 10000 ) ]
    factorial [ 0 ] = 1
    for i in range ( 1 , 10000 ) :
        factorial [ i ] = factorial [ i - 1 ] * i
    answer = 0
    for i in range ( 1 , n + 1 ) :
        sets = CountSets ( n , i ) * factorial [ i ]
        answer = answer + sets
    return answer
if __name__ == " _ _ main _ _ " :
    N = 3
    print ( CountOrderedSets ( N ) )

Index/key variables: i, pos, x
Iterator variables: i
Python program:
def numberOfArithmeticSequences ( L , N ) :
    if ( N <= 2 ) :
        return 0
    count = 0
    res = 0
    for i in range ( 2 , N ) :
        if ( ( L [ i ] - L [ i - 1 ] ) == ( L [ i - 1 ] - L [ i - 2 ] ) ) :
            count += 1
        else :
            count = 0
        res += count
    return res
L = [ 1 , 3 , 5 , 6 , 7 , 8 ]
N = len ( L )
print ( numberOfArithmeticSequences ( L , N ) )

Index/key variables: i
Accumulator variables: count, res
Iterator variables: i
Python program:
def CountOfTriplets ( a , n ) :
    answer = 0
    x = 0
    count = [ 0 for i in range ( 100005 ) ]
    ways = [ 0 for i in range ( 100005 ) ]
    for i in range ( n ) :
        x ^= a [ i ]
        answer += count [ x ] * i - ways [ x ]
        count [ x ] += 1
        ways [ x ] += ( i + 1 )
    return answer
if __name__ == ' _ _ main _ _ ' :
    Arr = [ 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 ]
    N = len ( Arr )
    print ( CountOfTriplets ( Arr , N ) )

Index/key variables: i, x
Accumulator variables: answer
Iterator variables: i
Python program:
def getmask ( val ) :
    mask = 0
    if val == 0 :
        return 1
    while ( val ) :
        d = val % 10 ;
        mask |= ( 1 << d )
        val = val // 10
    return mask
def countWays ( pos , mask , a , n ) :
    if pos == n :
        if mask > 0 :
            return 1
        else :
            return 0
    if dp [ pos ] [ mask ] != - 1 :
        return dp [ pos ] [ mask ]
    count = 0
    count = ( count + countWays ( pos + 1 , mask , a , n ) )
    if ( getmask ( a [ pos ] ) & mask ) == 0 :
        new_mask = ( mask | ( getmask ( a [ pos ] ) ) )
        count = ( count + countWays ( pos + 1 , new_mask , a , n ) )
    dp [ pos ] [ mask ] = count
    return count
def numberOfSubarrays ( a , n ) :
    return countWays ( 0 , 0 , a , n )
dp = [ [ - 1 for i in range ( cols ) ] for j in range ( rows ) ]
print ( numberOfSubarrays ( A , N ) )
N = 4
A = [ 1 , 12 , 23 , 34 ]
rows = 5000
cols = 1100

Index/key variables: mask, pos
Python program:
fib = [ ]
class node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def newNode ( data ) :
    temp = node ( data )
    return temp
def height ( root ) :
    ht = 0
    if ( root == None ) :
        return 0
    return ( max ( height ( root . left ) , height ( root . right ) ) + 1 )
def FibonacciSeries ( n ) :
    fib . append ( 0 )
    fib . append ( 1 )
    for i in range ( 2 , n ) :
        fib . append ( fib [ i - 1 ] + fib [ i - 2 ] )
def CountPathUtil ( root , i , count ) :
    if ( root == None or not ( fib [ i ] == root . data ) ) :
        return count
    if ( not root . left and not root . right ) :
        count += 1
    count = CountPathUtil ( root . left , i + 1 , count )
    return CountPathUtil ( root . right , i + 1 , count )
def CountPath ( root ) :
    ht = height ( root )
    FibonacciSeries ( ht )
    print ( CountPathUtil ( root , 0 , 0 ) )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 0 )
    root . left = newNode ( 1 )
    root . right = newNode ( 1 )
    root . left . left = newNode ( 1 )
    root . left . right = newNode ( 4 )
    root . right . right = newNode ( 1 )
    root . right . right . left = newNode ( 2 )
    CountPath ( root )

Index/key variables: i
Accumulator variables: fib
Iterator variables: i
Class/struct names: node
Python program:
M = 18
a = 0
b = 0
dp = [ [ [ [ - 1 for i in range ( 2 ) ] for j in range ( 90 ) ] for k in range ( 90 ) ] for l in range ( M ) ]
fib = set ( )
def fibonacci ( ) :
    prev = 0
    curr = 1
    fib . add ( prev )
    fib . add ( curr )
    while ( curr <= 100 ) :
        temp = curr + prev
        fib . add ( temp )
        prev = curr
        curr = temp
def count ( pos , even , odd , tight , num ) :
    if ( pos == len ( num ) ) :
        if ( ( len ( num ) & 1 ) ) :
            val = odd
            odd = even
            even = val
        d = even - odd
        if ( d in fib ) :
            return 1
        return 0
    if ( dp [ pos ] [ even ] [ odd ] [ tight ] != - 1 ) :
        return dp [ pos ] [ even ] [ odd ] [ tight ]
    ans = 0
    if ( tight == 1 ) :
        limit = 9
    else :
        limit = num [ pos ]
    for d in range ( limit ) :
        currF = tight
        currEven = even
        currOdd = odd
        if ( d < num [ pos ] ) :
            currF = 1
        if ( pos & 1 ) :
            currOdd += d
        else :
            currEven += d
        ans += count ( pos + 1 , currEven , currOdd , currF , num )
    return ans
def solve ( x ) :
    num = [ ]
    while ( x > 0 ) :
        num . append ( x % 10 )
        x //= 10
    num = num [ : : - 1 ]
    return count ( 0 , 0 , 0 , 0 , num )
if __name__ == ' _ _ main _ _ ' :
    fibonacci ( )
    L = 1
    R = 50
    print ( solve ( R ) - solve ( L - 1 ) + 1 )
    L = 50
    R = 100
    print ( solve ( R ) - solve ( L - 1 ) + 2 )

Index/key variables: even, odd, pos, tight
Accumulator variables: ans, currEven, currOdd, fib, num, x
Iterator variables: d
Python program:
import sys
def maximumOccurrence ( s ) :
    n = len ( s )
    freq = [ 0 ] * ( 26 )
    dp = [ [ 0 for i in range ( 26 ) ] for j in range ( 26 ) ]
    for i in range ( n ) :
        c = ( ord ( s [ i ] ) - ord ( ' a ' ) )
        for j in range ( 26 ) :
            dp [ j ] += freq [ j ]
        freq += 1
    answer = - sys . maxsize
    for i in range ( 26 ) :
        answer = max ( answer , freq [ i ] )
    for i in range ( 26 ) :
        for j in range ( 26 ) :
            answer = max ( answer , dp [ i ] [ j ] )
    return answer
if __name__ == ' _ _ main _ _ ' :
    s = " xxxyy "
    print ( maximumOccurrence ( s ) )

Index/key variables: i, j
Accumulator variables: freq
Iterator variables: i, j
Python program:
mod = 1000000007
dp = [ [ [ - 1 for i in range ( 2 ) ] for i in range ( 105 ) ] for i in range ( 1005 ) ]
powers = [ 0 ] * 1005
powersModk = [ 0 ] * 1005
def calculate ( pos , rem , z , k , n ) :
    if ( rem == 0 and z ) :
        if ( pos != n ) :
            return ( powers [ n - pos - 1 ] * 9 ) % mod
        else :
            return 1
    if ( pos == n ) :
        return 0
    if ( dp [ pos ] [ rem ] [ z ] != - 1 ) :
        return dp [ pos ] [ rem ] [ z ]
    count = 0
    for i in range ( 10 ) :
        if ( i == 0 ) :
            count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , z , k , n ) ) ) % mod
        else :
            count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , 1 , k , n ) ) ) % mod
    dp [ pos ] [ rem ] [ z ] = count
    return count
def countNumbers ( n , k ) :
    st = 1
    for i in range ( n + 1 ) :
        powers [ i ] = st
        st *= 10
        st %= mod
    st = 1
    for i in range ( n + 1 ) :
        powersModk [ i ] = st
        st *= 10
        st %= mod
    return calculate ( 0 , 0 , 0 , k , n )
if __name__ == ' _ _ main _ _ ' :
    N = 2
    K = 2
    print ( countNumbers ( N , K ) )

Index/key variables: i, pos, rem, z
Accumulator variables: st
Iterator variables: i
Python program:
inf = 100000000
def smPath ( s , d , ed , n , k ) :
    dis = [ inf ] * ( n + 1 )
    dis [ s ] = 0
    for i in range ( k ) :
        dis1 = [ inf ] * ( n + 1 )
        for it in ed :
            dis1 [ it [ 1 ] ] = min ( dis1 [ it [ 1 ] ] , dis [ it [ 0 ] ] + it [ 2 ] )
        for i in range ( n + 1 ) :
            dis [ i ] = dis1 [ i ]
    if ( dis [ d ] == inf ) :
        return - 1
    else :
        return dis [ d ]
if __name__ == ' _ _ main _ _ ' :
    n = 4
    ed = [ [ 0 , 1 , 10 ] , [ 0 , 2 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 3 , 7 ] , [ 2 , 3 , 7 ] ]
    s = 0
    d = 3
    k = 2
    print ( smPath ( s , d , ed , n , k ) )

Index/key variables: d, i, s
Iterator variables: i, it
Python program:
import sys
inf = sys . maxsize ;
def bellman ( s , d , ed , n ) :
    if ( s == d ) :
        return 0 ;
    dis = [ 0 ] * ( n + 1 ) ;
    for i in range ( 1 , n + 1 ) :
        dis [ i ] = inf ;
    dis [ s ] = 1 ;
    for i in range ( n - 1 ) :
        for it in ed :
            dis [ it [ 1 ] ] = min ( dis [ it [ 1 ] ] , dis [ it [ 0 ] ] * ed [ it ] ) ;
    for it in ed :
        if ( dis [ it [ 1 ] ] > dis [ it [ 0 ] ] * ed [ it ] ) :
            return - 2 ;
    if ( dis [ d ] == inf ) :
        return - 1 ;
    else :
        return dis [ d ] ;
if __name__ == " _ _ main _ _ " :
    n = 3 ;
    ed = { ( 1 , 2 ) : 0.5 , ( 1 , 3 ) : 1.9 , ( 2 , 3 ) : 3 } ;
    s = 1 ; d = 3 ;
    get = bellman ( s , d , ed , n ) ;
    if ( get == - 2 ) :
        print ( " Cycle ▁ Detected " ) ;
    else :
        print ( get ) ;

Index/key variables: d, i, it, s
Iterator variables: i, it
Python program:
import numpy as np
def maxLengthSquare ( row , column , arr , k ) :
    sum = np . zeros ( ( row + 1 , column + 1 ) ) ;
    cur_max = 1 ;
    max = 0 ;
    for i in range ( 1 , row + 1 ) :
        for j in range ( 1 , column + 1 ) :
            sum [ i ] [ j ] = sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] + arr [ i - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ;
            if ( i >= cur_max and j >= cur_max and sum [ i ] [ j ] - sum [ i - cur_max ] [ j ] - sum [ i ] [ j - cur_max ] + sum [ i - cur_max ] [ j - cur_max ] <= k ) :
                max = cur_max ;
                cur_max += 1 ;
    return max ;
if __name__ == " _ _ main _ _ " :
    row = 4 ;
    column = 4 ;
    matrix = [ [ 1 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] ] ;
    k = 6 ;
    ans = maxLengthSquare ( row , column , matrix , k ) ;
    print ( ans ) ;

Index/key variables: i, j
Accumulator variables: cur_max
Iterator variables: i, j
Python program:
import numpy as np
N = 101 ;
mod = int ( 1e9 ) + 7 ;
exactsum = np . zeros ( ( N , N , N ) ) ;
exactnum = np . zeros ( ( N , N , N ) ) ;
def getSum ( x , y , z ) :
    ans = 0 ;
    exactnum [ 0 ] [ 0 ] [ 0 ] = 1 ;
    for i in range ( x + 1 ) :
        for j in range ( y + 1 ) :
            for k in range ( z + 1 ) :
                if ( i > 0 ) :
                    exactsum [ i ] [ j ] [ k ] += ( exactsum [ i - 1 ] [ j ] [ k ] * 10 + 4 * exactnum [ i - 1 ] [ j ] [ k ] ) % mod ;
                    exactnum [ i ] [ j ] [ k ] += exactnum [ i - 1 ] [ j ] [ k ] % mod ;
                if ( j > 0 ) :
                    exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j - 1 ] [ k ] * 10 + 5 * exactnum [ i ] [ j - 1 ] [ k ] ) % mod ;
                    exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j - 1 ] [ k ] % mod ;
                if ( k > 0 ) :
                    exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j ] [ k - 1 ] * 10 + 6 * exactnum [ i ] [ j ] [ k - 1 ] ) % mod ;
                    exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j ] [ k - 1 ] % mod ;
                ans += exactsum [ i ] [ j ] [ k ] % mod ;
                ans %= mod ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    x = 1 ; y = 1 ; z = 1 ;
    print ( ( getSum ( x , y , z ) % mod ) ) ;

Index/key variables: i, j, k
Accumulator variables: ans
Iterator variables: i, j, k
Python program:
import numpy as np
def findMax ( a , n ) :
    dp = np . zeros ( ( n , 2 ) ) ;
    dp [ 0 ] [ 0 ] = a [ 0 ] + a [ 1 ] ;
    dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 1 ] ;
    for i in range ( 1 , n - 1 ) :
        dp [ i ] [ 0 ] = max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i + 1 ] ;
        dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] - a [ i ] + a [ i ] * a [ i + 1 ] ;
    print ( max ( dp [ n - 2 ] [ 0 ] , dp [ n - 2 ] [ 1 ] ) , end = " " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 ] ;
    findMax ( arr , 7 ) ;

Index/key variables: i
Iterator variables: i
Python program:
import numpy as np
def Sum ( arr , l , r ) :
    s = 0 ;
    for i in range ( l , r + 1 ) :
        s += arr [ i ] ;
    return s ;
dp = np . zeros ( ( 101 , 101 , 101 ) ) ;
def solve ( arr , l , r , k ) :
    if ( r - l + 1 <= k ) :
        return Sum ( arr , l , r ) ;
    if ( dp [ l ] [ r ] [ k ] ) :
        return dp [ l ] [ r ] [ k ] ;
    sum_ = Sum ( arr , l , r ) ;
    len_r = ( r - l + 1 ) - k ;
    length = ( r - l + 1 ) ;
    ans = 0 ;
    for i in range ( length - len_r + 1 ) :
        sum_sub = Sum ( arr , i + l , i + l + len_r - 1 ) ;
        ans = max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ;
    dp [ l ] [ r ] [ k ] = ans ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 15 , 20 , 9 , 2 , 5 ] ; k = 2 ;
    n = len ( arr ) ;
    print ( solve ( arr , 0 , n - 1 , k ) ) ;

Index/key variables: i, k, l, r
Accumulator variables: s
Iterator variables: i
Python program:
import numpy as np
import sys
MAXI = 50
INT_MAX = sys . maxsize
dp = np . ones ( ( MAXI , MAXI , MAXI * MAXI ) ) ;
dp *= - 1
def minDifference ( x , y , k , b , c ) :
    if ( x >= n or y >= m ) :
        return INT_MAX ;
    if ( x == n - 1 and y == m - 1 ) :
        diff = b [ x ] [ y ] - c [ x ] [ y ] ;
        return min ( abs ( k - diff ) , abs ( k + diff ) ) ;
    ans = dp [ x ] [ y ] [ k ] ;
    if ( ans != - 1 ) :
        return ans ;
    ans = INT_MAX ;
    diff = b [ x ] [ y ] - c [ x ] [ y ] ;
    ans = min ( ans , minDifference ( x + 1 , y , abs ( k + diff ) , b , c ) ) ;
    ans = min ( ans , minDifference ( x , y + 1 , abs ( k + diff ) , b , c ) ) ;
    ans = min ( ans , minDifference ( x + 1 , y , abs ( k - diff ) , b , c ) ) ;
    ans = min ( ans , minDifference ( x , y + 1 , abs ( k - diff ) , b , c ) ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    n = 2 ; m = 2 ; b = [ [ 1 , 4 ] , [ 2 , 4 ] ] ;
    c = [ [ 3 , 2 ] , [ 3 , 1 ] ] ;
    print ( minDifference ( 0 , 0 , 0 , b , c ) ) ;

Index/key variables: k, x, y
Python program:
def longest_subseq ( n , k , s ) :
    dp = [ 0 ] * n
    max_length = [ 0 ] * 26
    for i in range ( n ) :
        curr = ord ( s [ i ] ) - ord ( ' a ' )
        lower = max ( 0 , curr - k )
        upper = min ( 25 , curr + k )
        for j in range ( lower , upper + 1 ) :
            dp [ i ] = max ( dp [ i ] , max_length [ j ] + 1 )
        max_length [ curr ] = max ( dp [ i ] , max_length [ curr ] )
    return max ( dp )
def main ( ) :
    s = " geeksforgeeks "
    n = len ( s )
    k = 3
    print ( longest_subseq ( n , k , s ) )
main ( )

Index/key variables: curr, i, j
Iterator variables: i, j
Python program:
def max_sum ( a , n ) :
    dp = [ [ 0 for i in range ( 2 ) ] for j in range ( n + 1 ) ]
    dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = - 999999 ;
    for i in range ( 0 , n ) :
        dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] + a [ i ] , dp [ i ] [ 1 ] - a [ i ] ) ;
        dp [ i + 1 ] [ 1 ] = max ( dp [ i ] [ 0 ] - a [ i ] , dp [ i ] [ 1 ] + a [ i ] ) ;
    return dp [ n ] [ 0 ] ;
if __name__ == ' _ _ main _ _ ' :
    a = [ - 10 , 5 , - 4 ] ;
    n = len ( a ) ;
    print ( max_sum ( a , n ) ) ;

Index/key variables: i, n
Iterator variables: i
Python program:
MOD = 1000000007 ;
def number_of_ways ( arr , n , k ) :
    if ( k == 1 ) :
        return 1 ;
    dp = [ - 1 ] * ( k + 1 ) ;
    for i in range ( n ) :
        dp [ arr [ i ] ] = 0 ;
    dp [ 0 ] = 1 ;
    dp [ 1 ] = 1 if ( dp [ 1 ] == - 1 ) else dp [ 1 ] ;
    for i in range ( 2 , k + 1 ) :
        if ( dp [ i ] == 0 ) :
            continue ;
        dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
        dp [ i ] %= MOD ;
    return dp [ k ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ 3 ] ;
    n = len ( arr ) ;
    k = 6 ;
    print ( number_of_ways ( arr , n , k ) ) ;

Index/key variables: i, k
Iterator variables: i
Python program:
def count ( n ) :
    count = 0
    while ( n ) :
        count += n & 1
        n >>= 1
    return count
def findSetBits ( n ) :
    for i in range ( n + 1 ) :
        print ( count ( i ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    findSetBits ( n )

Accumulator variables: count, n
Iterator variables: i
Python program:
def countWays ( n ) :
    dp = [ 0 for i in range ( n + 1 ) ]
    dp [ 0 ] = 0
    dp [ 1 ] = 1
    dp [ 2 ] = 1
    for i in range ( 3 , n + 1 ) :
        dp [ i ] = dp [ i - 1 ] + dp [ i - 3 ] + 1
    return dp [ n ]
n = 6
print ( countWays ( n ) )

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