dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def dfs ( g , disRev , visit , u ) :
    visit [ u ] = True
    totalRev = 0
    for i in range ( len ( g [ u ] ) ) :
        v = g [ u ] [ i ] [ 0 ]
        if ( not visit [ v ] ) :
            disRev [ v ] [ 0 ] = disRev [ u ] [ 0 ] + 1
            disRev [ v ] [ 1 ] = disRev [ u ] [ 1 ]
            if ( g [ u ] [ i ] [ 1 ] ) :
                disRev [ v ] [ 1 ] = disRev [ u ] [ 1 ] + 1
                totalRev += 1
            totalRev += dfs ( g , disRev , visit , v )
    return totalRev
def printMinEdgeReverseForRootNode ( edges , e ) :
    V = e + 1
    g = [ [ ] for i in range ( V ) ]
    disRev = [ [ 0 , 0 ] for i in range ( V ) ]
    visit = [ False for i in range ( V ) ]
    for i in range ( e ) :
        u = edges [ i ] [ 0 ]
        v = edges [ i ] [ 1 ]
        g [ u ] . append ( [ v , 0 ] )
        g [ v ] . append ( [ u , 1 ] )
    for i in range ( V ) :
        visit [ i ] = False
        disRev [ i ] [ 0 ] = disRev [ i ] [ 1 ] = 0
    root = 0
    totalRev = dfs ( g , disRev , visit , root )
    res = sys . maxsize
    for i in range ( V ) :
        edgesToRev = ( ( totalRev - disRev [ i ] [ 1 ] ) + ( disRev [ i ] [ 0 ] - disRev [ i ] [ 1 ] ) )
        if ( edgesToRev < res ) :
            res = edgesToRev
            root = i
    print ( root , res )
if __name__ == ' _ _ main _ _ ' :
    edges = [ [ 0 , 1 ] , [ 2 , 1 ] , [ 3 , 2 ] , [ 3 , 4 ] , [ 5 , 4 ] , [ 5 , 6 ] , [ 7 , 6 ] ]
    e = len ( edges )
    printMinEdgeReverseForRootNode ( edges , e )

Index/key variables: i, u, v
Accumulator variables: totalRev
Iterator variables: i
Python program:
def findMin ( arr , N , k ) :
    pairs = 0
    for i in range ( N ) :
        pairs += arr [ i ] / 2
    if ( k > pairs ) :
        return - 1
    else :
        return 2 * k + N - 1
arr = [ 4 , 5 , 6 ]
k = 3
print ( findMin ( arr , 3 , k ) ) ;

Index/key variables: i
Accumulator variables: pairs
Iterator variables: i
Python program:
def minimumFlips ( A , B , C ) :
    res = 0
    for i in range ( 32 ) :
        x , y , z = 0 , 0 , 0
        if ( A & ( 1 << i ) ) :
            x = 1
        if ( B & ( 1 << i ) ) :
            y = 1
        if ( C & ( 1 << i ) ) :
            z = 1
        if ( z == 0 ) :
            if ( x ) :
                res += 1
            if ( y ) :
                res += 1
        if ( z == 1 ) :
            if ( x == 0 and y == 0 ) :
                res += 1
    return res
if __name__ == ' _ _ main _ _ ' :
    A , B , C = 2 , 6 , 5
    print ( minimumFlips ( A , B , C ) )

Accumulator variables: res
Iterator variables: i
Python program:
prefixCount = [ [ 0 for x in range ( 32 ) ] for y in range ( 10000 ) ]
def findPrefixCount ( arr , size ) :
    for i in range ( 32 ) :
        prefixCount [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 )
        for j in range ( 1 , size ) :
            prefixCount [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 )
            prefixCount [ i ] [ j ] += prefixCount [ i ] [ j - 1 ]
def arrayBitwiseAND ( size ) :
    result = 0
    for i in range ( 32 ) :
        temp = prefixCount [ i ] [ size - 1 ]
        if ( temp == size ) :
            result = ( result | ( 1 << i ) )
    print ( result , end = " ▁ " )
