dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from collections import deque
def reachesBottom ( a ) :
    n = len ( a )
    m = len ( a [ 0 ] )
    q = deque ( )
    for i in range ( n ) :
        for j in range ( m ) :
            if ( a [ i ] [ j ] == 1 ) :
                q . appendleft ( [ i , j , 1 ] )
            elif ( a [ i ] [ j ] == 2 ) :
                q . append ( [ i , j , 2 ] )
            a [ i ] [ j ] = 0
    dx = [ - 1 , 0 , 1 , 0 ]
    dy = [ 0 , 1 , 0 , - 1 ]
    while ( len ( q ) > 0 ) :
        front = q . popleft ( )
        i = front [ 0 ]
        j = front [ 1 ]
        t = front [ 2 ]
        if ( a [ i ] [ j ] ) :
            continue
        a [ i ] [ j ] = 1
        if ( t == 1 and ( i == n - 1 and j == m - 1 ) ) :
            return True
        for d in range ( 4 ) :
            ni = i + dx [ d ]
            nj = j + dy [ d ]
            if ( ni >= 0 and ni < n and nj >= 0 and nj < m ) :
                q . append ( [ ni , nj , t ] )
    return False
if __name__ == ' _ _ main _ _ ' :
    matrix = [ [ 0 , 2 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 2 , 0 ] ]
    if ( reachesBottom ( matrix ) ) :
        print ( " YES " )
    else :
        print ( " NO " )

Index/key variables: d, i, j
Accumulator variables: q
Iterator variables: d, i, j
Python program:
import sys
def matrixMinimum ( nums , K ) :
    N = len ( nums )
    M = len ( nums [ 0 ] )
    res = [ [ 0 for x in range ( M - K + 1 ) ] for y in range ( N - K + 1 ) ]
    for i in range ( N ) :
        for j in range ( M - K + 1 ) :
            mn = sys . maxsize
            for k in range ( j , j + K ) :
                mn = min ( mn , nums [ i ] [ k ] )
            nums [ i ] [ j ] = mn
    for j in range ( M ) :
        for i in range ( N - K + 1 ) :
            mn = sys . maxsize
            for k in range ( i , i + K ) :
                mn = min ( mn , nums [ k ] [ j ] )
            nums [ i ] [ j ] = mn
    for i in range ( N - K + 1 ) :
        for j in range ( M - K + 1 ) :
            res [ i ] [ j ] = nums [ i ] [ j ]
    return res
def smallestinKsubmatrices ( arr , K ) :
    res = matrixMinimum ( arr , K )
    for i in range ( len ( res ) ) :
        for j in range ( len ( res [ 0 ] ) ) :
            print ( res [ i ] [ j ] , end = " ▁ " )
        print ( )
arr = [ [ - 1 , 5 , 4 , 1 , - 3 ] , [ 4 , 3 , 1 , 1 , 6 ] , [ 2 , - 2 , 5 , 3 , 1 ] , [ 8 , 5 , 1 , 9 , - 4 ] , [ 12 , 3 , 5 , 8 , 1 ] ]
K = 3
smallestinKsubmatrices ( arr , K )

Index/key variables: i, j, k
Iterator variables: i, j, k
Python program:
def find_kth_largest ( strr , k ) :
    strr = sorted ( strr )
    strr = strr [ : : - 1 ]
    return strr [ k - 1 ]
def printCharacter ( strr , l , r , k ) :
    l = l - 1
    r = r - 1
    temp = strr [ l : r - l + 1 ]
    ans = find_kth_largest ( temp , k )
    return ans
def updateString ( strr , pos , s ) :
    index = pos - 1
    c = s
    strr [ index ] = c
if __name__ == ' _ _ main _ _ ' :
    strr = " abcddef "
    strr = [ i for i in strr ]
    Q = 3
    print ( printCharacter ( strr , 1 , 2 , 2 ) )
    updateString ( strr , 4 , ' g ' )
    print ( printCharacter ( strr , 1 , 5 , 4 ) )

