dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def getDistance ( x1 , y1 , x2 , y2 ) :
    return ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 )
def triangleWithNoPointInside ( points , N ) :
    first = 0
    second = 0
    third = 0
    minD = sys . maxsize
    for i in range ( 0 , N ) :
        if i == first :
            continue
        d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] )
        if minD > d :
            minD = d
            second = i
    minD = sys . maxsize
    for i in range ( 0 , N ) :
        if i == first or i == second :
            continue
        d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] )
        if ( ( points [ i ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ second ] [ 1 ] - points [ first ] [ 1 ] ) != ( points [ second ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ i ] [ 1 ] - points [ first ] [ 1 ] ) and minD > d ) :
            minD = d
            third = i
    print ( points [ first ] [ 0 ] , ' , ▁ ' , points [ first ] [ 1 ] )
    print ( points [ second ] [ 0 ] , ' , ▁ ' , points [ second ] [ 1 ] )
    print ( points [ third ] [ 0 ] , ' , ▁ ' , points [ third ] [ 1 ] )
points = [ [ 0 , 0 ] , [ 0 , 2 ] , [ 2 , 0 ] , [ 2 , 2 ] , [ 1 , 1 ] ]
N = len ( points )
triangleWithNoPointInside ( points , N )

Index/key variables: first, i, second, third
Iterator variables: i
Python program:
import math
mxn = 10000 + 1
prime = [ True for _ in range ( mxn + 1 ) ]
def SieveOfEratosthenes ( ) :
    global prime
    for p in range ( 2 , int ( math . sqrt ( mxn ) ) + 1 ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * p , mxn + 1 , p ) :
                prime [ i ] = False
def countMin ( arr , n ) :
    cMinSwaps = 0
    cPrimeIndices = 0
    cPrimeNos = 0
    for i in range ( 0 , n ) :
        if ( prime [ i + 1 ] ) :
            cPrimeIndices += 1
            if ( not prime [ arr [ i ] ] ) :
                cMinSwaps += 1
            else :
                cPrimeNos += 1
        elif ( prime [ arr [ i ] ] ) :
            cPrimeNos += 1
    if ( cPrimeNos >= cPrimeIndices ) :
        return cMinSwaps
    else :
        return - 1
if __name__ == " _ _ main _ _ " :
    SieveOfEratosthenes ( )
    n = 5
    arr = [ 2 , 7 , 8 , 5 , 13 ]
    print ( countMin ( arr , n ) )