def applyQuery ( currentVal , newVal , size ) :
    for i in range ( 32 ) :
        bit1 = ( ( currentVal >> i ) & 1 )
        bit2 = ( ( newVal >> i ) & 1 )
        if ( bit2 > 0 and bit1 == 0 ) :
            prefixCount [ i ] [ size - 1 ] += 1
        elif ( bit1 > 0 and bit2 == 0 ) :
            prefixCount [ i ] [ size - 1 ] -= 1
def findbitwiseAND ( queries , arr , N , M ) :
    findPrefixCount ( arr , N )
    for i in range ( M ) :
        id = queries [ i ] [ 0 ]
        newVal = queries [ i ] [ 1 ]
        currentVal = arr [ id ]
        arr [ id ] = newVal
        applyQuery ( currentVal , newVal , N )
        arrayBitwiseAND ( N )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    queries = [ [ 0 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] ]
    N = len ( arr )
    M = len ( queries )
    findbitwiseAND ( queries , arr , N , M )

Index/key variables: i, id, j
Iterator variables: i, j
Python program:
def evenXorSubarray ( arr , n ) :
    ans = 0
    for i in range ( n ) :
        XOR = 0
        for j in range ( i , n ) :
            XOR = XOR ^ arr [ j ]
            if ( ( XOR & 1 ) == 0 ) :
                ans += 1
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 ]
    N = len ( arr )
    evenXorSubarray ( arr , N )

Index/key variables: j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def findString ( S , N ) :
    strLen = 4 * N
    for i in range ( 1 , N + 1 ) :
        S [ strLen - 1 ] = '1'
        strLen -= 2
    for i in range ( 4 * N ) :
        print ( S [ i ] , end = " " )
N = 2
S = [ 0 ] * ( 4 * N )
for i in range ( 4 * N ) :
    S [ i ] = '0'
findString ( S , N )

Index/key variables: i
Accumulator variables: strLen
Iterator variables: i
Python program:
def isVowel ( ch ) :
    if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) :
        return True
    if ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) :
        return True
    return False
def containsEqualStrings ( S ) :
    totalVowels = 0
    for i in range ( len ( S ) ) :
        if ( isVowel ( S [ i ] ) ) :
            totalVowels += 1
    vowelsTillNow = 0
    for i in range ( len ( S ) ) :
        if ( isVowel ( S [ i ] ) ) :
            vowelsTillNow += 1
            totalVowels -= 1
            if ( vowelsTillNow == totalVowels ) :
                return " Yes "
    return " No "
if __name__ == " _ _ main _ _ " :
    S = " geeks "
    print ( containsEqualStrings ( S ) )

Index/key variables: i
Accumulator variables: totalVowels, vowelsTillNow
Iterator variables: i
Python program:
import sys
def getMaxSum ( A , M , N ) :
    sum = 0
    negative = 0
    minVal = sys . maxsize
    for i in range ( M ) :
        for j in range ( N ) :
            sum += abs ( A [ i ] [ j ] )
            if ( A [ i ] [ j ] < 0 ) :
                negative += 1
            minVal = min ( minVal , abs ( A [ i ] [ j ] ) )
    if ( negative % 2 ) :
        sum -= 2 * minVal
    print ( sum )
if __name__ == ' _ _ main _ _ ' :
    A = [ [ 4 , - 8 , 6 ] , [ 3 , 7 , 2 ] ]
    M = len ( A )
    N = len ( A [ 0 ] )
    getMaxSum ( A , M , N )

Index/key variables: i, j
Accumulator variables: negative, sum
Iterator variables: i, j
Python program:
def areaToPaint ( N , W , L , Heights ) :
    total = 0
    for i in range ( N ) :
        total += 2 * Heights [ i ] * W
    total += L * ( Heights [ 0 ] + Heights [ N - 1 ] )
    for i in range ( 1 , N ) :
        total += L * abs ( Heights [ i ] - Heights [ i - 1 ] )
    print ( total )
