dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def minSubarray ( arr , N ) :
    result = [ 0 ] * N
    l = [ ]
    r = [ ]
    for i in range ( N ) :
        count = 1 ;
        while ( len ( l ) != 0 and l [ - 1 ] [ 0 ] > arr [ i ] ) :
            count += l [ - 1 ] [ 1 ]
            l . pop ( )
        l . append ( [ arr [ i ] , count ] )
        result [ i ] = count ;
    for i in range ( N - 1 , - 1 , - 1 ) :
        count = 1 ;
        while ( len ( r ) != 0 and r [ - 1 ] [ 0 ] >= arr [ i ] ) :
            count += r [ - 1 ] [ 1 ]
            r . pop ( ) ;
        r . append ( [ arr [ i ] , count ] ) ;
        result [ i ] *= count
    for i in range ( N ) :
        print ( result [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 2 , 1 , 4 ]
    N = len ( arr )
    minSubarray ( arr , N )

Index/key variables: i
Accumulator variables: count, l, r
Iterator variables: i
Python program:
def isPrime ( num ) :
    if ( num <= 1 ) :
        return False
    for i in range ( 2 , num + 1 ) :
        if i * i > num :
            break
        if ( num % i == 0 ) :
            return False
    return True
def isFulPrime ( n ) :
    if ( not isPrime ( n ) ) :
        return False
    else :
        while ( n > 0 ) :
            rem = n % 10
            if ( not ( rem == 2 or rem == 3 or rem == 5 or rem == 7 ) ) :
                return False
            n = n // 10
    return True
def countFulPrime ( L , R ) :
    cnt = 0
    for i in range ( L , R + 1 ) :
        if ( ( i % 2 ) != 0 and isFulPrime ( i ) ) :
            cnt += 1
    return cnt
if __name__ == ' _ _ main _ _ ' :
    L = 1
    R = 100
    ans = 0
    if ( L < 3 ) :
        ans += 1
    print ( ans + countFulPrime ( L , R ) )

Accumulator variables: cnt
Iterator variables: i
Python program:
def printAlter ( arr , N ) :
    for currIndex in range ( 0 , N ) :
        if ( currIndex % 2 == 0 ) :
            print ( arr [ currIndex ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    N = len ( arr )
    printAlter ( arr , N )

Index/key variables: currIndex
Iterator variables: currIndex
Python program:
def findmin ( s ) :
    n = len ( s )
    maximum = 0
    incr = [ 0 ] * ( n + 1 )
    for i in range ( 0 , n ) :
        incr [ i + 1 ] = incr [ i ]
        if ( s [ i ] == '0' ) :
            incr [ i + 1 ] = incr [ i + 1 ] + 1
    for i in range ( 0 , n + 1 ) :
        for j in range ( i + 1 , n ) :
            maximum = max ( maximum , incr [ i ] + j - i + 1 - ( incr [ j + 1 ] - incr [ i ] ) + incr [ n ] - incr [ j + 1 ] )
    return n - maximum
if __name__ == " _ _ main _ _ " :
    S = "0110100110"
    minimum = findmin ( S )
    print ( minimum )

Index/key variables: i, n
Iterator variables: i, j
Python program:
def isPower ( x ) :
    if ( x != 0 and ( x & ( x - 1 ) ) == 0 ) :
        return True ;
    return False ;
def maximumlength ( arr , N ) :
    max_length = 0 ;
    max_len_subarray = 0 ;
    for i in range ( N ) :
        if ( isPower ( arr [ i ] ) ) :
            max_length += 1 ;
            max_len_subarray = max ( max_length , max_len_subarray ) ;
        else :
            max_length = 0 ;
    print ( max_len_subarray ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 5 , 4 , 6 , 8 , 8 , 2 ] ;
    N = len ( arr ) ;
    maximumlength ( arr , N ) ;

Index/key variables: i
Accumulator variables: max_length
Iterator variables: i
Python program:
import sys
def findLengthOfShortestSubarray ( arr ) :
    minlength = sys . maxsize
    left = 0
    right = len ( arr ) - 1
    while left < right and arr [ left + 1 ] >= arr [ left ] :
        left += 1
    if left == len ( arr ) - 1 :
        return 0
    while right > left and arr [ right - 1 ] <= arr [ right ] :
        right -= 1
    minlength = min ( len ( arr ) - left - 1 , right )
    j = right
    for i in range ( left + 1 ) :
        if arr [ i ] <= arr [ j ] :
            minlength = min ( minlength , j - i - 1 )
        elif j < len ( arr ) - 1 :
            j += 1
        else :
            break
    return minlength
arr = [ 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 ]
print ( findLengthOfShortestSubarray ( arr ) )

Index/key variables: i, j, left, right
Iterator variables: i
Python program:
def minSub ( N ) :
    count = 0
    while ( N >= 0 ) :
        num = N
        rev = 0
        while ( num != 0 ) :
            digit = num % 10
            rev = ( rev * 10 ) + digit
            num = num // 10
        if ( N == rev ) :
            break
        count += 1
        N -= 1
    print ( count )
if __name__ == ' _ _ main _ _ ' :
    N = 3456
    minSub ( N )

Accumulator variables: N, count
Python program:
def numberOfGP ( L , N ) :
    if ( N <= 2 ) :
        return 0
    count = 0
    res = 0
    for i in range ( 2 , N ) :
        if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) :
            count += 1
        else :
            count = 0
        res += count
    return res
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 4 , 8 , 16 , 24 ]
    N = len ( arr )
    print ( numberOfGP ( arr , N ) )

Index/key variables: i
Accumulator variables: count, res
Iterator variables: i
Python program:
def cntSubarr ( arr , N ) :
    res = 0
    prefixXor = 0
    for i in range ( N - 1 ) :
        prefixXor = arr [ i ]
        for j in range ( i + 1 , N ) :
            prefixXor ^= arr [ j ]
            if ( prefixXor == 0 and ( j - i + 1 ) % 2 == 0 ) :
                res += 1
    return res
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ]
    N = len ( arr )
    print ( cntSubarr ( arr , N ) )

