dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from collections import defaultdict
import math
def isPerfectSquare ( x ) :
    sr = math . sqrt ( x )
    return ( ( sr - math . floor ( sr ) ) == 0 )
def findSubarraySum ( arr , n , K ) :
    prevSum = defaultdict ( int )
    res = 0
    currsum = 0
    for i in range ( n ) :
        currsum += arr [ i ]
        if ( currsum == K ) :
            res += 1
        if ( ( currsum - K ) in prevSum ) :
            res += ( prevSum [ currsum - K ] )
        prevSum [ currsum ] += 1
    return res
def countSubarray ( arr , n , K ) :
    for i in range ( n ) :
        if ( isPerfectSquare ( arr [ i ] ) ) :
            arr [ i ] = 1
        else :
            arr [ i ] = 0
    print ( findSubarraySum ( arr , n , K ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 4 , 9 , 2 ]
    K = 2
    N = len ( arr )
    countSubarray ( arr , N , K )

Index/key variables: currsum, i
Accumulator variables: res
Iterator variables: i
Python program:
mod = 1000000007
def calculate ( pos , left , k , L , R ) :
    if ( pos == k ) :
        if ( left == 0 ) :
            return 1
        else :
            return 0
    if ( left == 0 ) :
        return 0
    answer = 0
    for i in range ( L , R + 1 ) :
        if ( i > left ) :
            break
        answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod
    return answer
def countWaystoDivide ( n , k , L , R ) :
    return calculate ( 0 , n , k , L , R )
N = 12
K = 3
L = 1
R = 5
print ( countWaystoDivide ( N , K , L , R ) )

Iterator variables: i
Python program:
def PossibleSum ( n , m , v , k ) :
    dp = [ [ 0 ] * ( k + 1 ) for i in range ( n + 1 ) ]
    dp [ 0 ] [ 0 ] = 1
    for i in range ( n ) :
        for j in range ( k + 1 ) :
            if dp [ i ] [ j ] == 1 :
                for d in range ( m ) :
                    if ( j + v [ i ] [ d ] ) <= k :
                        dp [ i + 1 ] [ j + v [ i ] [ d ] ] = 1
    if dp [ n ] [ k ] == 1 :
        print ( " YES " )
    else :
        print ( " NO " )
N = 2
M = 10
K = 5
arr = [ [ 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 ] , [ 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 ] ]
PossibleSum ( N , M , arr , K )

Index/key variables: d, i, j, k, n
Iterator variables: d, i, j
Python program:
dp = [ ]
def maxSum ( i , subarr ) :
    if ( i >= len ( subarr ) ) :
        return 0
    if ( dp [ i ] != - 1 ) :
        return dp [ i ]
    dp [ i ] = max ( maxSum ( i + 1 , subarr ) , subarr [ i ] + maxSum ( i + 2 , subarr ) )
    return dp [ i ]
def Func ( arr ) :
    subarr = arr
    subarr . pop ( )
    global dp
    dp = [ - 1 ] * ( len ( subarr ) )
    max1 = maxSum ( 0 , subarr )
    subarr = arr
    subarr = subarr [ : ]
    del dp
    dp = [ - 1 ] * ( len ( subarr ) )
    max2 = maxSum ( 0 , subarr )
    print ( max ( max1 , max2 ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 1 ]
    Func ( arr )

Index/key variables: i
Python program:
dim = 5
def createTable ( mtrx , k , p , dp ) :
    dp [ 0 ] [ 0 ] = mtrx [ 0 ] [ 0 ]
    for j in range ( 1 , dim ) :
        dp [ 0 ] [ j ] = mtrx [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ]
    for i in range ( 1 , dim ) :
        dp [ i ] [ 0 ] = mtrx [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ]
    for i in range ( 1 , dim ) :
        for j in range ( 1 , dim ) :
            dp [ i ] [ j ] = ( mtrx [ i ] [ j ] + dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] )
def countSubMatrixUtil ( dp , k , p ) :
    count = 0
    subMatSum = 0
    for i in range ( k - 1 , dim ) :
        for j in range ( k - 1 , dim , 1 ) :
            if ( i == ( k - 1 ) or j == ( k - 1 ) ) :
                if ( i == ( k - 1 ) and j == ( k - 1 ) ) :
                    subMatSum = dp [ i ] [ j ]
                elif ( i == ( k - 1 ) ) :
                    subMatSum = dp [ i ] [ j ] - dp [ i ] [ j - k ]
                else :
                    subMatSum = dp [ i ] [ j ] - dp [ i - k ] [ j ]
            else :
                subMatSum = ( dp [ i ] [ j ] - dp [ i - k ] [ j ] - dp [ i ] [ j - k ] + dp [ i - k ] [ j - k ] )
            if ( subMatSum >= p ) :
                count += 1
    return count
def countSubMatrix ( mtrx , k , p ) :
    dp = [ [ 0 for i in range ( dim ) ] for j in range ( dim ) ]
    for i in range ( dim ) :
        for j in range ( dim ) :
            dp [ i ] [ j ] = 0
    createTable ( mtrx , k , p , dp )
    return countSubMatrixUtil ( dp , k , p )
if __name__ == ' _ _ main _ _ ' :
    mtrx = [ [ 1 , 7 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 9 , 6 , 7 , 3 ] , [ 4 , 3 , 2 , 4 , 5 ] , [ 5 , 1 , 5 , 3 , 1 ] ]
    k = 3
    p = 35
    print ( countSubMatrix ( mtrx , k , p ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
import sys
MAX = 100
dp = [ ]
for i in range ( 1000 ) :
    temp1 = [ ]
    for j in range ( MAX ) :
        temp2 = [ ]
        for k in range ( MAX ) :
            temp2 . append ( 0 )
        temp1 . append ( temp2 )
    dp . append ( temp1 )
def productDigitSum ( x , y ) :
    sumx = 0
    while x :
        sumx += x % 10
        x = x // 10
    sumy = 0
    while y :
        sumy += y % 10
        y = y // 10
    return sumx * sumy
def solve ( arr , i , len , prev , n , k ) :
    if len == k :
        return 0
    if i == n :
        return - sys . maxsize - 1
    if dp [ i ] [ len ] [ prev ] :
        return dp [ i ] [ len ] [ prev ]
    if len & 1 :
        inc = ( productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , i , n , k ) )
    else :
        inc = solve ( arr , i + 1 , len + 1 , i , n , k )
    exc = solve ( arr , i + 1 , len , prev , n , k )
    dp [ i ] [ len ] [ prev ] = max ( inc , exc )
    return dp [ i ] [ len ] [ prev ]
arr = [ 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 ]
n = len ( arr )
k = 6
print ( solve ( arr , 0 , 0 , 0 , n , k ) )

Index/key variables: i, len, prev
Accumulator variables: dp, sumx, sumy, temp1, temp2
Iterator variables: i, j, k
Python program:
factors = [ [ ] for i in range ( 100005 ) ] ;
dp = [ 0 for i in range ( 100005 ) ] ;
def precompute ( ) :
    for i in range ( 1 , 100001 ) :
        for j in range ( i , 100001 , i ) :
            factors [ j ] . append ( i ) ;
def solve ( arr , k , n ) :
    if ( k == n - 1 ) :
        return 0 ;
    if ( k >= n ) :
        return 1000000000
    if ( dp [ k ] ) :
        return dp [ k ] ;
    ans = 1000000000
    for j in factors [ arr [ k ] ] :
        res = solve ( arr , k + j , n ) ;
        if ( res != 1000000000 ) :
            ans = min ( ans , res + 1 ) ;
    dp [ k ] = ans ;
    return ans
if __name__ == ' _ _ main _ _ ' :
    precompute ( )
    arr = [ 2 , 8 , 16 , 55 , 99 , 100 ]
    n = len ( arr )
    print ( solve ( arr , 0 , n ) )

Index/key variables: j, k
Iterator variables: i, j
Python program:
def maxDistance ( grid ) :
    q = [ ]
    M = len ( grid )
    N = len ( grid [ 0 ] )
    ans = - 1
    dirs = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 0 , - 1 ] , [ - 1 , 0 ] ]
    for i in range ( M ) :
        for j in range ( N ) :
            if ( grid [ i ] [ j ] == 1 ) :
                q . append ( [ i , j ] )
    if ( len ( q ) == 0 or M * N == len ( q ) ) :
        return - 1
    while ( len ( q ) > 0 ) :
        cnt = len ( q )
        while ( cnt > 0 ) :
            p = q [ 0 ]
            q . pop ( )
            for Dir in dirs :
                x = p [ 0 ] + Dir [ 0 ]
                y = p [ 1 ] + Dir [ 1 ]
                if ( x < 0 or x >= M or y < 0 or y >= N or grid [ x ] [ y ] ) :
                    continue
                q . append ( [ x , y ] )
                grid [ x ] [ y ] = 1
            cnt -= 1
        ans += 2
    return ans
arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] ]
print ( maxDistance ( arr ) )

Index/key variables: i, j, x, y
Accumulator variables: ans, cnt, q
Iterator variables: Dir, i, j
Python program:
mod = 1e9 + 7
dp = [ [ - 1 for x in range ( 6 ) ] for y in range ( 6 ) ]
def f ( n , k ) :
    if ( n < k ) :
        return 0
    if ( n == k ) :
        return 1
    if ( k == 1 ) :
        return 1
    if ( dp [ n ] [ k ] != - 1 ) :
        return dp [ n ] [ k ]
    dp [ n ] [ k ] = ( ( ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ) )
    return dp [ n ] [ k ]
def operation ( n ) :
    global dp
    ans = 0
    fac = 1
    for k in range ( 1 , n + 1 ) :
        fac *= k
        ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod
    return ans
if __name__ == " _ _ main _ _ " :
    n = 5
    print ( int ( operation ( n ) ) )

Index/key variables: k, n
Accumulator variables: fac
Iterator variables: k
Python program:
def printLDS ( Max ) :
    for it in Max :
        print ( it , end = " ▁ " )
def LongestDividingSeq ( arr , N ) :
    LDS = [ [ ] for i in range ( N ) ]
    LDS [ 0 ] . append ( arr [ 0 ] )
    for i in range ( 1 , N ) :
        for j in range ( i ) :
            if ( ( arr [ i ] % arr [ j ] == 0 ) and ( len ( LDS [ i ] ) < len ( LDS [ j ] ) + 1 ) ) :
                LDS [ i ] = LDS [ j ] . copy ( )
        LDS [ i ] . append ( arr [ i ] )
    maxLength = 0
    for i in range ( N ) :
        x = len ( LDS [ i ] )
        maxLength = max ( maxLength , x )
    for i in range ( N ) :
        size = len ( LDS [ i ] )
        if ( size == maxLength ) :
            printLDS ( LDS [ i ] )
            print ( )
arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 ]
N = len ( arr )
LongestDividingSeq ( arr , N ) ;

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

Index/key variables: i
Iterator variables: i
Python program:
import sys
def MaximumSum ( a , n ) :
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        for j in range ( n + 1 ) :
            dp [ i ] [ j ] = - sys . maxsize - 1
    for i in range ( n + 1 ) :
        dp [ i ] [ 0 ] = 0
    for i in range ( 1 , n + 1 , 1 ) :
        for j in range ( 1 , i + 1 , 1 ) :
            val = - sys . maxsize - 1
            if ( ( i - 2 >= 0 and dp [ i - 2 ] [ j - 1 ] != - sys . maxsize - 1 ) or i - 2 < 0 ) :
                if ( i - 2 >= 0 ) :
                    val = a [ i - 1 ] + dp [ i - 2 ] [ j - 1 ]
                else :
                    val = a [ i - 1 ]
            if ( i - 1 >= j ) :
                val = max ( val , dp [ i - 1 ] [ j ] )
            dp [ i ] [ j ] = val
    return dp [ n ] [ n // 2 ]
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 2 , 3 , 4 , 5 , 6 ]
    N = len ( A )
    print ( MaximumSum ( A , N ) )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
kPowKform = dict ( )
def func ( n ) :
    global kPowKform
    if ( n <= 0 ) :
        return 0
    if ( n in kPowKform ) :
        return 1
    answer = 0
    x = n
    while ( x > 0 ) :
        d = x % 10
        if ( d != 0 ) :
            if ( func ( n - d * d ) ) :
                answer = 1
                break
        x //= 10
    return answer
