dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def countBits ( n ) :
    count = 0
    while ( n ) :
        count += 1
        n >>= 1
    return count
i = 65
print ( countBits ( i ) )

Accumulator variables: count, n
Python program:
INT_MAX = 2147483647
def isKthBitSet ( x , k ) :
    return 1 if ( x & ( 1 << ( k - 1 ) ) ) else 0
def leftmostSetBit ( x ) :
    count = 0
    while ( x ) :
        count += 1
        x = x >> 1
    return count
def isBinPalindrome ( x ) :
    l = leftmostSetBit ( x )
    r = 1
    while ( l > r ) :
        if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) :
            return 0
        l -= 1
        r += 1
    return 1
def findNthPalindrome ( n ) :
    pal_count = 0
    i = 0
    for i in range ( 1 , INT_MAX + 1 ) :
        if ( isBinPalindrome ( i ) ) :
            pal_count += 1
        if ( pal_count == n ) :
            break
    return i
if __name__ == " _ _ main _ _ " :
    n = 9
    print ( findNthPalindrome ( n ) )

Accumulator variables: count, l, pal_count, r
Iterator variables: i
Python program:
def getNext ( n ) :
    c = n
    c0 = 0
    c1 = 0
    while ( ( ( c & 1 ) == 0 ) and ( c != 0 ) ) :
        c0 = c0 + 1
        c >>= 1
    while ( ( c & 1 ) == 1 ) :
        c1 = c1 + 1
        c >>= 1
    if ( c0 + c1 == 31 or c0 + c1 == 0 ) :
        return - 1
    return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1
if __name__ == ' _ _ main _ _ ' :
    n = 5
    print ( getNext ( n ) )
    n = 8
    print ( getNext ( n ) )

Accumulator variables: c
Python program:
def totalFlips ( A , B , C , N ) :
    count = 0
    for i in range ( N ) :
        if A [ i ] == B [ i ] and C [ i ] == '1' :
            count = count + 1
        elif A [ i ] != B [ i ] and C [ i ] == '0' :
            count = count + 1
    return count
