dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def findPathUtil ( root : Node , k : int , path : list , flag : int ) :
    global ans
    if root is None :
        return
    if root . data >= k :
        flag = 1
    if root . left is None and root . right is None :
        if flag :
            ans = 1
            print ( " ( " , end = " " )
            for i in range ( len ( path ) ) :
                print ( path [ i ] , end = " , ▁ " )
            print ( root . data , end = " ) , ▁ " )
        return
    path . append ( root . data )
    findPathUtil ( root . left , k , path , flag )
    findPathUtil ( root . right , k , path , flag )
    path . pop ( )
def findPath ( root : Node , k : int ) :
    global ans
    flag = 0
    ans = 0
    v = [ ]
    findPathUtil ( root , k , v , flag )
    if ans == 0 :
        print ( - 1 )
if __name__ == " _ _ main _ _ " :
    ans = 0
    k = 25
    root = Node ( 10 )
    root . left = Node ( 5 )
    root . right = Node ( 8 )
    root . left . left = Node ( 29 )
    root . left . right = Node ( 2 )
    root . right . right = Node ( 98 )
    root . right . left = Node ( 1 )
    root . right . right . right = Node ( 50 )
    root . left . left . left = Node ( 20 )
    findPath ( root , k )

Index/key variables: i
Iterator variables: i
Class/struct names: Node
Python program:
def MaximumHeight ( a , n ) :
    result = 1
    for i in range ( 1 , n ) :
        y = ( i * ( i + 1 ) ) / 2
        if ( y < n ) :
            result = i
        else :
            break
    return result
arr = [ 40 , 100 , 20 , 30 ]
n = len ( arr )
print ( MaximumHeight ( arr , n ) )

Iterator variables: i
Python program:
def findK ( n , k ) :
    a = list ( )
    i = 1
    while i < n :
        a . append ( i )
        i = i + 2
    i = 2
    while i < n :
        a . append ( i )
        i = i + 2
    return ( a [ k - 1 ] )
n = 10
k = 3
print ( findK ( n , k ) )

Accumulator variables: a
Python program:
def pell ( n ) :
    if ( n <= 2 ) :
        return n
    a = 1
    b = 2
    for i in range ( 3 , n + 1 ) :
        c = 2 * b + a
        a = b
        b = c
    return b
n = 4
print ( pell ( n ) )

Iterator variables: i
Python program:
import math
MAX = 10000 ;
primes = [ ] ;
def Sieve ( ) :
    n = MAX ;
    nNew = int ( math . sqrt ( n ) ) ;
    marked = [ 0 ] * ( int ( n / 2 + 500 ) ) ;
    for i in range ( 1 , int ( ( nNew - 1 ) / 2 ) + 1 ) :
        for j in range ( ( ( i * ( i + 1 ) ) << 1 ) , ( int ( n / 2 ) + 1 ) , ( 2 * i + 1 ) ) :
            marked [ j ] = 1 ;
    primes . append ( 2 ) ;
    for i in range ( 1 , int ( n / 2 ) + 1 ) :
        if ( marked [ i ] == 0 ) :
            primes . append ( 2 * i + 1 ) ;
def binarySearch ( left , right , n ) :
    if ( left <= right ) :
        mid = int ( ( left + right ) / 2 ) ;
        if ( mid == 0 or mid == len ( primes ) - 1 ) :
            return primes [ mid ] ;
        if ( primes [ mid ] == n ) :
            return primes [ mid - 1 ] ;
        if ( primes [ mid ] < n and primes [ mid + 1 ] > n ) :
            return primes [ mid ] ;
        if ( n < primes [ mid ] ) :
            return binarySearch ( left , mid - 1 , n ) ;
        else :
            return binarySearch ( mid + 1 , right , n ) ;
    return 0 ;
Sieve ( ) ;
n = 17 ;
print ( binarySearch ( 0 , len ( primes ) - 1 , n ) ) ;

