dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import math
def isPrime ( n ) :
    flag = 1
    i = 2
    while ( i * i <= n ) :
        if ( n % i == 0 ) :
            flag = 0
            break
        i += 1
    return ( True if flag == 1 else False )
def isPerfectSquare ( x ) :
    sr = math . sqrt ( x )
    return ( ( sr - math . floor ( sr ) ) == 0 )
def countInterestingPrimes ( n ) :
    answer = 0
    for i in range ( 2 , n ) :
        if ( isPrime ( i ) ) :
            j = 1
            while ( j * j * j * j <= i ) :
                if ( isPerfectSquare ( i - j * j * j * j ) ) :
                    answer += 1
                    break
                j += 1
    return answer
if __name__ == ' _ _ main _ _ ' :
    N = 10
    print ( countInterestingPrimes ( N ) )

Accumulator variables: answer, j
Iterator variables: i
Python program:
import math
def decBinary ( arr , n ) :
    k = int ( math . log2 ( n ) )
    while ( n > 0 ) :
        arr [ k ] = n % 2
        k = k - 1
        n = n // 2
def binaryDec ( arr , n ) :
    ans = 0
    for i in range ( 0 , n ) :
        ans = ans + ( arr [ i ] << ( n - i - 1 ) )
    return ans
def maxNum ( n , k ) :
    l = int ( math . log2 ( n ) ) + 1
    a = [ 0 for i in range ( 0 , l ) ]
    decBinary ( a , n )
    cn = 0
    for i in range ( 0 , l ) :
        if ( a [ i ] == 0 and cn < k ) :
            a [ i ] = 1
            cn = cn + 1
    return binaryDec ( a , l )
n = 4
k = 1
print ( maxNum ( n , k ) )

Index/key variables: i, k
Iterator variables: i
Python program:
def findSubSeq ( arr , n , sum ) :
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( sum < arr [ i ] ) :
            arr [ i ] = - 1 ;
        else :
            sum -= arr [ i ] ;
    for i in range ( n ) :
        if ( arr [ i ] != - 1 ) :
            print ( arr [ i ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 17 , 25 , 46 , 94 , 201 , 400 ] ;
    n = len ( arr ) ;
    sum = 272 ;
    findSubSeq ( arr , n , sum ) ;

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
MAX = 26
def maxAlpha ( str , len ) :
    first = [ - 1 for x in range ( MAX ) ]
    last = [ - 1 for x in range ( MAX ) ]
    for i in range ( 0 , len ) :
        index = ord ( str [ i ] ) - 97
        if ( first [ index ] == - 1 ) :
            first [ index ] = i
        last [ index ] = i
    ans = - 1
    maxVal = - 1
    for i in range ( 0 , MAX ) :
        if ( first [ i ] == - 1 ) :
            continue
        if ( ( last [ i ] - first [ i ] ) > maxVal ) :
            maxVal = last [ i ] - first [ i ] ;
            ans = i
    return chr ( ans + 97 )
str = " abbba "
len = len ( str )
print ( maxAlpha ( str , len ) )

Index/key variables: i, index
Iterator variables: i
Python program:
MAX = 100001 ;
def find_distinct ( a , n , q , queries ) :
    check = [ 0 ] * MAX ;
    idx = [ 0 ] * MAX ;
    cnt = 1 ;
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( check [ a [ i ] ] == 0 ) :
            idx [ i ] = cnt ;
            check [ a [ i ] ] = 1 ;
            cnt += 1 ;
        else :
            idx [ i ] = cnt - 1 ;
    for i in range ( 0 , q ) :
        m = queries [ i ] ;
        print ( idx [ m ] , end = " ▁ " ) ;
a = [ 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 ] ;
n = len ( a ) ;
queries = [ 0 , 3 , 5 , 7 ] ;
q = len ( queries ) ;
find_distinct ( a , n , q , queries ) ;

Index/key variables: i, m
Accumulator variables: cnt
Iterator variables: i
Python program:
MAX = 24 ;
def countOp ( x ) :
    arr = [ 0 ] * MAX ;
    arr [ 0 ] = 1 ;
    for i in range ( 1 , MAX ) :
        arr [ i ] = arr [ i - 1 ] * 2 ;
    temp = x ;
    flag = True ;
    ans = 0 ;
    operations = 0 ;
    flag2 = False ;
    for i in range ( MAX ) :
        if ( arr [ i ] - 1 == x ) :
            flag2 = True ;
        if ( arr [ i ] > x ) :
            ans = i ;
            break ;
    if ( flag2 ) :
        return 0 ;
    while ( flag ) :
        if ( arr [ ans ] < x ) :
            ans += 1 ;
        operations += 1 ;
        for i in range ( MAX ) :
            take = x ^ ( arr [ i ] - 1 ) ;
            if ( take <= arr [ ans ] - 1 ) :
                if ( take > temp ) :
                    temp = take ;
        if ( temp == arr [ ans ] - 1 ) :
            flag = False ;
            break ;
        temp += 1 ;
        operations += 1 ;
        x = temp ;
        if ( x == arr [ ans ] - 1 ) :
            flag = False ;
    return operations ;
if __name__ == " _ _ main _ _ " :
    x = 39 ;
    print ( countOp ( x ) ) ;

Index/key variables: ans, i
Accumulator variables: operations, temp
Iterator variables: i
Boolean variables: flag, flag2
Python program:
def minOperations ( arr , n ) :
    result = 0
    freq = [ 0 ] * 1000001
    for i in range ( 0 , n ) :
        freq [ arr [ i ] ] += 1
    maxi = max ( arr )
    for i in range ( 1 , maxi + 1 ) :
        if freq [ i ] != 0 :
            for j in range ( i * 2 , maxi + 1 , i ) :
                freq [ j ] = 0
            result += 1
    return result
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 4 , 2 , 4 , 4 , 4 ]
    n = len ( arr )
    print ( minOperations ( arr , n ) )