Index/key variables: i, p
Accumulator variables: cMinSwaps, cPrimeIndices, cPrimeNos
Iterator variables: i, p
Python program:
def largestSubset ( a , N ) :
    bit = [ 0 for i in range ( 32 ) ]
    for i in range ( N ) :
        x = 31
        while ( a [ i ] > 0 ) :
            if ( a [ i ] & 1 == 1 ) :
                bit [ x ] += 1
            a [ i ] = a [ i ] >> 1
            x -= 1
    print ( max ( bit ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 13 , 8 , 2 , 3 ]
    N = len ( arr )
    largestSubset ( arr , N )

Index/key variables: i, x
Iterator variables: i
Python program:
def Find ( parent , a ) :
    if ( parent [ parent [ a ] ] != parent [ a ] ) :
        parent [ a ] = findParent ( parent , parent [ a ] )
    return parent [ a ]
def Union ( parent , rank , maxValue , a , b ) :
    a = Find ( parent , a )
    b = Find ( parent , b )
    if ( a == b ) :
        return
    if ( rank [ a ] == rank [ b ] ) :
        rank [ a ] += 1
    if ( rank [ a ] < rank [ b ] ) :
        temp = a
        a = b
        b = temp
    parent [ b ] = a
    maxValue [ a ] = max ( maxValue [ a ] , maxValue [ b ] )
def findMaxValueOfSet ( arr , queries , R , N , M ) :
    parent = [ 1 for i in range ( R + 1 ) ]
    rank = [ 0 for i in range ( R + 1 ) ]
    maxValue = [ 0 for i in range ( R + 1 ) ]
    for i in range ( 1 , R + 1 , 1 ) :
        parent [ i ] = maxValue [ i ] = i
    for i in range ( N ) :
        Union ( parent , rank , maxValue , arr [ i ] [ 0 ] , arr [ i ] [ 1 ] )
    for i in range ( M ) :
        P = Find ( parent , queries [ i ] )
        print ( maxValue [ P ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    R = 5
    arr = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ;
    queries = [ 2 , 4 , 1 , 3 ]
    N = len ( arr )
    M = len ( queries )
    findMaxValueOfSet ( arr , queries , R , N , M )

Index/key variables: P, a, b, i
Iterator variables: i
Python program:
import sys
maxAns = - sys . maxsize - 1
def Find ( parent , a ) :
    if ( parent [ a ] == a ) :
        return a
    return Find ( parent , parent [ a ] )
def Union ( parent , rank , setSum , a , b ) :
    a = Find ( parent , a )
    b = Find ( parent , b )
    if ( a == b ) :
        return
    if ( rank [ a ] > rank [ b ] ) :
        rank [ a ] += 1
    if ( rank [ b ] > rank [ a ] ) :
        swap ( a , b )
    parent [ b ] = a
    setSum [ a ] += setSum [ b ]
def maxValues ( arr , queries , N ) :
    global maxAns
    rank = [ 0 ] * ( N + 1 )
    setSum = [ 0 ] * ( N + 1 )
    currMax = [ ]
    for i in range ( 1 , N + 1 ) :
        parent [ i ] = - 1
        setSum [ i ] = arr [ i - 1 ]
    currMax . append ( 0 )
    for i in range ( N - 1 , 0 , - 1 ) :
        if ( parent [ queries [ i ] ] == - 1 ) :
            parent [ queries [ i ] ] = queries [ i ]
        if ( queries [ i ] - 1 >= 0 and parent [ queries [ i ] - 1 ] != - 1 ) :
            Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 )
        if ( queries [ i ] + 1 <= N and parent [ queries [ i ] + 1 ] != - 1 ) :
            Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 )
        maxAns = max ( setSum [ queries [ i ] ] , maxAns )
        currMax . append ( maxAns )
    for i in range ( len ( currMax ) - 1 , - 1 , - 1 ) :
        print ( currMax [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 2 , 5 ]
    queries = [ 3 , 4 , 1 , 2 ]
    N = len ( arr )
    maxValues ( arr , queries , N )

Index/key variables: a, b, i
Accumulator variables: currMax
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , key ) :
        self . val = key
        self . child = [ ]
def newNode ( key ) :
    temp = Node ( key )
    return temp
def zigzagLevelOrder ( root ) :
    if ( root == None ) :
        return
    result = [ ]
    q = [ ]
    q . append ( root )
    while len ( q ) > 0 :
        size = len ( q )
        curLevel = [ ]
        for i in range ( size ) :
            node = q [ 0 ]
            q . pop ( 0 )
            curLevel . append ( node . val )
            for j in range ( len ( node . child ) ) :
                q . append ( node . child [ j ] )
        result . append ( curLevel )
    for i in range ( len ( result ) ) :
        if ( ( i + 1 ) % 2 == 0 ) :
            result [ i ] . reverse ( )
        for j in range ( len ( result [ i ] ) ) :
            print ( result [ i ] [ j ] , end = " ▁ " )
        print ( )
root = newNode ( 1 )
( root . child ) . append ( newNode ( 2 ) )
( root . child ) . append ( newNode ( 3 ) )
( root . child [ 0 ] . child ) . append ( newNode ( 4 ) )
( root . child [ 0 ] . child ) . append ( newNode ( 5 ) )
( root . child [ 1 ] . child ) . append ( newNode ( 6 ) )
( root . child [ 1 ] ) . child . append ( newNode ( 7 ) )
( root . child [ 1 ] . child ) . append ( newNode ( 8 ) )
zigzagLevelOrder ( root )

Index/key variables: i, j
Accumulator variables: curLevel, q, result
Iterator variables: i, j
Class/struct names: Node
Python program:
def minDisIncludingNode ( n , m , edges ) :
    g = [ [ ] for i in range ( 10005 ) ]
    for i in range ( m ) :
        a = edges [ i ] [ 0 ] - 1
        b = edges [ i ] [ 1 ] - 1
        g [ a ] . append ( b )
        g [ b ] . append ( a )
    q = [ ]
    q . append ( [ 0 , 0 ] )
    dist = [ 1e9 for i in range ( n ) ]
    dist [ 0 ] = 0
    while ( len ( q ) > 0 ) :
        up = q [ 0 ]
        q = q [ 1 : ]
        x = up [ 0 ]
        lev = up [ 1 ]
        if ( lev > dist [ x ] ) :
            continue
        if ( x == n - 1 ) :
            continue
        for y in g [ x ] :
            if ( dist [ y ] > lev + 1 ) :
                dist [ y ] = lev + 1
                q . append ( [ y , lev + 1 ] )
    q1 = [ ]
    q1 . append ( [ n - 1 , 0 ] )
    dist1 = [ 1e9 for i in range ( n ) ]
    dist1 [ n - 1 ] = 0
    while ( len ( q1 ) > 0 ) :
        up = q1 [ 0 ]
        q1 = q1 [ 1 : ]
        x = up [ 0 ]
        lev = up [ 1 ]
        if ( lev > dist1 [ x ] ) :
            continue
        if ( x == 0 ) :
            continue
        for y in g [ x ] :
            if ( dist1 [ y ] > lev + 1 ) :
                dist1 [ y ] = lev + 1
                q1 . append ( [ y , lev + 1 ] )
    for i in range ( n ) :
        if ( dist [ i ] + dist1 [ i ] > 1e9 ) :
            print ( - 1 , end = " ▁ " )
        else :
            print ( dist [ i ] + dist1 [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    m = 7
    edges = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 5 ] , [ 4 , 3 ] , [ 4 , 5 ] , [ 1 , 5 ] ]
    minDisIncludingNode ( n , m , edges )

Index/key variables: a, b, i, x, y
Accumulator variables: q, q1
Iterator variables: i, y
Python program:
maxSumPath = 0
class Node :
    def __init__ ( self , key ) :
        self . val = key
        self . child = [ ]
def newNode ( key ) :
    temp = Node ( key )
    return temp
def DFS ( root , Sum ) :
    global maxSumPath
    if ( len ( root . child ) == 0 ) :
        maxSumPath = max ( maxSumPath , Sum )
        return
    for i in range ( len ( root . child ) ) :
        DFS ( root . child [ i ] , Sum + root . child [ i ] . val )
root = newNode ( 1 )
( root . child ) . append ( newNode ( 2 ) )
( root . child ) . append ( newNode ( 3 ) )
( root . child [ 0 ] . child ) . append ( newNode ( 4 ) )
( root . child [ 1 ] . child ) . append ( newNode ( 6 ) )
( root . child [ 0 ] . child ) . append ( newNode ( 5 ) )
( root . child [ 1 ] ) . child . append ( newNode ( 7 ) )
( root . child [ 1 ] . child ) . append ( newNode ( 8 ) )
DFS ( root , root . val )
print ( maxSumPath )

Index/key variables: i
Iterator variables: i
Class/struct names: Node
Python program:
def countTriplets ( size , queries , arr , Q ) :
    arr_even = [ 0 for i in range ( size + 1 ) ]
    arr_odd = [ 0 for i in range ( size + 1 ) ]
    even = 0
    odd = 0
    arr_even [ 0 ] = 0
    arr_odd [ 0 ] = 0
    for i in range ( size ) :
        if ( arr [ i ] % 2 ) :
            odd += 1
        else :
            even += 1
        arr_even [ i + 1 ] = even
        arr_odd [ i + 1 ] = odd
    for i in range ( queries ) :
        l = Q [ i ] [ 0 ]
        r = Q [ i ] [ 1 ]
        odd = arr_odd [ r ] - arr_odd [ l - 1 ]
        even = arr_even [ r ] - arr_even [ l - 1 ]
        ans = ( even * ( even - 1 ) * ( even - 2 ) ) // 6 + ( odd * ( odd - 1 ) // 2 ) * even
        print ( ans , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 6
    Q = 2
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
    queries = [ [ 1 , 3 ] , [ 2 , 5 ] ]
    countTriplets ( N , Q , arr , queries )

Index/key variables: i, r
Accumulator variables: even, odd
Iterator variables: i
Python program:
def maxRightmostElement ( N , k , p , arr ) :
    while ( k ) :
        for i in range ( N - 2 , - 1 , - 1 ) :
            if ( arr [ i ] >= p ) :
                arr [ i ] = arr [ i ] - p
                arr [ i + 1 ] = arr [ i + 1 ] + p
                break
        k = k - 1
    print ( arr [ N - 1 ] )
N = 4
p = 2
k = 5
arr = [ 3 , 8 , 1 , 4 ]
maxRightmostElement ( N , k , p , arr )

Index/key variables: i
Iterator variables: i
Python program:
def nCr ( n , r , f ) :
    if ( n < r ) :
        return 0
    return f [ n ] / ( f [ r ] * f [ n - r ] )
def findMean ( N , X ) :
    f = [ 0 for i in range ( N + 1 ) ]
    f [ 0 ] = 1
    for i in range ( 1 , N + 1 , 1 ) :
        f [ i ] = f [ i - 1 ] * i
    total = nCr ( N , X , f )
    count = 0
    for i in range ( 1 , N + 1 , 1 ) :
        count += nCr ( N - i , X - 1 , f ) * i
    E_X = ( count ) / ( total )
    print ( " { 0 : . 9f } " . format ( E_X ) )
    return 0
if __name__ == ' _ _ main _ _ ' :
    N = 3
    X = 2
    findMean ( N , X )

Index/key variables: i, n, r
Accumulator variables: count
Iterator variables: i
Python program:
def maximumNumberofpairs ( n , arr ) :
    x = 0
    y = 0
    for i in range ( n ) :
        if ( arr [ i ] % 2 == 0 ) :
            x += 1
        else :
            y += 1
    answer = min ( x , y )
    if ( abs ( x - y ) >= 2 ) :
        answer += 1
    return answer
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 4 , 6 , 5 , 10 , 12 ]
    n = len ( arr )
    print ( maximumNumberofpairs ( n , arr ) )

Index/key variables: i
Accumulator variables: x, y
Iterator variables: i
Python program:
def pairwiseProductSum ( a , l , r ) :
    sum = 0
    for j in range ( l - 1 , r , 1 ) :
        for k in range ( j + 1 , r , 1 ) :
            sum += ( a [ j ] * a [ k ] ) ;
    print ( sum )
def updateArray ( a , p , x ) :
    a [ p - 1 ] = x
def solveQueries ( a , n , Q , query ) :
    for i in range ( Q ) :
        if ( query [ i ] [ 0 ] == 1 ) :
            pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] )
        else :
            updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] )