Index/key variables: i, j, mid
Accumulator variables: primes
Iterator variables: i, j
Python program:
def Maximum_xor_Triplet ( n , a ) :
    s = set ( )
    for i in range ( 0 , n ) :
        for j in range ( i , n ) :
            s . add ( a [ i ] ^ a [ j ] )
    ans = 0
    for i in s :
        for j in range ( 0 , n ) :
            ans = max ( ans , i ^ a [ j ] )
    print ( ans )
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 3 , 8 , 15 ]
    n = len ( a )
    Maximum_xor_Triplet ( n , a )

Index/key variables: i, j
Accumulator variables: s
Iterator variables: i, j
Python program:
from bisect import bisect_left
def printMissing ( arr , n , low , high ) :
    arr . sort ( )
    ptr = bisect_left ( arr , low )
    index = ptr
    i = index
    x = low
    while ( i < n and x <= high ) :
        if ( arr [ i ] != x ) :
            print ( x , end = " ▁ " )
        else :
            i = i + 1
        x = x + 1
    while ( x <= high ) :
        print ( x , end = " ▁ " )
        x = x + 1
arr = [ 1 , 3 , 5 , 4 ]
n = len ( arr )
low = 1
high = 10
printMissing ( arr , n , low , high ) ;

Index/key variables: i
Python program:
def printMissing ( arr , n , low , high ) :
    points_of_range = [ False ] * ( high - low + 1 )
    for i in range ( n ) :
        if ( low <= arr [ i ] and arr [ i ] <= high ) :
            points_of_range [ arr [ i ] - low ] = True
    for x in range ( high - low + 1 ) :
        if ( points_of_range [ x ] == False ) :
            print ( low + x , end = " ▁ " )
arr = [ 1 , 3 , 5 , 4 ]
n = len ( arr )
low , high = 1 , 10
printMissing ( arr , n , low , high )

Index/key variables: i, x
Iterator variables: i, x
Python program:
def printMissing ( arr , n , low , high ) :
    s = set ( arr )
    for x in range ( low , high + 1 ) :
        if x not in s :
            print ( x , end = ' ▁ ' )
arr = [ 1 , 3 , 5 , 4 ]
n = len ( arr )
low , high = 1 , 10
printMissing ( arr , n , low , high )

Iterator variables: x
Python program:
def find ( a , b , k , n1 , n2 ) :
    s = set ( )
    for i in range ( n2 ) :
        s . add ( b [ i ] )
    missing = 0
    for i in range ( n1 ) :
        if a [ i ] not in s :
            missing += 1
        if missing == k :
            return a [ i ]
    return - 1
a = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 ]
b = [ 4 , 10 , 6 , 8 , 12 ]
n1 = len ( a )
n2 = len ( b )
k = 3
print ( find ( a , b , k , n1 , n2 ) )

Index/key variables: i
Accumulator variables: missing, s
Iterator variables: i
Python program:
def findString ( S , N ) :
    amounts = [ 0 ] * 26
    for i in range ( len ( S ) ) :
        amounts [ ord ( S [ i ] ) - 97 ] += 1
    count = 0
    for i in range ( 26 ) :
        if amounts [ i ] > 0 :
            count += 1
    if count > N :
        print ( " - 1" )
    else :
        ans = " "
        high = 100001
        low = 0
        while ( high - low ) > 1 :
            total = 0
            mid = ( high + low ) // 2
            for i in range ( 26 ) :
                if amounts [ i ] > 0 :
                    total += ( amounts [ i ] - 1 ) // mid + 1
            if total <= N :
                high = mid
            else :
                low = mid
        print ( high , end = " ▁ " )
        total = 0
        for i in range ( 26 ) :
            if amounts [ i ] > 0 :
                total += ( amounts [ i ] - 1 ) // high + 1
                for j in range ( ( amounts [ i ] - 1 ) // high + 1 ) :
                    ans += chr ( i + 97 )
        for i in range ( total , N ) :
            ans += ' a '
        ans = ans [ : : - 1 ]
        print ( ans )
S = " toffee "
K = 4
findString ( S , K )

Index/key variables: i
Accumulator variables: ans, count, total
Iterator variables: i, j
Python program:
def printFirstRepeating ( arr , n ) :
    Min = - 1
    myset = dict ( )
    for i in range ( n - 1 , - 1 , - 1 ) :
        if arr [ i ] in myset . keys ( ) :
            Min = i
        else :
            myset [ arr [ i ] ] = 1
    if ( Min != - 1 ) :
        print ( " The ▁ first ▁ repeating ▁ element ▁ is " , arr [ Min ] )
    else :
        print ( " There ▁ are ▁ no ▁ repeating ▁ elements " )
arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ]
n = len ( arr )
printFirstRepeating ( arr , n )