def canBeConverted ( n ) :
    if ( func ( n ) ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    N = 90
    for i in range ( 1 , 9 ) :
        val = 1
        for j in range ( 1 , i + 1 ) :
            val *= i
        kPowKform [ val ] = 1
    canBeConverted ( N )

Index/key variables: val
Accumulator variables: x
Iterator variables: i, j
Python program:
def check ( v ) :
    digits = set ( )
    for i in range ( len ( v ) ) :
        d = set ( )
        while ( v [ i ] != 0 ) :
            d . add ( v [ i ] % 10 )
            v [ i ] //= 10
        for it in d :
            if it in digits :
                return False
        for it in d :
            digits . add ( it )
    return True
def numberOfSubarrays ( a , n ) :
    answer = 0
    for i in range ( 1 , 1 << n ) :
        temp = [ ]
        for j in range ( n ) :
            if ( i & ( 1 << j ) ) :
                temp . append ( a [ j ] )
        if ( check ( temp ) ) :
            answer += 1
    return answer
if __name__ == " _ _ main _ _ " :
    N = 4
    A = [ 1 , 12 , 23 , 34 ]
    print ( numberOfSubarrays ( A , N ) )

Index/key variables: i, j
Accumulator variables: answer, d, digits, temp
Iterator variables: i, it, j
Python program:
N = 3
M = 5
Large = int ( 1e6 ) ;
prime = [ ] ;
def SieveOfEratosthenes ( ) :
    isPrime = [ True ] * ( Large + 1 ) ;
    for p in range ( 2 , int ( Large ** ( 1 / 2 ) ) ) :
        if ( isPrime [ p ] == True ) :
            for i in range ( p * 2 , Large + 1 , p ) :
                isPrime [ i ] = False ;
    for p in range ( 2 , Large + 1 ) :
        if ( isPrime [ p ] ) :
            prime . append ( p ) ;
def Display ( arr , row ) :
    for i in range ( M ) :
        print ( arr [ row ] [ i ] , end = " ▁ " ) ;
def countDivisorsMult ( arr ) :
    mp = { } ;
    row_no = 0 ; max_factor = 0 ;
    for i in range ( N ) :
        for j in range ( M ) :
            no = arr [ i ] [ j ]
            for k in range ( len ( prime ) ) :
                while ( no > 1 and no % prime [ k ] == 0 ) :
                    no //= prime [ k ] ;
                    if prime [ k ] not in mp :
                        mp [ prime [ k ] ] = 0
                    mp [ prime [ k ] ] += 1 ;
                if ( no == 1 ) :
                    break ;
        res = 1 ;
        for it in mp :
            res *= mp [ it ] ;
        if ( max_factor < res ) :
            row_no = i ;
            max_factor = res ;
        mp . clear ( ) ;
    Display ( arr , row_no ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ [ 1 , 2 , 3 , 10 , 23 ] , [ 4 , 5 , 6 , 7 , 8 ] , [ 7 , 8 , 9 , 15 , 45 ] ] ;
    SieveOfEratosthenes ( ) ;
    countDivisorsMult ( arr ) ;

Index/key variables: i, it, j, k, p, row
Accumulator variables: no, prime, res
Iterator variables: i, it, j, k, p
Python program:
from math import sqrt
def isSquare ( n ) :
    k = int ( sqrt ( n ) )
    return ( k * k == n )
def calculate ( pos , prev , sum , v ) :
    if ( pos == len ( v ) ) :
        return isSquare ( sum )
    for i in range ( prev , 9 + 1 ) :
        v [ pos ] = i
        sum += i * i
        if ( calculate ( pos + 1 , i , sum , v ) ) :
            return 1
        sum -= i * i
    return 0
def minValue ( n ) :
    v = [ 0 ] * ( n )
    if ( calculate ( 0 , 1 , 0 , v ) ) :
        answer = " "
        for i in range ( len ( v ) ) :
            answer += chr ( v [ i ] + ord ( '0' ) )
        return answer
    else :
        return " - 1"
if __name__ == ' _ _ main _ _ ' :
    N = 2
    print ( minValue ( N ) )

Index/key variables: i, pos
Accumulator variables: answer, sum
Iterator variables: i
Python program:
def checkOdd ( number ) :
    n = len ( number )
    num = ord ( number [ n - 1 ] ) - 48
    return ( num & 1 )
def splitIntoOdds ( number ) :
    numLen = len ( number )
    splitDP = [ - 1 for i in range ( numLen + 1 ) ]
    for i in range ( 1 , numLen + 1 ) :
        if ( i <= 9 and checkOdd ( number [ 0 : i ] ) > 0 ) :
            splitDP [ i ] = 1
        if ( splitDP [ i ] != - 1 ) :
            for j in range ( 1 , 10 ) :
                if ( i + j > numLen ) :
                    break ;
                if ( checkOdd ( number [ i : i + j ] ) ) :
                    if ( splitDP [ i + j ] == - 1 ) :
                        splitDP [ i + j ] = 1 + splitDP [ i ]
                    else :
                        splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] )
    return splitDP [ numLen ]
print ( splitIntoOdds ( "123456789123456789123" ) )

Index/key variables: i, numLen
Iterator variables: i, j
Python program:
def checkPrime ( number ) :
    num = int ( number )
    for i in range ( 2 , int ( num ** 0.5 ) ) :
        if ( ( num % i ) == 0 ) :
            return False
    return True