if __name__ == " _ _ main _ _ " :
    N = 7
    W = 1
    L = 1
    Heights = [ 4 , 3 , 1 , 2 , 3 , 4 , 2 ]
    areaToPaint ( N , W , L , Heights )

Index/key variables: i
Accumulator variables: total
Iterator variables: i
Python program:
MAX = 1000000
def maxSumOfDistinctPrimeFactors ( arr , N , K ) :
    CountDistinct = [ 0 ] * ( MAX + 1 )
    prime = [ False ] * ( MAX + 1 )
    for i in range ( MAX + 1 ) :
        CountDistinct [ i ] = 0
        prime [ i ] = True
    for i in range ( 2 , MAX + 1 ) :
        if ( prime [ i ] == True ) :
            CountDistinct [ i ] = 1
            for j in range ( i * 2 , MAX + 1 , i ) :
                CountDistinct [ j ] += 1
                prime [ j ] = False
    sum = 0
    PrimeFactor = [ 0 ] * 20
    for i in range ( N ) :
        PrimeFactor [ CountDistinct [ arr [ i ] ] ] += 1
    for i in range ( 19 , 0 , - 1 ) :
        while ( PrimeFactor [ i ] > 0 ) :
            sum += i
            PrimeFactor [ i ] -= 1
            K -= 1
            if ( K == 0 ) :
                break
        if ( K == 0 ) :
            break
    print ( sum )
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , 9 , 12 ]
    N = len ( arr )
    K = 2
    maxSumOfDistinctPrimeFactors ( arr , N , K )

Index/key variables: i, j
Accumulator variables: K, sum
Iterator variables: i, j
Python program:
def findMinKToMakeAllEqual ( N , A ) :
    B = { }
    for i in range ( N ) :
        B [ A [ i ] ] = 1
    M = len ( B )
    itr , i = list ( B . keys ( ) ) , 0
    if ( M > 3 ) :
        print ( " - 1" )
    elif ( M == 3 ) :
        B_1 , i = itr [ i ] , i + 1
        B_2 , i = itr [ i ] , i + 1
        B_3 , i = itr [ i ] , i + 1
        if ( B_2 - B_1 == B_3 - B_2 ) :
            print ( B_2 - B_1 )
        else :
            print ( " - 1" )
    elif ( M == 2 ) :
        B_1 , i = itr [ i ] , i + 1
        B_2 , i = itr [ i ] , i + 1
        if ( ( B_2 - B_1 ) % 2 == 0 ) :
            print ( ( B_2 - B_1 ) // 2 )
        else :
            print ( B_2 - B_1 )
    else :
        print ( 0 )
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 3 , 5 , 1 ]
    N = len ( A )
    findMinKToMakeAllEqual ( N , A )

Index/key variables: i
Iterator variables: i
Python program:
def computeLPSArray ( pat , M , lps ) :
    lenn = 0
    lps [ 0 ] = 0
    i = 1
    while ( i < M ) :
        if ( pat [ i ] == pat [ lenn ] ) :
            lenn += 1
            lps [ i ] = lenn
            i += 1
        else :
            if ( lenn != 0 ) :
                lenn = lps [ lenn - 1 ]
            else :
                lps [ i ] = 0
                i += 1
def KMPSearch ( pat , txt ) :
    M = len ( pat )
    N = len ( txt )
    lps = [ 0 for i in range ( M ) ]
    computeLPSArray ( pat , M , lps )
    i = 0
    j = 0
    while ( i < N ) :
        if ( pat [ j ] == txt [ i ] ) :
            j += 1
            i += 1
        if ( j == M ) :
            return 1
            j = lps [ j - 1 ]
        elif ( i < N and pat [ j ] != txt [ i ] ) :
            if ( j != 0 ) :
                j = lps [ j - 1 ]
            else :
                i = i + 1
    return 0
def maxRepeating ( seq , word ) :
    resCount = 0
    curWord = word
    numWords = len ( seq ) // len ( word )
    for i in range ( numWords ) :
        if ( KMPSearch ( curWord , seq ) ) :
            curWord += word
            resCount += 1
        else :
            break
    print ( resCount )