Index/key variables: i, j
Accumulator variables: prefixXor, res
Iterator variables: i, j
Python program:
M = 1000000 ;
def cntSubXor ( arr , N ) :
    prefixXor = 0 ;
    Even = [ 0 ] * M ;
    Odd = [ 0 ] * M ;
    cntSub = 0 ;
    Odd [ 0 ] = 1 ;
    for i in range ( 0 , N ) :
        prefixXor ^= arr [ i ] ;
        if ( i % 2 == 1 ) :
            cntSub += Odd [ prefixXor ] ;
            Odd [ prefixXor ] += 1 ;
        else :
            cntSub += Even [ prefixXor ] ;
            Even [ prefixXor ] += 1 ;
    return cntSub ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ] ;
    N = len ( arr ) ;
    print ( cntSubXor ( arr , N ) ) ;

Index/key variables: i, prefixXor
Accumulator variables: cntSub
Iterator variables: i
Python program:
def findMaxSum ( arr , n , min , k , S ) :
    A = [ ]
    for i in range ( n ) :
        A . append ( ( arr [ i ] , min [ i ] ) )
    A = sorted ( A )
    A = A [ : : - 1 ]
    K = 0
    for i in range ( n ) :
        S += max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] )
        K += k
    return S
if __name__ == ' _ _ main _ _ ' :
    arr , min = [ ] , [ ]
    arr = [ 3 , 5 , 2 , 1 ]
    min = [ 3 , 2 , 1 , 3 ]
    N = len ( arr )
    K = 3
    S = 0
    S = findMaxSum ( arr , N , min , K , S )
    print ( S )

Index/key variables: i
Accumulator variables: A, K, S
Iterator variables: i
Python program:
import sys
def minLength ( a , b ) :
    Char = { }
    for i in range ( len ( b ) ) :
        Char [ b [ i ] ] = Char . get ( b [ i ] , 0 ) + 1
    CharacterIndex = { }
    for i in range ( len ( a ) ) :
        x = a [ i ]
        if ( x in Char ) :
            CharacterIndex [ x ] = CharacterIndex . get ( x , [ ] )
            CharacterIndex [ x ] . append ( i )
    l = sys . maxsize
    while ( True ) :
        flag = 1
        firstVar = 0
        lastVar = 0
        for i in range ( len ( b ) ) :
            if ( i == 0 ) :
                if ( b [ i ] not in CharacterIndex ) :
                    flag = 0
                    break
                else :
                    x = CharacterIndex [ b [ i ] ] [ 0 ]
                    CharacterIndex [ b [ i ] ] . remove ( CharacterIndex [ b [ i ] ] [ 0 ] )
                    firstVar = x
                    lastVar = x
            else :
                elementFound = 0
                for e in CharacterIndex [ b [ i ] ] :
                    if ( e > lastVar ) :
                        elementFound = 1
                        lastVar = e
                        break
                if ( elementFound == 0 ) :
                    flag = 0
                    break
        if ( flag == 0 ) :
            break
        l = min ( l , abs ( lastVar - firstVar ) + 1 )
    return l