if __name__ == ' _ _ main _ _ ' :
    A = [ 5 , 7 , 2 , 3 , 1 ]
    N = len ( A )
    Q = 3
    query = [ [ 1 , 1 , 3 ] , [ 2 , 2 , 5 ] , [ 1 , 2 , 5 ] ]
    solveQueries ( A , N , Q , query )

Index/key variables: i, j, k
Accumulator variables: sum
Iterator variables: i, j, k
Python program:
def Reduced ( a , n ) :
    original_array = a [ : ]
    while len ( a ) != 2 :
        mid = len ( a ) // 2
        mid_ele = a [ mid ]
        start = a . pop ( 0 )
        end = a . pop ( )
        rmd = ( start * end ) % mid_ele
        a . append ( rmd )
    remainder = ( a [ 0 ] * a [ 1 ] ) % n
    if remainder in original_array :
        return 1
    return 0
Arr = [ 2 , 3 , 4 , 8 , 5 , 7 ]
N = len ( Arr )
x = Reduced ( Arr , N )
if x :
    print ( " YES " )
else :
    print ( " NO " )

Index/key variables: mid
Accumulator variables: a
Python program:
MaxBit = 32
def possiblePair ( X , Y ) :
    Z = Y - X
    if ( Z < 0 ) :
        print ( " - 1" )
        return 0
    for k in range ( MaxBit ) :
        bit1 = ( Z >> k ) & 1
        bit2 = ( Z >> k ) & 1
        if ( bit1 == 1 and bit2 == 0 ) :
            print ( " - 1" )
            return 0
    print ( Z , X )
    return 0
