dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def maxEqualIdx ( arr , k ) :
    arr . sort ( )
    prefixSum = [ 0 ] * ( len ( arr ) + 1 )
    prefixSum [ 1 ] = arr [ 0 ]
    for i in range ( 1 , len ( prefixSum ) - 1 , 1 ) :
        prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ]
    max = len ( arr )
    min = 1
    ans = 1
    while ( min <= max ) :
        mid = ( max + min ) // 2
        if ( check ( prefixSum , mid , k , arr ) ) :
            ans = mid
            min = mid + 1
        else :
            max = mid - 1
    return ans
def check ( pSum , lenn , k , a ) :
    i = 0
    j = lenn
    while ( j <= len ( a ) ) :
        maxSize = a [ j - 1 ]
        totalNumbers = maxSize * lenn
        currNumbers = pSum [ j ] - pSum [ i ]
        if ( currNumbers + k >= totalNumbers ) :
            return True
        else :
            i += 1
            j += 1
    return False
arr = [ 1 , 1 , 1 ]
k = 7
print ( maxEqualIdx ( arr , k ) )

Index/key variables: i, j
Iterator variables: i
Python program:
def productSum ( arr ) :
    cntOne = 0
    N = len ( arr )
    for i in range ( N ) :
        if ( arr [ i ] == 1 ) :
            cntOne += 1
    return cntOne * ( cntOne - 1 ) // 2
arr = [ 0 , 1 , 1 , 0 , 1 ]
print ( productSum ( arr ) )

Index/key variables: i
Accumulator variables: cntOne
Iterator variables: i
Python program:
import sys
def findWays ( nums , S ) :
    sum = 0
    for i in range ( len ( nums ) ) :
        sum += nums [ i ]
    memo = [ [ - sys . maxsize - 1 for i in range ( 2 * sum + 1 ) ] for j in range ( len ( nums ) + 1 ) ]
    return dfs ( memo , nums , S , 0 , 0 , sum )
def dfs ( memo , nums , S , curr_sum , index , sum ) :
    if ( index == len ( nums ) ) :
        if ( S == curr_sum ) :
            return 1
        else :
            return 0
    if ( memo [ index ] [ curr_sum + sum ] != - sys . maxsize - 1 ) :
        return memo [ index ] [ curr_sum + sum ]
    ans = ( dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) )
    memo [ index ] [ curr_sum + sum ] = ans
    return ans
if __name__ == ' _ _ main _ _ ' :
    S = 3
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    answer = findWays ( arr , S )
    print ( answer )

Index/key variables: i, index
Accumulator variables: sum
Iterator variables: i
Python program:
def max_length ( N , x , v ) :
    preff , suff = [ ] , [ ]
    ct = 0
    for i in range ( N ) :
        a = v [ i ]
        if a % x == 0 :
            ct += 1
    if ct == N :
        print ( - 1 )
        return
    v . reverse ( )
    suff . append ( v [ 0 ] )
    for i in range ( 1 , N ) :
        suff . append ( v [ i ] + suff [ i - 1 ] )
    v . reverse ( )
    suff . reverse ( )
    preff . append ( v [ 0 ] )
    for i in range ( 1 , N ) :
        preff . append ( v [ i ] + preff [ i - 1 ] )
    ans = 0
    lp = 0
    rp = N - 1
    for i in range ( N ) :
        if suff [ i ] % x != 0 and ans < N - 1 :
            lp = i
            rp = N - 1
            ans = max ( ans , N - i )
        if preff [ i ] % x != 0 and ans < i + 1 :
            lp = 0
            rp = i
            ans = max ( ans , i + 1 )
    for i in range ( lp , rp + 1 ) :
        print ( v [ i ] , end = " ▁ " )
x = 3
v = [ 1 , 3 , 2 , 6 ]
N = len ( v )
max_length ( N , x , v )

Index/key variables: i
Accumulator variables: ct, preff, suff
Iterator variables: i
Python program:
def maximumUpdates ( arr ) :
    min = arr [ 0 ]
    max = arr [ 0 ]
    minCount , maxCount = 1 , 1
    for arr in arr :
        if arr > max :
            maxCount += 1
            max = arr
        if arr < min :
            minCount += 1 ;
            min = arr
    print ( " Number ▁ of ▁ times ▁ maximum ▁ " , end = " " )
    print ( " value ▁ updated ▁ = ▁ " , maxCount )
    print ( " Number ▁ of ▁ times ▁ minimum ▁ " , end = " " )
    print ( " value ▁ updated ▁ = ▁ " , minCount )
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 5 , 20 , 22 ]
    maximumUpdates ( arr )