def splitIntoPrimes ( number ) :
    if ( number == ' ' ) :
        return 0
    if ( len ( number ) <= 6 and checkPrime ( number ) ) :
        return 1
    else :
        numLen = len ( number )
        ans = 1000000
        for i in range ( 1 , ( min ( 6 , numLen ) + 1 ) ) :
            if ( checkPrime ( number [ : i ] ) ) :
                val = splitIntoPrimes ( number [ i : ] )
                if ( val != - 1 ) :
                    ans = min ( ans , 1 + val )
        if ( ans == 1000000 ) :
            return - 1
        return ans
print ( splitIntoPrimes ( "13499315" ) )
print ( splitIntoPrimes ( "43" ) )

Iterator variables: i
Python program:
def k_nonzero_numbers ( s , n , k ) :
    dp = [ [ [ 0 for i in range ( k + 2 ) ] for i in range ( 2 ) ] for i in range ( n + 2 ) ]
    for i in range ( n + 1 ) :
        for j in range ( 2 ) :
            for x in range ( k + 1 ) :
                dp [ i ] [ j ] [ x ] = 0
    dp [ 0 ] [ 0 ] [ 0 ] = 1
    for i in range ( n ) :
        sm = 0
        while ( sm < 2 ) :
            for j in range ( k + 1 ) :
                x = 0
                y = 0
                if sm :
                    y = 9
                else :
                    y = ord ( s [ i ] ) - ord ( '0' )
                while ( x <= y ) :
                    dp [ i + 1 ] [ ( sm or x < ( ord ( s [ i ] ) - ord ( '0' ) ) ) ] [ j + ( x > 0 ) ] += dp [ i ] [ sm ] [ j ]
                    x += 1
            sm += 1
    return dp [ n ] [ 0 ] [ k ] + dp [ n ] [ 1 ] [ k ]
if __name__ == ' _ _ main _ _ ' :
    s = "25"
    k = 2
    n = len ( s )
    print ( k_nonzero_numbers ( s , n , k ) )

Index/key variables: i, j, k, n, sm, x
Iterator variables: i, j, x
Python program:
def maximumOccurrence ( s ) :
    n = len ( s )
    freq = { }
    for i in s :
        temp = " "
        temp += i
        freq [ temp ] = freq . get ( temp , 0 ) + 1
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            temp = " "
            temp += s [ i ]
            temp += s [ j ]
            freq [ temp ] = freq . get ( temp , 0 ) + 1
    answer = - 10 ** 9
    for it in freq :
        answer = max ( answer , freq [ it ] )
    return answer
if __name__ == ' _ _ main _ _ ' :
    s = " xxxyy "
    print ( maximumOccurrence ( s ) )

Index/key variables: i, it, j, temp
Iterator variables: i, it, j
Python program:
def distinctChars ( s ) :
    freq = [ 0 ] * 26 ;
    count = 0 ;
    for i in range ( len ( s ) ) :
        freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ;
    for i in range ( 26 ) :
        if ( freq [ i ] > 0 ) :
            count += 1 ;
    return count ;
def waysToSplit ( s ) :
    n = len ( s ) ;
    answer = 0 ;
    for i in range ( 1 , n ) :
        left = s [ 0 : i ] ;
        right = s [ i : n ] ;
        if ( distinctChars ( left ) == distinctChars ( right ) ) :
            answer += 1 ;
    return answer ;
if __name__ == " _ _ main _ _ " :
    s = " ababa " ;
    print ( waysToSplit ( s ) ) ;