if __name__ == ' _ _ main _ _ ' :
    X = 7
    Y = 11
    possiblePair ( X , Y )

Iterator variables: k
Python program:
def maxFrequencySubarrayUtil ( A , N , M ) :
    i = 0
    m = { }
    val = 0
    while ( i < M ) :
        if A [ i ] in m :
            m [ A [ i ] ] += 1
        else :
            m [ A [ i ] ] = 1
        val = max ( val , m [ A [ i ] ] )
        i += 1
    print ( val , end = " ▁ " )
    for i in range ( M , N , 1 ) :
        if A [ i - M ] in m :
            m [ A [ i - M ] ] -= 1
        else :
            m [ A [ i - M ] ] = 0
        if A [ i ] in m :
            m [ A [ i ] ] += 1
        val = 0
        for key , value in m . items ( ) :
            val = max ( val , value )
        print ( val , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 1 , 2 , 2 , 3 , 5 ]
    N = len ( A )
    M = 4
    maxFrequencySubarrayUtil ( A , N , M )

Index/key variables: i
Iterator variables: i, key, value
Python program:
def maximumPairs ( a , n ) :
    freq = { }
    count = 0
    for i in range ( n ) :
        if a [ i ] in freq :
            freq [ a [ i ] ] += 1
        else :
            freq [ a [ i ] ] = 1
    pq = [ ]
    for key , value in freq . items ( ) :
        pq . append ( value )
    pq . sort ( )
    while ( len ( pq ) > 1 ) :
        freq1 = pq [ len ( pq ) - 1 ]
        pq = pq [ : - 1 ]
        freq2 = pq [ len ( pq ) - 1 ]
        pq = pq [ : - 1 ]
        count += 1
        freq1 -= 1
        freq2 -= 1
        if ( freq1 > 0 ) :
            pq . append ( freq1 )
        if ( freq2 > 0 ) :
            pq . append ( freq2 )
        pq . sort ( )
    return count
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 2 , 4 , 1 , 4 , 3 ]
    N = len ( arr )
    print ( maximumPairs ( arr , N ) )

