dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
mp = dict ( )
class Node :
    def __init__ ( self , key ) :
        self . key = key
        self . left = None
        self . right = None
def newNode ( key ) :
    temp = Node ( key )
    return temp
def deleteNode ( nodeVal ) :
    if nodeVal in mp :
        return True
    else :
        return False
def treePruning ( root , result ) :
    if ( root == None ) :
        return None ;
    root . left = treePruning ( root . left , result ) ;
    root . right = treePruning ( root . right , result ) ;
    if ( deleteNode ( root . key ) ) :
        if ( root . left ) :
            result . append ( root . left ) ;
        if ( root . right ) :
            result . append ( root . right ) ;
        return None ;
    return root ;
def printInorderTree ( root ) :
    if ( root == None ) :
        return ;
    printInorderTree ( root . left ) ;
    print ( root . key , end = ' ▁ ' )
    printInorderTree ( root . right ) ;
def printForests ( root , arr , n ) :
    for i in range ( n ) :
        mp [ arr [ i ] ] = True ;
    result = [ ]
    if ( treePruning ( root , result ) ) :
        result . append ( root ) ;
    for i in range ( len ( result ) ) :
        printInorderTree ( result [ i ] ) ;
        print ( )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 ) ;
    root . left = newNode ( 12 ) ;
    root . right = newNode ( 13 ) ;
    root . right . left = newNode ( 14 ) ;
    root . right . right = newNode ( 15 ) ;
    root . right . left . left = newNode ( 21 ) ;
    root . right . left . right = newNode ( 22 ) ;
    root . right . right . left = newNode ( 23 ) ;
    root . right . right . right = newNode ( 24 ) ;
    arr = [ 14 , 23 , 1 ]
    n = len ( arr )
    printForests ( root , arr , n ) ;

