dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
INF = 99999
def floyd_warshall ( graph , V ) :
    dist = [ [ 0 for i in range ( V ) ] for i in range ( V ) ]
    for i in range ( V ) :
        for j in range ( V ) :
            dist [ i ] [ j ] = graph [ i ] [ j ]
    for k in range ( V ) :
        for i in range ( V ) :
            for j in range ( V ) :
                if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) :
                    dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ]
    sum = 0
    for i in range ( V ) :
        for j in range ( i + 1 , V ) :
            sum += dist [ i ] [ j ]
    return sum
def sumOfshortestPath ( N , E , edges ) :
    g = [ [ INF for i in range ( N ) ] for i in range ( N ) ]
    for i in range ( E ) :
        u = edges [ i ] [ 0 ]
        v = edges [ i ] [ 1 ]
        w = edges [ i ] [ 2 ]
        g [ u ] [ v ] = w
        g [ v ] [ u ] = w
    return floyd_warshall ( g , N )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    E = 3
    Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , 2 ] , [ 2 , 3 , 3 ] ]
    print ( sumOfshortestPath ( N , E , Edges ) )

Index/key variables: i, j, k, u, v
Accumulator variables: sum
Iterator variables: i, j, k
Python program:
import sys
def minPeaks ( list1 ) :
    n = len ( list1 )
    result = [ ]
    for i in range ( n ) :
        min = sys . maxsize
        index = - 1
        size = len ( list1 )
        for j in range ( size ) :
            if ( j == 0 and j + 1 < size ) :
                if ( list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) :
                    min = list1 [ j ] ;
                    index = j ;
            elif ( j == size - 1 and j - 1 >= 0 ) :
                if ( list1 [ j ] > list1 [ j - 1 ] and min > list1 [ j ] ) :
                    min = list1 [ j ]
                    index = j
            elif ( size == 1 ) :
                min = list1 [ j ]
                index = j
            elif ( list1 [ j ] > list1 [ j - 1 ] and list1 [ j ] > list1 [ j + 1 ] and min > list1 [ j ] ) :
                min = list1 [ j ]
                index = j
        list1 . pop ( index )
        result . append ( min )
    print ( result )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 9 , 7 , 8 , 2 , 6 ]
    minPeaks ( arr )

Index/key variables: j
Accumulator variables: result
Iterator variables: i, j
Python program:
def binarySearch ( arr , N , X ) :
    start = 0
    end = N
    while ( start <= end ) :
        mid = start + ( end - start ) // 2
        if ( X == arr [ mid ] ) :
            return mid
        elif ( X < arr [ mid ] ) :
            start = mid + 1
        else :
            end = mid - 1
    return - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 4 , 3 , 2 , 1 ]
    N = len ( arr )
    X = 5
    print ( binarySearch ( arr , N , X ) )

Index/key variables: mid
Python program:
import sys
def check_both_present ( arr , N , a , b ) :
    f1 = False
    f2 = False
    for i in range ( 0 , N ) :
        if arr [ i ] == a :
            f1 = True
        if arr [ i ] == b :
            f2 = True
    if f1 and f2 :
        return True
    else :
        return False
def print_array ( ans , N ) :
    for i in range ( 0 , N ) :
        print ( ans [ i ] , end = " ▁ " )
def build_AP ( N , a , b ) :
    INT_MAX = sys . maxsize
    arr = [ None for i in range ( N ) ]
    ans = [ INT_MAX for i in range ( N ) ]
    flag = 0
    if a > b :
        a , b = b , a
    diff = b - a
    for start in range ( 1 , a + 1 ) :
        for d in range ( 1 , diff + 1 ) :
            arr [ 0 ] = start
            for i in range ( 1 , N ) :
                arr [ i ] = arr [ i - 1 ] + d
            if ( ( check_both_present ( arr , N , a , b ) and arr [ N - 1 ] < ans [ N - 1 ] ) ) :
                for i in range ( 0 , N ) :
                    ans [ i ] = arr [ i ]
    print_array ( ans , N )
