dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def countPS ( str ) :
    N = len ( str )
    cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ]
    for i in range ( N ) :
        cps [ i ] [ i ] = 1
    for L in range ( 2 , N + 1 ) :
        for i in range ( N ) :
            k = L + i - 1
            if ( k < N ) :
                if ( str [ i ] == str [ k ] ) :
                    cps [ i ] [ k ] = ( cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] + 1 )
                else :
                    cps [ i ] [ k ] = ( cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] - cps [ i + 1 ] [ k - 1 ] )
    return cps [ 0 ] [ N - 1 ]
str = " abcb "
print ( " Total ▁ palindromic ▁ subsequence ▁ are ▁ : ▁ " , countPS ( str ) )

Index/key variables: i, k
Iterator variables: L, i
Python program:
str = " abcb "
def countPS ( i , j ) :
    if ( i > j ) :
        return 0
    if ( dp [ i ] [ j ] != - 1 ) :
        return dp [ i ] [ j ]
    if ( i == j ) :
        dp [ i ] [ j ] = 1
        return dp [ i ] [ j ]
    elif ( str [ i ] == str [ j ] ) :
        dp [ i ] [ j ] = ( countPS ( i + 1 , j ) + countPS ( i , j - 1 ) + 1 )
        return dp [ i ] [ j ]
    else :
        dp [ i ] [ j ] = ( countPS ( i + 1 , j ) + countPS ( i , j - 1 ) - countPS ( i + 1 , j - 1 ) )
        return dp [ i ] [ j ]
if __name__ == " _ _ main _ _ " :
    dp = [ [ - 1 for x in range ( 1000 ) ] for y in range ( 1000 ) ]
    n = len ( str )
    print ( " Total ▁ palindromic ▁ subsequence ▁ are ▁ : " , countPS ( 0 , n - 1 ) )

Index/key variables: i, j
Python program:
def countSub ( arr , n ) :
    count = [ 0 for i in range ( 10 ) ]
    for i in range ( n ) :
        for j in range ( arr [ i ] - 1 , - 1 , - 1 ) :
            count [ arr [ i ] ] += count [ j ]
        count [ arr [ i ] ] += 1
    result = 0
    for i in range ( 10 ) :
        result += count [ i ]
    return result
arr = [ 3 , 2 , 4 , 5 , 4 ]
n = len ( arr )
print ( countSub ( arr , n ) )

Index/key variables: i, j
Accumulator variables: result
Iterator variables: i, j
Python program:
def minimum ( a , b , c ) :
    return min ( min ( a , b ) , c ) ;
def findMinSum ( arr , n ) :
    sum = [ ]
    sum . append ( arr [ 0 ] )
    sum . append ( arr [ 1 ] )
    sum . append ( arr [ 2 ] )
    for i in range ( 3 , n ) :
        sum . append ( arr [ i ] + minimum ( sum [ i - 3 ] , sum [ i - 2 ] , sum [ i - 1 ] ) )
    return minimum ( sum [ n - 1 ] , sum [ n - 2 ] , sum [ n - 3 ] )
