dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
class TreeNode :
    def __init__ ( self , val = 0 , left = None , right = None ) :
        self . val = val
        self . left = left
        self . right = right
def replaceDiag ( root , d , diagMap ) :
    if not root :
        return
    root . val = diagMap [ d ]
    replaceDiag ( root . left , d + 1 , diagMap )
    replaceDiag ( root . right , d , diagMap )
def getDiagSum ( root , d , diagMap ) :
    if not root :
        return
    if d in diagMap :
        diagMap [ d ] += root . val
    else :
        diagMap [ d ] = root . val
    getDiagSum ( root . left , d + 1 , diagMap )
    getDiagSum ( root . right , d , diagMap )
def levelOrder ( root ) :
    que = [ root ]
    while True :
        length = len ( que )
        if not length :
            break
        while length :
            temp = que . pop ( 0 )
            print ( temp . val , end = ' ▁ ' )
            if temp . left :
                que . append ( temp . left )
            if temp . right :
                que . append ( temp . right )
            length -= 1
if __name__ == ' _ _ main _ _ ' :
    root = TreeNode ( 5 )
    root . left = TreeNode ( 6 )
    root . right = TreeNode ( 3 )
    root . left . left = TreeNode ( 4 )
    root . left . right = TreeNode ( 9 )
    root . right . right = TreeNode ( 2 )
    diagMap = { }
    getDiagSum ( root , 0 , diagMap )
    replaceDiag ( root , 0 , diagMap )
    levelOrder ( root )

Index/key variables: d
Accumulator variables: length, que
Class/struct names: TreeNode
Python program:
def countDivBy3InArray ( arr ) :
    ans = 0
    taken = [ False ] * len ( arr )
    for i in range ( len ( arr ) ) :
        if taken [ i ] :
            continue
        for j in range ( i + 1 , len ( arr ) ) :
            if taken [ j ] :
                continue
            if ( not int ( str ( arr [ i ] ) + str ( arr [ j ] ) ) % 3 or not int ( str ( arr [ j ] ) + str ( arr [ i ] ) ) % 3 ) :
                ans += 1
                taken [ i ] = True
                taken [ j ] = True
    return ans