Index/key variables: Min, i
Iterator variables: i
Python program:
def printFirstRepeating ( arr , n ) :
    k = 0
    max = n
    for i in range ( n ) :
        if ( max < arr [ i ] ) :
            max = arr [ i ]
    a = [ 0 for i in range ( max + 1 ) ]
    b = [ 0 for i in range ( max + 1 ) ]
    for i in range ( n ) :
        if ( a [ arr [ i ] ] ) :
            b [ arr [ i ] ] = 1
            k = 1
            continue
        else :
            a [ arr [ i ] ] = i
    if ( k == 0 ) :
        print ( " No ▁ repeating ▁ element ▁ found " )
    else :
        min = max + 1
        for i in range ( max + 1 ) :
            if ( a [ i ] and ( min > ( a [ i ] ) ) and b [ i ] ) :
                min = a [ i ]
        print ( arr [ min ] )
arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ]
n = len ( arr )
printFirstRepeating ( arr , n )

Index/key variables: i, min
Iterator variables: i
Python program:
def printKDistinct ( arr , n , k ) :
    dist_count = 0
    for i in range ( n ) :
        j = 0
        while j < n :
            if ( i != j and arr [ j ] == arr [ i ] ) :
                break
            j += 1
        if ( j == n ) :
            dist_count += 1
        if ( dist_count == k ) :
            return arr [ i ]
    return - 1
ar = [ 1 , 2 , 1 , 3 , 4 , 2 ]
n = len ( ar )
k = 2
print ( printKDistinct ( ar , n , k ) )

Index/key variables: i, j
Accumulator variables: dist_count
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , data ) :
        self . left = None
        self . right = None
        self . val = data
def newNode ( data ) :
    temp = Node ( data )
    return temp
def isEvenOddBinaryTree ( root ) :
    if ( root == None ) :
        return True
    q = [ ]
    q . append ( root )
    level = 0
    while ( len ( q ) != 0 ) :
        size = len ( q )
        for i in range ( size ) :
            node = q [ 0 ]
            q . pop ( 0 )
            if ( level % 2 == 0 ) :
                if ( node . val % 2 == 1 ) :
                    return False
                elif ( level % 2 == 1 ) :
                    if ( node . val % 2 == 0 ) :
                        return False
                if ( node . left != None ) :
                    q . append ( node . left )
                if ( node . right != None ) :
                    q . append ( node . right )
            level += 1
        return True
if __name__ == " _ _ main _ _ " :
    root = None
    root = newNode ( 2 )
    root . left = newNode ( 3 )
    root . right = newNode ( 9 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 10 )
    root . right . right = newNode ( 6 )
    if ( isEvenOddBinaryTree ( root ) ) :
        print ( " YES " )
    else :
        print ( " NO " )