Index/key variables: index
Python program:
import sys
def minDiffSubArray ( arr , n ) :
    total_sum = 0
    for i in range ( n ) :
        total_sum += arr [ i ]
    prefix_sum = 0
    minDiff = sys . maxsize
    for i in range ( n - 1 ) :
        prefix_sum += arr [ i ]
        diff = abs ( ( total_sum - prefix_sum ) - prefix_sum )
        if ( diff < minDiff ) :
            minDiff = diff
    return minDiff
arr = [ 7 , 9 , 5 , 10 ]
n = len ( arr )
print ( minDiffSubArray ( arr , n ) )

Index/key variables: i
Accumulator variables: prefix_sum, total_sum
Iterator variables: i
Python program:
def CtSubarr ( arr , N , K ) :
    st = set ( )
    prefixSum = 0
    st . add ( prefixSum )
    res = 0
    for i in range ( N ) :
        prefixSum += arr [ i ]
        if ( ( prefixSum - K ) in st ) :
            res += 1
            prefixSum = 0
            st . clear ( )
            st . add ( 0 )
        st . add ( prefixSum )
    return res
arr = [ - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 ]
N = len ( arr )
K = 10
print ( CtSubarr ( arr , N , K ) )

Index/key variables: i
Accumulator variables: prefixSum, res, st
Iterator variables: i
Python program:
def is_prime ( n ) :
    if ( n <= 1 ) :
        return 0
    i = 2
    while ( i * i <= n ) :
        if ( n % i == 0 ) :
            return 0
        i += 1
    return 1
def count_prime_subarrays ( ar , n ) :
    ans = 0
    count = 0
    for i in range ( n ) :
        if ( is_prime ( ar [ i ] ) ) :
            count += 1
        else :
            if ( count ) :
                ans += count * ( count + 1 ) // 2
                count = 0
    if ( count ) :
        ans += count * ( count + 1 ) // 2
    return ans
N = 10
ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ]
print ( count_prime_subarrays ( ar , N ) )

Index/key variables: i
Accumulator variables: ans, count
Iterator variables: i
Python program:
def minSteps ( arr , n , k ) :
    cnt = 0
    if ( k == 1 ) :
        flag = 0
    else :
        flag = 1
    for i in range ( n ) :
        if ( arr [ i ] == flag ) :
            cnt += 1
            flag = ( flag + 1 ) % 2
    return cnt
arr = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ]
n = len ( arr )
k = 1
print ( minSteps ( arr , n , k ) )

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i
Python program:
import math , collections
def add_seg ( seg , start , end , current , index ) :
    if ( index > end or index < start ) :
        return
    if ( start == end ) :
        seg [ current ] = 1
        return
    mid = int ( ( start + end ) / 2 )
    add_seg ( seg , start , mid , 2 * current + 1 , index )
    add_seg ( seg , mid + 1 , end , 2 * current + 2 , index )
    seg [ current ] = seg [ 2 * current + 1 ] + seg [ 2 * current + 2 ]
def deleted ( seg , l , r , start , end , current ) :
    if ( end < l or start > r ) :
        return 0
    if ( start >= l and end <= r ) :
        return seg [ current ]
    mid = int ( ( start + end ) / 2 )
    return deleted ( seg , l , r , start , mid , 2 * current + 1 ) + deleted ( seg , l , r , mid + 1 , end , 2 * current + 2 )
def sumOfIndices ( s ) :
    N = len ( s )
    x = ( int ) ( math . ceil ( math . log ( N ) / math . log ( 2 ) ) )
    seg_size = 2 * pow ( 2 , x ) - 1
    segment = [ 0 ] * ( seg_size )
    count = 4
    fre = { }
    for i in range ( N ) :
        key = ( ord ) ( s [ i ] )
        if key in fre :
            que = fre [ key ]
        else :
            que = collections . deque ( [ ] )
        que . append ( i )
        fre [ key ] = que
    while len ( fre ) > 0 :
        it = list ( fre . keys ( ) ) [ 0 ]
        if len ( fre [ it ] ) == 0 :
            del fre [ it ]
        else :
            que = fre [ it ]
            original_index = que [ 0 ]
            curr_index = deleted ( segment , 0 , original_index - 1 , 0 , N - 1 , 0 )
            new_index = original_index - curr_index
            count += new_index + 1
            add_seg ( segment , 0 , N - 1 , 0 , original_index )
            que . popleft ( )
            fre [ it ] = que
    print ( count )