arr = [ 5 , 3 , 2 , 8 , 7 ]
print ( countDivBy3InArray ( arr ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def findMaxLengthSequence ( N , arr ) :
    rightmost_element = - 1
    i = 0
    j = N - 1
    sequence = [ ]
    while ( i <= j ) :
        if ( arr [ i ] > arr [ j ] ) :
            if ( arr [ j ] > rightmost_element ) :
                sequence . append ( arr [ j ] )
                rightmost_element = arr [ j ]
                j -= 1
            elif ( arr [ i ] > rightmost_element ) :
                sequence . append ( arr [ i ] )
                rightmost_element = arr [ i ]
                i += 1
            else :
                break
        elif ( arr [ i ] < arr [ j ] ) :
            if ( arr [ i ] > rightmost_element ) :
                sequence . append ( arr [ i ] )
                rightmost_element = arr [ i ]
                i += 1
            elif ( arr [ j ] > rightmost_element ) :
                sequence . append ( arr [ j ] )
                rightmost_element = arr [ j ]
                j -= 1
            else :
                break
        elif ( arr [ i ] == arr [ j ] ) :
            if ( i == j ) :
                if ( arr [ i ] > rightmost_element ) :
                    sequence . append ( arr [ i ] )
                    rightmost_element = arr [ i ]
                    i += 1
                break
            else :
                sequence . append ( arr [ i ] )
                k = i + 1
                max_left = [ ]
                while ( k < j and arr [ k ] > arr [ k - 1 ] ) :
                    max_left . append ( arr [ k ] )
                    k += 1
                l = j - 1
                max_right = [ ]
                while ( l > i and arr [ l ] > arr [ l + 1 ] ) :
                    max_right . append ( arr [ l ] )
                    l -= 1
                if ( len ( max_left ) > len ( max_right ) ) :
                    for element in max_left :
                        sequence . append ( element )
                else :
                    for element in max_right :
                        sequence . append ( element )
                break
    for element in sequence :
        print ( element , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    arr = [ 1 , 3 , 2 , 1 ]
    findMaxLengthSequence ( N , arr )

Index/key variables: i, j, k, l
Accumulator variables: max_left, max_right, sequence
Iterator variables: element
Python program:
class TreeNode :
    def __init__ ( self , val ) :
        self . val = val
        self . children = [ ]
ans = 0.0
def MaxAverage ( root ) :
    global ans
    if ( root != None and len ( root . children ) == 0 ) :
        ans = max ( ans , ( root . val ) )
        return [ root . val , 1 ]
    childResult = [ 0 for i in range ( 2 ) ]
    for child in root . children :
        childTotal = MaxAverage ( child )
        childResult [ 0 ] = childResult [ 0 ] + childTotal [ 0 ]
        childResult [ 1 ] = childResult [ 1 ] + childTotal [ 1 ]
    sum = childResult [ 0 ] + root . val
    count = childResult [ 1 ] + 1
    ans = max ( ans , sum / count )
    return [ sum , count ]
if __name__ == ' _ _ main _ _ ' :
    root = TreeNode ( 20 )
    left = TreeNode ( 12 )
    right = TreeNode ( 18 )
    root . children . append ( left )
    root . children . append ( right )
    left . children . append ( TreeNode ( 11 ) )
    left . children . append ( TreeNode ( 3 ) )
    right . children . append ( TreeNode ( 15 ) )
    right . children . append ( TreeNode ( 8 ) )
    MaxAverage ( root )
    print ( ans * 1.0 )

Iterator variables: child
Class/struct names: TreeNode
Python program:
def canTransformStrings ( A , B ) :
    n1 = len ( A ) ;
    n2 = len ( B ) ;
    count1A = 0 ;
    count1B = 0 ;
    odd1A = 0 ; odd1B = 0 ;
    even1A = 0 ; even1B = 0 ;
    for i in range ( n1 ) :
        if ( A [ i ] == '1' ) :
            count1A += 1 ;
        else :
            if ( ( count1A & 1 ) == 1 ) :
                odd1A += 1 ;
            else :
                even1A += 1 ;
    for i in range ( n2 ) :
        if ( B [ i ] == '1' ) :
            count1B += 1 ;
        else :
            if ( ( count1B & 1 ) == 1 ) :
                odd1B += 1 ;
            else :
                even1B += 1 ;
    if ( count1A == count1B and odd1A == odd1B and even1A == even1B ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;
if __name__ == ' _ _ main _ _ ' :
    A = "10011" ;
    B = "11100" ;
    canTransformStrings ( A , B ) ;

Index/key variables: i
Accumulator variables: count1A, count1B, even1A, even1B, odd1A, odd1B
Iterator variables: i
Python program:
def printAnswer ( answer , N ) :
    for i in range ( N + 1 ) :
        print ( answer [ i ] , end = " ▁ " )
def updateAnswerArray ( answer , N ) :
    i = 0
    while ( answer [ i ] == - 1 ) :
        i += 1
    minimum = answer [ i ]
    while ( i <= N ) :
        if ( answer [ i ] == - 1 ) :
            answer [ i ] = minimum
        else :
            answer [ i ] = min ( minimum , answer [ i ] )
        minimum = min ( minimum , answer [ i ] )
        i += 1
def lengthOfSubarray ( indices , st , N ) :
    answer = [ - 1 for i in range ( N + 1 ) ]
    for itr in st :
        start = - 1
        gap = - 1
        indices [ itr ] . append ( N )
        for i in range ( len ( indices [ itr ] ) ) :
            gap = max ( gap , indices [ itr ] [ i ] - start )
            start = indices [ itr ] [ i ]
        if ( answer [ gap ] == - 1 ) :
            answer [ gap ] = itr
    updateAnswerArray ( answer , N )
    printAnswer ( answer , N )
def smallestPresentNumber ( arr , N ) :
    indices = [ [ ] for i in range ( N + 1 ) ]
    elements = [ ]
    for i in range ( N ) :
        indices [ arr [ i ] ] . append ( i )
        elements . append ( arr [ i ] )
    elements = list ( set ( elements ) )
    lengthOfSubarray ( indices , elements , N )
arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ]
N = len ( arr )
smallestPresentNumber ( arr , N )

Index/key variables: gap, i, itr
Accumulator variables: elements
Iterator variables: i, itr
Python program:
def smallestLeft ( arr , total , sum , i , dp ) :
    if ( i == 0 ) :
        return abs ( total - 2 * sum )
    if ( dp [ i ] [ sum ] != - 1 ) :
        return dp [ i ] [ sum ]
    X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp )
    Y = smallestLeft ( arr , total , sum , i - 1 , dp )
    dp [ i ] [ sum ] = min ( X , Y )
    return dp [ i ] [ sum ]
def UtilSmallestElement ( arr , N ) :
    total = 0
    for i in range ( 0 , N ) :
        total += arr [ i ]
    dp = [ [ - 1 for y in range ( total ) ] for x in range ( N + 1 ) ]
    print ( smallestLeft ( arr , total , 0 , N , dp ) )
arr = [ 2 , 7 , 4 , 1 , 8 , 1 ]
N = len ( arr )
UtilSmallestElement ( arr , N )

Index/key variables: i, sum
Accumulator variables: total
Iterator variables: i
Python program:
def SmallestElementLeft ( arr , N ) :
    totalSum = 0
    for i in range ( N ) :
        totalSum += arr [ i ]
    req = totalSum // 2
    dp = [ False for i in range ( req + 1 ) ]
    memset ( dp , false , sizeof ( dp ) ) ;
    dp [ 0 ] = True
    reach = 0
    for i in range ( N ) :
        j = req
        while j >= arr [ i ] :
            dp [ j ] = dp [ j ] or dp [ j - arr [ i ] ]
            if ( dp [ j ] ) :
                reach = max ( reach , j )
            j -= 1
    return totalSum - ( 2 * reach )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 2 , 2 ]
    N = len ( arr )
    print ( SmallestElementLeft ( arr , N ) )

Index/key variables: i, j
Accumulator variables: totalSum
Iterator variables: i
Python program:
def minOperationsReq ( str11 , str22 ) :
    str1 = list ( str11 )
    str2 = list ( str22 )
    N = len ( str1 )
    cntOp = 0
    for i in range ( N ) :
        if ( str1 [ i ] == str2 [ i ] ) :
            continue
        ptr = i
        while ( ptr < N and str1 [ ptr ] != str2 [ ptr ] ) :
            str1 [ ptr ] = str2 [ ptr ]
            ptr += 2
        cntOp += 1
    return cntOp
str1 = " abcdef "
str2 = " ffffff "
print ( minOperationsReq ( str1 , str2 ) )

Index/key variables: i, ptr
Accumulator variables: cntOp
Iterator variables: i
Python program:
import math
def isComposite ( n ) :
    if n <= 1 :
        return False
    if n <= 3 :
        return False
    if n % 2 == 0 or n % 3 == 0 :
        return True
    i = 5
    while i * i <= n :
        if ( ( n % i == 0 ) or ( n % ( i + 2 ) == 0 ) ) :
            return True
        i += 6
    return False
def isPerfectSquare ( x ) :
    s = int ( math . sqrt ( x ) )
    return ( s * s == x )
def isFibonacci ( n ) :
    return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) )