arr = [ 1 , 2 , 3 , 20 , 2 , 10 , 1 ]
n = len ( arr )
print ( " Min ▁ Sum ▁ is ▁ " , findMinSum ( arr , n ) )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
INF = 1000000
def MinimumCost ( cost , n , W ) :
    val = list ( )
    wt = list ( )
    size = 0
    for i in range ( n ) :
        if ( cost [ i ] != - 1 ) :
            val . append ( cost [ i ] )
            wt . append ( i + 1 )
            size += 1
    n = size
    min_cost = [ [ 0 for i in range ( W + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( W + 1 ) :
        min_cost [ 0 ] [ i ] = INF
    for i in range ( 1 , n + 1 ) :
        min_cost [ i ] [ 0 ] = 0
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , W + 1 ) :
            if ( wt [ i - 1 ] > j ) :
                min_cost [ i ] [ j ] = min_cost [ i - 1 ] [ j ]
            else :
                min_cost [ i ] [ j ] = min ( min_cost [ i - 1 ] [ j ] , min_cost [ i ] [ j - wt [ i - 1 ] ] + val [ i - 1 ] )
    if ( min_cost [ n ] [ W ] == INF ) :
        return - 1
    else :
        return min_cost [ n ] [ W ]
cost = [ 1 , 2 , 3 , 4 , 5 ]
W = 5
n = len ( cost )
print ( MinimumCost ( cost , n , W ) )

Index/key variables: W, i, j, n
Accumulator variables: size, val, wt
Iterator variables: i, j
Python program:
def lcs ( X , Y , m , n ) :
    L = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ]
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            if i == 0 or j == 0 :
                L [ i ] [ j ] = 0
            elif X [ i - 1 ] == Y [ j - 1 ] :
                L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1
            else :
                L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] )
    return L [ m ] [ n ]
def findMinCost ( X , Y , costX , costY ) :
    m = len ( X )
    n = len ( Y )
    len_LCS = lcs ( X , Y , m , n )
    return ( costX * ( m - len_LCS ) + costY * ( n - len_LCS ) )
X = " ef "
Y = " gh "
print ( ' Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings ▁ ' , end = ' ' )
print ( ' identical ▁ is ▁ = ▁ ' , findMinCost ( X , Y , 10 , 20 ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
N = 100
L = [ [ 0 for i in range ( N ) ] for j in range ( N ) ]
def findLCS ( x , y , m , n ) :
    s = set ( )
    if m == 0 or n == 0 :
        s . add ( " " )
        return s
    if x [ m - 1 ] == y [ n - 1 ] :
        tmp = findLCS ( x , y , m - 1 , n - 1 )
        for string in tmp :
            s . add ( string + x [ m - 1 ] )
    else :
        if L [ m - 1 ] [ n ] >= L [ m ] [ n - 1 ] :
            s = findLCS ( x , y , m - 1 , n )
        if L [ m ] [ n - 1 ] >= L [ m - 1 ] [ n ] :
            tmp = findLCS ( x , y , m , n - 1 )
            for i in tmp :
                s . add ( i )
    return s
def LCS ( x , y , m , n ) :
    for i in range ( m + 1 ) :
        for j in range ( n + 1 ) :
            if i == 0 or j == 0 :
                L [ i ] [ j ] = 0
            elif x [ i - 1 ] == y [ j - 1 ] :
                L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1
            else :
                L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] )
    return L [ m ] [ n ]
if __name__ == " _ _ main _ _ " :
    x = " AGTGATG "
    y = " GTTAG "
    m = len ( x )
    n = len ( y )
    print ( " LCS ▁ length ▁ is " , LCS ( x , y , m , n ) )
    s = findLCS ( x , y , m , n )
    for i in s :
        print ( i )

Index/key variables: i, j, m, n
Accumulator variables: s
Iterator variables: i, j, string
Python program:
def countIntegralSolutions ( n ) :
    result = 0
    for i in range ( n + 1 ) :
        for j in range ( n + 1 ) :
            for k in range ( n + 1 ) :
                if i + j + k == n :
                    result += 1
    return result
n = 3
print ( countIntegralSolutions ( n ) )

Accumulator variables: result
Iterator variables: i, j, k
Python program:
def maxLeftSubArraySum ( a , size , sum ) :
    max_so_far = a [ 0 ]
    curr_max = a [ 0 ]
    sum [ 0 ] = max_so_far
    for i in range ( 1 , size ) :
        curr_max = max ( a [ i ] , curr_max + a [ i ] )
        max_so_far = max ( max_so_far , curr_max )
        sum [ i ] = max_so_far
    return max_so_far