if __name__ == " _ _ main _ _ " :
    N = 5
    A = 20
    B = 50
    build_AP ( N , A , B )

Index/key variables: i
Iterator variables: d, i, start
Boolean variables: f1, f2
Python program:
def isPrefixOfWord ( sentence , word ) :
    a = sentence . split ( " ▁ " )
    v = [ ]
    for i in a :
        v . append ( i )
    for i in range ( len ( v ) ) :
        for j in range ( len ( v [ i ] ) ) :
            if ( v [ i ] [ j ] != word [ j ] ) :
                break
            elif ( j == len ( word ) - 1 ) :
                return v [ i ]
    return - 1
s = " Welcome ▁ to ▁ Geeksforgeeks "
word = " Gee "
print ( isPrefixOfWord ( s , word ) )

Index/key variables: i, j
Accumulator variables: v
Iterator variables: i, j
Python program:
from queue import Queue
import sys
class Pair ( object ) :
    idx = 0
    psf = " "
    jmps = 0
    def __init__ ( self , idx , psf , jmps ) :
        self . idx = idx
        self . psf = psf
        self . jmps = jmps
def minJumps ( arr ) :
    MAX_VALUE = sys . maxsize
    dp = [ MAX_VALUE for i in range ( len ( arr ) ) ]
    n = len ( dp )
    dp [ n - 1 ] = 0
    for i in range ( n - 2 , - 1 , - 1 ) :
        steps = arr [ i ]
        minimum = MAX_VALUE
        for j in range ( 1 , steps + 1 , 1 ) :
            if i + j >= n :
                break
            if ( ( dp [ i + j ] != MAX_VALUE ) and ( dp [ i + j ] < minimum ) ) :
                minimum = dp [ i + j ]
        if minimum != MAX_VALUE :
            dp [ i ] = minimum + 1
    return dp
def possiblePath ( arr , dp ) :
    queue = Queue ( maxsize = 0 )
    p1 = Pair ( 0 , "0" , dp [ 0 ] )
    queue . put ( p1 )
    while queue . qsize ( ) > 0 :
        tmp = queue . get ( )
        if tmp . jmps == 0 :
            print ( tmp . psf )
            continue
        for step in range ( 1 , arr [ tmp . idx ] + 1 , 1 ) :
            if ( ( tmp . idx + step < len ( arr ) ) and ( tmp . jmps - 1 == dp [ tmp . idx + step ] ) ) :
                p2 = Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + str ( ( tmp . idx + step ) ) , tmp . jmps - 1 )
                queue . put ( p2 )
def Solution ( arr ) :
    dp = minJumps ( arr )
    possiblePath ( arr , dp )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 ]
    size = len ( arr )
    Solution ( arr )

Index/key variables: i
Iterator variables: i, j, step
Class/struct names: Pair
Python program:
def EvenLIS ( arr ) :
    n = len ( arr )
    lis = [ 1 ] * n
    for i in range ( 1 , n ) :
        for j in range ( 0 , i ) :
            if abs ( arr [ i ] ) > abs ( arr [ j ] ) and abs ( arr [ i ] % 2 ) == 0 and abs ( arr [ j ] % 2 ) == 0 and lis [ i ] < lis [ j ] + 1 :
                lis [ i ] = lis [ j ] + 1
    maxlen = 0
    for i in range ( n ) :
        maxlen = max ( maxlen , lis [ i ] )
    print ( maxlen )
arr = [ 11 , - 22 , 43 , - 54 , 66 , 5 ]
EvenLIS ( arr )

Index/key variables: i, j
Iterator variables: i, j
Python program:
class Node :
    def __init__ ( self , val ) :
        self . data = val
        self . left = None
        self . right = None