N = 5
a = "10100"
b = "00010"
c = "10011"
print ( totalFlips ( a , b , c , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def findTwoMissingNumbers ( arr , n ) :
    XOR = arr [ 0 ]
    for i in range ( 1 , n - 2 ) :
        XOR ^= arr [ i ]
    for i in range ( 1 , n + 1 ) :
        XOR ^= i
    set_bit_no = XOR & ~ ( XOR - 1 )
    x = 0
    y = 0
    for i in range ( 0 , n - 2 ) :
        if arr [ i ] & set_bit_no :
            x = x ^ arr [ i ]
        else :
            y = y ^ arr [ i ]
    for i in range ( 1 , n + 1 ) :
        if i & set_bit_no :
            x = x ^ i
        else :
            y = y ^ i
    print ( " Two Missing Numbers are % d % d " % ( x , y ) )
arr = [ 1 , 3 , 5 , 6 ]
n = 2 + len ( arr )
findTwoMissingNumbers ( arr , n )

Index/key variables: i
Accumulator variables: XOR
Iterator variables: i
Python program:
def Divisible ( arr , n ) :
    count_even = 0
    for i in range ( n ) :
        if ( arr [ i ] % 2 == 0 ) :
            count_even += 1
    return count_even + ( n - count_even ) // 2
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len ( arr )
print ( Divisible ( arr , n ) )

Index/key variables: i
Accumulator variables: count_even
Iterator variables: i
Python program:
from random import randint
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . children = 0
        self . left = None
        self . right = None
def getElements ( root ) :
    if root == None :
        return 0
    return ( getElements ( root . left ) + getElements ( root . right ) + 1 )
def insertChildrenCount ( root ) :
    if root == None :
        return
    root . children = getElements ( root ) - 1
    insertChildrenCount ( root . left )
    insertChildrenCount ( root . right )
def children ( root ) :
    if root == None :
        return 0
    return root . children + 1
def randomNodeUtil ( root , count ) :
    if root == None :
        return 0
    if count == children ( root . left ) :
        return root . data
    if count < children ( root . left ) :
        return randomNodeUtil ( root . left , count )
    return randomNodeUtil ( root . right , count - children ( root . left ) - 1 )
def randomNode ( root ) :
    count = randint ( 0 , root . children )
    return randomNodeUtil ( root , count )
if __name__ == " _ _ main _ _ " :
    root = Node ( 10 )
    root . left = Node ( 20 )
    root . right = Node ( 30 )
    root . left . right = Node ( 40 )
    root . left . right = Node ( 50 )
    root . right . left = Node ( 60 )
    root . right . right = Node ( 70 )
    insertChildrenCount ( root )
    print ( " A ▁ Random ▁ Node ▁ From ▁ Tree ▁ : " , randomNode ( root ) )

Class/struct names: Node
Python program:
import random
def rand2 ( ) :
    tmp = random . randint ( 1 , 100 )
    return tmp % 2
def rand3 ( ) :
    r = 2 * rand2 ( ) + rand2 ( )
    if r < 3 :
        return r
    return rand3 ( )
if __name__ == ' _ _ main _ _ ' :
    for i in range ( 100 ) :
        print ( rand3 ( ) , end = " " )

Iterator variables: i
Python program:
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
def deleteLeaves ( root , x ) :
    if ( root == None ) :
        return None
    root . left = deleteLeaves ( root . left , x )
    root . right = deleteLeaves ( root . right , x )
    if ( root . data == x and root . left == None and root . right == None ) :
        return None
    return root
def inorder ( root ) :
    if ( root == None ) :
        return
    inorder ( root . left )
    print ( root . data , end = " ▁ " )
    inorder ( root . right )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 10 )
    root . left = newNode ( 3 )
    root . right = newNode ( 10 )
    root . left . left = newNode ( 3 )
    root . left . right = newNode ( 1 )
    root . right . right = newNode ( 3 )
    root . right . right . left = newNode ( 3 )
    root . right . right . right = newNode ( 3 )
    deleteLeaves ( root , 3 )
    print ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " )
    inorder ( root )

Class/struct names: newNode
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def _deleteTree ( root ) :
    if root is None :
        return
    q = [ ]
    q . append ( root )
    while ( len ( q ) > 0 ) :
        node = q . pop ( 0 )
        if node . left is not None :
            q . append ( node . left )
        if node . right is not None :
            q . append ( node . right )
        node = None
    return node
def deleteTree ( node_ref ) :
    node_ref = _deleteTree ( node_ref )
    return node_ref
root = Node ( 15 )
root . left = Node ( 10 )
root . right = Node ( 20 )
root . left . left = Node ( 8 )
root . left . right = Node ( 12 )
root . right . left = Node ( 16 )
root . right . right = Node ( 25 )
root = deleteTree ( root )

Accumulator variables: q
Class/struct names: Node
Python program:
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
def sizeoftree ( root ) :
    if root == None :
        return 0
    q = [ ]
    q . append ( root )
    count = 1
    while ( len ( q ) != 0 ) :
        root = q . pop ( 0 )
        if ( root . left ) :
            count += 1
            q . append ( root . left )
        if ( root . right ) :
            count += 1
            q . append ( root . right )
    return count
root = newNode ( 1 )
root . left = newNode ( 2 )
root . right = newNode ( 3 )
root . left . left = newNode ( 4 )
root . left . right = newNode ( 5 )
print ( sizeoftree ( root ) )

Accumulator variables: count, q
Class/struct names: newNode
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def maxDepth ( node ) :
    if node is None :
        return 0 ;
    else :
        lDepth = maxDepth ( node . left )
        rDepth = maxDepth ( node . right )
        if ( lDepth > rDepth ) :
            return lDepth + 1
        else :
            return rDepth + 1
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
print ( " Height ▁ of ▁ tree ▁ is ▁ % d " % ( maxDepth ( root ) ) )

Class/struct names: Node
Python program:
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def heightOfTreeUtil ( root , isEven ) :
    if ( not root ) :
        return 0
    if ( not root . left and not root . right ) :
        if ( isEven ) :
            return 1
        else :
            return 0
    left = heightOfTreeUtil ( root . left , not isEven )
    right = heightOfTreeUtil ( root . right , not isEven )
    if ( left == 0 and right == 0 ) :
        return 0
    return ( 1 + max ( left , right ) )
def heightOfTree ( root ) :
    return heightOfTreeUtil ( root , False )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 5 )
    root . left . right . left = newNode ( 6 )
    print ( " Height ▁ of ▁ tree ▁ is " , heightOfTree ( root ) )