def maxRightSubArraySum ( a , n , sum ) :
    max_so_far = a [ n ]
    curr_max = a [ n ]
    sum [ n ] = max_so_far
    for i in range ( n - 1 , - 1 , - 1 ) :
        curr_max = max ( a [ i ] , curr_max + a [ i ] )
        max_so_far = max ( max_so_far , curr_max )
        sum [ i ] = max_so_far
    return max_so_far
def findMaxAbsDiff ( arr , n ) :
    leftMax = [ 0 for i in range ( n ) ]
    maxLeftSubArraySum ( arr , n , leftMax )
    rightMax = [ 0 for i in range ( n ) ]
    maxRightSubArraySum ( arr , n - 1 , rightMax )
    invertArr = [ 0 for i in range ( n ) ]
    for i in range ( n ) :
        invertArr [ i ] = - arr [ i ]
    leftMin = [ 0 for i in range ( n ) ]
    maxLeftSubArraySum ( invertArr , n , leftMin )
    for i in range ( n ) :
        leftMin [ i ] = - leftMin [ i ]
    rightMin = [ 0 for i in range ( n ) ]
    maxRightSubArraySum ( invertArr , n - 1 , rightMin )
    for i in range ( n ) :
        rightMin [ i ] = - rightMin [ i ]
    result = - 2147483648
    for i in range ( n - 1 ) :
        absValue = max ( abs ( leftMax [ i ] - rightMin [ i + 1 ] ) , abs ( leftMin [ i ] - rightMax [ i + 1 ] ) )
        if ( absValue > result ) :
            result = absValue
    return result
a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ]
n = len ( a )
print ( findMaxAbsDiff ( a , n ) )

Index/key variables: i, n
Iterator variables: i
Python program:
def findMinRec ( arr , i , sumCalculated , sumTotal ) :
    if ( i == 0 ) :
        return abs ( ( sumTotal - sumCalculated ) - sumCalculated )
    return min ( findMinRec ( arr , i - 1 , sumCalculated + arr [ i - 1 ] , sumTotal ) , findMinRec ( arr , i - 1 , sumCalculated , sumTotal ) )
def findMin ( arr , n ) :
    sumTotal = 0
    for i in range ( n ) :
        sumTotal += arr [ i ]
    return findMinRec ( arr , n , 0 , sumTotal )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 1 , 4 , 2 , 2 , 1 ]
    n = len ( arr )
    print ( " The ▁ minimum ▁ difference ▁ " + " between ▁ two ▁ sets ▁ is ▁ " , findMin ( arr , n ) )

Index/key variables: i
Accumulator variables: sumTotal
Iterator variables: i
Python program:
def countP ( n , k ) :
    dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        dp [ i ] [ 0 ] = 0
    for i in range ( k + 1 ) :
        dp [ 0 ] [ k ] = 0
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , k + 1 ) :
            if ( j == 1 or i == j ) :
                dp [ i ] [ j ] = 1
            else :
                dp [ i ] [ j ] = ( j * dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] )
    return dp [ n ] [ k ]
if __name__ == ' _ _ main _ _ ' :
    print ( countP ( 5 , 2 ) )

Index/key variables: i, j, k, n
Iterator variables: i, j
Python program:
def prCountDP ( dist ) :
    ways = [ 0 ] * 3
    n = dist
    ways [ 0 ] = 1
    ways [ 1 ] = 1
    ways [ 2 ] = 2
    for i in range ( 3 , n + 1 ) :
        ways [ i % 3 ] = ways [ ( i - 1 ) % 3 ] + ways [ ( i - 2 ) % 3 ] + ways [ ( i - 3 ) % 3 ]
    return ways [ n % 3 ]
dist = 4
print ( prCountDP ( dist ) )

Iterator variables: i
Python program:
def countNumbersWith4 ( n ) :
    for x in range ( 1 , n + 1 ) :
        if ( has4 ( x ) == True ) :
            result = result + 1
    return result
def has4 ( x ) :
    while ( x != 0 ) :
        if ( x % 10 == 4 ) :
            return True
        x = x // 10
    return False
n = 328
print ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " , n , " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " , countNumbersWith4 ( n ) )