Index/key variables: i
Iterator variables: i
Class/struct names: Node
Python program:
def knapSack ( nums , S ) :
    sum = 0 ;
    for i in range ( len ( nums ) ) :
        sum += nums [ i ] ;
    if ( sum < S or - sum > - S or ( S + sum ) % 2 == 1 ) :
        return 0 ;
    dp = [ 0 ] * ( ( ( S + sum ) // 2 ) + 1 ) ;
    dp [ 0 ] = 1 ;
    for j in range ( len ( nums ) ) :
        for i in range ( len ( dp ) - 1 , nums [ j ] - 1 , - 1 ) :
            dp [ i ] += dp [ i - nums [ j ] ] ;
    return dp [ len ( dp ) - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    S = 3 ;
    arr = [ 1 , 2 , 3 , 4 , 5 ] ;
    answer = knapSack ( arr , S ) ;
    print ( answer ) ;

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def maxDistance ( arr , K ) :
    map = { }
    maxDist = 0
    for i in range ( len ( arr ) ) :
        if not arr [ i ] in map :
            map [ arr [ i ] ] = i
        if arr [ i ] + K in map :
            maxDist = max ( maxDist , i - map [ arr [ i ] + K ] )
        if arr [ i ] - K in map :
            maxDist = max ( maxDist , i - map [ arr [ i ] - K ] )
    if maxDist == 0 :
        return - 1
    else :
        return maxDist
arr = [ 11 , 2 , 3 , 8 , 5 , 2 ]
K = 2
print ( maxDistance ( arr , K ) )

Index/key variables: i
Iterator variables: i
Python program:
def solve ( n ) :
    ans = 0
    y = n + 1
    while ( y <= n * n + n ) :
        if ( ( n * n ) % ( y - n ) == 0 ) :
            ans += 1
        y += 1
    print ( ans )
n = 5
solve ( n )

Accumulator variables: ans, y
Python program:
adj = [ [ ] for i in range ( 100005 ) ]
visited = [ 0 for i in range ( 100005 ) ]
ans = 0 ;
def dfs ( node , count , m , arr , k ) :
    global ans
    visited [ node ] = 1 ;
    if ( arr [ node - 1 ] == k ) :
        count += 1 ;
    else :
        count = 0 ;
    if ( count > m ) :
        return ;
    if ( len ( adj [ node ] ) == 1 and node != 1 ) :
        ans += 1
    for x in adj [ node ] :
        if ( not visited [ x ] ) :
            dfs ( x , count , m , arr , k ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 1 , 3 , 2 , 1 , 2 , 1 ]
    N = 7
    K = 2
    M = 2
    adj [ 1 ] . append ( 2 ) ;
    adj [ 2 ] . append ( 1 ) ;
    adj [ 1 ] . append ( 3 ) ;
    adj [ 3 ] . append ( 1 ) ;
    adj [ 2 ] . append ( 4 ) ;
    adj [ 4 ] . append ( 2 ) ;
    adj [ 2 ] . append ( 5 ) ;
    adj [ 5 ] . append ( 2 ) ;
    adj [ 3 ] . append ( 6 ) ;
    adj [ 6 ] . append ( 3 ) ;
    adj [ 3 ] . append ( 7 ) ;
    adj [ 7 ] . append ( 3 ) ;
    counter = 0 ;
    dfs ( 1 , counter , M , arr , K ) ;
    print ( ans )

Index/key variables: node, x
Iterator variables: x
Python program:
def reverse ( num ) :
    rev = 0 ;
    while ( num > 0 ) :
        r = int ( num % 10 ) ;
        rev = rev * 10 + r ;
        num = num // 10 ;
    return rev ;
def count ( N , A , B ) :
    l = int ( pow ( 10 , N - 1 ) ) ;
    r = int ( pow ( 10 , N ) - 1 ) ;
    if ( l == 1 ) :
        l = 0 ;
    ans = 0 ;
    for i in range ( l , r + 1 ) :
        even_sum = 0 ;
        odd_sum = 0 ;
        itr = 0 ;
        num = reverse ( i ) ;
        while ( num > 0 ) :
            if ( itr % 2 == 0 ) :
                odd_sum += num % 10 ;
            else :
                even_sum += num % 10 ;
            num = num // 10 ;
            itr += 1 ;
        if ( even_sum % A == 0 and odd_sum % B == 0 ) :
            ans += 1 ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    N = 2 ;
    A = 5 ;
    B = 3 ;
    print ( count ( N , A , B ) ) ;

Accumulator variables: ans, even_sum, itr, odd_sum
Iterator variables: i
Python program:
def calc_distance ( A , B , n ) :
    distance_traveled_A = 0
    distance_traveled_B = 0
    answer = 0
    for i in range ( 5 ) :
        distance_traveled_A += A [ i ]
        distance_traveled_B += B [ i ]
        if ( ( distance_traveled_A == distance_traveled_B ) and ( A [ i ] == B [ i ] ) ) :
            answer += A [ i ]
    return answer
A = [ 1 , 2 , 3 , 2 , 4 ]
B = [ 2 , 1 , 3 , 1 , 4 ]
N = len ( A )
print ( calc_distance ( A , B , N ) )

Index/key variables: i
Accumulator variables: answer, distance_traveled_A, distance_traveled_B
Iterator variables: i
Python program:
def constructTree ( n , edges ) :
    adjl = [ ]
    for i in range ( n ) :
        adjl . append ( [ ] )
    for i in range ( len ( edges ) ) :
        u = edges [ i ] [ 0 ]
        v = edges [ i ] [ 1 ]
        adjl [ u ] . append ( v )
        adjl [ v ] . append ( u )
    return adjl
def DFS ( adjl , parent , u , p ) :
    parent [ u ] = p
    for v in adjl [ u ] :
        if ( v != p ) :
            DFS ( adjl , parent , v , u )
def valuesFromChildren ( parent , values ) :
    valuesChildren = [ 0 ] * ( len ( parent ) )
    for i in range ( len ( parent ) ) :
        if ( parent [ i ] == - 1 ) :
            continue
        else :
            p = parent [ i ]
            valuesChildren [ p ] += values [ i ]
    return valuesChildren
def findSumOfValues ( v , parent , valuesChildren ) :
    cur_node = v
    Sum = 0
    while ( cur_node != - 1 ) :
        Sum += valuesChildren [ cur_node ]
        cur_node = parent [ cur_node ]
    return Sum
n = 8
edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 4 , 7 ] , [ 3 , 6 ] ]
v = 7
values = [ 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 ]
adjl = constructTree ( n , edges )
parent = [ 0 ] * ( n )
DFS ( adjl , parent , 0 , - 1 )
valuesChildren = valuesFromChildren ( parent , values )
Sum = findSumOfValues ( v , parent , valuesChildren )
Sum += values [ 0 ]
print ( Sum )

Index/key variables: cur_node, i, p, u, v
Accumulator variables: Sum, adjl
Iterator variables: i, v
Python program:
def maxProduct ( arr , N ) :
    if ( N < 3 ) :
        return ;
    a = arr [ 0 ] ; b = arr [ 1 ] ;
    c = 0 ; d = 0 ;
    for i in range ( 0 , N , 1 ) :
        for j in range ( i + 1 , N - 1 , 1 ) :
            if ( arr [ i ] * arr [ j ] > a * b ) :
                c = a ;
                d = b ;
                a = arr [ i ] ;
                b = arr [ j ] ;
            if ( arr [ i ] * arr [ j ] < a * b and arr [ i ] * arr [ j ] > c * d ) :
                c = arr [ i ] ;
            d = arr [ j ] ;
    print ( c , " ▁ " , d ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ;
    N = len ( arr ) ;
    maxProduct ( arr , N ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
def isVowel ( c ) :
    if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) :
        return True
    return False
def countSubstrings ( s , n ) :
    result = 0
    for i in range ( n ) :
        count = 0
        for j in range ( i , n ) :
            if ( isVowel ( s [ j ] ) ) :
                count += 1
            if ( count % 2 == 0 ) :
                result += 1
    print ( result )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    s = " abcde "
    countSubstrings ( s , n )

Index/key variables: j
Accumulator variables: count, result
Iterator variables: i, j
Python program:
def isVowel ( c ) :
    if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) :
        return True ;
    return False ;
def countSubstrings ( s , n ) :
    temp = [ 1 , 0 ] ;
    result = 0 ;
    sum = 0 ;
    for i in range ( 0 , n ) :
        sum += ( 1 if isVowel ( s [ i ] ) else 0 ) ;
        sum %= 2 ;
        temp [ sum ] += 1 ;
    result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) // 2 ) ;
    result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) // 2 ) ;
    print ( result ) ;
