dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def rearrangeArray ( A , B , N , K ) :
    B . sort ( reverse = True )
    flag = True
    for i in range ( N ) :
        if ( A [ i ] + B [ i ] > K ) :
            flag = False
            break
    if ( flag == False ) :
        print ( " - 1" )
    else :
        for i in range ( N ) :
            print ( B [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 2 , 3 , 4 , 2 ]
    B = [ 1 , 2 , 3 , 1 , 1 ]
    N = len ( A )
    K = 5 ;
    rearrangeArray ( A , B , N , K )

Index/key variables: i
Iterator variables: i
Boolean variables: flag
Python program:
def countRows ( mat ) :
    n = len ( mat )
    m = len ( mat [ 0 ] )
    count = 0
    totalSum = 0
    for i in range ( n ) :
        for j in range ( m ) :
            totalSum += mat [ i ] [ j ]
    for i in range ( n ) :
        currSum = 0
        for j in range ( m ) :
            currSum += mat [ i ] [ j ]
        if ( currSum > totalSum - currSum ) :
            count += 1
    print ( count )
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 2 , - 1 , 5 ] , [ - 3 , 0 , - 2 ] , [ 5 , 1 , 2 ] ]
    countRows ( mat )

Index/key variables: i, j
Accumulator variables: count, currSum, totalSum
Iterator variables: i, j
Python program:
def areElementsContiguous ( arr , n ) :
    arr . sort ( )
    for i in range ( 1 , n ) :
        if ( arr [ i ] - arr [ i - 1 ] > 1 ) :
            return 0
    return 1
arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ]
n = len ( arr )
if areElementsContiguous ( arr , n ) : print ( " Yes " )
else : print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def areElementsContiguous ( arr , n ) :
    max1 = max ( arr )
    min1 = min ( arr )
    m = max1 - min1 + 1
    if ( m > n ) :
        return False
    visited = [ 0 ] * m
    for i in range ( 0 , n ) :
        visited [ arr [ i ] - min1 ] = True
    for i in range ( 0 , m ) :
        if ( visited [ i ] == False ) :
            return False
    return True
arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ]
n = len ( arr )
if ( areElementsContiguous ( arr , n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def areElementsContiguous ( arr ) :
    us = set ( )
    for i in arr : us . add ( i )
    count = 1
    curr_ele = arr [ 0 ] - 1
    while curr_ele in us :
        count += 1
        curr_ele -= 1
    curr_ele = arr [ 0 ] + 1
    while curr_ele in us :
        count += 1
        curr_ele += 1
    return ( count == len ( us ) )
arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ]
if areElementsContiguous ( arr ) : print ( " Yes " )
else : print ( " No " )

Accumulator variables: count, curr_ele, us
Iterator variables: i
Python program:
import collections
def longest ( a , n , k ) :
    freq = collections . defaultdict ( int )
    start = 0
    end = 0
    now = 0
    l = 0
    for i in range ( n ) :
        freq [ a [ i ] ] += 1
        if ( freq [ a [ i ] ] == 1 ) :
            now += 1
        while ( now > k ) :
            freq [ a [ l ] ] -= 1
            if ( freq [ a [ l ] ] == 0 ) :
                now -= 1
            l += 1
        if ( i - l + 1 >= end - start + 1 ) :
            end = i
            start = l
    for i in range ( start , end + 1 ) :
        print ( a [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    a = [ 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 ]
    n = len ( a )
    k = 3
    longest ( a , n , k )

Index/key variables: i, l
Accumulator variables: now
Iterator variables: i
Python program:
def kOverlap ( pairs : list , k ) :
    vec = list ( )
    for i in range ( len ( pairs ) ) :
        vec . append ( ( pairs [ 0 ] , - 1 ) )
        vec . append ( ( pairs [ 1 ] , 1 ) )
    vec . sort ( key = lambda a : a [ 0 ] )
    st = list ( )
    for i in range ( len ( vec ) ) :
        cur = vec [ i ]
        if cur [ 1 ] == - 1 :
            st . append ( cur )
        else :
            st . pop ( )
        if len ( st ) >= k :
            return True
    return False
if __name__ == " _ _ main _ _ " :
    pairs = list ( )
    pairs . append ( ( 1 , 3 ) )
    pairs . append ( ( 2 , 4 ) )
    pairs . append ( ( 3 , 5 ) )
    pairs . append ( ( 7 , 10 ) )
    n = len ( pairs )
    k = 3
    if kOverlap ( pairs , k ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: st, vec
Iterator variables: i
Python program:
N = 5
ptr = [ 0 for i in range ( 501 ) ]
def findSmallestRange ( arr , n , k ) :
    i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
    for i in range ( k + 1 ) :
        ptr [ i ] = 0
    minrange = 10 ** 9
    while ( 1 ) :
        minind = - 1
        minval = 10 ** 9
        maxval = - 10 ** 9
        flag = 0
        for i in range ( k ) :
            if ( ptr [ i ] == n ) :
                flag = 1
                break
            if ( ptr [ i ] < n and arr [ i ] [ ptr [ i ] ] < minval ) :
                minind = i
                minval = arr [ i ] [ ptr [ i ] ]
            if ( ptr [ i ] < n and arr [ i ] [ ptr [ i ] ] > maxval ) :
                maxval = arr [ i ] [ ptr [ i ] ]
        if ( flag ) :
            break
        ptr [ minind ] += 1
        if ( ( maxval - minval ) < minrange ) :
            minel = minval
            maxel = maxval
            minrange = maxel - minel
    print ( " The ▁ smallest ▁ range ▁ is ▁ [ " , minel , maxel , " ] " )
arr = [ [ 4 , 7 , 9 , 12 , 15 ] , [ 0 , 8 , 10 , 14 , 20 ] , [ 6 , 12 , 16 , 30 , 50 ] ]
k = len ( arr )
findSmallestRange ( arr , N , k )

Index/key variables: i, minind
Iterator variables: i
Python program:
def findLargestd ( S , n ) :
    found = False
    S . sort ( )
    for i in range ( n - 1 , - 1 , - 1 ) :
        for j in range ( 0 , n ) :
            if ( i == j ) :
                continue
            for k in range ( j + 1 , n ) :
                if ( i == k ) :
                    continue
                for l in range ( k + 1 , n ) :
                    if ( i == l ) :
                        continue
                    if ( S [ i ] == S [ j ] + S [ k ] + S [ l ] ) :
                        found = True
                        return S [ i ]
    if ( found == False ) :
        return - 1
S = [ 2 , 3 , 5 , 7 , 12 ]
n = len ( S )
ans = findLargestd ( S , n )
if ( ans == - 1 ) :
    print ( " No ▁ Solution " )
else :
    print ( " Largest ▁ d ▁ such ▁ that ▁ a ▁ + ▁ b ▁ + " , " c ▁ = ▁ d ▁ is " , ans )

Index/key variables: i, j, k, l
Iterator variables: i, j, k, l
Boolean variables: found
Python program:
def findFourElements ( arr , n ) :
    mp = dict ( )
    for i in range ( n - 1 ) :
        for j in range ( i + 1 , n ) :
            mp [ arr [ i ] + arr [ j ] ] = ( i , j )
    d = - 10 ** 9
    for i in range ( n - 1 ) :
        for j in range ( i + 1 , n ) :
            abs_diff = abs ( arr [ i ] - arr [ j ] )
            if abs_diff in mp . keys ( ) :
                p = mp [ abs_diff ]
                if ( p [ 0 ] != i and p [ 0 ] != j and p [ 1 ] != i and p [ 1 ] != j ) :
                    d = max ( d , max ( arr [ i ] , arr [ j ] ) )
    return d
arr = [ 2 , 3 , 5 , 7 , 12 ]
n = len ( arr )
res = findFourElements ( arr , n )
if ( res == - 10 ** 9 ) :
    print ( " No ▁ Solution . " )
else :
    print ( res )

Index/key variables: abs_diff, i, j
Iterator variables: i, j
Python program:
def CountMaximum ( arr , n , k ) :
    arr . sort ( )
    Sum , count = 0 , 0
    for i in range ( 0 , n ) :
        Sum += arr [ i ]
        if ( Sum > k ) :
            break
        count += 1
    return count
arr = [ 30 , 30 , 10 , 10 ]
n = len ( arr )
k = 50
print ( CountMaximum ( arr , n , k ) )

Index/key variables: i
Accumulator variables: Sum, count
Iterator variables: i
Python program:
def leftRotatebyOne ( arr , n ) :
    temp = arr [ 0 ]
    for i in range ( n - 1 ) :
        arr [ i ] = arr [ i + 1 ]
    arr [ n - 1 ] = temp
def leftRotate ( arr , d , n ) :
    for i in range ( d ) :
        leftRotatebyOne ( arr , n )
def printArray ( arr , size ) :
    for i in range ( size ) :
        print ( " % ▁ d " % arr [ i ] , end = " ▁ " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
leftRotate ( arr , 2 , 7 )
printArray ( arr , 7 )

Index/key variables: i
Iterator variables: i
Python program:
def partSort ( arr , N , a , b ) :
    l = min ( a , b )
    r = max ( a , b )
    temp = [ 0 for i in range ( r - l + 1 ) ]
    j = 0
    for i in range ( l , r + 1 , 1 ) :
        temp [ j ] = arr [ i ]
        j += 1
    temp . sort ( reverse = False )
    j = 0
    for i in range ( l , r + 1 , 1 ) :
        arr [ i ] = temp [ j ]
        j += 1
    for i in range ( 0 , N , 1 ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 8 , 4 , 5 , 2 ]
    a = 1
    b = 4
    N = len ( arr )
    partSort ( arr , N , a , b )

Index/key variables: i, j
Iterator variables: i
Python program:
MAX_SIZE = 10
def sortByRow ( mat , n , descending ) :
    for i in range ( n ) :
        if ( descending == True ) :
            mat [ i ] . sort ( reverse = True )
        else :
            mat [ i ] . sort ( )
def transpose ( mat , n ) :
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            mat [ i ] [ j ] , mat [ j ] [ i ] = mat [ j ] [ i ] , mat [ i ] [ j ]
def sortMatRowAndColWise ( mat , n ) :
    sortByRow ( mat , n , True )
    transpose ( mat , n )
    sortByRow ( mat , n , False )
    transpose ( mat , n ) ;
def printMat ( mat , n ) :
    for i in range ( n ) :
        for j in range ( n ) :
            print ( mat [ i ] [ j ] , end = " ▁ " )
        print ( )
if __name__ == " _ _ main _ _ " :
    n = 3
    mat = [ [ 3 , 2 , 1 ] , [ 9 , 8 , 7 ] , [ 6 , 5 , 4 ] ]
    print ( " Original ▁ Matrix : ▁ " )
    printMat ( mat , n )
    sortMatRowAndColWise ( mat , n )
    print ( " Matrix ▁ After ▁ Sorting : " )
    printMat ( mat , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def pushZerosToEnd ( arr , n ) :
    count = 0
    for i in range ( n ) :
        if arr [ i ] != 0 :
            arr [ count ] = arr [ i ]
            count += 1
    while count < n :
        arr [ count ] = 0
        count += 1
arr = [ 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ]
n = len ( arr )
pushZerosToEnd ( arr , n )
print ( " Array ▁ after ▁ pushing ▁ all ▁ zeros ▁ to ▁ end ▁ of ▁ array : " )
print ( arr )

Index/key variables: count, i
Iterator variables: i
Python program:
def pushZerosToEnd ( arr , n ) :
    count = 0
    for i in range ( 0 , n ) :
        if arr [ i ] != 0 :
            arr [ count ] = arr [ i ]
            count += 1
    while ( count < n ) :
        arr [ count ] = 0
        count += 1
def modifyAndRearrangeArr ( ar , n ) :
    if n == 1 :
        return
    for i in range ( 0 , n - 1 ) :
        if ( arr [ i ] != 0 ) and ( arr [ i ] == arr [ i + 1 ] ) :
            arr [ i ] = 2 * arr [ i ]
            arr [ i + 1 ] = 0
            i += 1
    pushZerosToEnd ( arr , n )
def printArray ( arr , n ) :
    for i in range ( 0 , n ) :
        print ( arr [ i ] , end = " ▁ " )
arr = [ 0 , 2 , 2 , 2 , 0 , 6 , 6 , 0 , 0 , 8 ]
n = len ( arr )
print ( " Original ▁ array : " , end = " ▁ " )
printArray ( arr , n )
modifyAndRearrangeArr ( arr , n )
print ( " Modified array : " , end = "   " )
printArray ( arr , n )

Index/key variables: count, i
Iterator variables: i
Python program:
def shiftAllZeroToLeft ( arr , n ) :
    lastSeenNonZero = 0
    for index in range ( 0 , n ) :
        if ( array [ index ] != 0 ) :
            array [ index ] , array [ lastSeenNonZero ] = array [ lastSeenNonZero ] , array [ index ]
            lastSeenNonZero += 1

Index/key variables: index, lastSeenNonZero
Iterator variables: index
Python program:
def printArray ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )
    print ( )
def RearrangePosNeg ( arr , n ) :
    for i in range ( 1 , n ) :
        key = arr [ i ]
        if ( key > 0 ) :
            continue
        j = i - 1
        while ( j >= 0 and arr [ j ] > 0 ) :
            arr [ j + 1 ] = arr [ j ]
            j = j - 1
        arr [ j + 1 ] = key
if __name__ == " _ _ main _ _ " :
    arr = [ - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 ]
    n = len ( arr )
    RearrangePosNeg ( arr , n )
    printArray ( arr , n )

Index/key variables: i, j
Iterator variables: i
Python program:
def printArray ( A , size ) :
    for i in range ( 0 , size ) :
        print ( A [ i ] , end = " ▁ " )
    print ( )
def reverse ( arr , l , r ) :
    if l < r :
        arr [ l ] , arr [ r ] = arr [ r ] , arr [ l ]
        l , r = l + 1 , r - 1
        reverse ( arr , l , r )
def merge ( arr , l , m , r ) :
    i = l
    j = m + 1
    while i <= m and arr [ i ] < 0 :
        i += 1
    while j <= r and arr [ j ] < 0 :
        j += 1
    reverse ( arr , i , m )
    reverse ( arr , m + 1 , j - 1 )
    reverse ( arr , i , j - 1 )
def RearrangePosNeg ( arr , l , r ) :
    if l < r :
        m = l + ( r - l ) // 2
        RearrangePosNeg ( arr , l , m )
        RearrangePosNeg ( arr , m + 1 , r )
        merge ( arr , l , m , r )
if __name__ == " _ _ main _ _ " :
    arr = [ - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 ]
    arr_size = len ( arr )
    RearrangePosNeg ( arr , 0 , arr_size - 1 )
    printArray ( arr , arr_size )

Index/key variables: i, j, l, r
Iterator variables: i
Python program:
def RearrangePosNeg ( arr , n ) :
    i = 0
    j = n - 1
    while ( True ) :
        while ( arr [ i ] < 0 and i < n ) :
            i += 1
        while ( arr [ j ] > 0 and j >= 0 ) :
            j -= 1
        if ( i < j ) :
            arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
        else :
            break
arr = [ - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 ]
n = len ( arr )
RearrangePosNeg ( arr , n )
print ( * arr )

Index/key variables: i, j
Python program:
import math
sz = 20
sqr = int ( math . sqrt ( sz ) ) + 1
def precomputeExpressionForAllVal ( arr , N , dp ) :
    for i in range ( N - 1 , - 1 , - 1 ) :
        for j in range ( 1 , int ( math . sqrt ( N ) ) + 1 ) :
            if ( i + j < N ) :
                dp [ i ] [ j ] = arr [ i ] + dp [ i + j ] [ j ]
            else :
                dp [ i ] [ j ] = arr [ i ]
def querySum ( arr , N , Q , M ) :
    dp = [ [ 0 for x in range ( sz ) ] for x in range ( sqr ) ]
    precomputeExpressionForAllVal ( arr , N , dp )
    for i in range ( 0 , M ) :
        x = Q [ i ] [ 0 ]
        y = Q [ i ] [ 1 ]
        if ( y <= math . sqrt ( N ) ) :
            print ( dp [ x ] [ y ] )
            continue
        sum = 0
        while ( x < N ) :
            sum += arr [ x ]
            x += y
        print ( sum )
arr = [ 1 , 2 , 7 , 5 , 4 ]
Q = [ [ 2 , 1 ] , [ 3 , 2 ] ]
N = len ( arr )
M = len ( Q [ 0 ] )
querySum ( arr , N , Q , M )

Index/key variables: i, j, x, y
Accumulator variables: sum
Iterator variables: i, j
Python program:
def findElements ( arr , n ) :
    for i in range ( n ) :
        count = 0
        for j in range ( 0 , n ) :
            if arr [ j ] > arr [ i ] :
                count = count + 1
        if count >= 2 :
            print ( arr [ i ] , end = " ▁ " )
arr = [ 2 , - 6 , 3 , 5 , 1 ]
n = len ( arr )
findElements ( arr , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findElements ( arr , n ) :
    arr . sort ( )
    for i in range ( 0 , n - 2 ) :
        print ( arr [ i ] , end = " ▁ " )
arr = [ 2 , - 6 , 3 , 5 , 1 ]
n = len ( arr )
findElements ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
import sys
def findElements ( arr , n ) :
    first = - sys . maxsize
    second = - sys . maxsize
    for i in range ( 0 , n ) :
        if ( arr [ i ] > first ) :
            second = first
            first = arr [ i ]
        elif ( arr [ i ] > second ) :
            second = arr [ i ]
    for i in range ( 0 , n ) :
        if ( arr [ i ] < second ) :
            print ( arr [ i ] , end = " ▁ " )
arr = [ 2 , - 6 , 3 , 5 , 1 ]
n = len ( arr )
findElements ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
def getMinOps ( arr ) :
    res = 0
    for i in range ( len ( arr ) - 1 ) :
        res += max ( arr [ i + 1 ] - arr [ i ] , 0 )
    return res
arr = [ 1 , 3 , 4 , 1 , 2 ]
print ( getMinOps ( arr ) )

Index/key variables: i
Accumulator variables: res
Iterator variables: i
Python program:
def findFirstMissing ( array , start , end ) :
    if ( start > end ) :
        return end + 1
    if ( start != array [ start ] ) :
        return start ;
    mid = int ( ( start + end ) / 2 )
    if ( array [ mid ] == mid ) :
        return findFirstMissing ( array , mid + 1 , end )
    return findFirstMissing ( array , start , mid )
arr = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 10 ]
n = len ( arr )
print ( " Smallest ▁ missing ▁ element ▁ is " , findFirstMissing ( arr , 0 , n - 1 ) )

Index/key variables: mid, start
Python program:
def findFirstMissing ( arr , start , end , first ) :
    if ( start < end ) :
        mid = int ( ( start + end ) / 2 )
        if ( arr [ mid ] != mid + first ) :
            return findFirstMissing ( arr , start , mid , first )
        else :
            return findFirstMissing ( arr , mid + 1 , end , first )
    return start + first
def findSmallestMissinginSortedArray ( arr ) :
    if ( arr [ 0 ] != 0 ) :
        return 0
    if ( arr [ - 1 ] == len ( arr ) - 1 ) :
        return len ( arr )
    first = arr [ 0 ]
    return findFirstMissing ( arr , 0 , len ( arr ) - 1 , first )
arr = [ 0 , 1 , 2 , 3 , 4 , 5 , 7 ]
n = len ( arr )
print ( " First ▁ Missing ▁ element ▁ is ▁ : " , findSmallestMissinginSortedArray ( arr ) )

Index/key variables: mid
Python program:
def countChanges ( matrix , n , m ) :
    dist = n + m - 1
    freq = [ [ 0 ] * 10 for i in range ( dist ) ]
    for i in range ( n ) :
        for j in range ( m ) :
            freq [ i + j ] [ matrix [ i ] [ j ] ] += 1
    min_changes_sum = 0
    for i in range ( dist // 2 ) :
        maximum = 0
        total_values = 0
        for j in range ( 10 ) :
            maximum = max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] )
            total_values += ( freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] )
        min_changes_sum += ( total_values - maximum )
    return min_changes_sum
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 2 ] , [ 3 , 5 ] ]
    print ( countChanges ( mat , 2 , 2 ) )

Index/key variables: i, j
Accumulator variables: min_changes_sum, total_values
Iterator variables: i, j
Python program:
import math
def buildSparseTable ( arr , n ) :
    for i in range ( 0 , n ) :
        lookup [ i ] [ 0 ] = arr [ i ]
    j = 1
    while ( 1 << j ) <= n :
        i = 0
        while ( i + ( 1 << j ) - 1 ) < n :
            if ( lookup [ i ] [ j - 1 ] < lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) :
                lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ]
            else :
                lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ]
            i += 1
        j += 1