Index/key variables: i
Accumulator variables: count, freq1, freq2, pq
Iterator variables: i, key, value
Python program:
class Node :
    def __init__ ( self , val ) :
        self . data = val
        self . left = None
        self . right = None
def getNewNode ( val ) :
    newNode = Node ( val )
    return newNode
def buildPerfectBT_helper ( preStart , preEnd , pre ) :
    if ( preStart > preEnd ) :
        return None
    root = getNewNode ( pre [ preStart ] )
    if ( preStart == preEnd ) :
        return root
    leftPreStart = preStart + 1
    rightPreStart = leftPreStart + int ( ( preEnd - leftPreStart + 1 ) / 2 )
    leftPreEnd = rightPreStart - 1
    rightPreEnd = preEnd
    root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre )
    root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre )
    return root
def buildPerfectBT ( pre , size ) :
    return buildPerfectBT_helper ( 0 , size - 1 , pre )
def printInorder ( root ) :
    if ( root == None ) :
        return
    printInorder ( root . left )
    print ( root . data , " " , end = " " )
    printInorder ( root . right )
pre = [ 1 , 2 , 4 , 5 , 3 , 6 , 7 ]
N = len ( pre )
root = buildPerfectBT ( pre , N )
print ( " Inorder ▁ traversal ▁ of ▁ the ▁ tree : ▁ " , end = " " )
printInorder ( root )

Index/key variables: preStart
Class/struct names: Node
Python program:
N = 3
adj = [ ]
for i in range ( N + 1 ) :
    adj . append ( [ ] )
visited = [ ]
ans = 0
def addEdges ( u , v ) :
    global adj
    adj [ u ] . append ( v )
    adj [ v ] . append ( u )