if __name__ == ' _ _ main _ _ ' :
    n = 5 ;
    s = " abcde " ;
    countSubstrings ( s , n ) ;

Index/key variables: i, sum
Iterator variables: i
Python program:
from collections import deque
class Node :
    def __init__ ( self , key ) :
        self . key = key
        self . left = None
        self . right = None
def findLevel ( root : Node , k : int , level : int ) -> int :
    qt = deque ( )
    qt . append ( [ root , 0 ] )
    count = 1
    b = 0
    a = 0
    while qt :
        temp = qt . popleft ( )
        if ( count == 1 ) :
            b = temp [ 1 ]
        if ( temp [ 0 ] . left ) :
            qt . append ( [ temp [ 0 ] . left , 2 * temp [ 1 ] ] )
        if ( temp [ 0 ] . right ) :
            qt . append ( [ temp [ 0 ] . right , 2 * temp [ 1 ] + 1 ] )
        count -= 1
        if ( count == 0 ) :
            if ( b - a + 1 == k ) :
                return level
            secondLabel = qt [ 0 ]
            a = secondLabel [ 1 ]
            level += 1
            count = len ( qt )
    return - 1
if __name__ == " _ _ main _ _ " :
    root = Node ( 5 )
    root . left = Node ( 6 )
    root . right = Node ( 2 )
    root . right . right = Node ( 8 )
    root . left . left = Node ( 7 )
    root . left . left . left = Node ( 5 )
    root . left . right = Node ( 3 )
    root . left . right . right = Node ( 4 )
    k = 4
    print ( findLevel ( root , k , 1 ) )