Class/struct names: newNode
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def isBalanced ( root ) :
    if root is None :
        return True
    lh = height ( root . left )
    rh = height ( root . right )
    if ( abs ( lh - rh ) <= 1 ) and isBalanced ( root . left ) is True and isBalanced ( root . right ) is True :
        return True
    return False
def height ( root ) :
    if root is None :
        return 0
    return max ( height ( root . left ) , height ( root . right ) ) + 1
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
root . left . left . left = Node ( 8 )
if isBalanced ( root ) :
    print ( " Tree ▁ is ▁ balanced " )
else :
    print ( " Tree ▁ is ▁ not ▁ balanced " )

Class/struct names: Node
Python program:
class newNode :
    def __init__ ( self , key ) :
        self . data = key
        self . left = None
        self . right = None
def isLeaf ( node ) :
    return node . left and node . left . right == node and node . right and node . right . left == node
def maxDepth ( node ) :
    if ( node == None ) :
        return 0
    if ( isLeaf ( node ) ) :
        return 1
    return 1 + max ( maxDepth ( node . left ) , maxDepth ( node . right ) )
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 5 )
    root . left . left . left = newNode ( 6 )
    L1 = root . left . left . left
    L2 = root . left . right
    L3 = root . right
    L1 . right = L2
    L2 . right = L3
    L3 . right = L1
    L3 . left = L2
    L2 . left = L1
    L1 . left = L3
    print ( " Height ▁ of ▁ tree ▁ is ▁ " , maxDepth ( root ) )

Class/struct names: newNode
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def height ( node ) :
    if node is None :
        return 0
    return 1 + max ( height ( node . left ) , height ( node . right ) )
def diameter ( root ) :
    if root is None :
        return 0
    lheight = height ( root . left )
    rheight = height ( root . right )
    ldiameter = diameter ( root . left )
    rdiameter = diameter ( root . right )
    return max ( lheight + rheight + 1 , max ( ldiameter , rdiameter ) )
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
print ( diameter ( root ) )