Iterator variables: x
Python program:
import math as mt
def countNumbersWith4 ( n ) :
    if ( n < 4 ) :
        return 0
    d = int ( mt . log10 ( n ) )
    a = [ 1 for i in range ( d + 1 ) ]
    a [ 0 ] = 0
    if len ( a ) > 1 :
        a [ 1 ] = 1
    for i in range ( 2 , d + 1 ) :
        a [ i ] = a [ i - 1 ] * 9 + mt . ceil ( pow ( 10 , i - 1 ) )
    p = mt . ceil ( pow ( 10 , d ) )
    msd = n // p
    if ( msd == 4 ) :
        return ( msd ) * a [ d ] + ( n % p ) + 1
    if ( msd > 4 ) :
        return ( ( msd - 1 ) * a [ d ] + p + countNumbersWith4 ( n % p ) )
    return ( msd ) * a [ d ] + countNumbersWith4 ( n % p )
n = 328
print ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to " , n , " that ▁ have ▁ 4 ▁ as ▁ a ▁ digit ▁ is " , countNumbersWith4 ( n ) )

Index/key variables: d, i
Iterator variables: i
Python program:
import sys ;
def minRemovalsDP ( arr , n ) :
    longest_start = - 1 ;
    longest_end = 0 ;
    for start in range ( n ) :
        min = sys . maxsize ;
        max = - sys . maxsize ;
        for end in range ( start , n ) :
            val = arr [ end ] ;
            if ( val < min ) :
                min = val ;
            if ( val > max ) :
                max = val ;
            if ( 2 * min <= max ) :
                break ;
            if ( end - start > longest_end - longest_start or longest_start == - 1 ) :
                longest_start = start ;
                longest_end = end ;
    if ( longest_start == - 1 ) :
        return n ;
    return ( n - ( longest_end - longest_start + 1 ) ) ;
arr = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] ;
n = len ( arr ) ;
print ( minRemovalsDP ( arr , n ) ) ;

Index/key variables: end
Iterator variables: end, start
Python program:
def arithematicThree ( set_ , n ) :
    for j in range ( n ) :
        i , k = j - 1 , j + 1
        while i > - 1 and k < n :
            if set_ [ i ] + set_ [ k ] == 2 * set_ [ j ] :
                return True
            elif set_ [ i ] + set_ [ k ] < 2 * set_ [ j ] :
                i -= 1
            else :
                k += 1
    return False

Index/key variables: i, j, k
Iterator variables: j
Python program:
def optimalStrategyOfGame ( arr , n ) :
    table = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
    for gap in range ( n ) :
        for j in range ( gap , n ) :
            i = j - gap
            x = 0
            if ( ( i + 2 ) <= j ) :
                x = table [ i + 2 ] [ j ]
            y = 0
            if ( ( i + 1 ) <= ( j - 1 ) ) :
                y = table [ i + 1 ] [ j - 1 ]
            z = 0
            if ( i <= ( j - 2 ) ) :
                z = table [ i ] [ j - 2 ]
            table [ i ] [ j ] = max ( arr [ i ] + min ( x , y ) , arr [ j ] + min ( y , z ) )
    return table [ 0 ] [ n - 1 ]
arr1 = [ 8 , 15 , 3 , 7 ]
n = len ( arr1 )
print ( optimalStrategyOfGame ( arr1 , n ) )
arr2 = [ 2 , 2 , 2 , 2 ]
n = len ( arr2 )
print ( optimalStrategyOfGame ( arr2 , n ) )
arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ]
n = len ( arr3 )
print ( optimalStrategyOfGame ( arr3 , n ) )

Index/key variables: i, j
Iterator variables: gap, j
Python program:
def maxSumIS ( arr , n ) :
    max = 0
    msis = [ 0 for x in range ( n ) ]
    for i in range ( n ) :
        msis [ i ] = arr [ i ]
    for i in range ( 1 , n ) :
        for j in range ( i ) :
            if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) :
                msis [ i ] = msis [ j ] + arr [ i ]
    for i in range ( n ) :
        if max < msis [ i ] :
            max = msis [ i ]
    return max