def ifgcdFibonacci ( a , n , k ) :
    compositeset = [ ]
    for i in range ( n ) :
        if ( isComposite ( a [ i ] ) and a [ i ] % k == 0 ) :
            compositeset . append ( a [ i ] )
    gcd = compositeset [ 0 ]
    for i in range ( 1 , len ( compositeset ) , 1 ) :
        gcd = math . gcd ( gcd , compositeset [ i ] )
        if gcd == 1 :
            break
    if ( isFibonacci ( gcd ) ) :
        print ( " Yes " )
        return
    print ( " No " )
    return
if __name__ == " _ _ main _ _ " :
    arr = [ 34 , 2 , 4 , 8 , 5 , 7 , 11 ]
    n = len ( arr )
    k = 2
    ifgcdFibonacci ( arr , n , k )

Index/key variables: i
Accumulator variables: compositeset
Iterator variables: i
Python program:
def find_longest_subarray ( A , N , K ) :
    MaxLen = 0
    Len = 0
    for i in range ( N ) :
        if ( K % A [ i ] == 0 ) :
            Len += 1
            MaxLen = max ( MaxLen , Len )
        else :
            Len = 0
    return MaxLen
A = [ 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 ]
N = len ( A )
K = 60
print ( find_longest_subarray ( A , N , K ) )