Class/struct names: Node
Python program:
def findpath ( N , a ) :
    if ( a [ 0 ] ) :
        print ( N + 1 )
        for i in range ( 1 , N + 1 , 1 ) :
            print ( i , end = " ▁ " )
        return
    for i in range ( N - 1 ) :
        if ( a [ i ] == 0 and a [ i + 1 ] ) :
            for j in range ( 1 , i + 1 , 1 ) :
                print ( j , end = " ▁ " )
            print ( N + 1 , end = " ▁ " ) ;
            for j in range ( i + 1 , N + 1 , 1 ) :
                print ( j , end = " ▁ " )
            return
    for i in range ( 1 , N + 1 , 1 ) :
        print ( i , end = " ▁ " )
    print ( N + 1 , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 3
    arr = [ 0 , 1 , 0 ]
    findpath ( N , arr )

Index/key variables: i
Iterator variables: i, j
Python program:
def balBracketSequence ( str ) :
    n = len ( str )
    if ( str [ 0 ] == str [ n - 1 ] ) :
        print ( " No " , end = " " )
    else :
        cntForOpen = 0
        cntForClose = 0
        check = 1
        for i in range ( n ) :
            if ( str [ i ] == str [ 0 ] ) :
                cntForOpen += 1
            elif str [ i ] == str [ n - 1 ] :
                cntForOpen -= 1
            else :
                cntForOpen += 1
            if ( cntForOpen < 0 ) :
                check = 0
                break
        if ( check and cntForOpen == 0 ) :
            print ( " Yes , ▁ " , end = " " )
            for i in range ( n ) :
                if ( str [ i ] == str [ n - 1 ] ) :
                    print ( ' ) ' , end = " " )
                else :
                    print ( ' ( ' , end = " " )
            return
        else :
            for i in range ( n ) :
                if ( str [ i ] == str [ 0 ] ) :
                    cntForClose += 1
                else :
                    cntForClose -= 1
                if ( cntForClose < 0 ) :
                    check = 0
                    break
            if ( check and cntForClose == 0 ) :
                print ( " Yes , ▁ " , end = " " )
                for i in range ( n ) :
                    if ( str [ i ] == str [ 0 ] ) :
                        print ( ' ( ' , end = " " )
                    else :
                        print ( ' ) ' , end = " " )
                return
        print ( " NO " , end = " " )
str = "123122"
balBracketSequence ( str )

Index/key variables: i
Accumulator variables: cntForClose, cntForOpen
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
class Height :
    def __init ( self ) :
        self . h = 0
def diameterOpt ( root , height ) :
    lh = Height ( )
    rh = Height ( )
    if root is None :
        height . h = 0
        return 0
    ldiameter = diameterOpt ( root . left , lh )
    rdiameter = diameterOpt ( root . right , rh )
    height . h = max ( lh . h , rh . h ) + 1
    return max ( lh . h + rh . h + 1 , max ( ldiameter , rdiameter ) )
def diameter ( root ) :
    height = Height ( )
    return diameterOpt ( root , height )
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
print ( diameter ( root ) )

Class/struct names: Height, Node
Python program:
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
def height ( root , ans ) :
    if ( root == None ) :
        return 0
    left_height = height ( root . left , ans )
    right_height = height ( root . right , ans )
    ans [ 0 ] = max ( ans [ 0 ] , 1 + left_height + right_height )
    return 1 + max ( left_height , right_height )
def diameter ( root ) :
    if ( root == None ) :
        return 0
    ans = [ - 999999999999 ]
    height_of_tree = height ( root , ans )
    return ans [ 0 ]
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 5 )
    print ( " Diameter ▁ is " , diameter ( root ) )

Class/struct names: newNode
Python program:
def getPostOrderBST ( pre , N ) :
    pivotPoint = 0
    for i in range ( 1 , N ) :
        if ( pre [ 0 ] <= pre [ i ] ) :
            pivotPoint = i
            break
    for i in range ( pivotPoint - 1 , 0 , - 1 ) :
        print ( pre [ i ] , end = " ▁ " )
    for i in range ( N - 1 , pivotPoint - 1 , - 1 ) :
        print ( pre [ i ] , end = " ▁ " )
    print ( pre [ 0 ] )

Index/key variables: i
Iterator variables: i
Python program:
def countSubstrings ( s ) :
    ans = 0
    subs = 1
    pre = ' '
    for i in s :
        if pre == i :
            subs += 1
        else :
            subs = 1
        ans += subs
        pre = i
    print ( ans )
s = ' geeksforgeeks '
countSubstrings ( s )

Accumulator variables: ans, subs
Iterator variables: i
Python program:
def find_original_array ( A ) :
    mp = [ [ ] for i in range ( 6 ) ]
    res = [ ]
    visited = { }
    for it in A :
        mp [ it [ 0 ] ] . append ( it [ 1 ] )
        mp [ it [ 1 ] ] . append ( it [ 0 ] )
    start = 0
    for it in range ( 6 ) :
        if ( len ( mp [ it ] ) == 1 ) :
            start = it + 3
            break
    adjacent = start
    res . append ( start )
    visited [ start ] = True
    while ( len ( res ) != len ( A ) + 1 ) :
        for elements in mp [ adjacent ] :
            if ( elements not in visited ) :
                res . append ( elements )
                visited [ elements ] = True
                adjacent = elements
    print ( * res )