Index/key variables: i, j
Accumulator variables: result
Iterator variables: i, j
Python program:
from math import gcd
def minGCD ( arr , n ) :
    minGCD = 0 ;
    for i in range ( n ) :
        minGCD = gcd ( minGCD , arr [ i ] ) ;
    return minGCD ;
def minLCM ( arr , n ) :
    minLCM = arr [ 0 ] ;
    for i in range ( 1 , n ) :
        minLCM = min ( minLCM , arr [ i ] ) ;
    return minLCM ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 66 , 14 , 521 ] ;
    n = len ( arr ) ;
    print ( " LCM ▁ = ▁ " , minLCM ( arr , n ) , " , ▁ GCD ▁ = " , minGCD ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
import math
def formStringMinOperations ( ss ) :
    count = [ 0 ] * 3 ;
    s = list ( ss ) ;
    for i in range ( len ( s ) ) :
        count [ ord ( s [ i ] ) - ord ( '0' ) ] += 1 ;
    processed = [ 0 ] * 3 ;
    reqd = math . floor ( len ( s ) / 3 ) ;
    for i in range ( len ( s ) ) :
        if ( count [ ord ( s [ i ] ) - ord ( '0' ) ] == reqd ) :
            continue ;
        if ( s [ i ] == '0' and count [ 0 ] > reqd and processed [ 0 ] >= reqd ) :
            if ( count [ 1 ] < reqd ) :
                s [ i ] = '1' ;
                count [ 1 ] += 1 ;
                count [ 0 ] -= 1 ;
            elif ( count [ 2 ] < reqd ) :
                s [ i ] = '2' ;
                count [ 2 ] += 1 ;
                count [ 0 ] -= 1 ;
        if ( s [ i ] == '1' and count [ 1 ] > reqd ) :
            if ( count [ 0 ] < reqd ) :
                s [ i ] = '0' ;
                count [ 0 ] += 1 ;
                count [ 1 ] -= 1 ;
            elif ( count [ 2 ] < reqd and processed [ 1 ] >= reqd ) :
                s [ i ] = '2' ;
                count [ 2 ] += 1 ;
                count [ 1 ] -= 1 ;
        if ( s [ i ] == '2' and count [ 2 ] > reqd ) :
            if ( count [ 0 ] < reqd ) :
                s [ i ] = '0' ;
                count [ 0 ] += 1 ;
                count [ 2 ] -= 1 ;
            elif ( count [ 1 ] < reqd ) :
                s [ i ] = '1' ;
                count [ 1 ] += 1 ;
                count [ 2 ] -= 1 ;
        processed [ ord ( s [ i ] ) - ord ( '0' ) ] += 1 ;
    return ' ' . join ( s ) ;
s = "011200" ;
print ( formStringMinOperations ( s ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def findMinimumAdjacentSwaps ( arr , N ) :
    visited = [ False ] * ( N + 1 )
    minimumSwaps = 0
    for i in range ( 2 * N ) :
        if ( visited [ arr [ i ] ] == False ) :
            visited [ arr [ i ] ] = True
            count = 0
            for j in range ( i + 1 , 2 * N ) :
                if ( visited [ arr [ j ] ] == False ) :
                    count += 1
                elif ( arr [ i ] == arr [ j ] ) :
                    minimumSwaps += count
    return minimumSwaps
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 3 , 1 , 2 ]
    N = len ( arr )
    N //= 2
    print ( findMinimumAdjacentSwaps ( arr , N ) )

Index/key variables: i, j
Accumulator variables: count, minimumSwaps
Iterator variables: i, j
Python program:
from collections import defaultdict
def possibility ( m , length , s ) :
    countodd = 0
    for i in range ( 0 , length ) :
        if m [ int ( s [ i ] ) ] & 1 :
            countodd += 1
        if countodd > 1 :
            return False
    return True
def largestPalindrome ( s ) :
    l = len ( s )
    m = defaultdict ( lambda : 0 )
    for i in range ( 0 , l ) :
        m [ int ( s [ i ] ) ] += 1
    if possibility ( m , l , s ) == False :
        print ( " Palindrome ▁ cannot ▁ be ▁ formed " )
        return
    largest = [ None ] * l
    front = 0
    for i in range ( 9 , - 1 , - 1 ) :
        if m [ i ] & 1 :
            largest [ l // 2 ] = chr ( i + 48 )
            m [ i ] -= 1
            while m [ i ] > 0 :
                largest [ front ] = chr ( i + 48 )
                largest [ l - front - 1 ] = chr ( i + 48 )
                m [ i ] -= 2
                front += 1
        else :
            while m [ i ] > 0 :
                largest [ front ] = chr ( i + 48 )
                largest [ l - front - 1 ] = chr ( i + 48 )
                m [ i ] -= 2
                front += 1
    for i in range ( 0 , l ) :
        print ( largest [ i ] , end = " " )
if __name__ == " _ _ main _ _ " :
    s = "313551"
    largestPalindrome ( s )

Index/key variables: front, i
Accumulator variables: countodd
Iterator variables: i
Python program:
def minimumCostOfBreaking ( X , Y , m , n ) :
    res = 0
    X . sort ( reverse = True )
    Y . sort ( reverse = True )
    hzntl = 1 ; vert = 1
    i = 0 ; j = 0
    while ( i < m and j < n ) :
        if ( X [ i ] > Y [ j ] ) :
            res += X [ i ] * vert
            hzntl += 1
            i += 1
        else :
            res += Y [ j ] * hzntl
            vert += 1
            j += 1
    total = 0
    while ( i < m ) :
        total += X [ i ]
        i += 1
    res += total * vert
    total = 0
    while ( j < n ) :
        total += Y [ j ]
        j += 1
    res += total * hzntl
    return res
m = 6 ; n = 4
X = [ 2 , 1 , 3 , 1 , 4 ]
Y = [ 4 , 1 , 2 ]
print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) )

Index/key variables: i, j
Accumulator variables: hzntl, res, total, vert
Python program:
def getMin ( x , y , z ) :
    return min ( min ( x , y ) , z )
def editDistance ( str1 , str2 , m , n ) :
    dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ]
    for i in range ( 0 , m + 1 ) :
        for j in range ( 0 , n + 1 ) :
            if ( i == 0 ) :
                dp [ i ] [ j ] = j
            elif ( j == 0 ) :
                dp [ i ] [ j ] = i
            elif ( str1 [ i - 1 ] == str2 [ j - 1 ] ) :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ]
            else :
                dp [ i ] [ j ] = 1 + getMin ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] )
    return dp [ m ] [ n ]