def query ( L , R ) :
    j = int ( math . log2 ( R - L + 1 ) )
    if lookup [ L ] [ j ] <= lookup [ R - ( 1 << j ) + 1 ] [ j ] :
        return lookup [ L ] [ j ]
    else :
        return lookup [ R - ( 1 << j ) + 1 ] [ j ]
if __name__ == " _ _ main _ _ " :
    a = [ 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 ]
    n = len ( a )
    MAX = 500
    lookup = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
    buildSparseTable ( a , n )
    print ( query ( 0 , 4 ) )
    print ( query ( 4 , 7 ) )
    print ( query ( 7 , 8 ) )

Index/key variables: L, i, j
Iterator variables: i
Python program:
import math
def buildSparseTable ( arr , n ) :
    for i in range ( 0 , n ) :
        table [ i ] [ 0 ] = arr [ i ]
    j = 1
    while ( 1 << j ) <= n :
        i = 0
        while i <= n - ( 1 << j ) :
            table [ i ] [ j ] = math . gcd ( table [ i ] [ j - 1 ] , table [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] )
            i += 1
        j += 1
def query ( L , R ) :
    j = int ( math . log2 ( R - L + 1 ) )
    return math . gcd ( table [ L ] [ j ] , table [ R - ( 1 << j ) + 1 ] [ j ] )