if __name__ == ' _ _ main _ _ ' :
    A = [ [ 5 , 1 ] , [ 3 , 4 ] , [ 3 , 5 ] ]
    find_original_array ( A )

Index/key variables: adjacent, elements, it, start
Accumulator variables: res
Iterator variables: elements, it
Python program:
def constructTree ( n , d , h ) :
    if d == 1 :
        if n == 2 and h == 1 :
            print ( "1 ▁ 2" )
            return 0
        print ( " - 1" )
        return 0
    if d > 2 * h :
        print ( " - 1" )
        return 0
    for i in range ( 1 , h + 1 ) :
        print ( i , " ▁ " , i + 1 )
    if d > h :
        print ( 1 , " ▁ " , h + 2 )
        for i in range ( h + 2 , d + 1 ) :
            print ( i , " ▁ " , i + 1 )
    for i in range ( d + 1 , n ) :
        k = 1
        if d == h :
            k = 2
        print ( k , " ▁ " , i + 1 )
n = 5
d = 3
h = 2
constructTree ( n , d , h )

Iterator variables: i
Python program:
def uniqueElements ( arr , start , K , mp ) :
    st = set ( ) ;
    for i in range ( K ) :
        st . add ( arr [ start + i ] ) ;
    for itr in st :
        if itr in mp :
            mp [ itr ] += 1 ;
        else :
            mp [ itr ] = 1 ;
def checkAnswer ( mp , N , K ) :
    for i in range ( 1 , N + 1 ) :
        if i in mp :
            if ( mp [ i ] == ( N - K + 1 ) ) :
                print ( i , end = " ▁ " ) ;
                return ;
    print ( - 1 , end = " ▁ " ) ;
def smallestPresentNumber ( arr , N , K ) :
    mp = { } ;
    for i in range ( N - K + 1 ) :
        uniqueElements ( arr , i , K , mp ) ;
    checkAnswer ( mp , N , K ) ;
def generateK ( arr , N ) :
    for k in range ( 1 , N + 1 ) :
        smallestPresentNumber ( arr , N , k ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] ;
    N = len ( arr ) ;
    generateK ( arr , N ) ;

Index/key variables: i, itr
Accumulator variables: st
Iterator variables: i, itr, k
Python program:
class newnode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def getDeepestRightLeafNode ( root ) :
    if ( not root ) :
        return None
    q = [ ]
    q . append ( root )
    result = None
    while ( len ( q ) ) :
        temp = q [ 0 ]
        q . pop ( 0 )
        if ( temp . left ) :
            q . append ( temp . left )
        if ( temp . right ) :
            q . append ( temp . right )
            if ( not temp . right . left and not temp . right . right ) :
                result = temp . right
    return result
if __name__ == ' _ _ main _ _ ' :
    root = newnode ( 1 )
    root . left = newnode ( 2 )
    root . right = newnode ( 3 )
    root . left . right = newnode ( 4 )
    root . right . left = newnode ( 5 )
    root . right . right = newnode ( 6 )
    root . right . left . right = newnode ( 7 )
    root . right . right . right = newnode ( 8 )
    root . right . left . right . left = newnode ( 9 )
    root . right . right . right . right = newnode ( 10 )
    result = getDeepestRightLeafNode ( root )
    if result :
        print ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : " , result . data )
    else :
        print ( " No ▁ result , ▁ right ▁ leaf ▁ not ▁ found " )

