dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from queue import Queue
class newNode :
    def __init__ ( self , key ) :
        self . key = key
        self . left = self . right = None
def isSorted ( root ) :
    prevMax = - 999999999999
    minval = None
    maxval = None
    levelSize = None
    q = Queue ( )
    q . put ( root )
    while ( not q . empty ( ) ) :
        levelSize = q . qsize ( )
        minval = 999999999999
        maxval = - 999999999999
        while ( levelSize > 0 ) :
            root = q . queue [ 0 ]
            q . get ( )
            levelSize -= 1
            minval = min ( minval , root . key )
            maxval = max ( maxval , root . key )
            if ( root . left ) :
                q . put ( root . left )
            if ( root . right ) :
                q . put ( root . right )
        if ( minval <= prevMax ) :
            return 0
        prevMax = maxval
    return 1
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 1 )
    root . left = newNode ( 4 )
    root . left . right = newNode ( 6 )
    root . left . right . left = newNode ( 8 )
    root . left . right . right = newNode ( 9 )
    root . left . right . left . left = newNode ( 12 )
    root . left . right . right . right = newNode ( 10 )
    if ( isSorted ( root ) ) :
        print ( " Sorted " )
    else :
        print ( " Not ▁ sorted " )

Accumulator variables: levelSize
Class/struct names: newNode
Python program:
import math
def printSmallSub ( arr , N ) :
    gcdArr = 0
    for i in range ( 0 , N ) :
        gcdArr = math . gcd ( gcdArr , arr [ i ] )
    for i in range ( 0 , N ) :
        if ( arr [ i ] == gcdArr ) :
            print ( arr [ i ] , end = " ▁ " )
            return
        for i in range ( 0 , N ) :
            for j in range ( i + 1 , N ) :
                if ( math . gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) :
                    print ( arr [ i ] , end = " ▁ " )
                    print ( arr [ j ] , end = " ▁ " )
                    return
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 6 , 12 ]
    N = len ( arr )
    printSmallSub ( arr , N )

Index/key variables: i, j
Iterator variables: i, j
Python program:
X = 1000000
def getPrimeNum ( ) :
    isPrime = [ True ] * ( X )
    isPrime [ 0 ] = False
    isPrime [ 1 ] = False
    i = 2
    while i * i <= X :
        if ( isPrime [ i ] == True ) :
            for j in range ( i * i , X , i ) :
                isPrime [ j ] = False
        i += 1
    return isPrime
