dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from bisect import bisect_left as lower_bound
def merge ( v1 , v2 ) :
    i = 0
    j = 0
    v = [ ]
    while ( i < len ( v1 ) and j < len ( v2 ) ) :
        if ( v1 [ i ] <= v2 [ j ] ) :
            v . append ( v1 [ i ] )
            i += 1
        else :
            v . append ( v2 [ j ] )
            j += 1
    for k in range ( i , len ( v1 ) ) :
        v . append ( v1 [ k ] )
    for k in range ( j , len ( v2 ) ) :
        v . append ( v2 [ k ] )
    return v
def buildTree ( tree , arr , index , s , e ) :
    if ( s == e ) :
        tree [ index ] . append ( arr [ s ] )
        return
    mid = ( s + e ) // 2
    buildTree ( tree , arr , 2 * index , s , mid )
    buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e )
    tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] )
def query ( tree , index , s , e , l , r , k ) :
    if ( r < s or l > e ) :
        return 0
    if ( s >= l and e <= r ) :
        return len ( tree [ index ] ) - ( lower_bound ( tree [ index ] , k ) )
    mid = ( s + e ) // 2
    return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) )
def performQueries ( L , R , K , n , q , tree ) :
    for i in range ( q ) :
        print ( query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 3 , 9 , 13 , 5 , 4 ]
    n = len ( arr )
    tree = [ [ ] for i in range ( 4 * n + 1 ) ]
    buildTree ( tree , arr , 1 , 0 , n - 1 )
    L = [ 1 , 2 ]
    R = [ 4 , 6 ]
    K = [ 6 , 8 ]
    q = len ( L )
    performQueries ( L , R , K , n , q , tree )

Index/key variables: i, index, j, k, s
Accumulator variables: v
Iterator variables: i, k
Python program:
import math
def combine ( a , b ) :
    if a != 0 and b != 0 :
        return a
    if a >= b :
        return a
    return b
def build_tree ( arr , tree , ind , low , high , x ) :
    if low == high :
        if arr [ low ] > x :
            tree [ ind ] = arr [ low ]
        else :
            tree [ ind ] = 0
        return
    mid = ( high - low ) / 2 + low
    mid = math . floor ( mid )
    mid = int ( mid )
    build_tree ( arr , tree , 2 * ind + 1 , low , mid , x )
    build_tree ( arr , tree , 2 * ind + 2 , mid + 1 , high , x )
    tree [ ind ] = combine ( tree [ 2 * ind + 1 ] , tree [ 2 * ind + 2 ] )
def query ( tree , ind , low , high , l , r ) :
    mid = ( high - low ) / 2 + low
    mid = math . floor ( mid )
    mid = int ( mid )
    if low > r or high < l :
        return 0
    if l <= low and r >= high :
        return tree [ ind ]
    q1 = query ( tree , 2 * ind + 1 , low , mid , l , r )
    q2 = query ( tree , 2 * ind + 2 , mid + 1 , high , l , r )
    return combine ( q1 , q2 )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 3 , 9 , 13 , 5 , 4 ]
    n = len ( arr )
    k = 6
    tree = [ [ ] for i in range ( 4 * n ) ]
    l = 1
    r = 4
    build_tree ( arr , tree , 0 , 0 , n - 1 , k )
    print ( query ( tree , 0 , 0 , n - 1 , l - 1 , r - 1 ) )

Index/key variables: ind, low
Python program:
def findGCDSum ( n , a ) :
    GCDSum = 0 ;
    tempGCD = 0 ;
    for i in range ( n ) :
        for j in range ( i , n ) :
            tempGCD = 0 ;
            for k in range ( i , j + 1 ) :
                tempGCD = __gcd ( tempGCD , a [ k ] ) ;
            GCDSum += tempGCD ;
    return GCDSum ;
def __gcd ( a , b ) :
    return a if ( b == 0 ) else __gcd ( b , a % b ) ;