Accumulator variables: maxCount, minCount
Iterator variables: arr
Python program:
def farthest_min ( a , n ) :
    suffix_min = [ 0 for i in range ( n ) ]
    suffix_min [ n - 1 ] = a [ n - 1 ]
    for i in range ( n - 2 , - 1 , - 1 ) :
        suffix_min [ i ] = min ( suffix_min [ i + 1 ] , a [ i ] )
    for i in range ( n ) :
        low = i + 1
        high = n - 1
        ans = - 1
        while ( low <= high ) :
            mid = ( low + high ) // 2
            if ( suffix_min [ mid ] < a [ i ] ) :
                ans = mid
                low = mid + 1
            else :
                high = mid - 1
        print ( ans , end = " ▁ " )
a = [ 3 , 1 , 5 , 2 , 4 ]
n = len ( a )
farthest_min ( a , n )

Index/key variables: i, mid
Iterator variables: i
Python program:
def printElements ( a , n ) :
    a . sort ( reverse = True )
    cnt = 1
    for i in range ( n - 1 ) :
        if ( a [ i ] != a [ i + 1 ] ) :
            print ( a [ i ] , " ▁ occurs ▁ " , cnt , " times " )
            cnt = 1
        else :
            cnt += 1
    print ( a [ n - 1 ] , " occurs " , cnt , " times " )
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ]
    n = len ( a )
    printElements ( a , n )

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i
Python program:
def checkSorted ( n , arr ) :
    b = [ ]
    for i in range ( n ) :
        b . append ( arr [ i ] )
    b . sort ( )
    ct = 0
    for i in range ( n ) :
        if arr [ i ] != b [ i ] :
            ct += 1
    if ct == 0 or ct == 2 :
        return True
    else :
        return False
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 5 , 3 , 4 , 2 ]
    n = len ( arr )
    if checkSorted ( n , arr ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: b, ct
Iterator variables: i
Python program:
def checkIndices ( Arr , N ) :
    for i in range ( N - 1 ) :
        for j in range ( i + 1 , N ) :
            if ( Arr [ i ] != Arr [ j ] ) :
                if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) :
                    return True
    return False