def minimumSteps ( S , N ) :
    ans = 10 ** 10
    for i in range ( 1 , N ) :
        S1 = S [ : i ]
        S2 = S [ i : ]
        count = editDistance ( S1 , S2 , len ( S1 ) , len ( S2 ) )
        ans = min ( ans , count )
    print ( ans )
S = " aabb "
N = len ( S )
minimumSteps ( S , N )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
def minimumOperations ( N ) :
    dp = [ 0 for i in range ( N + 1 ) ]
    for i in range ( N + 1 ) :
        dp [ i ] = 1000000000
    dp [ 2 ] = 0
    for i in range ( 2 , N + 1 , 1 ) :
        if ( dp [ i ] == 1000000000 ) :
            continue
        if ( i * 5 <= N ) :
            dp [ i * 5 ] = min ( dp [ i * 5 ] , dp [ i ] + 1 )
        if ( i + 3 <= N ) :
            dp [ i + 3 ] = min ( dp [ i + 3 ] , dp [ i ] + 1 )
    if ( dp [ N ] == 1000000000 ) :
        return - 1
    return dp [ N ]
if __name__ == ' _ _ main _ _ ' :
    N = 25
    print ( minimumOperations ( N ) )

Index/key variables: N, i
Iterator variables: i
Python program:
def MaxProfit ( arr , n , transactionFee ) :
    buy = - arr [ 0 ]
    sell = 0
    for i in range ( 1 , n , 1 ) :
        temp = buy
        buy = max ( buy , sell - arr [ i ] )
        sell = max ( sell , temp + arr [ i ] - transactionFee )
    return max ( sell , buy )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 6 , 1 , 7 , 2 , 8 , 4 ]
    n = len ( arr )
    transactionFee = 2
    print ( MaxProfit ( arr , n , transactionFee ) )