def DFSUtil ( u , foo , foo1 , initial , finall ) :
    global visited , ans , adj
    visited [ u ] = True
    if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == True ) :
        ans += 1
        foo ^= True
    for i in range ( len ( adj [ u ] ) ) :
        if ( visited [ adj [ u ] [ i ] ] == False ) :
            DFSUtil ( adj [ u ] [ i ] , foo1 , foo , initial , finall )
def DFS ( V , initial , finall ) :
    global ans , visited
    ans = 0
    visited = [ False ] * V
    for u in range ( 1 , 2 ) :
        if ( visited [ u ] == False ) :
            DFSUtil ( u , 0 , 0 , initial , finall )
    print ( ans )
def countOperations ( N , initial , finall , Edges ) :
    for i in range ( N - 1 ) :
        addEdges ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] )
    DFS ( N + 1 , initial , finall )
Edges = [ [ 1 , 2 ] , [ 1 , 3 ] ]
initial = [ True , True , False ]
finall = [ False , True , True ]
countOperations ( N , initial , finall , Edges )

Index/key variables: i, u, v
Accumulator variables: adj
Iterator variables: i, u
Python program:
def countOperations ( N , M ) :
    visited = [ False ] * ( 100001 )
    Q = [ ]
    Q . append ( [ N , 0 ] )
    visited [ N ] = True
    while ( len ( Q ) > 0 ) :
        aux = Q [ 0 ] [ 0 ]
        cont = Q [ 0 ] [ 1 ]
        Q . pop ( 0 )
        if ( aux == M ) :
            return cont
        i = 2
        while i * i <= aux :
            if ( aux % i == 0 ) :
                if ( aux + i <= M and not visited [ aux + i ] ) :
                    Q . append ( [ aux + i , cont + 1 ] )
                    visited [ aux + i ] = True
                if ( aux + int ( aux / i ) <= M and not visited [ aux + int ( aux / i ) ] ) :
                    Q . append ( [ aux + int ( aux / i ) , cont + 1 ] )
                    visited [ aux + int ( aux / i ) ] = True
            i += 1
    return - 1
N , M = 4 , 24
print ( countOperations ( N , M ) )

Index/key variables: N
Accumulator variables: Q, i
Python program:
adj = [ ]
visited = [ ]
num = 0
def dfs ( v ) :
    global adj , visited , num
    visited [ v ] = True
    num += 1
    for i in range ( len ( adj [ v ] ) ) :
        if ( not visited [ adj [ v ] [ i ] ] ) :
            dfs ( adj [ v ] [ i ] )
def numberOfPairs ( N , arr ) :
    global adj , visited , num
    adj = [ ]
    for i in range ( N ) :
        adj . append ( [ ] )
    for i in range ( len ( arr ) ) :
        adj [ arr [ i ] [ 0 ] ] . append ( arr [ i ] [ 1 ] )
        adj [ arr [ i ] [ 1 ] ] . append ( arr [ i ] [ 0 ] )
    visited = [ False ] * ( N )
    v = [ ]
    num = 0
    for i in range ( N ) :
        if ( not visited [ i ] ) :
            dfs ( i )
            v . append ( num )
            num = 0
    ans = N * int ( ( N - 1 ) / 2 )
    for i in range ( len ( v ) ) :
        ans -= ( v [ i ] * int ( ( v [ i ] - 1 ) / 2 ) )
    ans += 1
    print ( ans )
N = 6
arr = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 2 , 5 ] ]
numberOfPairs ( N , arr )

Index/key variables: i, v
Accumulator variables: adj, ans
Iterator variables: i
Python program:
MAX = 100005
from math import sqrt
def isPerfect ( N ) :
    sum = 1
    for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) :
        if ( N % i == 0 ) :
            if ( i * i != N ) :
                sum = sum + i + N // i
            else :
                sum = sum + i
    if ( sum == N and N != 1 ) :
        return True
    return False