if __name__ == " _ _ main _ _ " :
    a = [ 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 ]
    n = len ( a )
    MAX = 500
    table = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
    buildSparseTable ( a , n )
    print ( query ( 0 , 2 ) )
    print ( query ( 1 , 3 ) )
    print ( query ( 4 , 5 ) )

Index/key variables: L, i, j
Iterator variables: i
Python program:
def findMaxAverage ( arr , n , k ) :
    if k > n :
        return - 1
    csum = [ 0 ] * n
    csum [ 0 ] = arr [ 0 ]
    for i in range ( 1 , n ) :
        csum [ i ] = csum [ i - 1 ] + arr [ i ] ;
    max_sum = csum [ k - 1 ]
    max_end = k - 1
    for i in range ( k , n ) :
        curr_sum = csum [ i ] - csum [ i - k ]
        if curr_sum > max_sum :
            max_sum = curr_sum
            max_end = i
    return max_end - k + 1
arr = [ 1 , 12 , - 5 , - 6 , 50 , 3 ]
k = 4
n = len ( arr )
print ( " The ▁ maximum ▁ average ▁ subarray ▁ of ▁ length " , k , " begins ▁ at ▁ index " , findMaxAverage ( arr , n , k ) )

Index/key variables: i
Iterator variables: i
Python program:
def findMaxAverage ( arr , n , k ) :
    if ( k > n ) :
        return - 1
    sum = arr [ 0 ]
    for i in range ( 1 , k ) :
        sum += arr [ i ]
    max_sum = sum
    max_end = k - 1
    for i in range ( k , n ) :
        sum = sum + arr [ i ] - arr [ i - k ]
        if ( sum > max_sum ) :
            max_sum = sum
            max_end = i
    return max_end - k + 1