Index/key variables: i
Iterator variables: i
Python program:
start = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ]
ending = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ]
def calculateStart ( n , m ) :
    for i in range ( 1 , m , 1 ) :
        start [ 0 ] [ i ] += start [ 0 ] [ i - 1 ]
    for i in range ( 1 , n , 1 ) :
        start [ i ] [ 0 ] += start [ i - 1 ] [ 0 ]
    for i in range ( 1 , n , 1 ) :
        for j in range ( 1 , m , 1 ) :
            start [ i ] [ j ] += max ( start [ i - 1 ] [ j ] , start [ i ] [ j - 1 ] )
def calculateEnd ( n , m ) :
    i = n - 2
    while ( i >= 0 ) :
        ending [ i ] [ m - 1 ] += ending [ i + 1 ] [ m - 1 ]
        i -= 1
    i = m - 2
    while ( i >= 0 ) :
        ending [ n - 1 ] [ i ] += ending [ n - 1 ] [ i + 1 ]
        i -= 1
    i = n - 2
    while ( i >= 0 ) :
        j = m - 2
        while ( j >= 0 ) :
            ending [ i ] [ j ] += max ( ending [ i + 1 ] [ j ] , ending [ i ] [ j + 1 ] )
            j -= 1
        i -= 1
def maximumPathSum ( mat , n , m , q , coordinates ) :
    for i in range ( n ) :
        for j in range ( m ) :
            start [ i ] [ j ] = mat [ i ] [ j ]
            ending [ i ] [ j ] = mat [ i ] [ j ]
    calculateStart ( n , m )
    calculateEnd ( n , m )
    ans = 0
    for i in range ( q ) :
        X = coordinates [ i ] [ 0 ] - 1
        Y = coordinates [ i ] [ 1 ] - 1
        ans = max ( ans , start [ X ] [ Y ] + ending [ X ] [ Y ] - mat [ X ] [ Y ] )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
    N = 3
    M = 3
    Q = 2
    coordinates = [ [ 1 , 2 ] , [ 2 , 2 ] ]
    maximumPathSum ( mat , N , M , Q , coordinates )