def cntPairs ( arr , N ) :
    isPrime = getPrimeNum ( )
    cntOne = 0
    cntPrime = 0
    for i in range ( N ) :
        if ( arr [ i ] == 1 ) :
            cntOne += 1
        elif ( isPrime [ i ] ) :
            cntPrime += 1
    cntNonComp = 0
    cntNonComp = ( cntPrime * cntOne + cntOne * ( cntOne - 1 ) // 2 )
    res = 0
    res = ( N * ( N - 1 ) // 2 - cntNonComp )
    return res
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 2 , 2 , 8 ]
    N = len ( arr )
    print ( cntPairs ( arr , N ) )

Index/key variables: i, j
Accumulator variables: cntOne, cntPrime
Iterator variables: i, j
Python program:
def RearrangeB ( A , B ) :
    n = len ( A )
    if ( n == 1 and A [ 0 ] == B [ 0 ] ) :
        print ( - 1 )
        return
    for i in range ( n // 2 ) :
        t = B [ i ]
        B [ i ] = B [ n - i - 1 ]
        B [ n - i - 1 ] = t
    for i in range ( n - 1 ) :
        if ( B [ i ] == A [ i ] ) :
            B [ i ] , B [ i - 1 ] = B [ i - 1 ] , B [ i ]
            break
    for k in B :
        print ( k , end = " ▁ " )
A = [ 2 , 4 , 5 , 8 ]
B = [ 2 , 4 , 5 , 8 ]
RearrangeB ( A , B )

Index/key variables: i
Iterator variables: i, k
Python program:
N , M = 6 , 6
def swap ( mat , startx_X , starty_X , startx_Y , starty_Y ) :
    row , col = 0 , 0
    i = startx_X
    while ( bool ( True ) ) :
        col = 0
        j = startx_X
        while ( bool ( True ) ) :
            temp = mat [ i ] [ j ]
            mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ]
            mat [ startx_Y + row ] [ starty_Y + col ] = temp
            col += 1
            if col >= M // 2 :
                break
            j += 1
        row += 1
        if row >= N // 2 :
            break
        i += 1
def swapQuadOfMatrix ( mat , X , Y ) :
    if ( X == 1 and Y == 2 ) :
        swap ( mat , 0 , 0 , 0 , M // 2 )
    elif ( X == 1 and Y == 3 ) :
        swap ( mat , 0 , 0 , N // 2 , 0 )
    elif ( X == 1 and Y == 4 ) :
        swap ( mat , 0 , 0 , N // 2 , M // 2 )
    elif ( X == 2 and Y == 3 ) :
        swap ( mat , 0 , M // 2 , N // 2 , 0 )
    elif ( X == 2 and Y == 4 ) :
        swap ( mat , 0 , M // 2 , N // 2 , M // 2 )
    elif ( X == 3 and Y == 4 ) :
        swap ( mat , N // 2 , 0 , N // 2 , M // 2 )
    printMat ( mat )
def printMat ( mat ) :
    for i in range ( N ) :
        for j in range ( M ) :
            print ( mat [ i ] [ j ] , end = " ▁ " )
        print ( )
mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] , [ 19 , 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 , 30 ] , [ 31 , 32 , 33 , 34 , 35 , 36 ] ]
X , Y = 1 , 4
swapQuadOfMatrix ( mat , X , Y )

Index/key variables: i, j
Accumulator variables: col, row
Iterator variables: i, j
Python program:
class Node :
    def __init__ ( self , key ) :
        self . data = key
        self . hd = 1000000
        self . left = None
        self . right = None
def bottomView ( root ) :
    if ( root == None ) :
        return
    hd = 0
    m = dict ( )
    q = [ ]
    root . hd = hd
    q . append ( root )
    while ( len ( q ) != 0 ) :
        temp = q [ 0 ]
        q . pop ( 0 )
        hd = temp . hd
        m [ hd ] = temp . data
        if ( temp . left != None ) :
            temp . left . hd = hd - 1
            q . append ( temp . left )
        if ( temp . right != None ) :
            temp . right . hd = hd + 1
            q . append ( temp . right )
    for i in sorted ( m . keys ( ) ) :
        print ( m [ i ] , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 20 )
    root . left = Node ( 8 )
    root . right = Node ( 22 )
    root . left . left = Node ( 5 )
    root . left . right = Node ( 3 )
    root . right . left = Node ( 4 )
    root . right . right = Node ( 25 )
    root . left . right . left = Node ( 10 )
    root . left . right . right = Node ( 14 )
    print ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : " )
    bottomView ( root )

Index/key variables: hd, i
Accumulator variables: q
Iterator variables: i
Class/struct names: Node
Python program:
def incrementCount ( arr , N ) :
    mini = arr [ 0 ] - arr [ 1 ]
    for i in range ( 2 , N ) :
        mini = min ( mini , arr [ i - 1 ] - arr [ i ] )
    print ( mini )
N = 3
arr = [ 12 , 8 , 4 ]
incrementCount ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def countOperations ( n , k ) :
    div = 2 * k + 1
    if ( n // 2 <= k ) :
        print ( 1 )
        if ( n > k ) :
            print ( k + 1 )
        else :
            print ( n )
    else :
        if ( n % div == 0 ) :
            oprn = n // div
            print ( oprn )
            pos = k + 1
            print ( pos , end = " ▁ " )
            for i in range ( 1 , oprn + 1 ) :
                print ( pos , end = " ▁ " )
                pos += div
        else :
            oprn = n // div + 1
            print ( oprn )
            pos = n % div
            if ( n % div > k ) :
                pos -= k
            for i in range ( 1 , oprn + 1 ) :
                print ( pos , end = " ▁ " )
                pos += div
if __name__ == ' _ _ main _ _ ' :
    str = " edfreqwsazxet "
    ch = ' $ '
    n = len ( str )
    k = 4
    countOperations ( n , k )

Accumulator variables: pos
Iterator variables: i
Python program:
def largestSubsequence ( s ) :
    maxi = 0
    for i in range ( 10 ) :
        for j in range ( 10 ) :
            if ( i != j ) :
                lenn = 0
                prev1 = chr ( j + ord ( '0' ) )
                for k in range ( len ( s ) ) :
                    if ( s [ k ] == chr ( i + ord ( '0' ) ) and prev1 == chr ( j + ord ( '0' ) ) ) :
                        prev1 = s [ k ]
                        lenn += 1
                    elif ( s [ k ] == chr ( j + ord ( '0' ) ) and prev1 == chr ( i + ord ( '0' ) ) ) :
                        prev1 = s [ k ]
                        lenn += 1
                maxi = max ( lenn , maxi )
    if ( maxi != 1 ) :
        print ( maxi )
    else :
        print ( 0 )
if __name__ == ' _ _ main _ _ ' :
    s = "1542745249842"
    largestSubsequence ( s )

Index/key variables: k
Accumulator variables: lenn
Iterator variables: i, j, k
Python program:
class Node :
    def __init__ ( self , key = None , left = None , right = None ) :
        self . data = key
        self . left = left
        self . right = right
def printBottomViewUtil ( root , d , hd , level ) :
    if root is None :
        return
    if hd in d :
        if level >= d [ hd ] [ 1 ] :
            d [ hd ] = [ root . data , level ]
    else :
        d [ hd ] = [ root . data , level ]
    printBottomViewUtil ( root . left , d , hd - 1 , level + 1 )
    printBottomViewUtil ( root . right , d , hd + 1 , level + 1 )
def printBottomView ( root ) :
    d = dict ( )
    printBottomViewUtil ( root , d , 0 , 0 )
    for i in sorted ( d . keys ( ) ) :
        print ( d [ i ] [ 0 ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 20 )
    root . left = Node ( 8 )
    root . right = Node ( 22 )
    root . left . left = Node ( 5 )
    root . left . right = Node ( 3 )
    root . right . left = Node ( 4 )
    root . right . right = Node ( 25 )
    root . left . right . left = Node ( 10 )
    root . left . right . right = Node ( 14 )
    print ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ : " )
    printBottomView ( root )

Index/key variables: hd, i
Iterator variables: i
Class/struct names: Node
Python program:
global maxm
maxm = 0
class Node :
    def __init__ ( self , x ) :
        self . val = x
        self . left = None
        self . right = None
def gcd ( a , b ) :
    if ( b == 0 ) :
        return a
    return gcd ( b , a % b )
def find_gcd ( arr ) :
    if ( len ( arr ) == 1 ) :
        return arr [ 0 ]
    g = arr [ 0 ]
    for i in range ( 1 , len ( arr ) ) :
        g = gcd ( g , arr [ i ] )
    return g
def maxm_gcd ( root , ans ) :
    global maxm
    if ( not root ) :
        return
    if ( root . left == None and root . right == None ) :
        ans . append ( root . val )
        maxm = max ( find_gcd ( ans ) , maxm )
        return
    ans . append ( root . val )
    maxm_gcd ( root . left , ans )
    maxm_gcd ( root . right , ans )
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 15 )
    root . left = Node ( 3 )
    root . right = Node ( 7 )
    root . left . left = Node ( 15 )
    root . left . right = Node ( 1 )
    root . right . left = Node ( 31 )
    root . right . right = Node ( 9 )
    maxm_gcd ( root , [ ] )
    print ( maxm )

Index/key variables: i
Iterator variables: i
Class/struct names: Node
Python program:
def palindromeWinner ( S ) :
    freq = [ 0 for i in range ( 0 , 26 ) ]
    count = 0
    for i in range ( 0 , len ( S ) ) :
        if ( freq [ ord ( S [ i ] ) - 97 ] == 0 ) :
            count += 1
        freq [ ord ( S [ i ] ) - 97 ] += 1
    unique = 0
    duplicate = 0
    for i in range ( 0 , 26 ) :
        if ( freq [ i ] == 1 ) :
            unique += 1
        elif ( freq [ i ] >= 2 ) :
            duplicate += 1
    if ( unique == 1 and ( unique + duplicate ) == count ) :
        return 1
    return 2
S = " abcbc " ;
print ( " Player - " , palindromeWinner ( S ) )

Index/key variables: i
Accumulator variables: count, duplicate, unique
Iterator variables: i
Python program:
import math
def countOfSubstrings ( s ) :
    n = len ( s )
    x = int ( math . sqrt ( n ) )
    ones = [ ]
    for i in range ( n ) :
        if ( s [ i ] == '1' ) :
            ones . append ( i )
    if ( len ( ones ) == 0 ) :
        return 0
    ones . append ( n )
    totCount = [ 0 ] * ( n * x + n )
    sum = 0
    for k in range ( x + 1 ) :
        now = 0
        totCount [ k * n ] += 1
        for j in range ( 1 , n + 1 ) :
            if ( s [ j - 1 ] == '1' ) :
                now += 1
            index = j - k * now
            sum += totCount [ index + k * n ]
            totCount [ index + k * n ] += 1
        now = 0
        totCount [ k * n ] -= 1
        for j in range ( 1 , n + 1 ) :
            if ( s [ j - 1 ] == '1' ) :
                now += 1
            index = j - k * now
            totCount [ index + k * n ] -= 1
    prefix_sum = [ - 1 ] * n
    cnt = 0
    for i in range ( n ) :
        prefix_sum [ i ] = cnt
        if ( s [ i ] == '1' ) :
            cnt += 1
    for k in range ( n ) :
        j = 1
        while ( j <= ( n // x ) and prefix_sum [ k ] + j <= cnt ) :
            l = ( ones [ prefix_sum [ k ] + j - 1 ] - k + 1 )
            r = ones [ prefix_sum [ k ] + j ] - k
            l = max ( l , j * ( x + 1 ) )
            if ( l <= r ) :
                sum += r // j - ( l - 1 ) // j
            j += 1
    return sum
if __name__ == " _ _ main _ _ " :
    S = "1111100000"
    print ( countOfSubstrings ( S ) )

Index/key variables: i, k
Accumulator variables: cnt, now, ones, sum
Iterator variables: i, j, k
Python program:
def largestNum ( n ) :
    num = 0 ;
    max_setBits = 0 ;
    for i in range ( n + 1 ) :
        setBits = bin ( i ) . count ( '1' ) ;
        if ( setBits >= max_setBits ) :
            num = i ;
            max_setBits = setBits ;
    return num ;
if __name__ == " _ _ main _ _ " :
    N = 345 ;
    print ( largestNum ( N ) ) ;

Iterator variables: i
Python program:
from queue import Queue
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . left = self . right = None
def getLeafCount ( node ) :
    if ( not node ) :
        return 0
    q = Queue ( )
    count = 0
    q . put ( node )
    while ( not q . empty ( ) ) :
        temp = q . queue [ 0 ]
        q . get ( )
        if ( temp . left != None ) :
            q . put ( temp . left )
        if ( temp . right != None ) :
            q . put ( temp . right )
        if ( temp . left == None and temp . right == None ) :
            count += 1
    return count
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 ( getLeafCount ( root ) )

Accumulator variables: count
Class/struct names: newNode
Python program:
def solve ( Array , N , K ) :
    count_Arr = [ 0 ] * ( N + 2 ) ;
    factor = 1 ;
    size = N ;
    while ( size ) :
        start = 1 ;
        end = size ;
        count_Arr [ 1 ] += factor * N ;
        count_Arr [ end + 1 ] -= factor * N ;
        factor += 1 ;
        size //= 2 ;
    for i in range ( 2 , N + 1 ) :
        count_Arr [ i ] += count_Arr [ i - 1 ] ;
    element = [ ] ;
    for i in range ( N ) :
        element . append ( ( Array [ i ] , count_Arr [ i + 1 ] ) ) ;
    element . sort ( ) ;
    start = 1 ;
    for i in range ( N ) :
        end = start + element [ i ] [ 1 ] - 1 ;
        if ( K >= start and K <= end ) :
            return element [ i ] [ 0 ] ;
        start += element [ i ] [ 1 ] ;
    return - 1 ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 4 , 5 , 1 ] ;
    N = len ( arr ) ;
    K = 13 ;
    print ( solve ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: element, factor, size, start
Iterator variables: i
Python program:
def printMaxNumber ( n ) :
    if ( n % 2 == 1 ) :
        print ( "7" , end = " " ) ;
        for i in range ( int ( ( n - 3 ) / 2 ) ) :
            print ( "1" , end = " " ) ;
    else :
        for i in range ( n / 2 ) :
            print ( "1" , end = " " ) ;
n = 5 ;
printMaxNumber ( n ) ;

Iterator variables: i
Python program:
def getSum ( n ) :
    sumOdd = 0
    sumEven = 0
    num = str ( n )
    for i in range ( len ( num ) ) :
        if ( i % 2 == 0 ) :
            sumOdd = sumOdd + int ( num [ i ] )
        else :
            sumEven = sumEven + int ( num [ i ] )
    print ( " Sum ▁ odd ▁ = ▁ " , sumOdd )
    print ( " Sum ▁ even ▁ = ▁ " , sumEven )
if __name__ == " _ _ main _ _ " :
    n = 457892
    getSum ( n )

Index/key variables: i
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , x ) :
        self . data = x
        self . left = None
        self . right = None
def countLeaves ( node ) :
    if ( node == None ) :
        return 0
    if ( node . left == None and node . right == None ) :
        return 1
    return countLeaves ( node . left ) + countLeaves ( node . right )
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 1 )
    root . left = Node ( 2 )
    root . right = Node ( 3 )
    root . left . left = Node ( 4 )
    root . left . right = Node ( 5 )
    print ( countLeaves ( root ) )

Class/struct names: Node
Python program:
import sys
def sorted_partitions ( arr , n ) :
    right_min = [ 0 ] * ( n + 1 )
    right_min [ n ] = sys . maxsize
    for i in range ( n - 1 , - 1 , - 1 ) :
        right_min [ i ] = min ( right_min [ i + 1 ] , arr [ i ] )
    partitions = 0
    current_max = arr [ 0 ]
    for i in range ( n ) :
        current_max = max ( current_max , arr [ i ] )
        if ( current_max <= right_min [ i + 1 ] ) :
            partitions += 1
    return partitions
arr = [ 3 , 1 , 2 , 4 , 100 , 7 , 9 ]
n = len ( arr )
ans = sorted_partitions ( arr , n )
print ( ans )

Index/key variables: i, n
Accumulator variables: partitions
Iterator variables: i
Python program:
def countMinSwaps ( s ) :
    N = len ( s )
    one = 0
    zero = 0
    for i in range ( N ) :
        if ( s [ i ] == '1' ) :
            one += 1
        else :
            zero += 1
    if ( one > zero + 1 or zero > one + 1 ) :
        return - 1
    if ( N % 2 ) :
        num = ( N + 1 ) / 2
        one_even = 0
        zero_even = 0
        for i in range ( N ) :
            if ( i % 2 == 0 ) :
                if ( s [ i ] == '1' ) :
                    one_even += 1
                else :
                    zero_even += 1
        if ( one > zero ) :
            return num - one_even
        else :
            return num - zero_even
    else :
        one_odd = 0
        one_even = 0
        for i in range ( N ) :
            if ( s [ i ] == '1' ) :
                if ( i % 2 ) :
                    one_odd += 1
                else :
                    one_even += 1
        return min ( N // 2 - one_odd , N // 2 - one_even )
s = "111000"
print ( countMinSwaps ( s ) )

Index/key variables: i
Accumulator variables: one, one_even, one_odd, zero, zero_even
Iterator variables: i
Python program:
st = " NNNWEWESSS "
length = len ( st )
for i in range ( length ) :
    if ( st [ i ] == " N " ) :
        n += 1
    if ( st [ i ] == " S " ) :
        s += 1
    if ( st [ i ] == " W " ) :
        w += 1
    if ( st [ i ] == " E " ) :
        e += 1
if ( n == s and w == e ) :
    print ( " YES " )
else :
    print ( " NO " )

Index/key variables: i
Accumulator variables: e, n, s, w
Iterator variables: i
Python program:
import math
def minCost ( coin , n , k ) :
    coin . sort ( )
    coins_needed = math . ceil ( 1.0 * n // ( k + 1 ) ) ;
    ans = 0
    for i in range ( coins_needed - 1 + 1 ) :
        ans += coin [ i ]
    return ans
coin = [ 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 ]
n = len ( coin )
k = 3
print ( minCost ( coin , n , k ) )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
def firstFit ( blockSize , m , processSize , n ) :
    allocation = [ - 1 ] * n
    for i in range ( n ) :
        for j in range ( m ) :
            if blockSize [ j ] >= processSize [ i ] :
                allocation [ i ] = j
                blockSize [ j ] -= processSize [ i ]
                break
    print ( " ▁ Process ▁ No . ▁ Process ▁ Size ▁ Block ▁ no . " )
    for i in range ( n ) :
        print ( " ▁ " , i + 1 , " ▁ " , processSize [ i ] , " ▁ " , end = " ▁ " )
        if allocation [ i ] != - 1 :
            print ( allocation [ i ] + 1 )
        else :
            print ( " Not ▁ Allocated " )
if __name__ == ' _ _ main _ _ ' :
    blockSize = [ 100 , 500 , 200 , 300 , 600 ]
    processSize = [ 212 , 417 , 112 , 426 ]
    m = len ( blockSize )
    n = len ( processSize )
    firstFit ( blockSize , m , processSize , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findMin ( V ) :
    deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ]
    n = len ( deno )
    ans = [ ]
    i = n - 1
    while ( i >= 0 ) :
        while ( V >= deno [ i ] ) :
            V -= deno [ i ]
            ans . append ( deno [ i ] )
        i -= 1
    for i in range ( len ( ans ) ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 93
    print ( " Following ▁ is ▁ minimal ▁ number " , " of ▁ change ▁ for " , n , " : ▁ " , end = " " )
    findMin ( n )

Index/key variables: i
Accumulator variables: V, ans
Iterator variables: i
Python program:
def MinimumRemovals ( a , N , K ) :
    b = [ 0 for i in range ( N + 1 ) ]
    for i in range ( N ) :
        b [ i + 1 ] = a [ i ]
    dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ]
    for i in range ( N ) :
        for j in range ( i + 1 ) :
            dp [ i + 1 ] [ j ] = max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j ] )
            dp [ i + 1 ] [ j + 1 ] = max ( dp [ i + 1 ] [ j + 1 ] , dp [ i ] [ j ] + ( 1 if ( b [ i + 1 ] == j + 1 ) else 0 ) )
    j = N
    while ( j >= 0 ) :
        if ( dp [ N ] [ j ] >= K ) :
            return ( N - j )
        j -= 1
    return - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 1 , 3 , 2 , 3 ]
    K = 2
    N = len ( arr )
    print ( MinimumRemovals ( arr , N , K ) )

Index/key variables: N, i, j
Iterator variables: i, j
Python program:
def countArray ( arr , N , M ) :
    dp = [ [ 0 for i in range ( M + 2 ) ] for j in range ( N ) ]
    if ( arr [ 0 ] == - 1 ) :
        for j in range ( 1 , M + 1 , 1 ) :
            dp [ 0 ] [ j ] = 1
    else :
        dp [ 0 ] [ arr [ 0 ] ] = 1
    for i in range ( 1 , N , 1 ) :
        if ( arr [ i ] != - 1 ) :
            j = arr [ i ]
            dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ]
        if ( arr [ i ] == - 1 ) :
            for j in range ( 1 , M + 1 , 1 ) :
                dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j + 1 ]
    arrCount = 0
    for j in range ( 1 , M + 1 , 1 ) :
        arrCount += dp [ N - 1 ] [ j ]
    return arrCount
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , - 1 , 2 , 1 , - 1 , - 1 ]
    N = len ( arr )
    M = 10
    print ( countArray ( arr , N , M ) )

Index/key variables: i, j
Accumulator variables: arrCount
Iterator variables: i, j
Python program:
def maxMergingScore ( A , N ) :
    dp = [ [ 0 for i in range ( 101 ) ] for j in range ( 101 ) ]
    for len1 in range ( 1 , N , 1 ) :
        for i in range ( 0 , N - len1 , 1 ) :
            j = i + len1
            dp [ i ] [ j ] = 0
            for k in range ( i + 1 , j , 1 ) :
                dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] )
    return dp [ 0 ] [ N - 1 ]
if __name__ == ' _ _ main _ _ ' :
    N = 4
    A = [ 1 , 2 , 3 , 4 ]
    print ( maxMergingScore ( A , N ) )
    N = 2
    B = [ 1 , 55 ]
    print ( maxMergingScore ( B , N ) )

Index/key variables: i, j, k
Iterator variables: i, k, len1
Python program:
def LongestOddEvenSubarray ( A , N ) :
    dp = 1
    ans = 1
    for i in range ( 1 , N ) :
        if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) :
            dp = dp + 1
            ans = max ( ans , dp )
        else :
            dp = 1
    return ans
A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ]
N = len ( A )
print ( LongestOddEvenSubarray ( A , N ) )

Index/key variables: i
Iterator variables: i