arr = [ 1 , 12 , - 5 , - 6 , 50 , 3 ]
k = 4
n = len ( arr )
print ( " The ▁ maximum ▁ average ▁ subarray ▁ of ▁ length " , k , " begins ▁ at ▁ index " , findMaxAverage ( arr , n , k ) )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
m = dict ( )
def findMinimum ( a , n , pos , myturn ) :
    if ( pos , myturn ) in m :
        return m [ ( pos , myturn ) ] ;
    if ( pos >= n - 1 ) :
        return 0 ;
    if ( not myturn ) :
        ans = min ( findMinimum ( a , n , pos + 1 , not myturn ) + a [ pos ] , findMinimum ( a , n , pos + 2 , not myturn ) + a [ pos ] + a [ pos + 1 ] ) ;
        m [ ( pos , myturn ) ] = ans ;
        return ans ;
    if ( myturn ) :
        ans = min ( findMinimum ( a , n , pos + 1 , not myturn ) , findMinimum ( a , n , pos + 2 , not myturn ) ) ;
        m [ ( pos , myturn ) ] = ans ;
        return ans ;
    return 0 ;
def countPenality ( arr , N ) :
    pos = 0 ;
    turn = False ;
    return findMinimum ( arr , N , pos , turn ) + 1 ;
def printAnswer ( arr , N ) :
    a = countPenality ( arr , N ) ;
    sum = 0 ;
    for i in range ( N ) :
        sum += arr [ i ] ;
    print ( a )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 0 , 1 , 1 , 0 , 1 , 1 , 1 ]
    N = len ( arr )
    printAnswer ( arr , N ) ;

Index/key variables: i, pos
Accumulator variables: sum
Iterator variables: i
Boolean variables: turn