dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def cntMaxOperationToMakeN_1_0 ( arr , N ) :
    cntOp = 0
    PQ = [ ]
    for i in range ( N ) :
        PQ . append ( arr [ i ] )
    PQ = sorted ( PQ )
    while ( len ( PQ ) > 1 ) :
        X = PQ [ - 1 ]
        del PQ [ - 1 ]
        Y = PQ [ - 1 ]
        del PQ [ - 1 ]
        X -= 1
        Y -= 1
        if ( X != 0 ) :
            PQ . append ( X )
        if ( Y != 0 ) :
            PQ . append ( Y )
        cntOp += 1
        PQ = sorted ( PQ )
    return cntOp
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    N = len ( arr )
    print ( cntMaxOperationToMakeN_1_0 ( arr , N ) )

Index/key variables: i
Accumulator variables: PQ, X, Y, cntOp
Iterator variables: i
Python program:
def generateMatrix ( N ) :
    odd = 1 ;
    even = 2 ;
    mat = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] ;
    for i in range ( 1 , N + 1 ) :
        for j in range ( 1 , N + 1 ) :
            if ( ( i + j ) % 2 == 0 ) :
                mat [ i ] [ j ] = odd ;
                odd += 2 ;
            else :
                mat [ i ] [ j ] = even ;
                even += 2 ;
    for i in range ( 1 , N + 1 ) :
        for j in range ( 1 , N + 1 ) :
            print ( mat [ i ] [ j ] , end = " ▁ " ) ;
        print ( ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 4 ;
    generateMatrix ( N ) ;

Index/key variables: i, j
Accumulator variables: even, odd
Iterator variables: i, j
Python program:
def build ( tree , arr , start , end , index ) :
    if ( start == end ) :
        if ( start % 2 == 0 ) :
            tree [ index ] = arr [ start ]
        else :
            tree [ index ] = - arr [ start ]
        return
    mid = start + ( end - start ) // 2
    build ( tree , arr , start , mid , 2 * index + 1 )
    build ( tree , arr , mid + 1 , end , 2 * index + 2 )
    tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ]
def update ( tree , index , start , end , pos , val ) :
    if ( start == end ) :
        if ( start % 2 == 0 ) :
            tree [ index ] = val
        else :
            tree [ index ] = - val
        return
    mid = start + ( end - start ) // 2
    if ( mid >= pos ) :
        update ( tree , 2 * index + 1 , start , mid , pos , val )
    else :
        update ( tree , 2 * index + 2 , mid + 1 , end , pos , val )
    tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ]
def FindSum ( tree , start , end , L , R , index ) :
    if ( L > end or R < start ) :
        return 0
    if ( L <= start and R >= end ) :
        return tree [ index ]
    mid = start + ( end - start ) // 2
    X = FindSum ( tree , start , mid , L , R , 2 * index + 1 )
    Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 )
    return X + Y
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 ]
    N = len ( arr )
    tree = [ 0 for i in range ( 4 * N + 5 ) ]
    build ( tree , arr , 0 , N - 1 , 0 )
    Q = [ [ 2 , 0 , 3 ] , [ 1 , 1 , 5 ] , [ 2 , 1 , 2 ] ]
    cntQuey = 3
    for i in range ( cntQuey ) :
        if ( Q [ i ] [ 0 ] == 1 ) :
            update ( tree , 0 , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] )
        else :
            if ( Q [ i ] [ 1 ] % 2 == 0 ) :
                print ( FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) , end = " ▁ " )
            else :
                print ( - FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) , end = " ▁ " )

Index/key variables: i, index, start
Iterator variables: i
Python program:
def minimumSubarrays ( arr , n ) :
    current = ' N '
    answer = 1
    for i in range ( 1 , n ) :
        if ( current == ' N ' ) :
            if ( arr [ i ] < arr [ i - 1 ] ) :
                current = ' D '
            elif ( arr [ i ] == arr [ i - 1 ] ) :
                current = ' N '
            else :
                current = ' I '
        elif ( current == ' I ' ) :
            if ( arr [ i ] >= arr [ i - 1 ] ) :
                current = ' I '
            else :
                current = ' N '
                answer += 1
        else :
            if ( arr [ i ] <= arr [ i - 1 ] ) :
                current = ' D '
            else :
                current = ' N '
                answer += 1
    print ( answer )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 9 , 5 , 4 , 6 , 8 ]
    n = len ( arr )
    minimumSubarrays ( arr , n )