Index/key variables: i
Accumulator variables: Len
Iterator variables: i
Python program:
from bisect import bisect_left , bisect_right
import sys
def minTime ( meeting , n , entrance , m , exit , p ) :
    ans = sys . maxsize
    entrance = sorted ( entrance )
    exit = sorted ( exit )
    for i in range ( n ) :
        u = meeting [ i ] [ 0 ]
        v = meeting [ i ] [ 1 ]
        it1 = bisect_right ( entrance , u )
        it2 = bisect_left ( exit , v )
        start = it1 - 1
        end = it2
        if ( start >= 0 and start < m and end >= 0 and end < p ) :
            ans = min ( ans , exit [ end ] - entrance [ start ] )
    if ans >= sys . maxsize :
        ans = - 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    meeting = [ [ 15 , 19 ] , [ 5 , 10 ] , [ 7 , 25 ] ]
    entrance = [ 4 , 13 , 25 , 2 ]
    exit = [ 10 , 25 ]
    n = len ( meeting )
    m = len ( entrance )
    p = len ( exit )
    print ( minTime ( meeting , n , entrance , m , exit , p ) )

Index/key variables: end, i, start
Iterator variables: i
Python program:
MAX = 1001
def sieve ( ) :
    global MAX
    spf = [ 0 ] * MAX
    spf [ 0 ] = spf [ 1 ] = - 1
    for i in range ( 2 , MAX ) :
        spf [ i ] = i
    for i in range ( 4 , MAX , 2 ) :
        spf [ i ] = 2
    for i in range ( 3 , MAX ) :
        if ( spf [ i ] == i ) :
            for j in range ( i * i , MAX ) :
                if ( spf [ j ] == j ) :
                    spf [ j ] = i
    return spf
def countFactors ( spf , num ) :
    count = 0
    while ( num > 1 ) :
        count += 1
        num = num // spf [ num ]
    return count
def precalculateSum ( spf ) :
    sum = [ 0 ] * MAX
    for i in range ( 1 , MAX ) :
        prime_factor = countFactors ( spf , i )
        if ( spf [ prime_factor ] == prime_factor ) :
            sum [ i ] = sum [ i - 1 ] + 1
        else :
            sum [ i ] = sum [ i - 1 ]
    return sum
if __name__ == ' _ _ main _ _ ' :
    spf = sieve ( )
    sum = precalculateSum ( spf )
    Q = [ [ 4 , 8 ] , [ 30 , 32 ] ]
    sum [ Q [ 0 ] [ 1 ] ] += 1
    for i in range ( 0 , 2 ) :
        print ( ( sum [ Q [ i ] [ 1 ] ] - sum [ Q [ i ] [ 0 ] ] ) , end = " ▁ " )

Index/key variables: i, j, num, prime_factor
Accumulator variables: count
Iterator variables: i, j
Python program:
def isPerfect ( N ) :
    sum = 1
    for i in range ( 2 , N ) :
        if i * i > N :
            break
        if ( N % i == 0 ) :
            if ( i == N // i ) :
                sum += i
            else :
                sum += i + N // i
    if ( sum == N and N != 1 ) :
        return 1
    return 0
def maxSum ( arr , N , K ) :
    if ( N < K ) :
        print ( " Invalid " )
        return - 1
    res = 0
    for i in range ( K ) :
        res += arr [ i ]
    curr_sum = res
    for i in range ( K , N ) :
        curr_sum += arr [ i ] - arr [ i - K ]
        res = max ( res , curr_sum )
    return res
def max_PerfectNumbers ( arr , N , K ) :
    for i in range ( N ) :
        if isPerfect ( arr [ i ] ) :
            arr [ i ] = 1
        else :
            arr [ i ] = 0
    return maxSum ( arr , N , K )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 ]
    K = 4
    N = len ( arr )
    print ( max_PerfectNumbers ( arr , N , K ) )

Index/key variables: i
Accumulator variables: curr_sum, res, sum
Iterator variables: i
Python program:
def display ( countLeftGreater , N ) :
    for i in range ( N ) :
        print ( countLeftGreater [ i ] , end = " ▁ " )
def countGreater ( arr , N ) :
    St = set ( )
    countLeftGreater = [ 0 ] * ( N )
    for i in range ( N ) :
        St . add ( arr [ i ] )
        it = 0
        for st in St :
            if ( arr [ i ] < st ) :
                break
            it += 1
        countLeftGreater [ i ] = abs ( it - len ( St ) )
    display ( countLeftGreater , N )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 12 , 1 , 2 , 3 , 0 , 11 , 4 ]
    N = len ( arr )
    countGreater ( arr , N )