arr = [ 1 , 101 , 2 , 3 , 100 , 4 , 5 ]
n = len ( arr )
print ( " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ " + " subsequence ▁ is ▁ " + str ( maxSumIS ( arr , n ) ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def checkArray ( arr , N , k ) :
    if ( N < 3 ) :
        return - 1
    Sum = 0
    temp = 0
    for i in range ( 0 , N ) :
        Sum += arr [ i ]
    R = ( k * k + k + 1 )
    if ( Sum % R != 0 ) :
        return 0
    Mid = k * ( Sum // R )
    for i in range ( 1 , N - 1 ) :
        temp += arr [ i - 1 ]
        if ( arr [ i ] == Mid ) :
            if ( temp == Mid // k ) :
                return i + 1
            else :
                return 0
    return 0
if __name__ == " _ _ main _ _ " :
    arr = [ 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 ]
    N = len ( arr )
    K = 2
    print ( checkArray ( arr , N , K ) )

Index/key variables: i
Accumulator variables: Sum, temp
Iterator variables: i
Python program:
def encodeString ( str ) :
    res = " "
    small = [ 0 for i in range ( 26 ) ]
    capital = [ 0 for i in range ( 26 ) ]
    num = [ 0 for i in range ( 10 ) ]
    n = len ( str )
    for i in range ( n ) :
        if ( str [ i ] >= '0' and str [ i ] <= '9' ) :
            num [ ord ( str [ i ] ) - 48 ] = i
        elif ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) :
            small [ ord ( str [ i ] ) - 97 ] = i
        elif ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) :
            capital [ ord ( str [ i ] ) - 65 ] = i
    for i in range ( n ) :
        if ( ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) and small [ ord ( str [ i ] ) - 97 ] == i ) :
            occ = ord ( str [ i ] ) - 96
            while ( occ > 0 ) :
                res += str [ i ]
                occ -= 1
        elif ( ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) and capital [ ord ( str [ i ] ) - 65 ] == i ) :
            occ = ord ( str [ i ] ) - 64
            while ( occ > 0 ) :
                res += str [ i ]
                occ -= 1
        elif ( ( str [ i ] >= '0' and str [ i ] <= '9' ) and num [ ord ( str [ i ] ) - 48 ] == i ) :
            occ = ord ( str [ i ] ) - 48
            while ( occ > 0 ) :
                res += str [ i ]
                occ -= 1
        else :
            res += str [ i ]
    print ( res )
if __name__ == ' _ _ main _ _ ' :
    str = " Ea2 , ▁ 0 , ▁ E "
    encodeString ( str )

Index/key variables: i
Accumulator variables: occ, res
Iterator variables: i
Python program:
def possibleEqualArray ( A , N ) :
    pref = [ 0 for i in range ( N ) ]
    pref [ 0 ] = A [ 0 ]
    for i in range ( 1 , N , 1 ) :
        pref [ i ] = pref [ i - 1 ] ^ A [ i ]
    if ( pref [ N - 1 ] == 0 ) :
        print ( " YES " )
        return
    cur_xor = 0
    i = N - 1
    while ( i >= 0 ) :
        cur_xor ^= A [ i ]
        for j in range ( i ) :
            if ( j ) :
                middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ]
                left_xor = pref [ j - 1 ]
                right_xor = cur_xor
                if ( left_xor == middle_xor and middle_xor == right_xor ) :
                    print ( " YES " )
                    return
        i -= 1
    print ( " NO " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 0 , 2 , 2 ]
    N = len ( A )
    possibleEqualArray ( A , N )