def Sum ( root ) :
    global res
    if ( root == None ) :
        return 0
    leftSubSum = Sum ( root . left )
    rightSubSum = Sum ( root . right )
    if ( root . left and root . right and leftSubSum == rightSubSum ) :
        res += 1
    return ( root . data + leftSubSum + rightSubSum )
root = Node ( 9 )
root . left = Node ( 2 )
root . left . left = Node ( - 1 )
root . left . right = Node ( 3 )
root . right = Node ( 4 )
root . right . right = Node ( 0 )
global res
res = 0
Sum ( root )
print ( res )

Class/struct names: Node
Python program:
def countingSubarray ( arr , n ) :
    a = [ 0 for i in range ( n ) ]
    nsml = [ - 1 for i in range ( n ) ]
    nsmr = [ n for i in range ( n ) ]
    st = [ ]
    for i in range ( n - 1 , - 1 , - 1 ) :
        while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) :
            del st [ - 1 ]
        if ( len ( st ) > 0 ) :
            nsmr [ i ] = st [ - 1 ]
        else :
            nsmr [ i ] = n
        st . append ( i )
    while ( len ( st ) > 0 ) :
        del st [ - 1 ]
    for i in range ( n ) :
        while ( len ( st ) > 0 and arr [ st [ - 1 ] ] >= arr [ i ] ) :
            del st [ - 1 ]
        if ( len ( st ) > 0 ) :
            nsml [ i ] = st [ - 1 ]
        else :
            nsml [ i ] = - 1
        st . append ( i )
    for i in range ( n ) :
        nsml [ i ] += 1
        nsmr [ i ] -= 1
        r = nsmr [ i ] - i + 1 ;
        l = i - nsml [ i ] + 1 ;
        a [ i ] = r * l ;
    return a ;
N = 5
arr = [ 3 , 2 , 4 , 1 , 5 ]
a = countingSubarray ( arr , N )
print ( a )

Index/key variables: i
Accumulator variables: st
Iterator variables: i
Python program:
def reorderString ( A , B ) :
    size_a = len ( A )
    size_b = len ( B )
    freq = [ 0 ] * 300
    for i in range ( size_b ) :
        freq [ ord ( B [ i ] ) ] += 1
    for i in range ( size_a ) :
        freq [ ord ( A [ i ] ) ] -= 1
    for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) :
        if ( freq [ j ] < 0 ) :
            return " - 1"
    answer = [ ]
    for j in range ( ord ( ' a ' ) , ord ( A [ 0 ] ) ) :
        while ( freq [ j ] > 0 ) :
            answer . append ( j )
            freq [ j ] -= 1
    first = A [ 0 ]
    for j in range ( size_a ) :
        if ( A [ j ] > A [ 0 ] ) :
            break
        if ( A [ j ] < A [ 0 ] ) :
            answer += A
            A = " "
            break
    while ( freq [ ord ( first ) ] > 0 ) :
        answer . append ( first )
        freq [ ord ( first ) ] -= 1
    answer += A
    for j in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) :
        while ( freq [ j ] ) :
            answer . append ( chr ( j ) )
            freq [ j ] -= 1
    return " " . join ( answer )
if __name__ == ' _ _ main _ _ ' :
    A = " aa "
    B = " ababab "
    print ( reorderString ( A , B ) )