Index/key variables: X, Y, i, j
Iterator variables: i, j
Python program:
def MaxSubsetlength ( arr , A , B ) :
    dp = [ [ 0 for i in range ( B + 1 ) ] for i in range ( A + 1 ) ]
    for str in arr :
        zeros = str . count ( '0' )
        ones = str . count ( '1' )
        for i in range ( A , zeros - 1 , - 1 ) :
            for j in range ( B , ones - 1 , - 1 ) :
                dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - zeros ] [ j - ones ] + 1 )
    return dp [ A ] [ B ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ "1" , "0" , "0001" , "10" , "111001" ]
    A , B = 5 , 3
    print ( MaxSubsetlength ( arr , A , B ) )

Index/key variables: A, B, i, j
Iterator variables: i, j, str
Python program:
def numOfWays ( a , n , i = 0 , blue = [ ] ) :
    if i == n :
        return 1
    count = 0
    for j in range ( n ) :
        if mat [ i ] [ j ] == 1 and j not in blue :
            count += numOfWays ( mat , n , i + 1 , blue + [ j ] )
    return count
if __name__ == " _ _ main _ _ " :
    n = 3
    mat = [ [ 0 , 1 , 1 ] , [ 1 , 0 , 1 ] , [ 1 , 1 , 1 ] ]
    print ( numOfWays ( mat , n ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: j
Python program:
def minCost ( arr , n ) :
    if ( n < 3 ) :
        print ( arr [ 0 ] )
        return
    dp = [ 0 ] * n
    dp [ 0 ] = arr [ 0 ]
    dp [ 1 ] = dp [ 0 ] + arr [ 1 ] + arr [ 2 ]
    for i in range ( 2 , n - 1 ) :
        dp [ i ] = min ( dp [ i - 2 ] + arr [ i ] , dp [ i - 1 ] + arr [ i ] + arr [ i + 1 ] )
    dp [ n - 1 ] = min ( dp [ n - 2 ] , dp [ n - 3 ] + arr [ n - 1 ] )
    print ( dp [ n - 1 ] )
if __name__ == " _ _ main _ _ " :
    arr = [ 9 , 4 , 6 , 8 , 5 ]
    N = len ( arr )
    minCost ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
M = 1000000007
def power ( X , Y ) :
    res = 1
    X = X % M
    if ( X == 0 ) :
        return 0
    while ( Y > 0 ) :
        if ( Y & 1 ) :
            res = ( res * X ) % M
        Y = Y >> 1
        X = ( X * X ) % M
    return res
def findValue ( n ) :
    X = 0
    pow_10 = 1
    while ( n ) :
        if ( n & 1 ) :
            X += pow_10
        pow_10 *= 10
        n //= 2
    X = ( X * 2 ) % M
    res = power ( 2 , X )
    return res
if __name__ == " _ _ main _ _ " :
    n = 2
    print ( findValue ( n ) )

Accumulator variables: X, n, pow_10
Python program:
M = 1000000007 ;
def power ( X , Y ) :
    res = 1 ;
    X = X % M ;
    if ( X == 0 ) :
        return 0 ;
    while ( Y > 0 ) :
        if ( Y % 2 == 1 ) :
            res = ( res * X ) % M ;
        Y = Y >> 1 ;
        X = ( X * X ) % M ;
    return res ;
def findValue ( N ) :
    dp = [ 0 ] * ( N + 1 ) ;
    dp [ 1 ] = 2 ;
    dp [ 2 ] = 1024 ;
    for i in range ( 3 , N + 1 ) :
        y = ( i & ( - i ) ) ;
        x = i - y ;
        if ( x == 0 ) :
            dp [ i ] = power ( dp [ i // 2 ] , 10 ) ;
        else :
            dp [ i ] = ( dp [ x ] * dp [ y ] ) % M ;
    return ( dp [ N ] * dp [ N ] ) % M ;
if __name__ == ' _ _ main _ _ ' :
    n = 150 ;
    print ( findValue ( n ) ) ;

Index/key variables: N, i, x, y
Iterator variables: i
Python program:
def findWays ( N ) :
    if ( N == 0 ) :
        return 1
    cnt = 0
    for i in range ( 1 , 7 ) :
        if ( N - i >= 0 ) :
            cnt = cnt + findWays ( N - i )
    return cnt
if __name__ == ' _ _ main _ _ ' :
    N = 4
    print ( findWays ( N ) )

Iterator variables: i
Python program:
def checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) :
    if j == N :
        if sm1 == sm2 and sm2 == sm3 :
            return 1
        else :
            return 0
    else :
        l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 )
        m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 )
        r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 )
        return max ( l , m , r )