Index/key variables: i
Accumulator variables: cur_xor
Iterator variables: i, j
Python program:
def GenerateArray ( N , X ) :
    prev_xor = X
    for i in range ( 1 , N + 1 , 1 ) :
        print ( i ^ prev_xor , end = " " )
        if ( i != N ) :
            print ( " ▁ " , end = " " )
        prev_xor = i
if __name__ == ' _ _ main _ _ ' :
    N = 4
    X = 3
    print ( " The ▁ generated ▁ array ▁ is ▁ " , end = " " )
    GenerateArray ( N , X )

Iterator variables: i
Python program:
N = 200 * 1000 + 13
n = 0
arr = [ 0 for i in range ( N ) ]
brr = [ 0 for i in range ( N ) ]
import sys
def brute ( ind , l , r ) :
    if ( ind == n / 2 ) :
        for i in range ( n ) :
            print ( brr [ i ] , end = " ▁ " )
        sys . exit ( )
    for i in range ( l , arr [ ind ] // 2 + 1 , 1 ) :
        if ( arr [ ind ] - i <= r ) :
            brr [ ind ] = i
            brr [ n - ind - 1 ] = arr [ ind ] - i
            brute ( ind + 1 , i , arr [ ind ] - i )
if __name__ == ' _ _ main _ _ ' :
    n = 2
    n *= 2
    arr [ 0 ] = 5
    arr [ 1 ] = 6
    INF64 = 1000000000000000000
    brute ( 0 , 0 , INF64 )

Index/key variables: i, ind
Iterator variables: i
Python program:
import sys
def minCount ( N , K ) :
    SmallestNumber = [ 0 for i in range ( 10 ) ]
    MinimumSteps = [ 0 for i in range ( 10 ) ]
    for i in range ( 10 ) :
        SmallestNumber [ i ] = sys . maxsize ;
        MinimumSteps [ i ] = sys . maxsize
    for i in range ( 1 , 11 , 1 ) :
        num = K * i
        SmallestNumber [ num % 10 ] = min ( SmallestNumber [ num % 10 ] , num )
        MinimumSteps [ num % 10 ] = min ( MinimumSteps [ num % 10 ] , i )
    if ( N < SmallestNumber [ N % 10 ] ) :
        return - 1
    else :
        return MinimumSteps [ N % 10 ]
if __name__ == ' _ _ main _ _ ' :
    N = 42
    K = 7
    print ( minCount ( N , K ) )

Index/key variables: i
Iterator variables: i
Python program:
def countMinSteps ( arr , N ) :
    val = 0
    mx = - 10 ** 9
    for i in range ( N ) :
        curr = arr [ i ]
        mx = max ( mx , curr )
        val = max ( val , mx - curr )
    res = 0
    while ( ( 1 << res ) - 1 < val ) :
        res += 1
    return res
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 7 , 6 , 5 ]
    N = len ( arr )
    print ( countMinSteps ( arr , N ) )

Index/key variables: i
Accumulator variables: res
Iterator variables: i
Python program:
def setBits ( n ) :
    count = 0
    while ( n > 0 ) :
        n = n & ( n - 1 )
        count += 1
    return count
def findNthTerm ( N ) :
    x = setBits ( N ^ ( N - 1 ) )
    print ( x )
N = 8
findNthTerm ( N )

Accumulator variables: count
Python program:
def findMinMoves ( arr , N ) :
    sum = 0
    for i in range ( N ) :
        sum += arr [ i ]
    if ( sum % N != 0 ) :
        return - 1
    avg = sum // N
    total = 0
    needCount = 0
    for i in range ( N ) :
        needCount += ( arr [ i ] - avg )
        total = max ( max ( abs ( needCount ) , arr [ i ] - avg ) , total )
    return total
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 0 , 5 ]
    N = len ( arr )
    print ( findMinMoves ( arr , N ) )