s = " geeksforgeeks "
sumOfIndices ( s )

Index/key variables: current, i, it, key
Accumulator variables: count, que
Iterator variables: i
Python program:
import sys
spf = [ 0 ] * 100005
prime = [ False ] * 100005
dp = [ 0 ] * 100005
def sieve ( ) :
    for i in range ( 2 , 100005 ) :
        prime [ i ] = True
    i = 2
    while i * i < 100005 :
        if ( prime [ i ] ) :
            for j in range ( i * i , 100005 , i ) :
                prime [ j ] = False
        i += 1
def smallestPrimeFactors ( ) :
    for i in range ( 10005 ) :
        spf [ i ] = - 1
    i = 2
    while i * i < 100005 :
        for j in range ( i , 100005 , i ) :
            if ( spf [ j ] == - 1 ) :
                spf [ j ] = i
        i += 1
def sumOfPrimeFactors ( n ) :
    ans = 0
    while ( n > 1 ) :
        ans += spf [ n ]
        n //= spf [ n ]
    return ans
def findLength ( n ) :
    if ( prime [ n ] ) :
        return 1
    if ( dp [ n ] ) :
        return dp [ n ]
    sum = sumOfPrimeFactors ( n )
    dp [ n ] = 1 + findLength ( sum )
    return dp [ n ]
def maxLength ( n , m ) :
    sieve ( )
    smallestPrimeFactors ( )
    ans = - sys . maxsize - 1
    for i in range ( n , m + 1 ) :
        if ( i == 4 ) :
            continue
        ans = max ( ans , findLength ( i ) )
    return ans
n = 2
m = 14
print ( maxLength ( n , m ) )

Index/key variables: i, j, n
Accumulator variables: ans
Iterator variables: i, j
Python program:
from collections import defaultdict
def largest_subarray ( a , n ) :
    index = defaultdict ( lambda : 0 )
    ans = 0
    j = 0
    for i in range ( n ) :
        j = max ( index [ a [ i ] ] , j )
        ans = max ( ans , i - j + 1 )
        index [ a [ i ] ] = i + 1
        i += 1
    return ans
arr = [ 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 ]
n = len ( arr )
print ( largest_subarray ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def dfs ( nums , S , curr_sum , index ) :
    if ( index == len ( nums ) ) :
        if ( S == curr_sum ) :
            return 1 ;
        else :
            return 0 ;
    return ( dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ) ;
def findWays ( nums , S ) :
    return dfs ( nums , S , 0 , 0 ) ;
if __name__ == ' _ _ main _ _ ' :
    S = 3 ;
    arr = [ 1 , 2 , 3 , 4 , 5 ] ;
    answer = findWays ( arr , S ) ;
    print ( answer ) ;

Index/key variables: index
Python program:
mp = { }
class newNode :
    def __init__ ( self , key ) :
        self . key = key
        self . left = None
        self . right = None
def frequencyCounts ( root ) :
    global mp
    if ( root == None ) :
        return
    mp [ root . key ] = mp . get ( root . key , 0 ) + 1
    frequencyCounts ( root . left )
    frequencyCounts ( root . right )
def computeDistance ( root , value ) :
    if ( root == None ) :
        return - 1
    left = computeDistance ( root . left , value )
    right = computeDistance ( root . right , value )
    if ( left == - 1 and right == - 1 ) :
        if ( root . key == value ) :
            return 1
        else :
            return - 1
    if ( left == - 1 ) :
        return right + 1
    if ( right == - 1 ) :
        return left + 1
    else :
        return 1 + max ( left , right )
    return - 1
def solve ( root , dist ) :
    global mp
    frequencyCounts ( root )
    flag = 0
    for key , value in mp . items ( ) :
        if ( value > 1 ) :
            result = computeDistance ( root , key )
            if ( result > dist or result == - 1 ) :
                flag = 1
                break
    if flag == 0 :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 3 )
    root . right . right = newNode ( 4 )
    root . right . left = newNode ( 4 )
    dist = 7
    solve ( root , dist )