Accumulator variables: q
Class/struct names: newnode
Python program:
def checkString ( X , Y ) :
    L = - 1
    R = - 1
    for i in range ( len ( X ) ) :
        if ( X [ i ] != Y [ i ] ) :
            L = i
            break
    for i in range ( len ( X ) - 1 , 0 , - 1 ) :
        if ( X [ i ] != Y [ i ] ) :
            R = i
            break
    X = list ( X )
    X = X [ : L ] + X [ R : L - 1 : - 1 ] + X [ R + 1 : ]
    if ( X == list ( Y ) ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == " _ _ main _ _ " :
    X = " adcbef "
    Y = " abcdef "
    checkString ( X , Y )

Index/key variables: i
Iterator variables: i
Python program:
import math
seg = [ 0 for x in range ( 10000 ) ]
INT_MIN = int ( - 10000000 )
def maxLength ( a , b , n , c ) :
    if ( n == 0 ) :
        return 0
    max_length = int ( 0 )
    low = 0
    high = n
    while ( low <= high ) :
        mid = int ( low + int ( ( high - low ) / 2 ) )
        if ( possible ( a , b , n , c , mid ) != 0 ) :
            max_length = mid
            low = mid + 1
        else :
            high = mid - 1
    return max_length
def possible ( a , b , n , c , k ) :
    sum = int ( 0 )
    for i in range ( 0 , k ) :
        sum += a [ i ]
    total_cost = int ( sum * k + getMax ( b , 0 , n - 1 , 0 , k - 1 , 0 ) )
    if ( total_cost <= c ) :
        return 1
    for i in range ( k , n ) :
        sum += a [ i ]
        sum -= a [ i - k ]
        total_cost = int ( sum * k + getMax ( b , 0 , n - 1 , i - k + 1 , i , 0 ) )
        if ( total_cost <= c ) :
            return 1
    return 0
def build ( b , index , s , e ) :
    if ( s == e ) :
        seg [ index ] = b [ s ]
        return
    mid = int ( s + int ( ( e - s ) / 2 ) )
    build ( b , 2 * index + 1 , s , mid )
    build ( b , 2 * index + 2 , mid + 1 , e )
    seg [ index ] = max ( seg [ 2 * index + 1 ] , seg [ 2 * index + 2 ] )
def getMax ( b , ss , se , qs , qe , index ) :
    if ( se < qs or ss > qe ) :
        return int ( INT_MIN / 2 )
    if ( ss >= qs and se <= qe ) :
        return seg [ index ]
    mid = int ( int ( ss ) + int ( ( se - ss ) / 2 ) )
    return max ( getMax ( b , ss , mid , qs , qe , 2 * index + 1 ) , getMax ( b , mid + 1 , se , qs , qe , 2 * index + 2 ) )
A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ]
B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ]
C = int ( 40 )
N = len ( A )
build ( B , 0 , 0 , N - 1 )
print ( ( maxLength ( A , B , N , C ) ) )

Index/key variables: i, index, s
Accumulator variables: sum
Iterator variables: i
Python program:
def maxLength ( a , b , n , c ) :
    if ( n == 0 ) :
        return 0
    max_length = 0
    low = 0
    high = n
    while ( low <= high ) :
        mid = int ( low + ( high - low ) / 2 )
        if ( possible ( a , b , n , c , mid ) ) :
            max_length = mid
            low = mid + 1
        else :
            high = mid - 1
    return max_length
def possible ( a , b , n , c , k ) :
    dq = [ ]
    Sum = 0
    for i in range ( k ) :
        Sum += a [ i ]
        while ( len ( dq ) > 0 and b [ i ] > b [ dq [ len ( dq ) - 1 ] ] ) :
            dq . pop ( len ( dq ) - 1 )
        dq . append ( i )
    total_cost = Sum * k + b [ dq [ 0 ] ]
    if ( total_cost <= c ) :
        return True
    for i in range ( k , n ) :
        Sum += a [ i ]
        Sum -= a [ i - k ]
        while ( len ( dq ) > 0 and dq [ 0 ] <= i - k ) :
            dq . pop ( 0 )
        while ( len ( dq ) > 0 and b [ i ] > b [ dq [ len ( dq ) - 1 ] ] ) :
            dq . pop ( len ( dq ) - 1 )
        dq . append ( i )
        total_cost = Sum * k + b [ dq [ 0 ] ]
        if ( total_cost <= c ) :
            return True
    return False
A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ]
B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ]
N = len ( A )
C = 40
print ( maxLength ( A , B , N , C ) )

Index/key variables: i
Accumulator variables: Sum, dq
Iterator variables: i