if __name__ == " _ _ main _ _ " :
    Arr = [ 3 , 2 , 1 , 1 , 4 ]
    N = len ( Arr )
    if checkIndices ( Arr , N ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findPairs ( arr , size ) :
    Map = { }
    for i in range ( 0 , size - 1 ) :
        for j in range ( i + 1 , size ) :
            Sum = arr [ i ] + arr [ j ]
            if Sum in Map :
                for pair in Map [ Sum ] :
                    m , n = pair
                    if ( ( m != i and m != j ) and ( n != i and n != j ) ) :
                        print ( " Pair ▁ First ▁ ( { } , ▁ { } ) " . format ( arr [ i ] , arr [ j ] ) )
                        print ( " Pair ▁ Second ▁ ( { } , ▁ { } ) " . format ( arr [ m ] , arr [ n ] ) )
                        return
            if Sum not in Map :
                Map [ Sum ] = [ ]
            Map [ Sum ] . append ( ( i , j ) )
    print ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " )
if __name__ == " _ _ main _ _ " :
    arr = [ 8 , 4 , 7 , 8 , 4 ]
    size = len ( arr )
    findPairs ( arr , size )

Index/key variables: Sum, i, j, m, n
Iterator variables: i, j, pair
Python program:
def minSum ( arr , n ) :
    sm = arr [ 0 ]
    for i in range ( 1 , n ) :
        if arr [ i ] == arr [ i - 1 ] :
            j = i
            while j < n and arr [ j ] <= arr [ j - 1 ] :
                arr [ j ] = arr [ j ] + 1
                j += 1
        sm = sm + arr [ i ]
    return sm
arr = [ 2 , 2 , 3 , 5 , 6 ]
n = len ( arr )
print ( minSum ( arr , n ) )

Index/key variables: i, j
Iterator variables: i
Python program:
def minSum ( arr , n ) :
    sum = arr [ 0 ] ; prev = arr [ 0 ]
    for i in range ( 1 , n ) :
        if arr [ i ] <= prev :
            prev = prev + 1
            sum = sum + prev
        else :
            sum = sum + arr [ i ]
            prev = arr [ i ]
    return sum
arr = [ 2 , 2 , 3 , 5 , 6 ]
n = len ( arr )
print ( minSum ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
import random
def getRandom ( x , y ) :
    tmp = ( x + random . randint ( 0 , 100000 ) % ( y - x + 1 ) )
    return tmp
def randomizedBinarySearch ( arr , l , r , x ) :
    if r >= l :
        mid = getRandom ( l , r )
        if arr [ mid ] == x :
            return mid
        if arr [ mid ] > x :
            return randomizedBinarySearch ( arr , l , mid - 1 , x )
        return randomizedBinarySearch ( arr , mid + 1 , r , x )
    return - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 10 , 40 ]
    n = len ( arr )
    x = 10
    result = randomizedBinarySearch ( arr , 0 , n - 1 , x )
    if result == - 1 :
        print ( ' Element ▁ is ▁ not ▁ present ▁ in ▁ array ' )
    else :
        print ( ' Element ▁ is ▁ present ▁ at ▁ index ▁ ' , result )

Index/key variables: mid
Python program:
def minCostToMakeElementEqual ( a ) :
    l = len ( a )
    if ( l % 2 == 1 ) :
        y = a [ l // 2 ]
    else :
        y = ( a [ l // 2 ] + a [ ( l - 2 ) // 2 ] ) // 2
    s = 0
    for i in range ( l ) :
        s += abs ( a [ i ] - y )
    return s
a = [ 1 , 100 , 101 ]
print ( minCostToMakeElementEqual ( a ) )

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
def upperBound ( arr , N , K ) :
    l = 0 ;
    r = N ;
    while ( l < r ) :
        mid = ( l + r ) // 2 ;
        if ( arr [ mid ] <= K ) :
            l = mid + 1 ;
        else :
            r = mid ;
    return l ;
def NDivKWithFreq ( arr , N , K ) :
    arr . sort ( ) ;
    i = 0 ;
    while ( i < N ) :
        X = upperBound ( arr , N , arr [ i ] ) ;
        if ( ( X - i ) > N // 4 ) :
            print ( arr [ i ] , end = " " ) ;
        i = X ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ;
    N = len ( arr ) ;
    K = 4 ;
    NDivKWithFreq ( arr , N , K ) ;

Index/key variables: i, mid
Python program:
def maxSum ( a , n ) :
    count = [ 0 ] * ( n + 1 )
    for i in range ( 0 , n ) :
        count [ min ( a [ i ] , n ) ] += 1
    size = 0
    ans = 0
    for k in range ( 1 , n + 1 ) :
        while ( count [ k ] > 0 and size < k ) :
            size += 1
            ans += size
            count [ k ] -= 1
        ans += k * count [ k ]
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 5 , 1 ]
    n = len ( arr )
    print ( maxSum ( arr , n ) )

Index/key variables: i, k
Accumulator variables: ans, size
Iterator variables: i, k
Python program:
def answerQueries ( arr , n , queries ) :
    for i in range ( len ( queries ) ) :
        ans = 0
        l = queries [ i ] [ 0 ] - 1
        r = queries [ i ] [ 1 ] - 1
        freq = dict ( )
        for j in range ( l , r + 1 ) :
            freq [ arr [ j ] ] = freq . get ( arr [ j ] , 0 ) + 1
        for i in freq :
            ans += ( i * freq [ i ] )
        print ( ans )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 1 ]
    n = len ( arr )
    queries = [ [ 1 , 2 ] , [ 1 , 3 ] ]
    answerQueries ( arr , n , queries )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
from typing import List
class Query :
    def __init__ ( self ) -> None :
        self . l = 0
        self . r = 0
        self . x = 0
        self . index = 0
class Arrays :
    def __init__ ( self ) -> None :
        self . val = 0
        self . index = 0
def update ( Fenwick : List [ int ] , index : int , val : int , n : int ) -> None :
    while ( index <= n ) :
        Fenwick [ index ] += val
        index += index & ( - index )
def query ( Fenwick : List [ int ] , index : int , n : int ) -> int :
    summ = 0
    while ( index > 0 ) :
        summ = summ + Fenwick [ index ]
        index -= index & ( - index )
    return summ
def maxLength ( n : int , v : List [ int ] ) -> int :
    where = [ 0 for _ in range ( n + 2 ) ]
    arr = [ Arrays ( ) for _ in range ( n ) ]
    for i in range ( 1 , n + 1 ) :
        v [ i - 1 ] = v [ i - 1 ] - 1
        x = v [ i - 1 ]
        where [ x ] = i - 1
        arr [ i - 1 ] . val = x
        arr [ i - 1 ] . index = i - 1
    if ( n <= 2 ) :
        print ( n )
        return 0
    left = n
    right = 0
    mx = 0
    queries = [ Query ( ) for _ in range ( 4 * n ) ]
    j = 0
    for i in range ( n - 1 , - 1 , - 1 ) :
        left = min ( left , where [ i ] )
        right = max ( right , where [ i ] )
        diff = right - left
        if ( diff == 0 or diff == 1 ) :
            continue
        val1 = v [ left ]
        val2 = v [ right ]
        minn = min ( val1 , val2 )
        queries [ j ] . l = left + 1
        queries [ j ] . r = right - 1
        queries [ j ] . x = minn
        queries [ j ] . index = j
        j += 1
    Fenwick = [ 0 for _ in range ( n + 1 ) ]
    q = j - 1
    arr [ : n + 1 ] . sort ( key = lambda x : x . val )
    queries [ : q + 1 ] . sort ( key = lambda val : val . x )
    curr = 0
    ans = [ 0 for _ in range ( q + 1 ) ]
    for i in range ( q + 1 ) :
        while ( arr [ curr ] . val <= queries [ i ] . x and curr < n ) :
            update ( Fenwick , arr [ curr ] . index + 1 , 1 , n )
            curr += 1
        ans [ queries [ i ] . index ] = query ( Fenwick , queries [ i ] . r + 1 , n ) - query ( Fenwick , queries [ i ] . l , n )
    for i in range ( q + 1 ) :
        mx = max ( mx , ans [ i ] )
    mx = mx + 2
    return mx
if __name__ == " _ _ main _ _ " :
    n = 6
    v = [ 4 , 2 , 6 , 5 , 3 , 1 ]
    print ( maxLength ( n , v ) )

Index/key variables: curr, i, index, int, j, left, right, x
Iterator variables: i
Class/struct names: Arrays, Query
Python program:
def PandigitalProduct_1_9 ( n ) :
    i = 1
    while i * i <= n :
        if ( ( n % i == 0 ) and bool ( isPandigital ( str ( n ) + str ( i ) + str ( n // i ) ) ) ) :
            return bool ( True )
        i += 1
    return bool ( False )
def isPandigital ( Str ) :
    if ( len ( Str ) != 9 ) :
        return bool ( False )
    ch = " " . join ( sorted ( Str ) )
    if ( ch == "123456789" ) :
        return bool ( True )
    else :
        return bool ( False )
n = 6952
if ( bool ( PandigitalProduct_1_9 ( n ) ) ) :
    print ( " yes " )
else :
    print ( " no " )

Accumulator variables: i
Python program:
def printModeMedian ( a , n ) :
    b = [ 0 ] * n
    Max = max ( a )
    t = Max + 1
    count = [ 0 ] * t
    for i in range ( n ) :
        count [ a [ i ] ] += 1
    mode = 0
    k = count [ 0 ]
    for i in range ( 1 , t ) :
        if ( count [ i ] > k ) :
            k = count [ i ]
            mode = i
    for i in range ( 1 , t ) :
        count [ i ] = count [ i ] + count [ i - 1 ]
    for i in range ( n ) :
        b [ count [ a [ i ] ] - 1 ] = a [ i ]
        count [ a [ i ] ] -= 1
    median = 0.0
    if ( n % 2 != 0 ) :
        median = b [ n // 2 ]
    else :
        median = ( ( b [ ( n - 1 ) // 2 ] + b [ n // 2 ] ) / 2.0 )
    print ( " median ▁ = " , median )
    print ( " mode ▁ = " , mode )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 ]
    n = len ( arr )
    printModeMedian ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
MAX = 26
def function ( st ) :
    global MAX
    l = len ( st )
    counter1 , counter2 = [ 0 ] * MAX , [ 0 ] * MAX
    for i in range ( l // 2 ) :
        counter1 [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1
    for i in range ( l // 2 , l ) :
        counter2 [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1
    for i in range ( MAX ) :
        if ( counter2 [ i ] != counter1 [ i ] ) :
            return True
    return False
st = " abcasdsabcae "
if function ( st ) : print ( " Yes , ▁ both ▁ halves ▁ differ ▁ " , " by ▁ at ▁ least ▁ one ▁ character " )
else : print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " )

Index/key variables: i
Iterator variables: i
Python program:
MAX = 26
def function ( st ) :
    global MAX
    l = len ( st )
    counter = [ 0 ] * MAX
    for i in range ( l // 2 ) :
        counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1
    for i in range ( l // 2 , l ) :
        counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] -= 1
    for i in range ( MAX ) :
        if ( counter [ i ] != 0 ) :
            return True
    return False
st = " abcasdsabcae "
if function ( st ) :
    print ( " Yes , ▁ both ▁ halves ▁ differ ▁ by ▁ at ▁ " , " least ▁ one ▁ character " )
else :
    print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " )

Index/key variables: i
Iterator variables: i
Python program:
def minDifferenceAmongMaxMin ( arr , N , K ) :
    arr . sort ( )
    res = 2147483647
    for i in range ( ( N - K ) + 1 ) :
        curSeqDiff = arr [ i + K - 1 ] - arr [ i ]
        res = min ( res , curSeqDiff )
    return res
arr = [ 10 , 20 , 30 , 100 , 101 , 102 ]
N = len ( arr )
K = 3
print ( minDifferenceAmongMaxMin ( arr , N , K ) )

Index/key variables: i
Iterator variables: i
Python program:
def possible ( x , S , N ) :
    minSum = ( x * ( x + 1 ) ) // 2
    maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) // 2
    if ( S < minSum or S > maxSum ) :
        return False
    return True
def findPermutation ( N , L , R , S ) :
    x = R - L + 1
    if ( not possible ( x , S , N ) ) :
        print ( " - 1" )
        return
    else :
        v = [ ]
        for i in range ( N , 0 , - 1 ) :
            if ( ( S - i ) >= 0 and possible ( x - 1 , S - i , i - 1 ) ) :
                S = S - i
                x -= 1
                v . append ( i )
            if ( S == 0 ) :
                break
        if ( S != 0 ) :
            print ( - 1 )
            return
        v1 = [ ]
        for i in range ( 1 , N + 1 ) :
            it = i in v
            if ( not it ) :
                v1 . append ( i )
        j = 0
        f = 0
        for i in range ( 1 , L ) :
            print ( v1 [ j ] , end = " ▁ " )
            j += 1
        for i in range ( L , R + 1 ) :
            print ( v [ f ] , end = " ▁ " )
            f += 1
        for i in range ( R + 1 , N + 1 ) :
            print ( v1 [ j ] , end = " ▁ " )
            j += 1
    return
if __name__ == " _ _ main _ _ " :
    N = 6
    L = 3
    R = 5
    S = 8
    findPermutation ( N , L , R , S )

Index/key variables: f, j
Accumulator variables: v, v1, x
Iterator variables: i
Python program:
def skipIndices ( N , T , arr ) :
    sum = 0
    count = { }
    for i in range ( N ) :
        d = sum + arr [ i ] - T
        k = 0
        if ( d > 0 ) :
            for u in list ( count . keys ( ) ) [ : : - 1 ] :
                j = u
                x = j * count [ j ]
                if ( d <= x ) :
                    k += ( d + j - 1 ) // j
                    break
                k += count [ j ]
                d -= x
        sum += arr [ i ]
        count [ arr [ i ] ] = count . get ( arr [ i ] , 0 ) + 1
        print ( k , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    N = 7
    T = 15
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
    skipIndices ( N , T , arr )

Index/key variables: i, j
Accumulator variables: d, k, sum
Iterator variables: i, u
Python program:
def maxNumPalindrome ( S ) :
    i = 0
    freq = [ 0 ] * 26
    freqPair = 0
    ln = len ( S ) // 3
    while ( i < len ( S ) ) :
        freq [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1
        i += 1
    for i in range ( 26 ) :
        freqPair += ( freq [ i ] // 2 )
    return min ( freqPair , ln )
if __name__ == ' _ _ main _ _ ' :
    S = " geeksforg "
    print ( maxNumPalindrome ( S ) )

Index/key variables: i
Accumulator variables: freqPair
Iterator variables: i
Python program:
import sys
def height ( s , adj , visited ) :
    visited [ s ] = 1
    h = 0
    for child in adj [ s ] :
        if ( visited [ child ] == 0 ) :
            h = max ( h , 1 + height ( child , adj , visited ) )
    return h
def minimumGroups ( adj , N ) :
    visited = [ 0 for i in range ( N + 1 ) ]
    groups = - sys . maxsize
    for i in range ( 1 , N + 1 ) :
        if ( visited [ i ] == 0 ) :
            comHeight = height ( i , adj , visited )
            groups = max ( groups , comHeight )
    return groups
def addEdge ( adj , u , v ) :
    adj [ u ] . append ( v )
    adj [ v ] . append ( u )
if __name__ == " _ _ main _ _ " :
    N = 5
    adj = [ [ ] for i in range ( N + 1 ) ]
    addEdge ( adj , 1 , 2 )
    addEdge ( adj , 3 , 2 )
    addEdge ( adj , 4 , 3 )
    print ( minimumGroups ( adj , N ) )

Index/key variables: child, i, s, u, v
Iterator variables: child, i
Python program:
c = [ ]
ans = 0
def subsetSum ( ) :
    global ans
    L = len ( c )
    mul = pow ( 2 , L - 1 )
    i = 0
    while ( i < len ( c ) ) :
        ans += c [ i ] * mul
        i += 1
def subsetGen ( arr , i , n ) :
    if ( i == n ) :
        subsetSum ( )
        return
    subsetGen ( arr , i + 1 , n )
    c . append ( arr [ i ] )
    subsetGen ( arr , i + 1 , n )
    c . pop ( )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 ]
    n = len ( arr )
    subsetGen ( arr , 0 , n )
    print ( ans )

Index/key variables: i
Accumulator variables: ans
Python program:
def getMaxValue ( arr , arr_size ) :
    if ( arr_size < 2 ) :
        return 0
    first = - sys . maxsize - 1
    second = - sys . maxsize - 1
    for i in range ( arr_size ) :
        if ( arr [ i ] > first ) :
            second = first
            first = arr [ i ]
        elif ( arr [ i ] > second and arr [ i ] != first ) :
            second = arr [ i ]
    if ( second == - sys . maxsize - 1 ) :
        return 0
    else :
        return second
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 5 , 1 , 8 ]
    n = len ( arr )
    print ( getMaxValue ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def check ( n , marks ) :
    x = max ( marks )
    bonus = 100 - x
    c = 0
    for i in range ( n ) :
        if ( marks [ i ] + bonus >= 50 ) :
            c += 1
    return c
n = 5
marks = [ 0 , 21 , 83 , 45 , 64 ]
print ( check ( n , marks ) )

Index/key variables: i
Accumulator variables: c
Iterator variables: i
Python program:
def find_sum ( n , k ) :
    total_sum = ( n * ( n + 1 ) ) // 2
    power = k
    while power <= n :
        total_sum -= power
        power *= k
    return total_sum
n = 11 ; k = 2
print ( find_sum ( n , k ) )

Accumulator variables: power, total_sum
Python program:
MAX = 10000
hashTable = [ 0 ] * MAX
def minOperations ( arr , n ) :
    arr . sort ( )
    for i in range ( n ) :
        hashTable [ arr [ i ] ] += 1
    res = 0
    for i in range ( n ) :
        if ( hashTable [ arr [ i ] ] ) :
            for j in range ( i , n ) :
                if ( arr [ j ] % arr [ i ] == 0 ) :
                    hashTable [ arr [ j ] ] = 0
            res += 1
    return res
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ]
    n = len ( arr )
    print ( minOperations ( arr , n ) )

Index/key variables: i, j
Accumulator variables: res
Iterator variables: i, j
Python program:
def ifPossible ( arr , n ) :
    cp = [ 0 ] * n
    cp = arr
    cp . sort ( )
    for i in range ( 0 , n ) :
        if ( not ( arr [ i ] == cp [ i ] ) and not ( arr [ n - 1 - i ] == cp [ i ] ) ) :
            return False
    return True
arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ]
n = len ( arr )
if ( ifPossible ( arr , n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i