Index/key variables: i
Accumulator variables: answer
Iterator variables: i
Python program:
def ConstMatrix ( mat , N , M , A , B ) :
    if ( N * M != A * B ) :
        return - 1
    idx = 0 ;
    res = [ [ 0 for i in range ( B ) ] for i in range ( A ) ]
    for i in range ( N ) :
        for j in range ( M ) :
            res [ idx // B ] [ idx % B ] = mat [ i ] [ j ] ;
            idx += 1
    for i in range ( A ) :
        for j in range ( B ) :
            print ( res [ i ] [ j ] , end = " ▁ " )
        print ( )
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ]
    A = 2
    B = 3
    N = len ( mat )
    M = len ( mat [ 0 ] )
    ConstMatrix ( mat , N , M , A , B )

Index/key variables: i, j
Accumulator variables: idx
Iterator variables: i, j
Python program:
def printFinalPrices ( arr ) :
    minStk = [ ]
    reduce = [ 0 ] * len ( arr )
    for i in range ( len ( arr ) - 1 , - 1 , - 1 ) :
        if minStk :
            if minStk [ - 1 ] <= arr [ i ] :
                reduce [ i ] = minStk [ - 1 ]
            else :
                while minStk and minStk [ - 1 ] > arr [ i ] :
                    minStk . pop ( )
                if minStk :
                    reduce [ i ] = minStk [ - 1 ]
        minStk . append ( arr [ i ] )
    for i in range ( len ( arr ) ) :
        print ( arr [ i ] - reduce [ i ] , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 8 , 4 , 6 , 2 , 3 ]
    printFinalPrices ( arr )