Index/key variables: i
Accumulator variables: St, it
Iterator variables: i, st
Python program:
def minCost ( A , B ) :
    n = len ( A ) ;
    i = 0 ;
    maxlen = 0 ;
    while ( i < n ) :
        length = 0 ;
        for j in range ( 0 , n ) :
            if ( A [ i ] == B [ j ] ) :
                i += 1
                length += 1 ;
                if ( i == n ) :
                    break ;
        maxlen = max ( maxlen , length ) ;
    return n - maxlen ;
if __name__ == ' _ _ main _ _ ' :
    A = " edacb " ;
    B = " abcde " ;
    print ( minCost ( A , B ) ) ;

Index/key variables: i, j
Accumulator variables: length
Iterator variables: j
Python program:
def lexicographicRank ( s ) :
    n = len ( s )
    alphaIndex = [ [ ] for i in range ( 26 ) ]
    for i in range ( len ( s ) ) :
        x = ord ( s [ i ] ) - ord ( ' a ' )
        alphaIndex [ x ] . append ( i )
    rank = - 1
    for i in range ( 26 ) :
        if ord ( ' a ' ) + i >= ord ( s [ 0 ] ) :
            break
        if len ( alphaIndex [ i ] ) > 0 :
            for j in range ( len ( alphaIndex [ i ] ) ) :
                rank = rank + ( n - alphaIndex [ i ] [ j ] )
    strr = [ ]
    for i in range ( len ( alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] ) ) :
        substring = [ ]
        jj = alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] [ i ]
        for j in range ( jj , n ) :
            substring . append ( s [ j ] )
            strr . append ( substring )
    strr = sorted ( strr )
    for i in range ( len ( strr ) ) :
        if ( strr [ i ] != s ) :
            rank += 1
        else :
            break
    return rank + 1
if __name__ == ' _ _ main _ _ ' :
    strr = " enren "
    print ( lexicographicRank ( strr ) )

Index/key variables: i, j, x
Accumulator variables: rank, strr, substring
Iterator variables: i, j
Python program:
def hIndex ( citations , n ) :
    hindex = 0
    low = 0
    high = n - 1
    while ( low <= high ) :
        mid = ( low + high ) // 2
        if ( citations [ mid ] >= ( mid + 1 ) ) :
            low = mid + 1
            hindex = mid + 1
        else :
            high = mid - 1
    print ( hindex )
    return hindex
n = 5
citations = [ 5 , 3 , 3 , 2 , 2 ]
hIndex ( citations , n )

Index/key variables: mid
Python program:
def checkEqual ( arr , N ) :
    hash = [ 0 ] * 256
    M = len ( arr [ 0 ] )
    for i in range ( N ) :
        for j in range ( M ) :
            hash [ ord ( arr [ i ] [ j ] ) ] += 1
    for i in range ( 256 ) :
        if ( hash [ i ] % N != 0 ) :
            return False
    return True