Index/key variables: i, j
Accumulator variables: answer
Iterator variables: i, j
Python program:
def func ( n ) :
    count = 0
    if n == 2 or n == 1 :
        return 1
    if n % 2 == 0 :
        count = 1 + func ( n // 2 )
    if n % 2 != 0 :
        count = 1 + func ( n * 3 + 1 )
    return count
def findKthElement ( l , r , k ) :
    arr = list ( range ( l , r + 1 ) )
    result = [ ]
    for i in arr :
        result . append ( [ i , func ( i ) ] )
    result . sort ( )
    print ( result [ k - 1 ] [ 0 ] )
l = 12
r = 15
k = 2
findKthElement ( l , r , k )

Accumulator variables: result
Iterator variables: i
Python program:
def checkHex ( s ) :
    for ch in s :
        if ( ( ch < '0' or ch > '9' ) and ( ch < ' A ' or ch > ' F ' ) ) :
            print ( " No " )
            return
    print ( " Yes " )
s = " BF57C "
checkHex ( s )

Iterator variables: ch
Python program:
import math
def checkSumOfNatural ( n ) :
    i = 1
    flag = False
    while i * ( i + 1 ) < n * 2 :
        X = i * ( i + 1 )
        t = n * 2 - X
        k = int ( math . sqrt ( t ) )
        if k * ( k + 1 ) == t :
            flag = True
            break
        i += 1
    if flag :
        print ( ' YES ' )
    else :
        print ( ' NO ' )
if __name__ == " _ _ main _ _ " :
    n = 25
    checkSumOfNatural ( n )

Accumulator variables: i
Boolean variables: flag
Python program:
def binomialCoeff ( n , k ) :
    res = 1
    if ( k > n - k ) :
        k = n - k
    for i in range ( k ) :
        res = res * ( n - i )
        res = res // ( i + 1 )
    return res
def catalan ( n ) :
    c = binomialCoeff ( 2 * n , n )
    return c // ( n + 1 )
def findWays ( n ) :
    if ( ( n & 1 ) == 1 ) :
        return 0
    return catalan ( n // 2 )
def countNonNPeriodic ( N ) :
    print ( findWays ( 2 * N ) - findWays ( N ) )
N = 4
countNonNPeriodic ( N )

Iterator variables: i
Python program:
def checkSameDiag ( x , y ) :
    for i in range ( m ) :
        for j in range ( n ) :
            if li [ i ] [ j ] == x :
                I , J = i , j
            if li [ i ] [ j ] == y :
                P , Q = i , j
    if P - Q == I - J or P + Q == I + J :
        print ( " YES " )
    else :
        print ( " NO " )
if __name__ == " _ _ main _ _ " :
    m , n = 6 , 5
    li = [ [ 32 , 94 , 99 , 26 , 82 ] , [ 51 , 69 , 52 , 63 , 17 ] , [ 90 , 36 , 88 , 55 , 33 ] , [ 93 , 42 , 73 , 39 , 28 ] , [ 81 , 31 , 83 , 53 , 10 ] , [ 12 , 29 , 85 , 80 , 87 ] ]
    x , y = 42 , 80
    checkSameDiag ( x , y )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def isprm ( n ) :
    if ( n <= 1 ) :
        return 0
    if ( n <= 3 ) :
        return 1
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return 0
    i = 5
    while ( i * i <= n ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return 0
        i = i + 6
    return 1
def countprime ( n ) :
    count = 0
    primevector = [ ]
    for i in range ( 2 , n + 1 ) :
        if ( isprm ( i ) == 1 ) :
            primevector . append ( i )
    sum = primevector [ 0 ]
    for i in range ( 1 , len ( primevector ) ) :
        sum += primevector [ i ]
        if ( sum > n ) :
            break
        if ( isprm ( sum ) == 1 ) :
            count += 1
    return count
N = 45
print ( countprime ( N ) )

Index/key variables: i
Accumulator variables: count, primevector, sum
Iterator variables: i
Python program:
from math import sqrt , ceil , floor
def isPerfectSquare ( n ) :
    sr = sqrt ( n )
    return ( ( sr - floor ( sr ) ) == 0 )
def SubarrayHavingPerfectSquare ( arr , k ) :
    ans = [ 0 , 0 ]
    sum = 0
    i = 0
    while i < k :
        sum += arr [ i ]
        i += 1
    found = False
    if ( isPerfectSquare ( sum ) ) :
        ans [ 0 ] = 0
        ans [ 1 ] = i - 1
    else :
        for j in range ( i , len ( arr ) ) :
            sum = sum + arr [ j ] - arr [ j - k ]
            if ( isPerfectSquare ( sum ) ) :
                found = True
                ans [ 0 ] = j - k + 1
                ans [ 1 ] = j
        for k in range ( ans [ 0 ] , ans [ 1 ] + 1 ) :
            print ( arr [ k ] , end = " ▁ " )
    if ( found == False ) :
        print ( " - 1" )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 20 , 34 , 51 , 10 , 99 , 87 , 23 , 45 ]
    K = 3
    SubarrayHavingPerfectSquare ( arr , K )

Index/key variables: i, j, k
Accumulator variables: sum
Iterator variables: j, k
Boolean variables: found
Python program:
TEN = 10
def updateFreq ( n , freq ) :
    while ( n ) :
        digit = n % TEN
        freq [ digit ] += 1
        n //= TEN
def areAnagrams ( a , b ) :
    freqA = [ 0 ] * ( TEN )
    freqB = [ 0 ] * ( TEN )
    updateFreq ( a , freqA )
    updateFreq ( b , freqB )
    for i in range ( TEN ) :
        if ( freqA [ i ] != freqB [ i ] ) :
            return False
    return True
def isPowerOf2 ( N ) :
    for i in range ( 32 ) :
        if ( areAnagrams ( 1 << i , N ) ) :
            print ( 1 << i )
            return True
    return False
N = 46
if ( isPowerOf2 ( N ) == 0 ) :
    print ( " No " )

Index/key variables: digit, i
Accumulator variables: n
Iterator variables: i
Python program:
def right_angled ( n ) :
    count = 0
    for z in range ( 1 , n + 1 ) :
        for y in range ( 1 , z + 1 ) :
            for x in range ( 1 , y + 1 ) :
                if ( ( x * x ) + ( y * y ) == ( z * z ) ) :
                    count += 1
    return count
n = 5
print ( right_angled ( n ) )

Accumulator variables: count
Iterator variables: x, y, z
Python program:
def mirrorMatrix ( mat1 , mat2 , N ) :
    row = 0
    col = 0
    isMirrorImage = True
    for i in range ( N ) :
        for j in range ( N - 1 , - 1 , - 1 ) :
            if ( mat2 [ row ] [ col ] != mat1 [ i ] [ j ] ) :
                isMirrorImage = False
            col += 1
        col = 0
        row += 1
    if ( isMirrorImage ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    mat1 = [ [ 1 , 2 , 3 , 4 ] , [ 0 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ]
    mat2 = [ [ 4 , 3 , 2 , 1 ] , [ 8 , 7 , 6 , 0 ] , [ 12 , 11 , 10 , 9 ] , [ 16 , 15 , 14 , 13 ] ]
    mirrorMatrix ( mat1 , mat2 , N )

Index/key variables: col, i, j, row
Iterator variables: i, j
Boolean variables: isMirrorImage
Python program:
def dfs ( src , dest , vis , adj ) :
    vis [ src ] = 1
    if ( src == dest ) :
        return 1
    for u in adj [ src ] :
        if not vis [ u ] :
            temp = dfs ( u , dest , vis , adj )
            if ( temp != 0 ) :
                return temp + 1
    return 0
def countNodes ( V , E , src , dest , edges ) :
    adj = [ [ ] for i in range ( V + 1 ) ]
    for i in range ( E ) :
        adj [ edges [ i ] [ 0 ] ] . append ( edges [ i ] [ 1 ] )
        adj [ edges [ i ] [ 1 ] ] . append ( edges [ i ] [ 0 ] )
    vis = [ 0 ] * ( V + 1 )
    count = dfs ( src , dest , vis , adj )
    return count - 2
if __name__ == ' _ _ main _ _ ' :
    V = 8
    E = 7
    src = 5
    dest = 2
    edges = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 4 , 2 ] , [ 2 , 6 ] , [ 6 , 3 ] , [ 2 , 7 ] , [ 3 , 8 ] ]
    print ( countNodes ( V , E , src , dest , edges ) )

Index/key variables: i, src, u
Iterator variables: i, u
Python program:
def GCD ( a , b ) :
    if ( a == 0 ) :
        return b
    return GCD ( b % a , a )
def LCM ( a , b ) :
    return ( a * b ) // GCD ( a , b )
def checkPairwiseCoPrime ( A , n ) :
    prod = 1
    lcm = 1
    for i in range ( n ) :
        prod *= A [ i ]
        lcm = LCM ( A [ i ] , lcm )
    if ( prod == lcm ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 2 , 3 , 5 ]
    n = len ( A )
    checkPairwiseCoPrime ( A , n )

Index/key variables: i
Accumulator variables: prod
Iterator variables: i
Python program:
import sys
def maxDiffSubsets ( arr ) :
    totalSum = 0
    pos = False
    neg = False
    min = sys . maxsize
    for i in range ( len ( arr ) ) :
        totalSum += abs ( arr [ i ] )
        if ( arr [ i ] > 0 ) :
            pos = True
        if ( arr [ i ] < 0 ) :
            neg = True
        if ( arr [ i ] < min ) :
            min = arr [ i ]
    if ( pos and neg ) :
        return totalSum
    else :
        return totalSum - 2 * min
if __name__ == ' _ _ main _ _ ' :
    S = [ 1 , 2 , 1 ]
    N = len ( S )
    if ( N < 2 ) :
        print ( " Not ▁ Possible " )
    else :
        print ( maxDiffSubsets ( S ) )

Index/key variables: i
Accumulator variables: totalSum
Iterator variables: i
Boolean variables: neg, pos
Python program:
import sys
def minDiffSubArray ( arr , n ) :
    prefix_sum = [ 0 ] * n
    prefix_sum [ 0 ] = arr [ 0 ]
    for i in range ( 1 , n ) :
        prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i ] )
    suffix_sum = [ 0 ] * n
    suffix_sum [ n - 1 ] = arr [ n - 1 ]
    for i in range ( n - 2 , - 1 , - 1 ) :
        suffix_sum [ i ] = ( suffix_sum [ i + 1 ] + arr [ i ] )
    minDiff = sys . maxsize
    for i in range ( n - 1 ) :
        diff = abs ( prefix_sum [ i ] - suffix_sum [ i + 1 ] )
        if ( diff < minDiff ) :
            minDiff = diff
    return minDiff
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 9 , 5 , 10 ]
    n = len ( arr )
    print ( minDiffSubArray ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def dailyTemperatures ( T ) :
    n = len ( T )
    daysOfWait = [ - 1 ] * n
    s = [ ]
    for i in range ( n ) :
        while ( len ( s ) != 0 and T [ s [ - 1 ] ] < T [ i ] ) :
            daysOfWait [ s [ - 1 ] ] = i - s [ - 1 ]
            s . pop ( - 1 )
        s . append ( i )
    for i in range ( n ) :
        print ( daysOfWait [ i ] , end = " ▁ " )
arr = [ 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 ]
dailyTemperatures ( arr )

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
ans = 0
allsum = 0
edges = [ [ ] for i in range ( 100001 ) ]
def addedge ( a , b ) :
    global edges
    edges [ a ] . append ( b )
    edges [ b ] . append ( a )
def findCost ( r , p , arr ) :
    global edges
    global ans
    global allsum
    i = 0
    for i in range ( len ( edges [ r ] ) ) :
        cur = edges [ r ] [ i ]
        if ( cur == p ) :
            continue
        findCost ( cur , r , arr )
        arr [ r ] += arr [ cur ]
    t1 = arr [ r ]
    t2 = allsum - t1
    if ( t1 * t2 > ans ) :
        ans = t1 * t2
def maximumCost ( r , p , N , M , arr , Edges ) :
    global allsum
    for i in range ( N ) :
        allsum += arr [ i ]
    for i in range ( M ) :
        addedge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] )
    findCost ( r , p , arr )