if __name__ == ' _ _ main _ _ ' :
    S1 , S2 = " ababc " , " ab "
    maxRepeating ( S1 , S2 )

Index/key variables: i, j, lenn
Accumulator variables: curWord, resCount
Iterator variables: i
Python program:
def findMinimumTime ( p , n , target ) :
    um = { }
    for i in range ( n ) :
        um [ p [ i ] ] = um . get ( p [ i ] , 0 ) + 1
    time = 0
    sum = 0
    while ( sum < target ) :
        sum = 0
        time += 1
        for it in um :
            sum += um [ it ] * ( time // it )
    print ( time )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 3 , 4 ]
    N = len ( arr )
    target = 10
    findMinimumTime ( arr , N , target )

Index/key variables: i, it
Accumulator variables: sum, time
Iterator variables: i, it
Python program:
MAXN = 100001
def constructMEX ( arr , N ) :
    hash = [ 0 ] * MAXN
    for i in range ( N ) :
        hash [ arr [ i ] ] = 1
    MexOfArr = 0
    for i in range ( 1 , MAXN ) :
        if ( hash [ i ] == 0 ) :
            MexOfArr = i
            break
    B = [ 0 ] * N
    for i in range ( N ) :
        if ( arr [ i ] < MexOfArr ) :
            B [ i ] = arr [ i ]
        else :
            B [ i ] = MexOfArr
    for i in range ( N ) :
        print ( B [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 1 , 5 , 3 ]
    N = len ( arr )
    constructMEX ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def smallestNumberLeftInPQ ( arr , N ) :
    pq = [ ]
    pairsArr = [ ]
    for i in range ( N ) :
        pq . append ( arr [ i ] )
    pq = sorted ( pq )
    while ( len ( pq ) > 1 ) :
        X = pq [ - 1 ]
        del pq [ - 1 ]
        Y = pq [ - 1 ]
        del pq [ - 1 ]
        pq . append ( ( X + Y + 1 ) // 2 )
        pairsArr . append ( [ X , Y ] )
        pq = sorted ( pq )
    print ( " { " , pq [ - 1 ] , " } , ▁ " , end = " " )
    sz = len ( pairsArr )
    for i in range ( sz ) :
        if ( i == 0 ) :
            print ( " { ▁ " , end = " " )
        print ( " ( " , pairsArr [ i ] [ 0 ] , " , " , pairsArr [ i ] [ 1 ] , " ) " , end = " " )
        if ( i != sz - 1 ) :
            print ( end = " , ▁ " )
        if ( i == sz - 1 ) :
            print ( end = " ▁ } " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 1 ]
    N = len ( arr )
    smallestNumberLeftInPQ ( arr , N )

Index/key variables: i
Accumulator variables: pairsArr, pq
Iterator variables: i
Python program:
def dfs ( residue , curStep , wt , arr , N , steps ) :
    if ( curStep >= steps ) :
        return True
    for i in range ( N ) :
        if ( arr [ i ] > residue and arr [ i ] != wt [ curStep - 1 ] ) :
            wt [ curStep ] = arr [ i ]
            if ( dfs ( arr [ i ] - residue , curStep + 1 , wt , arr , N , steps ) ) :
                return True
    return False
def printWeightsOnScale ( arr , N , steps ) :
    wt = [ 0 ] * ( steps )
    if ( dfs ( 0 , 0 , wt , arr , N , steps ) ) :
        for i in range ( steps ) :
            print ( wt [ i ] , end = " ▁ " )
    else :
        print ( " Not ▁ possible " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 5 , 6 ]
    N = len ( arr )
    steps = 10
    printWeightsOnScale ( arr , N , steps )

Index/key variables: curStep, i
Iterator variables: i
Python program:
import sys
def findSum ( S , n , x ) :
    table = [ [ 0 for x in range ( x + 1 ) ] for y in range ( n + 1 ) ]
    for i in range ( 1 , x + 1 ) :
        table [ 0 ] [ i ] = sys . maxsize - 1
    for i in range ( 1 , n + 1 ) :
        for j in range ( 1 , x + 1 ) :
            if ( S [ i - 1 ] > j ) :
                table [ i ] [ j ] = table [ i - 1 ] [ j ]
            else :
                table [ i ] [ j ] = min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 )
    if ( table [ n ] [ x ] > n ) :
        return - 1
    return table [ n ] [ x ]
def minRemovals ( arr , n , m , x ) :
    sum = 0
    for i in range ( n ) :
        sum += arr [ i ]
    requied_Sum = 0
    if ( sum % m < x ) :
        requied_Sum = m + sum % m - x
    else :
        requied_Sum = sum % m - x
    print ( findSum ( arr , n , requied_Sum ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 2 , 1 , 2 ]
    n = len ( arr )
    m = 4
    x = 2
    minRemovals ( arr , n , m , x % m )

Index/key variables: i, j, n, x
Accumulator variables: sum
Iterator variables: i, j
Python program:
def minChange ( s , n ) :
    L = [ 0 ] * 26 ;
    R = [ 0 ] * 26 ;
    for i in range ( 0 , n // 2 ) :
        ch = s [ i ] ;
        L [ ord ( ch ) - ord ( ' a ' ) ] += 1 ;
    for i in range ( n // 2 , n ) :
        ch = s [ i ] ;
        R [ ord ( ch ) - ord ( ' a ' ) ] += 1 ;
    count = n ;
    for ch in range ( ord ( ' a ' ) , ord ( ' z ' ) ) :
        count = min ( count , n - L [ ch - ord ( ' a ' ) ] - R [ ch - ord ( ' a ' ) ] ) ;
    change = n / 2 ;
    for d in range ( 0 , 25 ) :
        change -= L [ d ] ;
        change += R [ d ] ;
        count = min ( count , change ) ;
    change = n / 2 ;
    for d in range ( 0 , 25 ) :
        change -= R [ d ] ;
        change += L [ d ] ;
        count = min ( change , count ) ;
    return int ( count ) ;
if __name__ == ' _ _ main _ _ ' :
    S = " aababc " ;
    N = len ( S ) ;
    print ( minChange ( S , N ) ) ;

Index/key variables: d, i
Accumulator variables: change
Iterator variables: ch, d, i
Python program:
def longestPalinSub ( st ) :
    N = len ( st )
    hash1 = [ 0 ] * 256
    for i in range ( N ) :
        hash1 [ ord ( st [ i ] ) ] += 1
    res1 = " "
    res2 = " "
    for i in range ( 256 ) :
        for j in range ( hash1 [ i ] // 2 ) :
            res1 += chr ( i )
        for j in range ( ( hash1 [ i ] + 1 ) // 2 , hash1 [ i ] ) :
            res2 += chr ( i )
    p = list ( res2 )
    p . reverse ( )
    res2 = ' ' . join ( p )
    res3 = " "
    f = False
    for i in range ( 256 ) :
        if ( hash1 [ i ] % 2 ) :
            if ( not f ) :
                res1 += chr ( i )
                f = True
            else :
                res3 += chr ( i )
    return ( res1 + res2 + res3 )
if __name__ == " _ _ main _ _ " :
    st = " geeksforgeeks "
    print ( longestPalinSub ( st ) )

Index/key variables: i
Accumulator variables: res1, res2, res3
Iterator variables: i, j
Boolean variables: f
Python program:
from collections import defaultdict
def No_Of_subsequences ( mp ) :
    count = 0
    left = 0
    for x in mp :
        mp [ x ] += left
        count += ( mp [ x ] // x )
        left = mp [ x ] % x
    return count
def maximumsubsequences ( arr , n ) :
    mp = defaultdict ( int )
    for i in range ( n ) :
        mp [ arr [ i ] ] += 1
    print ( No_Of_subsequences ( mp ) )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 1 , 1 ]
    N = len ( arr )
    maximumsubsequences ( arr , N )

Index/key variables: i, x
Accumulator variables: count
Iterator variables: i, x
Python program:
from array import *
def check ( arr , pieces ) :
    m = { }
    for i in range ( 0 , len ( arr ) ) :
        m [ arr [ i ] ] = i + 1
    for i in range ( 0 , len ( pieces ) ) :
        if ( len ( pieces [ i ] ) == 1 and m [ pieces [ i ] [ 0 ] ] != 0 ) :
            continue
        elif ( len ( pieces [ i ] ) > 1 and m [ pieces [ i ] [ 0 ] ] != 0 ) :
            idx = m [ pieces [ i ] [ 0 ] ] - 1
            idx = idx + 1
            if idx >= len ( arr ) :
                return False
            for j in range ( 1 , len ( pieces [ i ] ) ) :
                if arr [ idx ] == pieces [ i ] [ j ] :
                    idx = idx + 1
                    if ( idx >= len ( arr ) and j < len ( pieces [ i ] ) - 1 ) :
                        return False
                else :
                    return False
        else :
            return False
    return True
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 4 , 3 ]
    pieces = [ [ 1 ] , [ 4 , 3 ] , [ 2 ] ]
    if check ( arr , pieces ) == True :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, idx, j
Iterator variables: i, j
Python program:
def makeArraySumEqual ( a , N ) :
    count_0 = 0
    count_1 = 0
    odd_sum = 0
    even_sum = 0
    for i in range ( N ) :
        if ( a [ i ] == 0 ) :
            count_0 += 1
        else :
            count_1 += 1
        if ( ( i + 1 ) % 2 == 0 ) :
            even_sum += a [ i ]
        elif ( ( i + 1 ) % 2 > 0 ) :
            odd_sum += a [ i ]
    if ( odd_sum == even_sum ) :
        for i in range ( N ) :
            print ( a [ i ] , end = " ▁ " )
    else :
        if ( count_0 >= N / 2 ) :
            for i in range ( count_0 ) :
                print ( "0" , end = " ▁ " )
        else :
            is_Odd = count_1 % 2
            count_1 -= is_Odd
            for i in range ( count_1 ) :
                print ( "1" , end = " ▁ " )
arr = [ 1 , 1 , 1 , 0 ]
N = len ( arr )
makeArraySumEqual ( arr , N )

Index/key variables: i
Accumulator variables: count_0, count_1, even_sum, odd_sum
Iterator variables: i
Python program:
maxm = 10001 ;
prime = [ True ] * ( maxm + 1 ) ;
prime_number = [ 0 ] * ( maxm + 1 ) ;
def SieveOfEratosthenes ( ) :
    for p in range ( 2 , ( int ( maxm ** 1 / 2 ) ) ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * p , maxm , p ) :
                prime [ i ] = False ;
    prime [ 0 ] = False ;
    prime [ 1 ] = False ;
def num_prime ( ) :
    prime_number [ 0 ] = 0 ;
    for i in range ( 1 , maxm + 1 ) :
        tmp = - 1 ;
        if ( prime [ i ] == True ) :
            tmp = 1 ;
        else :
            tmp = 0 ;
        prime_number [ i ] = prime_number [ i - 1 ] + tmp ;
def min_steps ( arr , n ) :
    SieveOfEratosthenes ( ) ;
    num_prime ( ) ;
    if ( n == 1 ) :
        print ( "0" ) ;
    elif ( n == 2 ) :
        print ( "1" ) ;
    else :
        print ( prime_number [ n ] - 1 + ( n - 2 ) ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 4 , 3 , 2 , 1 ] ;
    N = len ( arr ) ;
    min_steps ( arr , N ) ;

Index/key variables: i, n, p
Iterator variables: i, p
Python program:
def findWinner ( a , n ) :
    win = 0
    if ( n % 2 == 0 ) :
        win = 1
    else :
        win = 0
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( i % 2 == 1 ) :
            if ( win == 0 and a [ i ] > 1 ) :
                win = 1
        else :
            if ( win == 1 and a [ i ] > 1 ) :
                win = 0
    if ( win == 0 ) :
        print ( " A " )
    else :
        print ( " B " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 1 , 1 , 2 ]
    N = len ( arr )
    findWinner ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def isReachable ( x1 , y1 , x2 , y2 ) :
    while ( x2 > x1 and y2 > y1 ) :
        if ( x2 > y2 ) :
            x2 %= y2
        else :
            y2 %= x2
    if ( x2 == x1 ) :
        return ( y2 - y1 ) >= 0 and ( y2 - y1 ) % x1 == 0
    elif ( y2 == y1 ) :
        return ( x2 - x1 ) >= 0 and ( x2 - x1 ) % y1 == 0
    else :
        return 0
source_x = 2
source_y = 10
dest_x = 26
dest_y = 12
if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Accumulator variables: x2, y2
Python program:
from collections import defaultdict
isprime = [ True ] * 2000010
def SieveOfEratosthenes ( n ) :
    isprime [ 1 ] = False
    p = 2
    while ( p * p <= n ) :
        if ( isprime [ p ] == True ) :
            for i in range ( p * p , n + 1 , p ) :
                isprime [ i ] = False
        p += 1
def KDistinctPrime ( arr , n , k ) :
    SieveOfEratosthenes ( 2000000 )
    cnt = defaultdict ( lambda : 0 )
    result = - 1
    j = - 1
    for i in range ( n ) :
        x = arr [ i ]
        if ( isprime [ x ] ) :
            cnt [ x ] += 1
            if ( cnt [ x ] == 1 ) :
                k -= 1
    while ( k < 0 ) :
        j += 1
        x = arr [ j ]
        if ( isprime [ x ] ) :
            cnt [ x ] -= 1
            if ( cnt [ x ] == 0 ) :
                k += 1
        if ( k == 0 ) :
            result = max ( result , i - j )
    return result
arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
K = 3
N = len ( arr )
print ( KDistinctPrime ( arr , N , K ) )

Index/key variables: i, j, p, x
Accumulator variables: k
Iterator variables: i
Python program:
MAX_SIZE = 1000005
def SieveOfEratosthenes ( StorePrimes ) :
    IsPrime = [ True for i in range ( MAX_SIZE ) ]
    p = 2
    while ( p * p < MAX_SIZE ) :
        if ( IsPrime [ p ] == True ) :
            for i in range ( p * p , MAX_SIZE , p ) :
                IsPrime [ i ] = False
        p += 1
    for p in range ( 2 , MAX_SIZE ) :
        if ( IsPrime [ p ] ) :
            StorePrimes . append ( p )
def Smallest_non_Prime ( StorePrimes , N ) :
    x = StorePrimes [ N ]
    return x * x
if __name__ == ' _ _ main _ _ ' :
    N = 3
    StorePrimes = [ ]
    SieveOfEratosthenes ( StorePrimes )
    print ( Smallest_non_Prime ( StorePrimes , N ) )

Index/key variables: N, i, p
Accumulator variables: StorePrimes
Iterator variables: i, p
Python program:
def getLeftmostBit ( n ) :
    m = 0 ;
    while ( n > 1 ) :
        n = n >> 1 ;
        m += 1 ;
    return m ;
def getNextLeftmostBit ( n , m ) :
    temp = 1 << m ;
    while ( n < temp ) :
        temp = temp >> 1 ;
        m -= 1 ;
    return m ;
def countSetBit ( n ) :
    m = getLeftmostBit ( n ) ;
    return _countSetBit ( n , m ) ;
def _countSetBit ( n , m ) :
    if ( n == 0 ) :
        return 0 ;
    m = getNextLeftmostBit ( n , m ) ;
    if ( n == int ( 1 << ( m + 1 ) ) - 1 ) :
        return int ( m + 1 ) * ( 1 << m ) ;
    n = n - ( 1 << m ) ;
    return ( ( n + 1 ) + countSetBit ( n ) + m * ( 1 << ( m - 1 ) ) ) ;
def countSetBits ( L , R ) :
    return abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ;
if __name__ == ' _ _ main _ _ ' :
    L = 3 ;
    R = 5 ;
    print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " , countSetBits ( L , R ) ) ;

Accumulator variables: m