arr = [ " fdd " , " fhh " ]
N = len ( arr )
if ( checkEqual ( arr , N ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j
Iterator variables: i, j
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def storeLeaf ( root : Node , arr : list ) -> None :
    if ( not root ) :
        return
    if ( not root . left and not root . right ) :
        arr . append ( root . data )
        return
    storeLeaf ( root . left , arr )
    storeLeaf ( root . right , arr )
def KSmallest ( root : Node , k : int ) -> None :
    arr = [ ]
    storeLeaf ( root , arr )
    arr . sort ( )
    for i in range ( k ) :
        if ( i < len ( arr ) ) :
            print ( arr [ i ] , end = " ▁ " )
        else :
            break
if __name__ == " _ _ main _ _ " :
    root = Node ( 1 )
    root . left = Node ( 2 )
    root . left . left = Node ( 4 )
    root . left . left . left = Node ( 21 )
    root . left . right = Node ( 5 )
    root . left . right . right = Node ( 8 )
    root . right = Node ( 3 )
    root . right . left = Node ( 6 )
    root . right . right = Node ( 7 )
    root . right . right . right = Node ( 19 )
    KSmallest ( root , 3 )

Index/key variables: i
Iterator variables: i
Class/struct names: Node
Python program:
def findString ( s , k ) :
    n = len ( s )
    ans = " "
    st = [ ]
    vis = [ False ] * n
    count = 0
    List < bool > vis ( n , false ) ;
    for i in range ( n ) :
        if ( s [ i ] == ' ( ' ) :
            st . append ( i )
        if ( count < k and s [ i ] == ' ) ' ) :
            vis [ st [ - 1 ] ] = 1
            del st [ - 1 ]
            vis [ i ] = True
            count += 2
    for i in range ( n ) :
        if ( vis [ i ] == True ) :
            ans += s [ i ]
    return ans
if __name__ == ' _ _ main _ _ ' :
    s = " ( ) ( ) ( ) "
    K = 2
    print ( findString ( s , K ) )

Index/key variables: i
Accumulator variables: ans, count, st
Iterator variables: i
Python program:
max_v = 0
rootIndex = 0
mp = { }
class newNode :
    def __init__ ( self , key ) :
        self . key = key
        self . left = None
        self . right = None
def findNodes ( root ) :
    global max_v
    global rootIndex
    global mp
    if ( root == None ) :
        return [ ]
    left = findNodes ( root . left )
    right = findNodes ( root . right )
    combined = [ ]
    count = 0
    for i in range ( len ( left ) ) :
        if ( left [ i ] < root . key ) :
            count += 1
        combined . append ( left [ i ] )
    for i in range ( len ( right ) ) :
        if ( right [ i ] < root . key ) :
            count += 1
        combined . append ( right [ i ] )
    combined . append ( root . key )
    if ( count > max_v ) :
        rootIndex = root . key
        max_v = count
    return combined
if __name__ == ' _ _ main _ _ ' :
    root = None
    root = newNode ( 3 )
    root . left = newNode ( 4 )
    root . right = newNode ( 6 )
    root . right . left = newNode ( 4 )
    root . right . right = newNode ( 5 )
    root . left . left = newNode ( 10 )
    root . left . right = newNode ( 2 )
    max_v = 0
    rootIndex = - 1
    findNodes ( root )
    print ( rootIndex )

Index/key variables: i
Accumulator variables: combined, count
Iterator variables: i
Class/struct names: newNode
Python program:
def MaximumPath ( grid ) :
    N = len ( grid )
    M = len ( grid [ 0 ] )
    sum = [ [ 0 for i in range ( M + 1 ) ] for i in range ( N + 1 ) ]
    for i in range ( 1 , N + 1 ) :
        for j in range ( 1 , M + 1 ) :
            sum [ i ] [ j ] = ( max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] )
    return sum [ N ] [ M ]
if __name__ == ' _ _ main _ _ ' :
    grid = [ [ 1 , 2 ] , [ 3 , 5 ] ]
    print ( MaximumPath ( grid ) )

Index/key variables: M, N, i, j
Iterator variables: i, j
Python program:
def Maxcost ( s , cost ) :
    count = 0
    maxcost = 0
    i = 0
    while i < len ( s ) - 1 :
        if s [ i ] == s [ i + 1 ] :
            if cost [ i ] > cost [ i + 1 ] :
                maxcost += cost [ i ]
            else :
                maxcost += cost [ i + 1 ]
                cost [ i + 1 ] = cost [ i ]
        i += 1
    return maxcost
s = " abaac "
cost = [ 1 , 2 , 3 , 4 , 5 ]
print ( Maxcost ( s , cost ) )

Index/key variables: i
Accumulator variables: maxcost
Python program:
dp = { }
def countMagicNumbers ( idx , sum , a , n , m , l , r ) :
    if ( idx == n ) :
        temp = sum % m
        if ( temp == l or temp == r or ( temp > l and temp < r ) ) :
            dp [ ( idx , sum ) ] = 1
            return dp [ ( idx , sum ) ]
        else :
            dp [ ( idx , sum ) ] = 0
            return dp [ ( idx , sum ) ]
    curr = ( idx , sum )
    if ( curr in dp ) :
        return dp [ curr ]
    ls = countMagicNumbers ( idx + 1 , sum + a [ idx ] , a , n , m , l , r )
    rs = countMagicNumbers ( idx + 1 , sum + ( a [ idx ] - 1 ) , a , n , m , l , r )
    temp1 = max ( ls , rs )
    temp = sum % m
    if ( ( temp == l or temp == r or ( temp > l and temp < r ) ) and idx != 0 ) :
        temp1 += 1
    dp [ ( idx , sum ) ] = temp1
    return dp [ ( idx , sum ) ]
if __name__ == ' _ _ main _ _ ' :
    N = 5
    M = 22
    L = 14
    R = 16
    arr = [ 17 , 11 , 10 , 8 , 15 ]
    print ( countMagicNumbers ( 0 , 0 , arr , N , M , L , R ) )

Index/key variables: curr, idx