if __name__ == ' _ _ main _ _ ' :
    N = 6
    arr = [ 13 , 8 , 7 , 4 , 5 , 9 ]
    M = 5
    Edges = [ [ 0 , 1 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] ]
    maximumCost ( 1 , - 1 , N , M , arr , Edges )
    print ( ans )

Index/key variables: a, b, cur, i, r
Accumulator variables: allsum
Iterator variables: i
Python program:
from collections import defaultdict
X = 1
diameter = 0
mp = defaultdict ( lambda : 0 )
def dfs ( current_node , prev_node , len , add_to_map , adj ) :
    global diameter , X
    if len > diameter :
        diameter = len
        X = current_node
    if add_to_map and len == diameter :
        mp [ current_node ] = 1
    for it in adj [ current_node ] :
        if it != prev_node :
            dfs ( it , current_node , len + 1 , add_to_map , adj )
def dfsUtility ( adj ) :
    dfs ( 1 , - 1 , 0 , 0 , adj )
    farthest_node = X
    dfs ( farthest_node , - 1 , 0 , 0 , adj )
    dfs ( farthest_node , - 1 , 0 , 1 , adj )
    dfs ( X , - 1 , 0 , 1 , adj )
def printDiameters ( adj ) :
    global diameter
    dfsUtility ( adj )
    for i in range ( 1 , 6 + 1 ) :
        if mp [ i ] == 1 :
            print ( diameter + 1 , end = " , ▁ " )
        else :
            print ( diameter , end = " , ▁ " )