Accumulator variables: count, level, qt
Class/struct names: Node
Python program:
def possible ( arr , N , mid , K ) :
    add = 0
    for i in range ( N // 2 - ( N + 1 ) % 2 , N ) :
        if ( mid - arr [ i ] > 0 ) :
            add += ( mid - arr [ i ] )
            if ( add > K ) :
                return False
    if ( add <= K ) :
        return True
    else :
        return False
def findMaxMedian ( arr , N , K ) :
    low = 1
    mx = 0
    for i in range ( N ) :
        mx = max ( mx , arr [ i ] )
    high = K + mx
    while ( low <= high ) :
        mid = ( high + low ) // 2
        if ( possible ( arr , N , mid , K ) ) :
            low = mid + 1
        else :
            high = mid - 1
    if ( N % 2 == 0 ) :
        if ( low - 1 < arr [ N // 2 ] ) :
            return ( arr [ N // 2 ] + low - 1 ) // 2
    return low - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 6 ]
    K = 10
    N = len ( arr )
    arr = sorted ( arr )
    print ( findMaxMedian ( arr , N , K ) )

Index/key variables: i
Accumulator variables: add
Iterator variables: i
Python program:
def countLessThanMid ( mid , N , M , K ) :
    count = 0
    for i in range ( 1 , min ( N , mid ) + 1 ) :
        count = count + min ( mid // i , M )
    if ( count >= K ) :
        return False
    else :
        return True
def findKthElement ( N , M , K ) :
    low = 1
    high = N * M
    while ( low <= high ) :
        mid = low + ( high - low ) // 2
        if ( countLessThanMid ( mid , N , M , K ) ) :
            low = mid + 1
        else :
            high = mid - 1
    return high + 1
if __name__ == " _ _ main _ _ " :
    N = 2
    M = 3
    K = 5
    print ( findKthElement ( N , M , K ) )

Iterator variables: i
Python program:
def countSubString ( S , C ) :
    count = 0
    conCount = 0
    for ch in S :
        if ( ch == C ) :
            conCount += 1
        else :
            count += ( ( conCount * ( conCount + 1 ) ) // 2 )
            conCount = 0
    count += ( ( conCount * ( conCount + 1 ) ) // 2 )
    print ( count )
if __name__ == ' _ _ main _ _ ' :
    S = " geeksforgeeks "
    C = ' e '
    countSubString ( S , C )

Accumulator variables: conCount, count
Iterator variables: ch
Python program:
def checkPalindrome ( S ) :
    n = len ( S )
    first_half = True
    second_half = True
    cnt = ( n // 2 ) - 1
    for i in range ( 0 , int ( ( n / 2 ) / 2 ) ) :
        if ( S [ i ] != S [ cnt ] ) :
            first_half = False
            break
        if ( S [ n // 2 + i ] != S [ n // 2 + cnt ] ) :
            second_half = False
            break
        cnt -= 1
    if ( first_half and second_half ) :
        print ( ' Yes ' , end = ' ' )
    else :
        print ( ' No ' , end = ' ' )
if __name__ == ' _ _ main _ _ ' :
    S = ' momdad '
    checkPalindrome ( S )

Index/key variables: cnt, i
Iterator variables: i
Boolean variables: first_half, second_half
Python program:
from collections import defaultdict
def getCount ( N , s ) :
    mp = defaultdict ( lambda : 0 )
    ans = 0
    for i in range ( N ) :
        a = [ 0 ] * 26
        for j in range ( len ( s [ i ] ) ) :
            a [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] += 1
        for j in range ( 26 ) :
            a [ j ] = a [ j ] % 2
        ans += mp [ tuple ( a ) ]
        for j in range ( 26 ) :
            changedCount = a [ : ]
            if ( a [ j ] == 0 ) :
                changedCount [ j ] = 1
            else :
                changedCount [ j ] = 0
            ans += mp [ tuple ( changedCount ) ]
        mp [ tuple ( a ) ] += 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 6
    A = [ " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " ]
    print ( getCount ( N , A ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
kth_smallest = 0
class newNode :
    def __init__ ( self , item ) :
        self . key = item
        self . left = None
        self . right = None
def insert ( node , key ) :
    if ( node == None ) :
        return newNode ( key )
    if ( key < node . key ) :
        node . left = insert ( node . left , key )
    elif ( key > node . key ) :
        node . right = insert ( node . right , key )
    return node
def KSmallestPerfectBST ( root , k , treeSize ) :
    global kth_smallest
    if ( root == None ) :
        return False
    median_loc = ( treeSize // 2 ) + 1
    if ( k == median_loc ) :
        kth_smallest = root . key
        return True
    newTreeSize = treeSize // 2
    if ( k < median_loc ) :
        return KSmallestPerfectBST ( root . left , k , newTreeSize )
    newK = k - median_loc
    return KSmallestPerfectBST ( root . right , newK , newTreeSize )
if __name__ == ' _ _ main _ _ ' :
    root = None
    root = insert ( root , 50 )
    insert ( root , 30 )
    insert ( root , 20 )
    insert ( root , 40 )
    insert ( root , 70 )
    insert ( root , 60 )
    insert ( root , 80 )
    insert ( root , 14 )
    insert ( root , 25 )
    insert ( root , 35 )
    insert ( root , 45 )
    insert ( root , 55 )
    insert ( root , 65 )
    insert ( root , 75 )
    insert ( root , 85 )
    n = 15
    k = 5
    if ( KSmallestPerfectBST ( root , k , n ) ) :
        print ( kth_smallest , end = " ▁ " )

Class/struct names: newNode
Python program:
def isNudeNum ( n ) :
    flag = 0
    copy = n
    temp = str ( copy )
    length = len ( temp )
    for i in range ( length ) :
        num = ord ( temp [ i ] ) - ord ( '0' )
        if ( ( num == 0 ) or ( n % num != 0 ) ) :
            flag = 1
    if ( flag == 1 ) :
        return False
    else :
        return True
def longestNudeSubseq ( arr , n ) :
    answer = 0
    for i in range ( n ) :
        if ( isNudeNum ( arr [ i ] ) ) :
            answer += 1
    return answer
arr = [ 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 ]
n = len ( arr )
print ( longestNudeSubseq ( arr , n ) )

Index/key variables: i
Accumulator variables: answer
Iterator variables: i
Python program:
def longestSubarray ( arr , n , k ) :
    Max = 1
    s = set ( )
    for i in range ( n - 1 ) :
        s . add ( arr [ i ] )
        for j in range ( i + 1 , n ) :
            if ( abs ( arr [ i ] - arr [ j ] ) == 0 or abs ( arr [ i ] - arr [ j ] ) == k ) :
                if ( not arr [ j ] in s ) :
                    if ( len ( s ) == 2 ) :
                        break
                    else :
                        s . add ( arr [ j ] )
            else :
                break
        if ( len ( s ) == 2 ) :
            Max = max ( Max , j - i )
            s . clear ( )
        else :
            s . clear ( )
    return Max
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 0 , 2 , 2 , 5 , 5 , 5 ]
    N = len ( arr )
    K = 1
    length = longestSubarray ( arr , N , K )
    if ( length == 1 ) :
        print ( " - 1" )
    else :
        print ( length )

Index/key variables: i, j
Accumulator variables: s
Iterator variables: i, j
Python program:
def longestSubarray ( a , n ) :
    l = [ 0 ] * ( n )
    r = [ 0 ] * ( n )
    count = 0
    for i in range ( n ) :
        if ( a [ i ] == 1 ) :
            count += 1
        else :
            l [ i ] = count
            count = 0
    count = 0
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( a [ i ] == 1 ) :
            count += 1
        else :
            r [ i ] = count
            count = 0
    ans = - 1
    for i in range ( n ) :
        if ( a [ i ] == 0 ) :
            ans = max ( ans , l [ i ] + r [ i ] )
    return ans < 0 and n or ans
arr = [ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ]
n = len ( arr )
print ( longestSubarray ( arr , n ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def findMaxValue ( N , K ) :
    packages = 0 ;
    maxi = 1 ;
    for i in range ( 1 , K + 1 ) :
        if ( N % i == 0 ) :
            maxi = max ( maxi , i ) ;
    packages = N // maxi ;
    print ( packages ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 8 ;
    K = 7 ;
    findMaxValue ( N , K ) ;

Iterator variables: i
Python program:
def maxSide ( a , n ) :
    sideLength = 0
    a . sort
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( a [ i ] > sideLength ) :
            sideLength += 1
        else :
            break
    print ( sideLength )
N = 6
arr = [ 3 , 2 , 1 , 5 , 2 , 4 ]
maxSide ( arr , N )

Index/key variables: i
Accumulator variables: sideLength
Iterator variables: i
Python program:
def Solution_dfs ( v , color , red , blue , sub_red , sub_blue , vis , adj , ans ) :
    vis [ v ] = 1 ;
    for i in range ( len ( adj [ v ] ) ) :
        if ( vis [ adj [ v ] [ i ] ] == 0 ) :
            ans = Solution_dfs ( adj [ v ] [ i ] , color , red , blue , sub_red , sub_blue , vis , adj , ans ) ;
            sub_red [ v ] += sub_red [ adj [ v ] [ i ] ] ;
            sub_blue [ v ] += sub_blue [ adj [ v ] [ i ] ] ;
    if ( color [ v ] == 1 ) :
        sub_red [ v ] += 1 ;
    if ( color [ v ] == 2 ) :
        sub_blue [ v ] += 1 ;
    if ( sub_red [ v ] == red and sub_blue [ v ] == 0 ) :
        ( ans ) += 1 ;
    if ( sub_red [ v ] == 0 and sub_blue [ v ] == blue ) :
        ( ans ) += 1 ;
    return ans
def countRed ( color , n ) :
    red = 0 ;
    for i in range ( n ) :
        if ( color [ i ] == 1 ) :
            red += 1 ;
    return red ;
def countBlue ( color , n ) :
    blue = 0 ;
    for i in range ( n ) :
        if ( color [ i ] == 2 ) :
            blue += 1
    return blue ;
def buildTree ( edge , m , n ) :
    u , v , i = 0 , 0 , 0
    for i in range ( n - 1 ) :
        u = edge [ i ] [ 0 ] - 1 ;
        v = edge [ i ] [ 1 ] - 1 ;
        if u not in m :
            m [ u ] = [ ]
        if v not in m :
            m [ v ] = [ ]
        m [ u ] . append ( v )
        m [ v ] . append ( u ) ;
def countSubtree ( color , n , edge ) :
    adj = dict ( )
    ans = 0 ;
    sub_red = [ 0 for i in range ( n + 3 ) ]
    sub_blue = [ 0 for i in range ( n + 3 ) ]
    vis = [ 0 for i in range ( n + 3 ) ]
    red = countRed ( color , n ) ;
    blue = countBlue ( color , n ) ;
    buildTree ( edge , adj , n ) ;
    ans = Solution_dfs ( 0 , color , red , blue , sub_red , sub_blue , vis , adj , ans ) ;
    print ( ans , end = ' ' )
if __name__ == ' _ _ main _ _ ' :
    N = 5 ;
    color = [ 1 , 0 , 0 , 0 , 2 ]
    edge = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] , [ 4 , 5 ] ] ;
    countSubtree ( color , N , edge ) ;

Index/key variables: i, u, v
Accumulator variables: blue, red
Iterator variables: i
Python program:
def minDiff ( a , n ) :
    for i in range ( n ) :
        if ( a [ i ] % 2 == 1 ) :
            a . append ( a [ i ] * 2 )
    a = sorted ( a )
    mindifference = a [ 1 ] - a [ 0 ]
    for i in range ( 1 , len ( a ) ) :
        mindifference = min ( mindifference , a [ i ] - a [ i - 1 ] )
    print ( mindifference )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 8 , 13 , 30 , 50 ]
    n = len ( arr )
    minDiff ( arr , n )

Index/key variables: i
Accumulator variables: a
Iterator variables: i
Python program:
v = [ ]
def bin1 ( val , k , n ) :
    global v
    lo = 0
    hi = n
    mid = 0
    ans = - 1
    while ( lo <= hi ) :
        mid = lo + ( ( hi - lo ) // 2 )
        if ( v [ mid ] - val <= k ) :
            lo = mid + 1
            ans = mid
        else :
            hi = mid - 1
    return ans
def findSubarraysum1K ( arr , N , K ) :
    global v
    sum1 = 0
    v . append ( 0 )
    for i in range ( N ) :
        sum1 += arr [ i ]
        v . append ( sum1 )
    l = 0
    ans = 0
    r = 0
    for i in range ( len ( v ) ) :
        r = bin1 ( v [ i ] , K , N )
        ans = max ( ans , r - i )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 6 , 8 , 14 , 9 , 4 , 11 , 10 ]
    N = len ( arr )
    K = 13
    findSubarraysum1K ( arr , N , K )

Index/key variables: i, mid
Accumulator variables: sum1, v
Iterator variables: i
Python program:
def minRev ( edges , n ) :
    graph = dict ( )
    graph_rev = dict ( )
    for i in range ( len ( edges ) ) :
        x = edges [ i ] [ 0 ] ;
        y = edges [ i ] [ 1 ] ;
        if x not in graph :
            graph [ x ] = [ ]
        graph [ x ] . append ( y ) ;
        if y not in graph_rev :
            graph_rev [ y ] = [ ]
        graph_rev [ y ] . append ( x ) ;
    q = [ ]
    visited = [ 0 for i in range ( n ) ]
    q . append ( 0 ) ;
    ans = 0 ;
    while ( len ( q ) != 0 ) :
        curr = q [ 0 ]
        visited [ curr ] = 1 ;
        count = 0 ;
        q . pop ( 0 ) ;
        if curr in graph_rev :
            for i in range ( len ( graph_rev [ curr ] ) ) :
                if ( not visited [ graph_rev [ curr ] [ i ] ] ) :
                    q . append ( graph_rev [ curr ] [ i ] ) ;
        if curr in graph :
            for i in range ( len ( graph [ curr ] ) ) :
                if ( not visited [ graph [ curr ] [ i ] ] ) :
                    q . append ( graph [ curr ] [ i ] ) ;
                    count += 1
        ans += count ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    edges = [ ]
    edges = [ [ 0 , 1 ] , [ 1 , 3 ] , [ 2 , 3 ] , [ 4 , 0 ] , [ 4 , 5 ] ] ;
    n = 6 ;
    print ( minRev ( edges , n ) )

Index/key variables: curr, i, x, y
Accumulator variables: ans, count, q
Iterator variables: i
Python program:
def splitArray ( arr , N ) :
    arr = sorted ( arr )
    result = 10 ** 9
    for i in range ( 1 , N ) :
        result = min ( result , arr [ i ] - arr [ i - 1 ] )
    return result
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 1 , 2 , 6 , 4 ]
    N = len ( arr )
    print ( splitArray ( arr , N ) )

Index/key variables: i
Iterator variables: i