def checkEqualSum ( arr , N ) :
    sum1 = sum2 = sum3 = 0
    if checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 :
        print ( " Yes " )
    else :
        print ( " No " )
arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ]
N = len ( arr )
checkEqualSum ( arr , N )

Index/key variables: j
Python program:
dp = { }
def checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) :
    s = str ( sm1 ) + " _ " + str ( sm2 ) + str ( j )
    if j == N :
        if sm1 == sm2 and sm2 == sm3 :
            return 1
        else :
            return 0
    if s in dp :
        return dp [ s ]
    l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 )
    m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 )
    r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 )
    dp [ s ] = max ( l , m , r )
    return dp [ s ]
def checkEqualSum ( arr , N ) :
    sum1 = sum2 = sum3 = 0
    if checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 :
        print ( " Yes " )
    else :
        print ( " No " )
arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ]
N = len ( arr )
checkEqualSum ( arr , N )

Index/key variables: j, s
Python program:
def precompute ( nextpos , arr , N ) :
    nextpos [ N - 1 ] = N
    for i in range ( N - 2 , - 1 , - 1 ) :
        if arr [ i ] == arr [ i + 1 ] :
            nextpos [ i ] = nextpos [ i + 1 ]
        else :
            nextpos [ i ] = i + 1
def findIndex ( query , arr , N , Q ) :
    nextpos = [ 0 ] * N
    precompute ( nextpos , arr , N )
    for i in range ( Q ) :
        l = query [ i ] [ 0 ]
        r = query [ i ] [ 1 ]
        x = query [ i ] [ 2 ]
        ans = - 1
        if arr [ l ] != x :
            ans = l
        else :
            d = nextpos [ l ]
            if d <= r :
                ans = d
        print ( ans )
N = 6
Q = 3
arr = [ 1 , 2 , 1 , 1 , 3 , 5 ]
query = [ [ 0 , 3 , 1 ] , [ 1 , 5 , 2 ] , [ 2 , 3 , 1 ] ]
findIndex ( query , arr , N , Q )

Index/key variables: i, l
Iterator variables: i
Python program:
mod = 1000000007
def countWays ( s , t , k ) :
    n = len ( s )
    a = 0
    b = 0
    for i in range ( n ) :
        p = s [ i : n - i + 1 ] + s [ : i + 1 ]
        if ( p == t ) :
            a += 1
        else :
            b += 1
    dp1 = [ 0 ] * ( k + 1 )
    dp2 = [ 0 ] * ( k + 1 )
    if ( s == t ) :
        dp1 [ 0 ] = 1
        dp2 [ 0 ] = 0
    else :
        dp1 [ 0 ] = 0
        dp2 [ 0 ] = 1
    for i in range ( 1 , k + 1 ) :
        dp1 [ i ] = ( ( dp1 [ i - 1 ] * ( a - 1 ) ) % mod + ( dp2 [ i - 1 ] * a ) % mod ) % mod
        dp2 [ i ] = ( ( dp1 [ i - 1 ] * ( b ) ) % mod + ( dp2 [ i - 1 ] * ( b - 1 ) ) % mod ) % mod
    return ( dp1 [ k ] )
S = ' ab '
T = ' ab '
K = 2
print ( countWays ( S , T , K ) )