adj = [ ]
for i in range ( 7 ) :
    adj . append ( [ ] )
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 [ 2 ] . append ( 6 )
adj [ 6 ] . append ( 2 )
printDiameters ( adj )

Index/key variables: current_node, i
Accumulator variables: adj
Iterator variables: i, it
Python program:
inf = 10 ** 18
def firstOccurance ( numbers , length , searchnum ) :
    answer = - 1
    start = 0
    end = length - 1
    while start <= end :
        middle = ( start + end ) // 2
        if numbers [ middle ] == searchnum :
            answer = middle
            end = middle - 1
        elif numbers [ middle ] > searchnum :
            end = middle - 1
        else :
            start = middle + 1
    return answer
def minimumReplacement ( arr , N , K ) :
    ans = inf
    max_values = [ ]
    min_values = [ ]
    sum_equal_to_x = dict ( )
    for i in range ( N // 2 ) :
        mn = min ( arr [ i ] , arr [ N - i - 1 ] )
        mx = max ( arr [ i ] , arr [ N - i - 1 ] )
        if ( arr [ i ] + arr [ N - i - 1 ] not in sum_equal_to_x ) :
            sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] = 0
        sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] += 1
        min_values . append ( mn )
        max_values . append ( mx )
    max_values . sort ( )
    min_values . sort ( )
    for x in range ( 2 , 2 * K + 1 ) :
        mp1 = firstOccurance ( max_values , len ( max_values ) , x - K )
        mp2 = firstOccurance ( min_values , len ( min_values ) , x )
        rep2 = mp1 + ( N // 2 - mp2 )
        if x in sum_equal_to_x :
            rep0 = sum_equal_to_x [ x ]
        else :
            rep0 = 0
        rep1 = ( N // 2 - rep2 - rep0 )
        ans = min ( ans , rep2 * 2 + rep1 )
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 4
    K = 3
    arr = [ 1 , 2 , 2 , 1 ]
    print ( minimumReplacement ( arr , N , K ) )

Index/key variables: i, middle, x
Accumulator variables: max_values, min_values
Iterator variables: i, x