if __name__ == ' _ _ main _ _ ' :
    a = " abcdefababaef "
    b = " abf "
    l = minLength ( a , b )
    if ( l != sys . maxsize ) :
        print ( l )
    else :
        print ( " Impossible " )

Index/key variables: i, x
Iterator variables: e, i
Python program:
def is_prime ( n ) :
    if ( n <= 1 ) :
        return 0
    for i in range ( 2 , n + 1 ) :
        if i * i > n :
            break
        if ( n % i == 0 ) :
            return 0
    return 1
def countSmallerPrimes ( ar , N ) :
    for i in range ( N ) :
        count = 0
        for j in range ( i + 1 , N ) :
            if ( ar [ j ] <= ar [ i ] and is_prime ( ar [ j ] ) ) :
                count += 1
        print ( count , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    ar = [ 43 , 3 , 5 , 7 , 2 , 41 ]
    N = len ( ar )
    countSmallerPrimes ( ar , N )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def checkConcat ( str1 , str2 ) :
    N = len ( str1 )
    M = len ( str2 )
    if ( N % M != 0 ) :
        return False
    for i in range ( N ) :
        if ( str1 [ i ] != str2 [ i % M ] ) :
            return False
    return True
if __name__ == ' _ _ main _ _ ' :
    str1 = " abcabcabc "
    str2 = " abc "
    if ( checkConcat ( str1 , str2 ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def MinLen ( str , K ) :
    N = len ( str )
    i = 0
    while ( i < N - 1 ) :
        if ( str [ i ] != str [ i + 1 ] ) :
            break
        i += 1
    if ( i == N - 1 ) :
        return N
    return max ( 1 , N - K )
if __name__ == ' _ _ main _ _ ' :
    str = " aabc "
    K = 1
    print ( MinLen ( str , K ) )

Index/key variables: i
Python program:
class node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def newnode ( d ) :
    temp = node ( d )
    return temp
def dfs ( root , l , M ) :
    if ( root == None ) :
        return
    if l not in M :
        M [ l ] = [ ]
    M [ l ] . append ( root . data )
    dfs ( root . left , l + 1 , M )
    dfs ( root . right , l + 1 , M )
def printMidNodes ( root ) :
    M = dict ( )
    dfs ( root , 0 , M )
    for it in M . values ( ) :
        size = len ( it )
        if ( size & 1 ) :
            print ( it [ ( size - 1 ) // 2 ] )
        else :
            print ( str ( it [ ( size - 1 ) // 2 ] ) + ' ▁ ' + str ( it [ ( size - 1 ) // 2 + 1 ] ) )
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 ( 11 )
    root . left . right . right = newnode ( 6 )
    root . left . right . right . left = newnode ( 7 )
    root . left . right . right . right = newnode ( 9 )
    root . right . left = newnode ( 10 )
    root . right . right = newnode ( 8 )
    printMidNodes ( root )

Index/key variables: l
Iterator variables: it
Class/struct names: node
Python program:
def minLength ( arr , N ) :
    for i in range ( 1 , N ) :
        if ( arr [ 0 ] != arr [ i ] ) :
            return 1
    return N
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 1 , 3 , 1 ]
    N = len ( arr )
    print ( minLength ( arr , N ) )

Index/key variables: i
Iterator variables: i
Python program:
import math
def PrintIndexes ( arr , N ) :
    for i in range ( N ) :
        currSubSum = 0
        for j in range ( i , N , 1 ) :
            currSubSum += arr [ j ]
            sq = int ( math . sqrt ( currSubSum ) )
            if ( sq * sq == currSubSum ) :
                print ( " ( " , i , " , " , j , " ) " , end = " ▁ " )
arr = [ 65 , 79 , 81 ]
N = len ( arr )
PrintIndexes ( arr , N )

Index/key variables: j
Accumulator variables: currSubSum
Iterator variables: i, j
Python program:
def countPosition ( mat ) :
    n = len ( mat )
    m = len ( mat [ 0 ] )
    row = [ 0 ] * n
    col = [ 0 ] * m
    for i in range ( n ) :
        for j in range ( m ) :
            if ( mat [ i ] [ j ] == 1 ) :
                col [ j ] += 1
                row [ i ] += 1
    count = 0
    for i in range ( n ) :
        for j in range ( m ) :
            if ( row [ i ] == col [ j ] ) :
                count += 1
    return count
mat = [ [ 0 , 1 ] , [ 1 , 1 ] ]
print ( countPosition ( mat ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def maxAtLevel ( N , M , Value , Edges ) :
    adj = [ [ ] for i in range ( N ) ]
    for i in range ( M ) :
        u = Edges [ i ] [ 0 ]
        v = Edges [ i ] [ 1 ]
        adj [ u ] . append ( v )
    q = [ ]
    q . append ( 0 )
    while ( len ( q ) ) :
        count = len ( q )
        maxVal = 0
        while ( count ) :
            temp = q [ 0 ]
            q . remove ( q [ 0 ] )
            maxVal = max ( maxVal , Value [ temp ] )
            for i in range ( len ( adj [ temp ] ) ) :
                q . append ( adj [ temp ] [ i ] )
            count -= 1
        print ( maxVal , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 10
    Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 3 , 6 ] , [ 6 , 7 ] , [ 6 , 8 ] , [ 6 , 9 ] ]
    Value = [ 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 ]
    maxAtLevel ( N , N - 1 , Value , Edges )

Index/key variables: i, temp, u
Accumulator variables: count, q
Iterator variables: i
Python program:
def ctTriplets ( arr , N ) :
    arr . sort ( )
    temp = [ 0 ] * N
    index = 0
    for i in range ( 1 , N , 2 ) :
        temp [ i ] = arr [ index ]
        index += 1
    for i in range ( 0 , N , 2 ) :
        temp [ i ] = arr [ index ]
        index += 1
    ct = 0
    for i in range ( N ) :
        if ( i > 0 and i + 1 < N ) :
            if ( temp [ i ] < temp [ i + 1 ] and temp [ i ] < temp [ i - 1 ] ) :
                ct += 1
    print ( " Count ▁ of ▁ triplets : " , ct )
    print ( " Array : " , end = " " )
    for i in range ( N ) :
        print ( temp [ i ] , end = " ▁ " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len ( arr )
ctTriplets ( arr , N )

Index/key variables: i, index
Accumulator variables: ct
Iterator variables: i
Python program:
import sys
def check ( a , n ) :
    b = [ None ] * n
    minElement = sys . maxsize
    for i in range ( 0 , n ) :
        b [ i ] = a [ i ]
        minElement = min ( minElement , a [ i ] )
    b . sort ( )
    k = 1
    for i in range ( 0 , n ) :
        if ( ( a [ i ] != b [ i ] ) and ( a [ i ] % minElement != 0 ) ) :
            k = 0
            break
    if k == 1 :
        return True
    else :
        return False
if __name__ == " _ _ main _ _ " :
    a = [ 4 , 3 , 6 , 6 , 2 , 9 ]
    n = len ( a )
    if check ( a , n ) == True :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def numSpecial ( mat ) :
    m = len ( mat )
    n = len ( mat [ 0 ] )
    rows = [ 0 ] * m
    cols = [ 0 ] * n
    i , j = 0 , 0
    for i in range ( m ) :
        rows [ i ] = 0
        for j in range ( n ) :
            rows [ i ] += mat [ i ] [ j ]
    for i in range ( n ) :
        cols [ i ] = 0
        for j in range ( m ) :
            cols [ i ] += mat [ j ] [ i ]
    cnt = 0
    for i in range ( m ) :
        for j in range ( n ) :
            if ( mat [ i ] [ j ] == 1 and rows [ i ] == 1 and cols [ j ] == 1 ) :
                cnt += 1
    return cnt
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] ]
    print ( numSpecial ( mat ) )

Index/key variables: i, j
Accumulator variables: cnt
Iterator variables: i, j
Python program:
import sys
def minmaxNumbers ( matrix , res ) :
    s = set ( )
    for i in range ( 0 , len ( matrix ) , 1 ) :
        minr = sys . maxsize
        for j in range ( 0 , len ( matrix [ i ] ) , 1 ) :
            minr = min ( minr , matrix [ i ] [ j ] )
        s . add ( minr )
    for j in range ( 0 , len ( matrix [ 0 ] ) , 1 ) :
        maxc = - sys . maxsize - 1
        for i in range ( 0 , len ( matrix ) , 1 ) :
            maxc = max ( maxc , matrix [ i ] [ j ] )
        if ( maxc in s ) :
            res . append ( maxc )
    return res
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 10 , 4 ] , [ 9 , 3 , 8 ] , [ 15 , 16 , 17 ] ]
    ans = [ ]
    minmaxNumbers ( mat , ans )
    if ( len ( ans ) == 0 ) :
        print ( " - 1" )
    for i in range ( len ( ans ) ) :
        print ( ans [ i ] )

Index/key variables: i, j
Accumulator variables: res, s
Iterator variables: i, j
Python program:
mini = 105 ;
ans = 0 ;
n = 0 ;
g = [ ] ;
size = [ 0 ] * 100 ;
def create_graph ( ) :
    g [ 1 ] . append ( 2 ) ;
    g [ 2 ] . append ( 1 ) ;
    g [ 1 ] . append ( 3 ) ;
    g [ 3 ] . append ( 1 ) ;
    g [ 1 ] . append ( 4 ) ;
    g [ 4 ] . append ( 1 ) ;
    g [ 2 ] . append ( 5 ) ;
    g [ 5 ] . append ( 2 ) ;
    g [ 2 ] . append ( 6 ) ;
    g [ 6 ] . append ( 2 ) ;
def dfs ( Node , parent ) :
    size [ Node ] = 1 ;
    mx = 0 ;
    global mini
    global ans
    for y in g [ Node ] :
        if ( y == parent ) :
            continue ;
        dfs ( y , Node ) ;
        size [ Node ] += size [ y ] ;
        mx = max ( mx , size [ y ] ) ;
    mx = max ( mx , n - size [ Node ] ) ;
    if ( mx < mini ) :
        mini = mx ;
        ans = Node ;
if __name__ == ' _ _ main _ _ ' :
    n = 6 ;
    for i in range ( 100 ) :
        g . append ( [ ] )
    create_graph ( ) ;
    dfs ( 1 , - 1 ) ;
    print ( ans ) ;

Index/key variables: Node, y
Accumulator variables: g
Iterator variables: i, y
Python program:
def findMinLength ( arr ) :
    index = len ( arr ) - 1 ;
    while ( index > 0 and arr [ index ] >= arr [ index - 1 ] ) :
        index -= 1 ;
    return index ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 ] ;
    n = len ( arr ) ;
    print ( findMinLength ( arr ) ) ;

Index/key variables: index
Python program:
def countPrecedingK ( a , n , K ) :
    prefix = [ 0 ] * n
    prefix [ 0 ] = a [ 0 ]
    for i in range ( 1 , n ) :
        prefix [ i ] = prefix [ i - 1 ] + a [ i ]
    ctr = 0
    if ( prefix [ K - 1 ] < a [ K ] ) :
        ctr += 1
    for i in range ( K + 1 , n ) :
        if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) :
            ctr += 1
    return ctr
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 8 , 10 , - 2 , 7 , 5 , 5 , 9 , 15 ]
    N = len ( arr )
    K = 2
    print ( countPrecedingK ( arr , N , K ) )

Index/key variables: K, i
Accumulator variables: ctr
Iterator variables: i
Python program:
def convertBfromA ( a , b ) :
    moves = 0
    x = abs ( a - b )
    for i in range ( 10 , 0 , - 1 ) :
        moves += x // i
        x = x % i
    print ( moves , end = " ▁ " )
A = 188
B = 4
convertBfromA ( A , B )

Accumulator variables: moves
Iterator variables: i
Python program:
import sys
def smallestNth ( A , B , N ) :
    res = sys . maxsize
    for i in range ( 1 , N ) :
        for j in range ( N , i , - 1 ) :
            if ( ( B - A ) % ( j - i ) == 0 ) :
                D = ( B - A ) // ( j - i )
                FirstTerm = A - ( i - 1 ) * D
                NthTerm = FirstTerm + ( N - 1 ) * D
                if ( FirstTerm > 0 ) :
                    res = min ( res , NthTerm )
    return res
if __name__ == ' _ _ main _ _ ' :
    N = 3
    A = 1
    B = 6
    print ( smallestNth ( A , B , N ) )

Iterator variables: i, j
Python program:
def sieve ( prime ) :
    for i in range ( 1000000 ) :
        prime [ i ] = True
    prime [ 0 ] = prime [ 1 ] = False
    for i in range ( 2 , 1000 + 1 ) :
        if ( prime [ i ] ) :
            for j in range ( i * i , 1000000 , i ) :
                prime [ j ] = False
    return prime
def subPrimeSum ( N , K , arr , prime ) :
    currSum = 0
    for i in range ( 0 , K ) :
        currSum += arr [ i ]
    if ( prime [ currSum ] ) :
        for i in range ( K ) :
            print ( arr [ i ] , end = " ▁ " )
        return
    st = 1
    en = K
    while ( en < N ) :
        currSum += arr [ en ] - arr [ st - 1 ]
        if ( prime [ currSum ] ) :
            for i in range ( st , en + 1 ) :
                print ( arr [ i ] , end = " ▁ " )
            return
        en += 1
        st += 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 ]
    K = 4
    N = len ( arr )
    prime = [ False ] * 1000000
    prime = sieve ( prime )
    subPrimeSum ( N , K , arr , prime )

Index/key variables: currSum, en, i, j
Accumulator variables: st
Iterator variables: i, j
Python program:
def maximumSum ( a , b , n , index , lastpicked ) :
    if ( index == n ) :
        return 0
    option1 = 0
    option2 = 0
    if ( lastpicked == - 1 or a [ lastpicked ] != a [ index ] ) :
        option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index )
    option2 = maximumSum ( a , b , n , index + 1 , lastpicked )
    return max ( option1 , option2 )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 3 , 3 , 1 ]
    brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ]
    N = len ( arr )
    print ( maximumSum ( arr , brr , N , 0 , - 1 ) )