Accumulator variables: level, q
Iterator variables: i
Class/struct names: Node
Python program:
import sys
def findMaxLen ( a ) :
    n = len ( a )
    freq = [ 0 ] * ( n + 1 )
    for i in range ( n ) :
        freq [ a [ i ] ] += 1
    maxFreqElement = - sys . maxsize - 1
    maxFreqCount = 1
    for i in range ( 1 , n + 1 ) :
        if ( freq [ i ] > maxFreqElement ) :
            maxFreqElement = freq [ i ]
            maxFreqCount = 1
        elif ( freq [ i ] == maxFreqElement ) :
            maxFreqCount += 1
    if ( maxFreqElement == 1 ) :
        ans = 0
    else :
        ans = ( ( n - maxFreqCount ) // ( maxFreqElement - 1 ) )
    return ans
a = [ 1 , 2 , 1 , 2 ]
print ( findMaxLen ( a ) )

Index/key variables: i
Accumulator variables: maxFreqCount
Iterator variables: i
Python program:
import math
def getMid ( s , e ) :
    return ( s + ( e - s ) // 2 )
def MaxUtil ( st , ss , se , l , r , node ) :
    if ( l <= ss and r >= se ) :
        return st [ node ]
    if ( se < l or ss > r ) :
        return - 1
    mid = getMid ( ss , se )
    return max ( MaxUtil ( st , ss , mid , l , r , 2 * node + 1 ) , MaxUtil ( st , mid + 1 , se , l , r , 2 * node + 2 ) )
def getMax ( st , n , l , r ) :
    if ( l < 0 or r > n - 1 or l > r ) :
        print ( " Invalid ▁ Input " )
        return - 1
    return MaxUtil ( st , 0 , n - 1 , l , r , 0 )
def constructSTUtil ( arr , ss , se , st , si ) :
    if ( ss == se ) :
        st [ si ] = arr [ ss ]
        return arr [ ss ]
    mid = getMid ( ss , se )
    st [ si ] = max ( constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) , constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) )
    return st [ si ]
def constructST ( arr , n ) :
    x = ( int ) ( math . ceil ( math . log ( n ) ) )
    max_size = 2 * ( int ) ( pow ( 2 , x ) ) - 1
    st = [ 0 ] * max_size
    constructSTUtil ( arr , 0 , n - 1 , st , 0 )
    return st
arr = [ 5 , 2 , 3 , 0 ]
n = len ( arr )
st = constructST ( arr , n )
Q = [ [ 1 , 3 ] , [ 0 , 2 ] ]
for i in range ( len ( Q ) ) :
    Max = getMax ( st , n , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] )
    ok = 0
    for j in range ( 30 , - 1 , - 1 ) :
        if ( ( Max & ( 1 << j ) ) != 0 ) :
            ok = 1
        if ( ok <= 0 ) :
            continue
        Max |= ( 1 << j )
    print ( Max , end = " ▁ " )

Index/key variables: i, node, si, ss
Accumulator variables: Max
Iterator variables: i, j
Python program:
def calculate ( a ) :
    a . sort ( )
    count = 1
    answer = 0
    for i in range ( 1 , len ( a ) ) :
        if a [ i ] == a [ i - 1 ] :
            count += 1
        else :
            answer = answer + count * ( count - 1 ) // 2
            count = 1
    answer = answer + count * ( count - 1 ) // 2
    return answer