def Query ( arr , N ) :
    prefix = [ 0 for i in range ( MAX + 1 ) ]
    for i in range ( 2 , MAX + 1 , 1 ) :
        prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i )
    for i in range ( N ) :
        print ( prefix [ arr [ i ] [ 1 ] ] - prefix [ arr [ i ] [ 0 ] - 1 ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 1 , 1000 ] , [ 1000 , 2000 ] , [ 2000 , 3000 ] ]
    N = len ( arr )
    Query ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def Create_Rectangle ( arr , n ) :
    for i in range ( n ) :
        arr [ i ] = [ i for i in arr [ i ] ]
    p1 = [ - 1 , - 1 ]
    p2 = [ - 1 , - 1 ]
    p3 = [ ]
    p4 = [ ]
    for i in range ( n ) :
        for j in range ( n ) :
            if ( arr [ i ] [ j ] == '1' ) :
                if ( p1 [ 0 ] == - 1 ) :
                    p1 = [ i , j ]
                else :
                    p2 = [ i , j ]
    p3 = p1
    p4 = p2
    if ( p1 [ 0 ] == p2 [ 0 ] ) :
        p3 [ 0 ] = ( p1 [ 0 ] + 1 ) % n
        p4 [ 0 ] = ( p2 [ 0 ] + 1 ) % n
    elif ( p1 [ 1 ] == p2 [ 1 ] ) :
        p3 [ 1 ] = ( p1 [ 1 ] + 1 ) % n
        p4 [ 1 ] = ( p2 [ 1 ] + 1 ) % n
    else :
        p3 [ 0 ] , p4 [ 0 ] = p4 [ 0 ] , p3 [ 0 ]
    arr [ p3 [ 0 ] ] [ p3 [ 1 ] ] = '1'
    arr [ p4 [ 0 ] ] [ p4 [ 1 ] ] = '1'
    for i in range ( n ) :
        print ( " " . join ( arr [ i ] ) )
if __name__ == ' _ _ main _ _ ' :
    n = 4
    arr = [ "0010" , "0000" , "1000" , "0000" ]
    Create_Rectangle ( arr , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import math
def SumOfPrimeDivisors ( n ) :
    sum = 0
    if n % 2 == 0 :
        sum += 2
    while n % 2 == 0 :
        n //= 2
    k = int ( math . sqrt ( n ) )
    for i in range ( 3 , k + 1 , 2 ) :
        if n % i == 0 :
            sum += i
        while n % i == 0 :
            n //= i
    if n > 2 :
        sum += n
    return sum
if __name__ == ' _ _ main _ _ ' :
    n = 10
    print ( SumOfPrimeDivisors ( n ) )

Accumulator variables: n, sum
Iterator variables: i
Python program:
import math
def countOfBase ( N ) :
    count = 0
    for i in range ( 2 , N + 1 ) :
        highestPower = int ( math . log ( N ) / math . log ( i ) )
        firstDigit = int ( N / int ( math . pow ( i , highestPower ) ) )
        if ( firstDigit == 1 ) :
            count += 1
    return count
N = 6
print ( countOfBase ( N ) )

Accumulator variables: count
Iterator variables: i
Python program:
def countWays ( arr , N ) :
    arr_xor = 0
    for i in range ( N ) :
        arr_xor ^= arr [ i ]
    pref_xor , suff_xor = 0 , 0
    pref_ind = [ ]
    suff_inds = [ 0 ] * ( N + 1 )
    for i in range ( N ) :
        pref_xor ^= arr [ i ]
        if ( pref_xor == arr_xor ) :
            pref_ind . append ( i )
    for i in range ( N - 1 , - 1 , - 1 ) :
        suff_xor ^= arr [ i ]
        suff_inds [ i ] += suff_inds [ i + 1 ]
        if ( suff_xor == arr_xor ) :
            suff_inds [ i ] += 1
    tot_ways = 0
    for idx in pref_ind :
        if ( idx < N - 1 ) :
            tot_ways += suff_inds [ idx + 2 ]
    return tot_ways
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 0 , 5 , 2 , 7 ]
    N = len ( arr )
    print ( countWays ( arr , N ) )

Index/key variables: i
Accumulator variables: arr_xor, pref_ind, pref_xor, suff_xor, tot_ways
Iterator variables: i, idx