Index/key variables: i
Accumulator variables: minStk
Iterator variables: i
Python program:
def constructArray ( A , N ) :
    left = [ 0 ] * ( N + 1 )
    right = [ 0 ] * ( N + 1 )
    X = [ 0 ] * ( N + 1 )
    Y = [ 0 ] * ( N + 1 )
    for i in range ( 0 , N ) :
        X [ i ] = left [ A [ i ] ]
        left [ A [ i ] ] += 1
    for i in range ( N - 1 , - 1 , - 1 ) :
        Y [ i ] = right [ A [ i ] ]
        right [ A [ i ] ] += 1
    for i in range ( 0 , N ) :
        print ( Y [ i ] - X [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 3 , 2 , 1 , 2 , 3 ]
    N = len ( A )
    constructArray ( A , N )

Index/key variables: i
Iterator variables: i
Python program:
class Node :
    def __init__ ( self ) :
        self . data = 0
        self . next = None
def push ( head_ref , new_data ) :
    new_node = Node ( )
    new_node . data = new_data ;
    new_node . next = head_ref
    head_ref = new_node ;
    return head_ref
def largestElement ( head_ref ) :
    Max = - 10000000
    head = head_ref ;
    while ( head != None ) :
        if ( Max < head . data ) :
            Max = head . data ;
        head = head . next ;
    return Max ;
def createHashMap ( Max ) :
    hashmap = set ( )
    prev = 0 ;
    curr = 1 ;
    hashmap . add ( prev ) ;
    hashmap . add ( curr ) ;
    while ( curr <= Max ) :
        temp = curr + prev ;
        hashmap . add ( temp ) ;
        prev = curr ;
        curr = temp ;
    return hashmap ;
def meanofnodes ( head ) :
    Max = largestElement ( head ) ;
    hashmap = createHashMap ( Max ) ;
    curr = head ;
    cnt = 0 ;
    sum = 0.0 ;
    while ( curr != None ) :
        if ( ( curr . data ) % 2 == 1 ) :
            if ( curr . data in hashmap ) :
                cnt += 1
                sum += curr . data ;
                hashmap . remove ( curr . data ) ;
        curr = curr . next ;
    return ( sum / cnt ) ;
if __name__ == ' _ _ main _ _ ' :
    head = None ;
    head = push ( head , 5 ) ;
    head = push ( head , 21 ) ;
    head = push ( head , 8 ) ;
    head = push ( head , 12 ) ;
    head = push ( head , 3 ) ;
    head = push ( head , 13 ) ;
    head = push ( head , 144 ) ;
    head = push ( head , 6 ) ;
    print ( meanofnodes ( head ) )

Accumulator variables: cnt, hashmap, sum
Class/struct names: Node
Python program:
def findNumbers ( n ) :
    i = 0
    while ( i <= n ) :
        print ( 2 * i * i + 4 * i + 1 + i % 2 , end = " ▁ " )
        i += 1
n = 6
findNumbers ( n )

Accumulator variables: i
Python program:
from collections import deque
def Queries ( arr , N , Q ) :
    dq = deque ( )
    for i in range ( N ) :
        dq . append ( arr [ i ] )
    sz = len ( Q )
    for i in range ( sz ) :
        if ( Q [ i ] [ 0 ] == 0 ) :
            front = dq [ 0 ]
            dq . popleft ( )
            dq . appendleft ( front )
        elif ( Q [ i ] [ 0 ] == 1 ) :
            back = dq [ N - 1 ]
            dq . popleft ( )
            dq . appendleft ( back )
        elif ( Q [ i ] [ 0 ] == 2 ) :
            dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ]
        else :
            print ( dq [ Q [ i ] [ 1 ] ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    N = len ( arr )
    Q = [ [ 0 ] , [ 1 ] , [ 3 , 1 ] , [ 2 , 2 , 54 ] , [ 3 , 2 ] ]
    Queries ( arr , N , Q )

Index/key variables: i
Accumulator variables: dq
Iterator variables: i
Python program:
import sys
def smallSubarray ( arr , n , total_sum ) :
    m = { }
    length = sys . maxsize
    prefixSum = 0
    for i in range ( n ) :
        prefixSum += arr [ i ]
        if ( prefixSum == total_sum ) :
            length = min ( length , i + 1 )
        m [ prefixSum ] = i
        if ( ( prefixSum - total_sum ) in m . keys ( ) ) :
            length = min ( length , i - m [ prefixSum - total_sum ] )
    return length
def smallestSubarrayremoved ( arr , n , k ) :
    total_sum = 0
    for i in range ( n ) :
        if ( arr [ i ] > k ) :
            arr [ i ] = 1
        elif ( arr [ i ] < k ) :
            arr [ i ] = - 1
        else :
            arr [ i ] = 0
        total_sum += arr [ i ]
    if ( total_sum == 0 ) :
        return 0
    else :
        return smallSubarray ( arr , n , total_sum )
arr = [ 12 , 16 , 12 , 13 , 10 ]
K = 13
n = len ( arr )
print ( smallestSubarrayremoved ( arr , n , K ) )

Index/key variables: i, prefixSum
Accumulator variables: total_sum
Iterator variables: i
Python program:
def balancedTernary ( n ) :
    output = " "
    while ( n > 0 ) :
        rem = n % 3
        n = n // 3
        if ( rem == 2 ) :
            rem = - 1
            n += 1
        if ( rem == 0 ) :
            output = '0' + output
        else :
            if ( rem == 1 ) :
                output = '1' + output
            else :
                output = ' Z ' + output
    return output
n = 238
print ( " Equivalent ▁ Balanced ▁ Ternary ▁ of " , n , " is : " , balancedTernary ( n ) )

Accumulator variables: n
Python program:
import sys
spt = 0
def printVector ( arr ) :
    global spt
    min_i = sys . maxsize
    for i in range ( len ( arr ) ) :
        min_i = min ( min_i , arr [ i ] )
    freq = arr . count ( min_i )
    spt += freq
def findWays ( arr , i , n ) :
    if ( n == 0 ) :
        printVector ( arr )
    for j in range ( i , n + 1 ) :
        arr . append ( j )
        findWays ( arr , j , n - j )
        del arr [ - 1 ]
def spt_function ( n ) :
    arr = [ ]
    findWays ( arr , 1 , n )
    print ( spt )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    spt_function ( N )

Index/key variables: i
Accumulator variables: arr
Iterator variables: i, j
Python program:
import math
def isPrime ( n ) :
    if ( n <= 1 ) :
        return False
    for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) :
        if ( n % i == 0 ) :
            return False
    return True
def takeSum ( a , n , m ) :
    sum = 0
    for i in range ( 0 , n ) :
        for j in range ( 0 , m ) :
            sum += a [ i ] [ j ]
    return sum
def checkIndex ( n , m , a ) :
    for i in range ( 0 , n ) :
        for j in range ( 0 , m ) :
            if ( isPrime ( i + j ) ) :
                if ( isPrime ( a [ i ] [ j ] ) != True ) :
                    return False
    return True
n = 4
m = 5
a = [ [ 1 , 2 , 3 , 2 , 2 ] , [ 2 , 2 , 7 , 7 , 7 ] , [ 7 , 7 , 21 , 7 , 10 ] , [ 2 , 2 , 3 , 6 , 7 ] ]
sum = takeSum ( a , n , m )
if ( isPrime ( sum ) and checkIndex ( n , m , a ) ) :
    print ( " YES " )
else :
    print ( " NO " )

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def countPairs ( A , n ) :
    count = 0
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( ( A [ i ] % 2 == 0 and A [ j ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ j ] % 2 != 0 ) ) :
                count += 1
    return count
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 2 , 3 , 1 , 3 ]
    n = len ( A )
    print ( countPairs ( A , n ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def isPrime ( n ) :
    if ( n <= 1 ) :
        return False
    if ( n <= 3 ) :
        return True
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return False
    for i in range ( 5 , int ( n ** 0.5 ) + 1 , 6 ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return False
    return True
def isWeaklyPrimeNum ( N ) :
    if ( isPrime ( N ) == False ) :
        return False
    s = str ( N )
    for j in range ( len ( s ) ) :
        str1 = s
        for i in range ( 10 ) :
            c = str ( i )
            str1 = str1 [ : j ] + c + str1 [ j + 1 : ]
            Num = int ( str1 )
            if ( str1 [ j ] != s [ j ] and isPrime ( Num ) ) :
                return False
    return True
n = 294001
if ( isWeaklyPrimeNum ( n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: j
Iterator variables: i, j
Python program:
def isPrime ( n ) :
    if ( n <= 1 ) :
        return False
    if ( n <= 3 ) :
        return True
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return False
    for i in range ( 5 , int ( n ** 0.5 ) + 1 , 6 ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return False
    return True
def isUnPrimeableNum ( N ) :
    if ( isPrime ( N ) ) :
        return False
    s = str ( N )
    for j in range ( len ( s ) ) :
        str1 = s
        for i in range ( 10 ) :
            c = str ( i )
            str1 = str1 [ : j ] + c + str1 [ j + 1 : ]
            Num = int ( str1 )
            if ( str1 [ j ] != s [ j ] and isPrime ( Num ) ) :
                return False
    return True
n = 200
if ( isUnPrimeableNum ( n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: j
Iterator variables: i, j
Python program:
def getRow ( rowIndex ) :
    currow = [ ]
    currow . append ( 1 )
    if ( rowIndex == 0 ) :
        return currow
    prev = getRow ( rowIndex - 1 )
    for i in range ( 1 , len ( prev ) ) :
        curr = prev [ i - 1 ] + prev [ i ]
        currow . append ( curr )
    currow . append ( 1 )
    return currow
n = 3
arr = getRow ( n )
for i in range ( len ( arr ) ) :
    if ( i == ( len ( arr ) - 1 ) ) :
        print ( arr [ i ] )
    else :
        print ( arr [ i ] , end = " , ▁ " )

Index/key variables: i
Accumulator variables: currow
Iterator variables: i
Python program:
def allQuotients ( N ) :
    s = set ( )
    for k in range ( 1 , N + 2 ) :
        s . add ( N // k )
    for it in s :
        print ( it , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    N = 5
    allQuotients ( N )

Accumulator variables: s
Iterator variables: it, k
Python program:
class Node :
    def __init__ ( self , s ) :
        self . i = s
        self . next = None
def Majority_in_linklist ( head ) :
    s , t = " " , " "
    p , q = 0 , 0
    ptr = None
    while head != None :
        if s == head . i :
            p = p + 1
        else :
            if t == head . i :
                q = q + 1
            else :
                if p == 0 :
                    s = head . i
                    p = 1
                else :
                    if q == 0 :
                        t = head . i
                        q = 1
                    else :
                        p = p - 1
                        q = q - 1
        head = head . next
    head = ptr
    p = 0
    q = 0
    while head != None :
        if s == head . i :
            p = 1
        else :
            if t == head . i :
                q = 1
        head = head . next
    if p > q :
        return s
    else :
        return t
ptr = None
head = Node ( " geeks " )
head . next = Node ( " geeks " )
head . next . next = Node ( " abcd " )
head . next . next . next = Node ( " game " )
head . next . next . next . next = Node ( " game " )
head . next . next . next . next . next = Node ( " knight " )
head . next . next . next . next . next . next = Node ( " harry " )
head . next . next . next . next . next . next . next = Node ( " geeks " )
print ( Majority_in_linklist ( head ) )

Class/struct names: Node
Python program:
def max_number ( arr , sum ) :
    frac = [ 0 ] * 9
    maxi = - 10 ** 9
    pos = 0
    for i in range ( 9 ) :
        frac [ i ] = sum // arr [ i ]
        if ( frac [ i ] > maxi ) :
            pos = i
            maxi = frac [ i ]
    an = str ( ( pos + 1 ) ) * maxi
    sum -= maxi * arr [ pos ]
    ans = [ i for i in an ]
    for i in range ( maxi ) :
        for j in range ( 1 , 10 ) :
            if ( sum + arr [ pos ] - arr [ j - 1 ] >= 0 ) :
                ans [ i ] = str ( j )
                sum += arr [ pos ] - arr [ j - 1 ]
                break
    if ( maxi == 0 ) :
        return 0
    else :
        return " " . join ( ans )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 ]
    s = 13
    print ( max_number ( arr , s ) )

Index/key variables: i, pos
Accumulator variables: sum
Iterator variables: i, j
Python program:
import math
def FindValue ( N , K ) :
    if ( N % 2 == 0 ) :
        N = N + 2 * K
    else :
        for i in range ( 2 , ( int ) ( math . sqrt ( N ) ) + 1 ) :
            if ( N % i == 0 ) :
                break
        N = N + i
        N = N + 2 * ( K - 1 )
    print ( N )
if __name__ == " _ _ main _ _ " :
    N = 9
    K = 4
    FindValue ( N , K )

Iterator variables: i
Python program:
def HexaDecimaltoBCD ( str ) :
    for i in range ( len ( str ) ) :
        print ( " { 0:04b } " . format ( int ( str [ i ] , 16 ) ) , end = " ▁ " )
str = "11F "
HexaDecimaltoBCD ( str )

Index/key variables: i
Iterator variables: i
Python program:
def heptadecagonal_num ( n ) :
    return ( ( 15 * n * n ) - 13 * n ) // 2
def sum_heptadecagonal_num ( n ) :
    summ = 0
    for i in range ( 1 , n + 1 ) :
        summ += heptadecagonal_num ( i )
    return summ
if __name__ == ' _ _ main _ _ ' :
    n = 5
    print ( sum_heptadecagonal_num ( n ) )

Accumulator variables: summ
Iterator variables: i
Python program:
pref = [ 0 ] * 100001
def isComposite ( n ) :
    if ( n <= 1 ) :
        return 0
    if ( n <= 3 ) :
        return 0
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return n
    i = 5
    while ( i * i <= n ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return n
        i = i + 6
    return 0
def preCompute ( ) :
    for i in range ( 1 , 100001 ) :
        pref [ i ] = pref [ i - 1 ] + isComposite ( i )
def printSum ( L , R ) :
    print ( pref [ R ] - pref [ L - 1 ] )
def printSumcomposite ( arr , Q ) :
    preCompute ( )
    for i in range ( Q ) :
        printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] )
if __name__ == " _ _ main _ _ " :
    Q = 2
    arr = [ [ 10 , 13 ] , [ 12 , 21 ] ]
    printSumcomposite ( arr , Q )

Index/key variables: R, i
Iterator variables: i
Python program:
from math import sqrt
pref = [ 0 ] * 100010
def isPerfectSquare ( x ) :
    s = int ( sqrt ( x ) )
    if ( s * s == x ) :
        return True
    return False
def isNonFibonacci ( n ) :
    x = 5 * n * n
    if ( isPerfectSquare ( x + 4 ) or isPerfectSquare ( x - 4 ) ) :
        return 0
    else :
        return n
def compute ( ) :
    for i in range ( 1 , 100001 ) :
        pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i )
def printSum ( L , R ) :
    sum = pref [ R ] - pref [ L - 1 ]
    print ( sum , end = " ▁ " )
compute ( )
Q = 2
arr = [ [ 1 , 5 ] , [ 6 , 10 ] ]
for i in range ( Q ) :
    printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] )

Index/key variables: R, i
Iterator variables: i
Python program:
from math import sqrt
def isPrime ( n ) :
    if ( n <= 1 ) :
        return False
    if ( n <= 3 ) :
        return True
    if ( n % 2 == 0 or n % 3 == 0 ) :
        return False
    for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) :
        if ( n % i == 0 or n % ( i + 2 ) == 0 ) :
            return False
    return True
def twinPrime ( n1 , n2 ) :
    return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 2 )
def countTwinPairs ( arr , n ) :
    count = 0
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( twinPrime ( arr [ i ] , arr [ j ] ) ) :
                count += 1
    return count
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 5 , 11 ]
    n = len ( arr )
    print ( countTwinPairs ( arr , n ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def solve ( arr , n ) :
    unfilled_indices = { }
    missing = { }
    for i in range ( n ) :
        missing [ i ] = 1
    for i in range ( 1 , n ) :
        if ( arr [ i ] == 0 ) :
            unfilled_indices [ i ] = 1
        else :
            del missing [ arr [ i ] ]
    it2 = list ( missing . keys ( ) )
    m = len ( it2 )
    for it in unfilled_indices :
        arr [ it ] = it2 [ m - 1 ]
        m -= 1
    pos = 0
    for i in range ( 1 , n ) :
        if ( arr [ i ] == i ) :
            pos = i
    x = 0
    if ( pos != 0 ) :
        for i in range ( 1 , n ) :
            if ( pos != i ) :
                if i in unfilled_indices :
                    x = arr [ i ]
                    arr [ i ] = pos
                    arr [ pos ] = x
                    break
    printArray ( arr , n )
def printArray ( arr , n ) :
    for i in range ( 1 , n ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 ]
    n = len ( arr )
    solve ( arr , n )

Index/key variables: i, it, pos
Accumulator variables: m
Iterator variables: i, it
Python program:
def check ( x , pos , neg , k ) :
    pairs = 0
    p = len ( neg ) - 1
    nn = len ( neg ) - 1
    pp = len ( pos ) - 1
    for i in range ( len ( neg ) ) :
        while ( p >= 0 and neg [ i ] * neg [ p ] <= x ) :
            p -= 1
        pairs += min ( nn - p , nn - i )
    p = 0
    for i in range ( len ( pos ) - 1 , - 1 , - 1 ) :
        while ( p < len ( pos ) and pos [ i ] * pos [ p ] <= x ) :
            p += 1
        pairs += min ( p , i )
    p = len ( pos ) - 1
    for i in range ( len ( neg ) - 1 , - 1 , - 1 ) :
        while ( p >= 0 and neg [ i ] * pos [ p ] <= x ) :
            p -= 1
        pairs += pp - p
    return ( pairs >= k )
def kth_element ( a , n , k ) :
    pos , neg = [ ] , [ ]
    for i in range ( n ) :
        if ( a [ i ] >= 0 ) :
            pos . append ( a [ i ] )
        else :
            neg . append ( a [ i ] )
    pos = sorted ( pos )
    neg = sorted ( neg )
    l = - 10 ** 18
    ans = 0
    r = 10 ** 18
    while ( l <= r ) :
        mid = ( l + r ) >> 1
        if ( check ( mid , pos , neg , k ) ) :
            ans = mid
            r = mid - 1
        else :
            l = mid + 1
    return ans
a = [ - 4 , - 2 , 3 , 3 ]
k = 3
n = len ( a )
print ( kth_element ( a , n , k ) )

Index/key variables: i, p
Accumulator variables: neg, pairs, pos
Iterator variables: i
Python program:
def squareRoot ( n , l ) :
    x = n
    count = 0
    while ( 1 ) :
        count += 1
        root = 0.5 * ( x + ( n / x ) )
        if ( abs ( root - x ) < l ) :
            break
        x = root
    return root
if __name__ == " _ _ main _ _ " :
    n = 327
    l = 0.00001
    print ( squareRoot ( n , l ) )

Accumulator variables: count