if __name__ == ' _ _ main _ _ ' :
    a = [ 1 , 2 , 1 , 2 , 4 ]
    print ( calculate ( a ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def calculate ( a ) :
    maximum = max ( a )
    frequency = [ 0 for x in range ( maximum + 1 ) ]
    for i in a :
        frequency [ i ] += 1
    answer = 0
    for i in frequency :
        answer = answer + i * ( i - 1 ) // 2
    return answer
a = [ 1 , 2 , 1 , 2 , 4 ]
print ( calculate ( a ) )

Index/key variables: i
Iterator variables: i
Python program:
def findSubArray ( arr , n ) :
    sum = 0
    maxsize = - 1
    for i in range ( 0 , n - 1 ) :
        sum = - 1 if ( arr [ i ] == 0 ) else 1
        for j in range ( i + 1 , n ) :
            sum = sum + ( - 1 ) if ( arr [ j ] == 0 ) else sum + 1
            if ( sum == 0 and maxsize < j - i + 1 ) :
                maxsize = j - i + 1
                startindex = i
    if ( maxsize == - 1 ) :
        print ( " No ▁ such ▁ subarray " ) ;
    else :
        print ( startindex , " to " , startindex + maxsize - 1 ) ;
    return maxsize
arr = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 ]
size = len ( arr )
findSubArray ( arr , size )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findMax ( arr , low , high ) :
    if ( high == low ) :
        return arr [ low ]
    mid = low + ( high - low ) // 2
    if ( mid == 0 and arr [ mid ] > arr [ mid + 1 ] ) :
        return arr [ mid ]
    if ( mid < high and arr [ mid + 1 ] < arr [ mid ] and mid > 0 and arr [ mid ] > arr [ mid - 1 ] ) :
        return arr [ mid ]
    if ( arr [ low ] > arr [ mid ] ) :
        return findMax ( arr , low , mid - 1 )
    else :
        return findMax ( arr , mid + 1 , high )
arr = [ 6 , 5 , 4 , 3 , 2 , 1 ]
n = len ( arr )
print ( findMax ( arr , 0 , n - 1 ) )

Index/key variables: low, mid
Python program:
def ternarySearch ( l , r , key , ar ) :
    while r >= l :
        mid1 = l + ( r - l ) // 3
        mid2 = r - ( r - l ) // 3
        if key == ar [ mid1 ] :
            return mid1
        if key == mid2 :
            return mid2
        if key < ar [ mid1 ] :
            r = mid1 - 1
        elif key > ar [ mid2 ] :
            l = mid2 + 1
        else :
            l = mid1 + 1
            r = mid2 - 1
    return - 1
ar = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
l = 0
r = 9
key = 5
p = ternarySearch ( l , r , key , ar )
print ( " Index ▁ of " , key , " is " , p )
key = 50
p = ternarySearch ( l , r , key , ar )
print ( " Index ▁ of " , key , " is " , p )

Index/key variables: mid1, mid2
Python program:
def search ( arr , l , h , key ) :
    if l > h :
        return - 1
    mid = ( l + h ) // 2
    if arr [ mid ] == key :
        return mid
    if arr [ l ] <= arr [ mid ] :
        if key >= arr [ l ] and key <= arr [ mid ] :
            return search ( arr , l , mid - 1 , key )
        return search ( arr , mid + 1 , h , key )
    if key >= arr [ mid ] and key <= arr [ h ] :
        return search ( a , mid + 1 , h , key )
    return search ( arr , l , mid - 1 , key )
arr = [ 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 ]
key = 6
i = search ( arr , 0 , len ( arr ) - 1 , key )
if i != - 1 :
    print ( " Index : ▁ % ▁ d " % i )
else :
    print ( " Key ▁ not ▁ found " )

Index/key variables: h, l, mid
Python program:
def findMin ( arr , low , high ) :
    if high < low :
        return arr [ 0 ]
    if high == low :
        return arr [ low ]
    mid = int ( ( low + high ) / 2 )
    if mid < high and arr [ mid + 1 ] < arr [ mid ] :
        return arr [ mid + 1 ]
    if mid > low and arr [ mid ] < arr [ mid - 1 ] :
        return arr [ mid ]
    if arr [ high ] > arr [ mid ] :
        return findMin ( arr , low , mid - 1 )
    return findMin ( arr , mid + 1 , high )
arr1 = [ 5 , 6 , 1 , 2 , 3 , 4 ]
n1 = len ( arr1 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr1 , 0 , n1 - 1 ) ) )
arr2 = [ 1 , 2 , 3 , 4 ]
n2 = len ( arr2 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr2 , 0 , n2 - 1 ) ) )
arr3 = [ 1 ]
n3 = len ( arr3 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr3 , 0 , n3 - 1 ) ) )
arr4 = [ 1 , 2 ]
n4 = len ( arr4 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr4 , 0 , n4 - 1 ) ) )
arr5 = [ 2 , 1 ]
n5 = len ( arr5 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr5 , 0 , n5 - 1 ) ) )
arr6 = [ 5 , 6 , 7 , 1 , 2 , 3 , 4 ]
n6 = len ( arr6 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr6 , 0 , n6 - 1 ) ) )
arr7 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
n7 = len ( arr7 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr7 , 0 , n7 - 1 ) ) )
arr8 = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 ]
n8 = len ( arr8 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr8 , 0 , n8 - 1 ) ) )
arr9 = [ 3 , 4 , 5 , 1 , 2 ]
n9 = len ( arr9 )
print ( " The ▁ minimum ▁ element ▁ is ▁ " + str ( findMin ( arr9 , 0 , n9 - 1 ) ) )