Index/key variables: index, lastpicked
Python program:
def sumOfKElements ( arr , n , k ) :
    rev = False ;
    if ( k < 0 ) :
        rev = True ;
        k *= - 1 ;
        l = 0 ;
        r = n - 1 ;
        while ( l < r ) :
            tmp = arr [ l ] ;
            arr [ l ] = arr [ r ] ;
            arr [ r ] = tmp ;
            l += 1 ;
            r -= 1 ;
    dp = [ 0 ] * n ;
    dp [ 0 ] = arr [ 0 ] ;
    for i in range ( 1 , n ) :
        dp [ i ] += dp [ i - 1 ] + arr [ i ] ;
    ans = [ 0 ] * n ;
    for i in range ( n ) :
        if ( i + k < n ) :
            ans [ i ] = dp [ i + k ] - dp [ i ] ;
        else :
            x = k - ( n - 1 - i ) ;
            y = x // n ;
            rem = x % n ;
            ans [ i ] = ( dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( dp [ rem - 1 ] if rem - 1 >= 0 else 0 ) ) ;
    if ( rev ) :
        for i in range ( n - 1 , - 1 , - 1 ) :
            print ( ans [ i ] , end = " ▁ " ) ;
    else :
        for i in range ( n ) :
            print ( ans [ i ] , end = " ▁ " ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 2 , - 5 , 11 ] ;
    N = len ( arr ) ;
    K = 3 ;
    sumOfKElements ( arr , N , K ) ;

Index/key variables: i, l, r
Iterator variables: i
Boolean variables: rev
Python program:
def binaryInsert ( boundary , i ) :
    l = 0
    r = len ( boundary ) - 1
    while l <= r :
        m = ( l + r ) // 2
        if boundary [ m ] < i :
            l = m + 1
        else :
            r = m - 1
    boundary . insert ( l , i )
    return l
def countingSubarray ( arr , n ) :
    index = { }
    for i in range ( n ) :
        index [ arr [ i ] ] = i
    boundary = [ - 1 , n ]
    arr . sort ( )
    ans = [ 0 for i in range ( n ) ]
    for num in arr :
        i = binaryInsert ( boundary , index [ num ] )
        l = boundary [ i ] - boundary [ i - 1 ] - 1
        r = boundary [ i + 1 ] - boundary [ i ] - 1
        cnt = l + r + l * r + 1
        ans [ index [ num ] ] += cnt
    return ans
N = 5
arr = [ 3 , 2 , 4 , 1 , 5 ]
print ( countingSubarray ( arr , N ) )

Index/key variables: i, m, num
Iterator variables: i, num