Index/key variables: i, k
Accumulator variables: a, b
Iterator variables: i
Python program:
def minOperation ( k ) :
    dp = [ 0 ] * ( k + 1 )
    for i in range ( 1 , k + 1 ) :
        dp [ i ] = dp [ i - 1 ] + 1
        if ( i % 2 == 0 ) :
            dp [ i ] = min ( dp [ i ] , dp [ i // 2 ] + 1 )
    return dp [ k ]
if __name__ == ' _ _ main _ _ ' :
    k = 12
    print ( minOperation ( k ) )

Index/key variables: i, k
Iterator variables: i
Python program:
def maxSum ( p0 , p1 , a , pos , n ) :
    if ( pos == n ) :
        if ( p0 == p1 ) :
            return p0 ;
        else :
            return 0 ;
    ans = maxSum ( p0 , p1 , a , pos + 1 , n ) ;
    ans = max ( ans , maxSum ( p0 + a [ pos ] , p1 , a , pos + 1 , n ) ) ;
    ans = max ( ans , maxSum ( p0 , p1 + a [ pos ] , a , pos + 1 , n ) ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    n = 4 ;
    a = [ 1 , 2 , 3 , 6 ] ;
    print ( maxSum ( 0 , 0 , a , 0 , n ) ) ;

Index/key variables: pos
Python program:
import numpy as np
import sys
INT_MIN = - ( sys . maxsize - 1 )
def maxSum ( a , n ) :
    sum = 0 ;
    for i in range ( n ) :
        sum += a [ i ] ;
    limit = 2 * sum + 1 ;
    dp = np . zeros ( ( n + 1 , limit ) ) ;
    for i in range ( n + 1 ) :
        for j in range ( limit ) :
            dp [ i ] [ j ] = INT_MIN ;
    dp [ 0 ] [ sum ] = 0 ;
    for i in range ( 1 , n + 1 ) :
        for j in range ( limit ) :
            if ( ( j - a [ i - 1 ] ) >= 0 and dp [ i - 1 ] [ j - a [ i - 1 ] ] != INT_MIN ) :
                dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - a [ i - 1 ] ] + a [ i - 1 ] ) ;
            if ( ( j + a [ i - 1 ] ) < limit and dp [ i - 1 ] [ j + a [ i - 1 ] ] != INT_MIN ) :
                dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j + a [ i - 1 ] ] ) ;
            if ( dp [ i - 1 ] [ j ] != INT_MIN ) :
                dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ;
    return dp [ n ] [ sum ] ;
if __name__ == " _ _ main _ _ " :
    n = 4 ;
    a = [ 1 , 2 , 3 , 6 ] ;
    print ( maxSum ( a , n ) ) ;

Index/key variables: i, j, n, sum
Iterator variables: i, j
Python program:
fib = [ 0 ] * 100005 ;
def computeFibonacci ( ) :
    fib [ 0 ] = 1 ;
    fib [ 1 ] = 1 ;
    for i in range ( 2 , 100005 ) :
        fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ;
def countString ( string ) :
    ans = 1 ;
    cnt = 1 ;
    for i in range ( 1 , len ( string ) ) :
        if ( string [ i ] == string [ i - 1 ] ) :
            cnt += 1 ;
        else :
            ans = ans * fib [ cnt ] ;
            cnt = 1 ;
    ans = ans * fib [ cnt ] ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    string = " abdllldefkkkk " ;
    computeFibonacci ( ) ;
    print ( countString ( string ) ) ;

Index/key variables: cnt, i
Iterator variables: i
Python program:
MAX = 100001
def printGolombSequence ( N ) :
    arr = [ 0 ] * MAX
    cnt = 0
    arr [ 0 ] = 0
    arr [ 1 ] = 1
    M = dict ( )
    M [ 2 ] = 2
    for i in range ( 2 , N + 1 ) :
        if ( cnt == 0 ) :
            arr [ i ] = 1 + arr [ i - 1 ]
            cnt = M [ arr [ i ] ]
            cnt -= 1
        else :
            arr [ i ] = arr [ i - 1 ]
            cnt -= 1
        M [ i ] = arr [ i ]
    for i in range ( 1 , N + 1 ) :
        print ( arr [ i ] , end = " ▁ " )
N = 11
printGolombSequence ( N )

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i