Index/key variables: high, low, mid
Python program:
def findMin ( arr , low , high ) :
    while ( low < high ) :
        mid = low + ( high - low ) // 2 ;
        if ( arr [ mid ] == arr [ high ] ) :
            high -= 1 ;
        elif ( arr [ mid ] > arr [ high ] ) :
            low = mid + 1 ;
        else :
            high = mid ;
    return arr [ high ] ;
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 5 , 6 , 1 , 2 , 3 , 4 ] ;
    n1 = len ( arr1 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr1 , 0 , n1 - 1 ) ) ;
    arr2 = [ 1 , 2 , 3 , 4 ] ;
    n2 = len ( arr2 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr2 , 0 , n2 - 1 ) ) ;
    arr3 = [ 1 ] ;
    n3 = len ( arr3 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr3 , 0 , n3 - 1 ) ) ;
    arr4 = [ 1 , 2 ] ;
    n4 = len ( arr4 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr4 , 0 , n4 - 1 ) ) ;
    arr5 = [ 2 , 1 ] ;
    n5 = len ( arr5 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr5 , 0 , n5 - 1 ) ) ;
    arr6 = [ 5 , 6 , 7 , 1 , 2 , 3 , 4 ] ;
    n6 = len ( arr6 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr6 , 0 , n6 - 1 ) ) ;
    arr7 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
    n7 = len ( arr7 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr7 , 0 , n7 - 1 ) ) ;
    arr8 = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 ] ;
    n8 = len ( arr8 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr8 , 0 , n8 - 1 ) ) ;
    arr9 = [ 3 , 4 , 5 , 1 , 2 ] ;
    n9 = len ( arr9 ) ;
    print ( " The ▁ minimum ▁ element ▁ is ▁ " , findMin ( arr9 , 0 , n9 - 1 ) ) ;

Index/key variables: high, mid
Python program:
from bisect import bisect as upper_bound
def countPairs ( a , n , mid ) :
    res = 0
    for i in range ( n ) :
        res += upper_bound ( a , a [ i ] + mid )
    return res
def kthDiff ( a , n , k ) :
    a = sorted ( a )
    low = a [ 1 ] - a [ 0 ]
    for i in range ( 1 , n - 1 ) :
        low = min ( low , a [ i + 1 ] - a [ i ] )
    high = a [ n - 1 ] - a [ 0 ]
    while ( low < high ) :
        mid = ( low + high ) >> 1
        if ( countPairs ( a , n , mid ) < k ) :
            low = mid + 1
        else :
            high = mid
    return low
k = 3
a = [ 1 , 2 , 3 , 4 ]
n = len ( a )
print ( kthDiff ( a , n , k ) )

Index/key variables: i
Accumulator variables: res
Iterator variables: i
Python program:
MAX = 1000
tree = [ 0 ] * ( 4 * MAX )
arr = [ 0 ] * MAX
def gcd ( a : int , b : int ) :
    if a == 0 :
        return b
    return gcd ( b % a , a )
def lcm ( a : int , b : int ) :
    return ( a * b ) // gcd ( a , b )
def build ( node : int , start : int , end : int ) :
    if start == end :
        tree [ node ] = arr [ start ]
        return
    mid = ( start + end ) // 2
    build ( 2 * node , start , mid )
    build ( 2 * node + 1 , mid + 1 , end )
    left_lcm = tree [ 2 * node ]
    right_lcm = tree [ 2 * node + 1 ]
    tree [ node ] = lcm ( left_lcm , right_lcm )
def query ( node : int , start : int , end : int , l : int , r : int ) :
    if end < l or start > r :
        return 1
    if l <= start and r >= end :
        return tree [ node ]
    mid = ( start + end ) // 2
    left_lcm = query ( 2 * node , start , mid , l , r )
    right_lcm = query ( 2 * node + 1 , mid + 1 , end , l , r )
    return lcm ( left_lcm , right_lcm )