Index/key variables: i
Accumulator variables: needCount, sum
Iterator variables: i
Python program:
def find ( N , Arr ) :
    amount = 0
    mid_odd = [ ]
    for i in range ( N ) :
        siz = len ( Arr [ i ] )
        for j in range ( 0 , siz // 2 ) :
            amount = amount + Arr [ i ] [ j ]
        if ( siz % 2 == 1 ) :
            mid_odd . append ( Arr [ i ] [ siz // 2 ] )
    mid_odd . sort ( reverse = True )
    for i in range ( len ( mid_odd ) ) :
        if i % 2 == 0 :
            amount = amount + mid_odd [ i ]
    print ( amount )
N = 2
Arr = [ [ 5 , 2 , 3 , 4 ] , [ 1 , 6 ] ]
find ( N , Arr )

Index/key variables: i, j
Accumulator variables: mid_odd
Iterator variables: i, j
Python program:
def checkPattern ( arr , m , k , n ) :
    count = 1
    t = 0
    for i in range ( n - m ) :
        if ( arr [ i ] == arr [ i + m ] ) :
            t += 1
            if ( t == m ) :
                t = 0
                count += 1
                if ( count == k ) :
                    return " Yes "
        else :
            t = 0
            count = 1
    return " No "
if __name__ == ' _ _ main _ _ ' :
    arr = [ 6 , 1 , 3 , 3 , 3 , 3 ]
    M = 1
    K = 3
    N = len ( arr )
    print ( checkPattern ( arr , M , K , N ) )

Index/key variables: i
Accumulator variables: count, t
Iterator variables: i
Python program:
def getZarr ( st , Z ) :
    n = len ( st )
    k = 0
    L = 0
    R = 0
    for i in range ( 1 , n ) :
        if ( i > R ) :
            L = R = i
            while ( R < n and st [ R - L ] == st [ R ] ) :
                R += 1
            Z [ i ] = R - L
            R -= 1
        else :
            k = i - L
            if ( Z [ k ] < R - i + 1 ) :
                Z [ i ] = Z [ k ]
            else :
                L = i
                while ( R < n and st [ R - L ] == st [ R ] ) :
                    R += 1
                Z [ i ] = R - L
                R -= 1
def goodStr ( st , word ) :
    concat = word + " $ " + st
    l = len ( concat )
    Z = [ 0 ] * l
    getZarr ( concat , Z )
    res = " "
    pSize = len ( word )
    for i in range ( l ) :
        if ( i + pSize < l - 1 and Z [ i + pSize + 1 ] == pSize ) :
            i += pSize - 1
        elif ( i < len ( st ) ) :
            res += st [ i ]
    return res
if __name__ == " _ _ main _ _ " :
    st = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching "
    word = " km "
    print ( goodStr ( st , word ) )

Index/key variables: R, i, k
Accumulator variables: res
Iterator variables: i
Python program:
def findOccurrences ( S , T ) :
    n1 = len ( S )
    n2 = len ( T )
    ans = 0
    last = 0
    for i in range ( n1 - n2 + 1 ) :
        chk = True
        for j in range ( n2 ) :
            if ( T [ j ] != S [ i + j ] ) :
                chk = False
                break
        if ( chk ) :
            ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) )
            last = i + 1
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    S , T = " dabc " , " ab "
    findOccurrences ( S , T )

Index/key variables: j
Accumulator variables: ans
Iterator variables: i, j
Boolean variables: chk
Python program:
def LargestArray ( N , low , high ) :
    high_index = N - ( high - low + 1 )
    if ( high_index > ( N - 1 ) // 2 ) :
        print ( " Not ▁ Possible " )
        return
    if ( high_index <= 0 ) :
        high_index = 1
    A = [ 0 ] * N
    temp = high
    for i in range ( high_index , - 1 , - 1 ) :
        A [ i ] = temp
        temp = temp - 1
    high -= 1
    for i in range ( high_index + 1 , N ) :
        A [ i ] = high
        high = high - 1
    for i in range ( N ) :
        print ( A [ i ] , end = " ▁ " )
N = 5
low = 2
high = 6
LargestArray ( N , low , high )

Index/key variables: i
Iterator variables: i