if __name__ == ' _ _ main _ _ ' :
    n = 5 ;
    a = [ 1 , 2 , 3 , 4 , 5 ] ;
    totalSum = findGCDSum ( n , a ) ;
    print ( totalSum ) ;

Index/key variables: k
Accumulator variables: GCDSum
Iterator variables: i, j, k
Python program:
def containsElement ( arr , n ) :
    xorArr = 0
    for i in range ( n ) :
        xorArr ^= arr [ i ]
    for i in range ( n ) :
        x = xorArr ^ arr [ i ]
        if ( arr [ i ] == x ) :
            return True
    return False
arr = [ 8 , 2 , 4 , 15 , 1 ]
n = len ( arr )
if ( containsElement ( arr , n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Accumulator variables: xorArr
Iterator variables: i
Python program:
def add ( adj , u , v ) :
    adj [ u ] . append ( v )
def topo ( adj , n ) :
    indeg = [ 0 for i in range ( n ) ]
    for i in range ( n ) :
        for x in adj [ i ] :
            indeg [ x ] += 1
    answer = [ ]
    visited = [ False for i in range ( n ) ]
    q = [ ]
    for i in range ( n ) :
        if ( indeg [ i ] == 0 ) :
            q . append ( i )
            visited [ i ] = True
    while ( len ( q ) != 0 ) :
        u = q [ 0 ]
        answer . append ( u )
        q . remove ( q [ 0 ] )
        for x in adj [ u ] :
            indeg [ x ] -= 1
            if ( indeg [ x ] == 0 and visited [ x ] == False ) :
                q . append ( x )
                visited [ x ] = True
    return answer
def who_wins ( topotable , u , v ) :
    for x in topotable :
        if ( x == u ) :
            return u
        if ( x == v ) :
            return v
if __name__ == ' _ _ main _ _ ' :
    adj = [ [ ] for i in range ( 10 ) ]
    n = 7
    add ( adj , 0 , 1 )
    add ( adj , 0 , 2 )
    add ( adj , 0 , 3 )
    add ( adj , 1 , 5 )
    add ( adj , 2 , 5 )
    add ( adj , 3 , 4 )
    add ( adj , 4 , 5 )
    add ( adj , 6 , 0 )
    topotable = topo ( adj , n )
    print ( who_wins ( topotable , 3 , 5 ) )
    print ( who_wins ( topotable , 1 , 2 ) )

Index/key variables: i, u, x
Accumulator variables: answer, q
Iterator variables: i, x
Python program:
def length_substring ( S , K ) :
    curr_cnt = 0
    prev_cnt = 0
    max_len = 0
    for i in range ( len ( S ) ) :
        if ( S [ i ] == K ) :
            curr_cnt += 1
        else :
            prev_cnt = max ( prev_cnt , curr_cnt )
            curr_cnt = 0
    prev_cnt = max ( prev_cnt , curr_cnt )
    max_len = prev_cnt
    return max_len
if __name__ == ' _ _ main _ _ ' :
    S = " abcd1111aabc "
    K = '1'
    print ( length_substring ( S , K ) )

Index/key variables: i
Accumulator variables: curr_cnt
Iterator variables: i
Python program:
def Xor_Sum ( arr , n ) :
    sum = 0
    index , left_xor = 0 , 0
    right_xor = 0
    for i in range ( n ) :
        left_xor = left_xor ^ arr [ i ]
        right_xor = 0
        for j in range ( i + 1 , n ) :
            right_xor = right_xor ^ arr [ j ]
        if ( left_xor + right_xor > sum ) :
            sum = left_xor + right_xor
            index = i
    return index + 1
arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ]
n = len ( arr )
print ( Xor_Sum ( arr , n ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def ComputePrefixXor ( arr , PrefixXor , n ) :
    PrefixXor [ 0 ] = arr [ 0 ] ;
    for i in range ( 1 , n ) :
        PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ;
def Xor_Sum ( arr , n ) :
    PrefixXor = [ 0 ] * n ;
    ComputePrefixXor ( arr , PrefixXor , n ) ;
    sum , index = 0 , 0 ;
    for i in range ( n ) :
        if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) :
            sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ;
            index = i ;
    return index + 1 ;
arr = [ 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 ] ;
n = len ( arr ) ;
print ( Xor_Sum ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def findColor ( mapWithColor , query ) :
    maximum = 0
    while ( query >= 1 ) :
        if ( query ) in mapWithColor . keys ( ) :
            maximum = max ( maximum , mapWithColor [ query ] )
        if ( query % 2 == 1 ) :
            query = ( query - 1 ) // 2
        else :
            query = query // 2
    return maximum
def buildMapWithColor ( arr , n ) :
    mapWithColor = { }
    for i in range ( n ) :
        mapWithColor [ arr [ i ] ] = i + 1
    return mapWithColor
arr = [ 3 , 2 , 1 , 3 ]
n = len ( arr )
k = 7
mapWithColor = buildMapWithColor ( arr , n )
print ( findColor ( mapWithColor , k ) )

Index/key variables: i, query
Iterator variables: i
Python program:
from bisect import bisect_left as lower_bound
MAX = 26
def findCount ( a , b , n , m ) :
    freq = [ 0 for i in range ( MAX ) ]
    smallestFreq = [ ]
    for i in range ( n ) :
        s = a [ i ]
        for i in range ( MAX ) :
            freq [ i ] = 0
        for j in range ( len ( s ) ) :
            freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1
        for j in range ( MAX ) :
            if ( freq [ j ] ) :
                smallestFreq . append ( freq [ j ] )
                break
    smallestFreq = sorted ( smallestFreq )
    ans = [ ]
    for i in range ( m ) :
        s = b [ i ]
        for i in range ( MAX ) :
            freq [ i ] = 0
        for j in range ( len ( s ) ) :
            freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1
        frequency = 0
        for j in range ( MAX ) :
            if ( freq [ j ] ) :
                frequency = freq [ j ]
                break
        ind = lower_bound ( smallestFreq , frequency )
        ans . append ( ind )
    return ans
def printAnswer ( a , b , n , m ) :
    ans = findCount ( a , b , n , m )
    for it in ans :
        print ( it , end = " ▁ " )
A = [ " aaa " , " aa " , " bdc " ]
B = [ " cccch " , " cccd " ]
n = len ( A )
m = len ( B )
printAnswer ( A , B , n , m )

Index/key variables: i, j
Accumulator variables: ans, smallestFreq
Iterator variables: i, it, j
Python program:
def LeftRightNode ( preorder , n ) :
    min = 10 ** 9
    max = - 10 ** 9
    for i in range ( n ) :
        if ( min > preorder [ i ] ) :
            min = preorder [ i ]
        if ( max < preorder [ i ] ) :
            max = preorder [ i ]
    print ( " Leftmost ▁ node ▁ is ▁ " , min )
    print ( " Rightmost ▁ node ▁ is ▁ " , max )
preorder = [ 3 , 2 , 1 , 5 , 4 ]
n = len ( preorder )
LeftRightNode ( preorder , n )

Index/key variables: i
Iterator variables: i
Python program:
m = 6 ;
n = 4 ;
def compareRow ( a1 , a2 ) :
    for i in range ( n ) :
        if ( a1 [ i ] < a2 [ i ] ) :
            return 1 ;
        elif ( a1 [ i ] > a2 [ i ] ) :
            return - 1 ;
    return 0 ;
def binaryCheck ( ar , arr ) :
    l = 0 ; r = m - 1 ;
    while ( l <= r ) :
        mid = ( l + r ) // 2 ;
        temp = compareRow ( ar [ mid ] , arr ) ;
        if ( temp == 0 ) :
            return mid + 1 ;
        elif ( temp == 1 ) :
            l = mid + 1 ;
        else :
            r = mid - 1 ;
    return - 1 ;
if __name__ == " _ _ main _ _ " :
    mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ;
    row = [ 10 , 9 , 22 , 23 ] ;
    print ( binaryCheck ( mat , row ) ) ;

Index/key variables: i, mid
Iterator variables: i
Python program:
from bisect import bisect as upper_bound
maxLen = 30
def findCnt ( arr , n , k ) :
    ans = 0
    for i in range ( 1 , n ) :
        arr [ i ] += arr [ i - 1 ]
        if ( arr [ i ] > k or arr [ i ] < - 1 * k ) :
            ans += 1
    if ( arr [ 0 ] > k or arr [ 0 ] < - 1 * k ) :
        ans += 1
    arr = sorted ( arr )
    for i in range ( n ) :
        ans += n - upper_bound ( arr , arr [ i ] + k )
    return ans
arr = [ - 1 , 4 , - 5 , 6 ]
n = len ( arr )
k = 0
print ( findCnt ( arr , n , k ) )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
from bisect import bisect as upper_bound
def FindIntersection ( type1 , n , type2 , m ) :
    ans = n * m
    start = [ ]
    end = [ ]
    for i in range ( n ) :
        start . append ( type1 [ i ] [ 0 ] )
        end . append ( type1 [ i ] [ 1 ] )
    start = sorted ( start )
    start = sorted ( end )
    for i in range ( m ) :
        L = type2 [ i ] [ 0 ]
        R = type2 [ i ] [ 1 ]
        ans -= ( len ( start ) - upper_bound ( start , R ) )
        ans -= ( upper_bound ( end , L - 1 ) )
    return ans
type1 = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 6 , 7 ] ]
type2 = [ [ 1 , 5 ] , [ 2 , 3 ] , [ 4 , 7 ] , [ 5 , 7 ] ]
n = len ( type1 )
m = len ( type2 )
print ( FindIntersection ( type1 , n , type2 , m ) )

Index/key variables: i
Accumulator variables: ans, end, start
Iterator variables: i
Python program:
MAX = 100000
bit1 , bit2 , bit3 = 0 , 0 , 0
def count_common ( a , n , b , m ) :
    for i in range ( n ) :
        global bit1 , bit2 , bit3
        bit1 = bit1 | ( 1 << a [ i ] )
    for i in range ( m ) :
        bit2 = bit2 | ( 1 << b [ i ] )
    bit3 = bit1 & bit2 ;
    count = bin ( bit3 ) . count ( '1' ) ;
    return count ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 4 , 7 , 2 , 3 ] ;
    b = [ 2 , 11 , 7 , 4 , 15 , 20 , 24 ] ;
    n = len ( a ) ;
    m = len ( b ) ;
    print ( count_common ( a , n , b , m ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def createSorted ( a : list , n : int ) :
    b = [ ]
    for j in range ( n ) :
        if len ( b ) == 0 :
            b . append ( a [ j ] )
        else :
            start = 0
            end = len ( b ) - 1
            pos = 0
            while start <= end :
                mid = start + ( end - start ) // 2
                if b [ mid ] == a [ j ] :
                    b . insert ( max ( 0 , mid + 1 ) , a [ j ] )
                    break
                elif b [ mid ] > a [ j ] :
                    pos = end = mid - 1
                else :
                    pos = start = mid + 1
                if start > end :
                    pos = start
                    b . insert ( max ( 0 , pos ) , a [ j ] )
                    break
    for i in range ( n ) :
        print ( b [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    a = [ 2 , 5 , 4 , 9 , 8 ]
    n = len ( a )
    createSorted ( a , n )

Index/key variables: i, j, mid
Accumulator variables: b
Iterator variables: i, j
Python program:
MAX = 26
def atLeastK ( freq , k ) :
    for i in range ( MAX ) :
        if ( freq [ i ] != 0 and freq [ i ] < k ) :
            return False ;
    return True ;
def setZero ( freq ) :
    for i in range ( MAX ) :
        freq [ i ] = 0 ;
def findlength ( string , n , k ) :
    maxLen = 0 ;
    freq = [ 0 ] * MAX ;
    for i in range ( n ) :
        setZero ( freq ) ;
        for j in range ( i , n ) :
            freq [ ord ( string [ j ] ) - ord ( ' a ' ) ] += 1 ;
            if ( atLeastK ( freq , k ) ) :
                maxLen = max ( maxLen , j - i + 1 ) ;
    return maxLen ;
if __name__ == " _ _ main _ _ " :
    string = " xyxyyz " ;
    n = len ( string ) ;
    k = 2 ;
    print ( findlength ( string , n , k ) ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
def check ( m , n , arr ) :
    desired = [ 0 ] * n ;
    for i in range ( n - 1 , - 1 , - 1 ) :
        desired [ i ] = m ;
        m -= 1 ;
    for i in range ( n ) :
        if ( arr [ i ] > desired [ i ] or desired [ i ] < 1 ) :
            return False ;
    return True
def minOperations ( arr , n ) :
    start = arr [ n - 1 ] ;
    end = max ( arr ) + n ;
    max_arr = 0 ;
    while ( start <= end ) :
        mid = ( start + end ) // 2 ;
        if ( check ( mid , n , arr ) ) :
            max_arr = mid ;
            end = mid - 1 ;
        else :
            start = mid + 1 ;
    desired = [ 0 ] * n ;
    for i in range ( n - 1 , - 1 , - 1 ) :
        desired [ i ] = max_arr ;
        max_arr -= 1 ;
    operations = 0 ;
    for i in range ( n ) :
        operations += ( desired [ i ] - arr [ i ] ) ;
    return operations ;
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 4 , 5 , 5 , 7 ] ;
    n = len ( arr ) ;
    print ( minOperations ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: m, max_arr, operations
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , data = None , next = None ) :
        self . next = next
        self . data = data
head = None
def insert ( ref_head , item ) :
    global head
    temp = Node ( )
    temp . data = item
    temp . next = ref_head
    head = temp
def countNode ( head ) :
    if ( head == None ) :
        return 0
    s = set ( )
    s . add ( head . data )
    count = 0
    curr = head . next
    while ( curr != None ) :
        if ( curr . data in s ) :
            count = count + 1
        s . add ( curr . data )
        curr = curr . next
    return count
insert ( head , 5 )
insert ( head , 7 )
insert ( head , 5 )
insert ( head , 1 )
insert ( head , 7 )
print ( countNode ( head ) )

Accumulator variables: s
Class/struct names: Node
Python program:
def minSteps ( arr , n ) :
    v = [ 0 for i in range ( n ) ]
    q = [ ]
    q . append ( 0 )
    depth = 0
    while ( len ( q ) != 0 ) :
        x = len ( q )
        while ( x >= 1 ) :
            i = q [ 0 ]
            q . remove ( i )
            x -= 1
            if ( v [ i ] ) :
                continue ;
            if ( i == n - 1 ) :
                return depth
            v [ i ] = 1
            if ( i + arr [ i ] < n ) :
                q . append ( i + arr [ i ] )
            if ( i - arr [ i ] >= 0 ) :
                q . append ( i - arr [ i ] )
        depth += 1
    return - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 1 , 1 , 1 , 1 , 1 ]
    n = len ( arr )
    print ( minSteps ( arr , n ) )

Index/key variables: i
Accumulator variables: depth, q, x
Python program:
from math import *
def divisorcount ( elem ) :
    ans = 0
    for i in range ( 1 , int ( sqrt ( elem ) ) + 1 ) :
        if ( elem % i == 0 ) :
            if ( i * i == elem ) :
                ans += 1
            else :
                ans += 2
    return ans
def findwinner ( A , B , N , M ) :
    for i in range ( N ) :
        A [ i ] = divisorcount ( A [ i ] )
    for i in range ( M ) :
        B [ i ] = divisorcount ( B [ i ] )
    A . sort ( )
    B . sort ( )
    winA = 0
    for i in range ( N ) :
        val = A [ i ]
        start = 0
        end = M - 1
        index = - 1
        while ( start <= end ) :
            mid = ( start + end ) // 2
            if ( B [ mid ] <= val ) :
                index = mid
                start = mid + 1
            else :
                end = mid - 1
        winA += ( index + 1 )
    winB = N * M - winA
    if ( winA > winB ) :
        return " A "
    elif ( winB > winA ) :
        return " B "
    return " Draw "
if __name__ == ' _ _ main _ _ ' :
    A = [ 4 , 12 , 24 ]
    N = len ( A )
    B = [ 25 , 28 , 13 , 45 ]
    M = len ( B )
    print ( findwinner ( A , B , N , M ) )

Index/key variables: i, mid
Accumulator variables: ans, winA
Iterator variables: i
Python program:
def isPossible ( box , truck , n , m , min_time ) :
    temp = 0
    count = 0
    while ( count < m ) :
        j = 0
        while ( j < min_time and temp < n and truck [ count ] >= box [ temp ] ) :
            temp += 1
            j += 2
        count += 1
    if ( temp == n ) :
        return True
    return False
def minTime ( box , truck , n , m ) :
    box . sort ( ) ;
    truck . sort ( ) ;
    l = 0
    h = 2 * n
    min_time = 0
    while ( l <= h ) :
        mid = ( l + h ) // 2
        if ( isPossible ( box , truck , n , m , mid ) ) :
            min_time = mid
            h = mid - 1
        else :
            l = mid + 1
    return min_time
if __name__ == " _ _ main _ _ " :
    box = [ 10 , 2 , 16 , 19 ]
    truck = [ 29 , 25 ]
    n = len ( box )
    m = len ( truck )
    print ( minTime ( box , truck , n , m ) )

Index/key variables: count, temp
Accumulator variables: j
Python program:
def solve ( interval , N , Q ) :
    Mark = [ 0 for i in range ( Q ) ]
    for i in range ( N ) :
        l = interval [ i ] [ 0 ] - 1
        r = interval [ i ] [ 1 ] - 1
        for j in range ( l , r + 1 ) :
            Mark [ j ] += 1
    count = 0
    for i in range ( Q ) :
        if ( Mark [ i ] ) :
            count += 1
    count1 = [ 0 for i in range ( Q ) ]
    if ( Mark [ 0 ] == 1 ) :
        count1 [ 0 ] = 1
    for i in range ( 1 , Q ) :
        if ( Mark [ i ] == 1 ) :
            count1 [ i ] = count1 [ i - 1 ] + 1
        else :
            count1 [ i ] = count1 [ i - 1 ]
    maxindex = 0
    maxcoverage = 0
    for i in range ( N ) :
        l = interval [ i ] [ 0 ] - 1
        r = interval [ i ] [ 1 ] - 1
        elem1 = 0
        if ( l != 0 ) :
            elem1 = count1 [ r ] - count1 [ l - 1 ]
        else :
            elem1 = count1 [ r ]
        if ( count - elem1 >= maxcoverage ) :
            maxcoverage = count - elem1
            maxindex = i
    print ( " Maximum ▁ Coverage ▁ is " , maxcoverage , " after ▁ removing ▁ interval ▁ at ▁ index " , maxindex )
interval = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 3 , 5 ] ]
N = len ( interval )
Q = 7
solve ( interval , N , Q )

Index/key variables: i, j, r
Accumulator variables: count
Iterator variables: i, j
Python program:
def isPossible ( arr , n , dist , k ) :
    req = 0
    curr = 0
    prev = 0
    for i in range ( 0 , n ) :
        while ( curr != n and ( arr [ curr ] - arr [ prev ] ) <= dist ) :
            curr = curr + 1
        req = req + 1
        if ( curr == n ) :
            break
        prev = curr - 1
    if ( curr != n ) :
        return False
    if ( req <= k ) :
        return True
    return False
def minDistance ( arr , n , k ) :
    l = 0
    h = arr [ n - 1 ]
    ans = 0
    while ( l <= h ) :
        m = ( l + h ) // 2 ;
        if ( isPossible ( arr , n , m , k ) ) :
            ans = m
            h = m - 1
        else :
            l = m + 1
    return ans
arr = [ 2 , 15 , 36 , 43 ]
n = len ( arr )
k = 2
print ( minDistance ( arr , n , k ) )

Index/key variables: curr, prev
Iterator variables: i
Python program:
def getKthElement ( n , k , L , R ) :
    l = 1
    h = n
    total = [ 0 for i in range ( n + 1 ) ]
    total [ 0 ] = 0
    for i in range ( n ) :
        total [ i + 1 ] = total [ i ] + ( R [ i ] - L [ i ] ) + 1
    index = - 1
    while ( l <= h ) :
        m = ( l + h ) // 2
        if ( total [ m ] > k ) :
            index = m
            h = m - 1
        elif ( total [ m ] < k ) :
            l = m + 1
        else :
            index = m
            break
    l = L [ index - 1 ]
    h = R [ index - 1 ]
    x = k - total [ index - 1 ]
    while ( l <= h ) :
        m = ( l + h ) // 2
        if ( ( m - L [ index - 1 ] ) + 1 == x ) :
            return m
        elif ( ( m - L [ index - 1 ] ) + 1 > x ) :
            h = m - 1
        else :
            l = m + 1
L = [ 1 , 8 , 21 ]
R = [ 4 , 10 , 23 ]
n = len ( L )
k = 6
print ( getKthElement ( n , k , L , R ) )

Index/key variables: i, m
Iterator variables: i
Python program:
def IfExists ( arr , n ) :
    sum = [ 0 ] * n ;
    arr . sort ( ) ;
    sum [ 0 ] = arr [ 0 ] ;
    for i in range ( 1 , n ) :
        sum [ i ] = sum [ i - 1 ] + arr [ i ] ;
    max = arr [ n - 1 ] ;
    flag = False ;
    for i in range ( 1 , max + 1 ) :
        findex = 0 ;
        lindex = 0 ;
        l = 0 ;
        r = n - 1 ;
        while ( l <= r ) :
            m = ( l + r ) // 2 ;
            if ( arr [ m ] < i ) :
                findex = m ;
                l = m + 1 ;
            else :
                r = m - 1 ;
        l = 1 ;
        r = n ;
        flag = False ;
        while ( l <= r ) :
            m = ( r + l ) // 2 ;
            if ( arr [ m ] > i ) :
                lindex = m ;
                r = m - 1 ;
            else :
                l = m + 1 ;
        if ( sum [ findex ] == sum [ n - 1 ] - sum [ lindex - 1 ] ) :
            flag = True ;
            break ;
    if ( flag ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 2 , 5 ] ;
    n = len ( arr ) ;
    IfExists ( arr , n ) ;

Index/key variables: findex, i, m
Iterator variables: i
Boolean variables: flag
Python program:
def findMissing ( arr , n ) :
    l , h = 0 , n - 1
    mid = 0
    while ( h > l ) :
        mid = l + ( h - l ) // 2
        if ( arr [ mid ] - mid == arr [ 0 ] ) :
            if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) :
                return arr [ mid ] + 1
            else :
                l = mid + 1
        else :
            if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) :
                return arr [ mid ] - 1
            else :
                h = mid - 1
    return - 1
arr = [ - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 ]
n = len ( arr )
print ( findMissing ( arr , n ) )

Index/key variables: mid