if __name__ == " _ _ main _ _ " :
    arr [ 0 ] = 5
    arr [ 1 ] = 7
    arr [ 2 ] = 5
    arr [ 3 ] = 2
    arr [ 4 ] = 10
    arr [ 5 ] = 12
    arr [ 6 ] = 11
    arr [ 7 ] = 17
    arr [ 8 ] = 14
    arr [ 9 ] = 1
    arr [ 10 ] = 44
    build ( 1 , 0 , 10 )
    print ( query ( 1 , 0 , 10 , 2 , 5 ) )
    print ( query ( 1 , 0 , 10 , 5 , 10 ) )
    print ( query ( 1 , 0 , 10 , 0 , 10 ) )

Index/key variables: node, start
Python program:
M = 1000000007
def waysOfDecoding ( s ) :
    dp = [ 0 ] * ( len ( s ) + 1 )
    dp [ 0 ] = 1
    if s [ 0 ] == ' * ' :
        dp [ 1 ] = 9
    elif s [ 0 ] == '0' :
        dp [ 1 ] = 0
    else :
        dp [ 1 ] = 1
    for i in range ( len ( s ) ) :
        if ( s [ i ] == ' * ' ) :
            dp [ i + 1 ] = 9 * dp [ i ]
            if ( s [ i - 1 ] == '1' ) :
                dp [ i + 1 ] = ( dp [ i + 1 ] + 9 * dp [ i - 1 ] ) % M
            elif ( s [ i - 1 ] == '2' ) :
                dp [ i + 1 ] = ( dp [ i + 1 ] + 6 * dp [ i - 1 ] ) % M
            elif ( s [ i - 1 ] == ' * ' ) :
                dp [ i + 1 ] = ( dp [ i + 1 ] + 15 * dp [ i - 1 ] ) % M
        else :
            if s [ i ] != '0' :
                dp [ i + 1 ] = dp [ i ]
            else :
                dp [ i + 1 ] = 0
            if ( s [ i - 1 ] == '1' ) :
                dp [ i + 1 ] = ( dp [ i + 1 ] + dp [ i - 1 ] ) % M
            elif ( s [ i - 1 ] == '2' and s [ i ] <= '6' ) :
                dp [ i + 1 ] = ( dp [ i + 1 ] + dp [ i - 1 ] ) % M
            elif ( s [ i - 1 ] == ' * ' ) :
                if ( s [ i ] <= '6' ) :
                    dp [ i + 1 ] = dp [ i + 1 ] + 2 * dp [ i - 1 ]
                else :
                    dp [ i + 1 ] = dp [ i + 1 ] + 1 * dp [ i - 1 ]
                dp [ i + 1 ] = dp [ i + 1 ] % M
    return dp [ len ( s ) ]
if __name__ == " _ _ main _ _ " :
    s = "12"
    print ( waysOfDecoding ( s ) )

Index/key variables: i
Iterator variables: i
Python program:
def countSubset ( arr , n , diff ) :
    sum = 0
    for i in range ( n ) :
        sum += arr [ i ]
    sum += diff
    sum = sum // 2
    t = [ [ 0 for i in range ( sum + 1 ) ] for i in range ( n + 1 ) ]
    for j in range ( sum + 1 ) :
        t [ 0 ] [ j ] = 0
    for i in range ( n + 1 ) :
        t [ i ] [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , sum + 1 ) :
            if ( arr [ i - 1 ] > j ) :
                t [ i ] [ j ] = t [ i - 1 ] [ j ]
            else :
                t [ i ] [ j ] = t [ i - 1 ] [ j ] + t [ i - 1 ] [ j - arr [ i - 1 ] ]
    return t [ n ] [ sum ]
if __name__ == ' _ _ main _ _ ' :
    diff , n = 1 , 4
    arr = [ 1 , 1 , 2 , 3 ]
    print ( countSubset ( arr , n , diff ) )

Index/key variables: i, j, n, sum
Iterator variables: i, j