Iterator variables: key, value
Class/struct names: newNode
Python program:
import sys
def findMinSum ( A , K ) :
    n = len ( A ) ;
    minimum_sum = sys . maxsize ;
    for i in range ( n ) :
        minimum = sys . maxsize ;
        for j in range ( i + K , n , 1 ) :
            minimum = min ( minimum , A [ j ] ) ;
        if ( minimum == sys . maxsize ) :
            continue ;
        minimum_sum = min ( minimum_sum , A [ i ] + minimum ) ;
    print ( minimum_sum ) ;
if __name__ == ' _ _ main _ _ ' :
    A = [ 4 , 2 , 5 , 4 , 3 , 2 , 5 ] ;
    K = 3 ;
    findMinSum ( A , K ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . next = next
def push ( head_ref , new_data ) :
    new_node = Node ( 0 )
    new_node . data = new_data
    new_node . next = ( head_ref )
    ( head_ref ) = new_node
    return head_ref
def distinctCount ( head_ref1 ) :
    s = set ( )
    ptr1 = head_ref1
    while ( ptr1 != None ) :
        s . add ( abs ( ptr1 . data ) )
        ptr1 = ptr1 . next
    return len ( s )
head1 = None
head1 = push ( head1 , - 1 )
head1 = push ( head1 , - 2 )
head1 = push ( head1 , 0 )
head1 = push ( head1 , 4 )
head1 = push ( head1 , 5 )
head1 = push ( head1 , 8 )
ans = distinctCount ( head1 )
print ( ans )

Accumulator variables: s
Class/struct names: Node
Python program:
from sys import maxsize
from typing import List
found = [ ]
N = 0
cap = [ ]
flow = [ ]
cost = [ ]
dad = [ ]
dist = [ ]
pi = [ ]
INF = maxsize // 2 - 1
def search ( src : int , sink : int ) -> bool :
    found = [ False for _ in range ( N ) ]
    dist = [ INF for _ in range ( N + 1 ) ]
    dist [ src ] = 0
    while ( src != N ) :
        best = N
        found [ src ] = True
        for k in range ( N ) :
            if ( found [ k ] ) :
                continue
            if ( flow [ k ] [ src ] != 0 ) :
                val = ( dist [ src ] + pi [ src ] - pi [ k ] - cost [ k ] [ src ] )
                if ( dist [ k ] > val ) :
                    dist [ k ] = val
                    dad [ k ] = src
            if ( flow [ src ] [ k ] < cap [ src ] [ k ] ) :
                val = ( dist [ src ] + pi [ src ] - pi [ k ] + cost [ src ] [ k ] )
                if ( dist [ k ] > val ) :
                    dist [ k ] = val
                    dad [ k ] = src
            if ( dist [ k ] < dist [ best ] ) :
                best = k
        src = best
    for k in range ( N ) :
        pi [ k ] = min ( pi [ k ] + dist [ k ] , INF )
    return found [ sink ]
def getMaxFlow ( capi : List [ List [ int ] ] , costi : List [ List [ int ] ] , src : int , sink : int ) -> List [ int ] :
    global cap , cost , found , dist , pi , N , flow , dad
    cap = capi
    cost = costi
    N = len ( capi )
    found = [ False for _ in range ( N ) ]
    flow = [ [ 0 for _ in range ( N ) ] for _ in range ( N ) ]
    dist = [ INF for _ in range ( N + 1 ) ]
    dad = [ 0 for _ in range ( N ) ]
    pi = [ 0 for _ in range ( N ) ]
    totflow = 0
    totcost = 0
    while ( search ( src , sink ) ) :
        amt = INF
        x = sink
        while x != src :
            amt = min ( amt , flow [ x ] [ dad [ x ] ] if ( flow [ x ] [ dad [ x ] ] != 0 ) else cap [ dad [ x ] ] [ x ] - flow [ dad [ x ] ] [ x ] )
            x = dad [ x ]
        x = sink
        while x != src :
            if ( flow [ x ] [ dad [ x ] ] != 0 ) :
                flow [ x ] [ dad [ x ] ] -= amt
                totcost -= amt * cost [ x ] [ dad [ x ] ]
            else :
                flow [ dad [ x ] ] [ x ] += amt
                totcost += amt * cost [ dad [ x ] ] [ x ]
            x = dad [ x ]
        totflow += amt
    return [ totflow , totcost ]
if __name__ == " _ _ main _ _ " :
    s = 0
    t = 4
    cap = [ [ 0 , 3 , 1 , 0 , 3 ] , [ 0 , 0 , 2 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 6 ] , [ 0 , 0 , 0 , 0 , 2 ] , [ 0 , 0 , 0 , 0 , 0 ] ]
    cost = [ [ 0 , 1 , 0 , 0 , 2 ] , [ 0 , 0 , 0 , 3 , 0 ] , [ 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 ] ]
    ret = getMaxFlow ( cap , cost , s , t )
    print ( " { } ▁ { } " . format ( ret [ 0 ] , ret [ 1 ] ) )

Index/key variables: best, int, k, sink, src, x
Accumulator variables: totcost, totflow
Iterator variables: k
Python program:
def __gcd ( a , b ) :
    if b == 0 :
        return a
    else :
        return __gcd ( b , a % b )
def lcm ( a , b ) :
    return ( a * b ) // __gcd ( a , b )
def countPair ( N ) :
    count = 0
    for i in range ( 1 , N + 1 ) :
        for j in range ( 1 , N + 1 ) :
            if ( __gcd ( i , j ) + lcm ( i , j ) == N ) :
                count += 1
    return count
if __name__ == " _ _ main _ _ " :
    N = 14
    print ( countPair ( N ) )

Accumulator variables: count
Iterator variables: i, j
Python program:
import sys
MAX = 10
adj = [ [ ] for i in range ( MAX ) ]
def addEdge ( u , v ) :
    adj [ u ] . append ( v )
    adj [ v ] . append ( u )
def dfs ( v , cost , vis , min_cost_node ) :
    vis [ v ] = True
    min_cost_node = min ( min_cost_node , cost [ v - 1 ] )
    for i in range ( len ( adj [ v ] ) ) :
        if ( vis [ adj [ v ] [ i ] ] == False ) :
            min_cost_node = dfs ( adj [ v ] [ i ] , cost , vis , min_cost_node )
    return min_cost_node
def minimumCost ( V , cost ) :
    vis = [ False for i in range ( V + 1 ) ]
    min_cost = 0
    for i in range ( 1 , V + 1 ) :
        if ( not vis [ i ] ) :
            min_cost_node = sys . maxsize
            min_cost_node = dfs ( i , cost , vis , min_cost_node )
            min_cost += min_cost_node
    return min_cost
if __name__ == " _ _ main _ _ " :
    V = 6
    E = 5
    cost = [ 12 , 25 , 8 , 11 , 10 , 7 ]
    addEdge ( 1 , 2 )
    addEdge ( 1 , 3 )
    addEdge ( 3 , 2 )
    addEdge ( 2 , 5 )
    addEdge ( 4 , 6 )
    min_cost = minimumCost ( V , cost )
    print ( min_cost )

Index/key variables: i, u, v
Accumulator variables: min_cost
Iterator variables: i
Python program:
maxm = 0
class Node :
    def __init__ ( self , x ) :
        self . val = x
        self . left = None
        self . right = None
def count_1 ( n ) :
    count = 0
    while ( n ) :
        count += n & 1
        n >>= 1
    return count
def maxm_setbits ( root , ans ) :
    global maxm
    if not root :
        return
    if ( root . left == None and root . right == None ) :
        ans += count_1 ( root . val )
        maxm = max ( ans , maxm )
        return
    maxm_setbits ( root . left , ans + count_1 ( root . val ) )
    maxm_setbits ( root . right , ans + count_1 ( root . val ) )
root = Node ( 15 )
root . left = Node ( 3 )
root . right = Node ( 7 )
root . left . left = Node ( 5 )
root . left . right = Node ( 1 )
root . right . left = Node ( 31 )
root . right . right = Node ( 9 )
maxm_setbits ( root , 0 )
print ( maxm )

Accumulator variables: count, n
Class/struct names: Node
Python program:
def getMinimumMoves ( n , k , d , a ) :
    MAX = 100000
    v = [ ]
    for i in range ( MAX ) :
        v . append ( [ ] )
    for i in range ( n ) :
        cnt = 0
        v [ a [ i ] ] += [ 0 ]
        while ( a [ i ] > 0 ) :
            a [ i ] //= d
            cnt += 1
            v [ a [ i ] ] += [ cnt ]
    ans = float ( ' inf ' )
    for i in range ( MAX ) :
        if ( len ( v [ i ] ) >= k ) :
            move = 0
            v [ i ] . sort ( )
            for j in range ( k ) :
                move += v [ i ] [ j ]
            ans = min ( ans , move )
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 5
    K = 3
    D = 2
    A = [ 1 , 2 , 3 , 4 , 5 ]
    print ( getMinimumMoves ( N , K , D , A ) )

Index/key variables: i, j
Accumulator variables: cnt, move, v
Iterator variables: i, j
Python program:
def Maxlen ( a , n ) :
    ans = 0
    count = 0
    for i in range ( n ) :
        if a [ i ] % 2 == 0 :
            count = 0
        else :
            count += 1
        ans = max ( ans , count )
    return ans
arr = [ 1 , 7 , 2 ]
n = len ( arr )
print ( Maxlen ( arr , n ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def get_submaxarr ( arr , n , y ) :
    j = 0
    stk = [ ]
    maxarr = [ 0 ] * n
    stk . append ( 0 )
    for i in range ( 1 , n ) :
        while ( len ( stk ) > 0 and arr [ i ] > arr [ stk [ - 1 ] ] ) :
            maxarr [ stk [ - 1 ] ] = i - 1
            stk . pop ( )
        stk . append ( i )
    while ( stk ) :
        maxarr [ stk [ - 1 ] ] = n - 1
        stk . pop ( )
    submax = [ ]
    for i in range ( n - y + 1 ) :
        while ( maxarr [ j ] < i + y - 1 or j < i ) :
            j += 1
        submax . append ( arr [ j ] )
    return submax
def get_subminarr ( arr , n , y ) :
    j = 0
    stk = [ ]
    minarr = [ 0 ] * n
    stk . append ( 0 )
    for i in range ( 1 , n ) :
        while ( stk and arr [ i ] < arr [ stk [ - 1 ] ] ) :
            minarr [ stk [ - 1 ] ] = i
            stk . pop ( )
        stk . append ( i )
    while ( stk ) :
        minarr [ stk [ - 1 ] ] = n
        stk . pop ( )
    submin = [ ]
    for i in range ( n - y + 1 ) :
        while ( minarr [ j ] <= i + y - 1 or j < i ) :
            j += 1
        submin . append ( arr [ j ] )
    return submin
def getMinDifference ( Arr , N , Y ) :
    submin = get_subminarr ( Arr , N , Y )
    submax = get_submaxarr ( Arr , N , Y )
    minn = submax [ 0 ] - submin [ 0 ]
    b = len ( submax )
    for i in range ( 1 , b ) :
        diff = submax [ i ] - submin [ i ]
        minn = min ( minn , diff )
    print ( minn )
arr = [ 1 , 2 , 3 , 3 , 2 , 2 ]
N = len ( arr )
Y = 4
getMinDifference ( arr , N , Y )

Index/key variables: i, j
Accumulator variables: stk, submax, submin
Iterator variables: i
Python program:
tree = { }
path = [ ]
maxHeight , maxHeightNode = - 1 , - 1
def getDiameterPath ( vertex , targetVertex , parent , path ) :
    if ( vertex == targetVertex ) :
        path . append ( vertex )
        return True
    for i in range ( len ( tree [ vertex ] ) ) :
        if ( tree [ vertex ] [ i ] == parent ) :
            continue
        if ( getDiameterPath ( tree [ vertex ] [ i ] , targetVertex , vertex , path ) ) :
            path . append ( vertex )
            return True
    return False
def farthestNode ( vertex , parent , height ) :
    global maxHeight , maxHeightNode
    if ( height > maxHeight ) :
        maxHeight = height
        maxHeightNode = vertex
    if ( vertex in tree ) :
        for i in range ( len ( tree [ vertex ] ) ) :
            if ( tree [ vertex ] [ i ] == parent ) :
                continue
            farthestNode ( tree [ vertex ] [ i ] , vertex , height + 1 )
def addedge ( a , b ) :
    if ( a not in tree ) :
        tree [ a ] = [ ]
    tree [ a ] . append ( b )
    if ( b not in tree ) :
        tree [ b ] = [ ]
    tree [ b ] . append ( a )
def FindCenter ( n ) :
    maxHeight = - 1
    maxHeightNode = - 1
    farthestNode ( 0 , - 1 , 0 )
    leaf1 = maxHeightNode
    maxHeight = - 1
    farthestNode ( maxHeightNode , - 1 , 0 )
    leaf2 = maxHeightNode
    path = [ ]
    getDiameterPath ( leaf1 , leaf2 , - 1 , path )
    pathSize = len ( path )
    if ( pathSize % 2 == 1 ) :
        print ( path [ int ( pathSize / 2 ) ] * - 1 )
    else :
        print ( path [ int ( pathSize / 2 ) ] , " , ▁ " , path [ int ( ( pathSize - 1 ) / 2 ) ] , sep = " " , end = " " )
N = 4
tree = { }
addedge ( 1 , 0 )
addedge ( 1 , 2 )
addedge ( 1 , 3 )
FindCenter ( N )

Index/key variables: a, b, i, vertex
Accumulator variables: path
Iterator variables: i
Python program:
maxm = 1000000000
def count ( v , n ) :
    odd , even = 0 , 0
    t = 1 << len ( v )
    size = len ( v )
    for i in range ( 1 , t ) :
        d , count = 1 , 0
        for j in range ( 0 , size ) :
            if ( i & ( 1 << j ) ) :
                d *= v [ j ]
                count += 1
        if ( count & 1 ) :
            odd += n // d
        else :
            even += n // d
    return ( odd - even )
def BinarySearch ( l , r , v , key ) :
    while ( r - l > 1 ) :
        mid = ( l + r ) // 2
        if ( key <= count ( v , mid ) ) :
            r = mid
        else :
            l = mid
    if ( key == count ( v , l ) ) :
        return l
    else :
        return r
N , K = 2 , 10
v = [ 2 , 3 ]
print ( BinarySearch ( 1 , maxm , v , K ) )

Index/key variables: j
Accumulator variables: count, d, even, odd
Iterator variables: i, j
Python program:
def minDistancePoints ( A , K , n ) :
    m = { }
    q = [ ]
    for i in range ( n ) :
        m [ A [ i ] ] = 1
        q . append ( A [ i ] )
    ans = [ ]
    while ( K > 0 ) :
        x = q [ 0 ]
        q = q [ 1 : : ]
        if ( ( x - 1 ) not in m and K > 0 ) :
            m [ x - 1 ] = m . get ( x - 1 , 0 ) + 1
            q . append ( x - 1 )
            ans . append ( x - 1 )
            K -= 1
        if ( ( x + 1 ) not in m and K > 0 ) :
            m [ x + 1 ] = m . get ( x + 1 , 0 ) + 1
            q . append ( x + 1 )
            ans . append ( x + 1 )
            K -= 1
    for i in ans :
        print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    A = [ - 1 , 4 , 6 ]
    K = 3
    n = len ( A )
    minDistancePoints ( A , K , n )

Index/key variables: i
Accumulator variables: K, ans, q
Iterator variables: i