Index/key variables: i
Accumulator variables: answer, count
Iterator variables: i
Python program:
def waysToSplit ( s ) :
    n = len ( s ) ;
    answer = 0 ;
    prefix = [ 0 ] * n ;
    suffix = [ 0 ] * n ;
    seen = [ 0 ] * 26 ;
    for i in range ( n ) :
        prev = prefix [ i - 1 ] if ( i - 1 >= 0 ) else 0 ;
        if ( seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] == 0 ) :
            prefix [ i ] += ( prev + 1 ) ;
        else :
            prefix [ i ] = prev ;
        seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 ;
    seen = [ 0 ] * len ( seen ) ;
    suffix [ n - 1 ] = 0 ;
    for i in range ( n - 1 , 0 , - 1 ) :
        prev = suffix [ i ] ;
        if ( seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] == 0 ) :
            suffix [ i - 1 ] += ( prev + 1 ) ;
        else :
            suffix [ i - 1 ] = prev ;
        seen [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 ;
    for i in range ( n ) :
        if ( prefix [ i ] == suffix [ i ] ) :
            answer += 1 ;
    return answer ;
if __name__ == " _ _ main _ _ " :
    s = " ababa " ;
    print ( waysToSplit ( s ) ) ;

Index/key variables: i
Accumulator variables: answer
Iterator variables: i
Python program:
def Count_Paths ( x , y ) :
    if ( x == 0 and y == 0 ) :
        return 0
    if ( x == 0 and y == 0 ) :
        return 1
    dp = [ 0 ] * ( max ( x , y ) + 1 )
    p = max ( x , y )
    q = min ( x , y )
    for i in range ( p + 1 ) :
        dp [ i ] = 1
    for i in range ( 1 , q + 1 ) :
        for j in range ( 1 , p + 1 ) :
            dp [ j ] += dp [ j - 1 ]
    return dp [ p ]
x = 3
y = 3
print ( " Number ▁ of ▁ Paths ▁ " , Count_Paths ( x , y ) )

Index/key variables: i, j, p
Iterator variables: i, j
Python program:
MAX_SIZE = 100005
fib = [ 0 ] * ( MAX_SIZE + 1 )
def fibonacci ( ) :
    fib [ 0 ] = 0
    fib [ 1 ] = 1
    for i in range ( 2 , MAX_SIZE + 1 ) :
        fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ]
def printLevel ( level ) :
    left_index = pow ( 2 , level - 1 )
    right_index = pow ( 2 , level ) - 1
    for i in range ( left_index , right_index + 1 ) :
        print ( fib [ i - 1 ] , end = " ▁ " )
    print ( )
fibonacci ( )
K = 4
printLevel ( K )

Index/key variables: i
Iterator variables: i
Python program:
def calculate ( pos , prev , left , k ) :
    if ( pos == k ) :
        if ( left == 0 ) :
            return 1 ;
        else :
            return 0 ;
    if ( left == 0 ) :
        return 0 ;
    answer = 0 ;
    for i in range ( prev , left + 1 ) :
        answer += calculate ( pos + 1 , i , left - i , k ) ;
    return answer ;
def countWaystoDivide ( n , k ) :
    return calculate ( 0 , 1 , n , k ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 8 ;
    K = 4 ;
    print ( countWaystoDivide ( N , K ) ) ;

Accumulator variables: answer
Iterator variables: i
Python program:
dp = [ [ [ 0 for i in range ( 50 ) ] for j in range ( 50 ) ] for j in range ( 50 ) ]
def calculate ( pos , prev , left , k ) :
    if ( pos == k ) :
        if ( left == 0 ) :
            return 1 ;
        else :
            return 0 ;
    if ( left == 0 ) :
        return 0 ;
    if ( dp [ pos ] [ prev ] [ left ] != - 1 ) :
        return dp [ pos ] [ prev ] [ left ] ;
    answer = 0 ;
    for i in range ( prev , left + 1 ) :
        answer += calculate ( pos + 1 , i , left - i , k ) ;
    dp [ pos ] [ prev ] [ left ] = answer ;
    return dp [ pos ] [ prev ] [ left ] ;
def countWaystoDivide ( n , k ) :
    for i in range ( 50 ) :
        for j in range ( 50 ) :
            for l in range ( 50 ) :
                dp [ i ] [ j ] [ l ] = - 1 ;
    return calculate ( 0 , 1 , n , k ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 8 ;
    K = 4 ;
    print ( countWaystoDivide ( N , K ) ) ;

Index/key variables: i, j, l, left, pos, prev
Accumulator variables: answer
Iterator variables: i, j, l
Python program:
def maxScore ( l , r , prefix_sum , num ) :
    if ( l > r ) :
        return 0 ;
    if ( ( l - 1 ) >= 0 ) :
        current_sum = ( prefix_sum [ r ] - prefix_sum [ l - 1 ] )
    else :
        current_sum = prefix_sum [ r ] - 0
    if ( num % 2 == 0 ) :
        current_sum *= - 1 ;
    return current_sum + max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ;
def findMaxScore ( a , n ) :
    prefix_sum = [ 0 ] * n
    prefix_sum [ 0 ] = a [ 0 ]
    for i in range ( 1 , n ) :
        prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ;
    return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ;
n = 6 ;
A = [ 1 , 2 , 3 , 4 , 2 , 6 ]
ans = findMaxScore ( A , n )
print ( ans )

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