dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def Minimum_Cells ( v ) :
    col = [ 0 ] * 3
    for i in range ( 3 ) :
        column_number = v [ i ] [ 1 ]
        col [ i ] = column_number
    col . sort ( )
    v . sort ( )
    MidRow = v [ 1 ] [ 0 ]
    s = set ( )
    Maxcol = col [ 2 ]
    MinCol = col [ 0 ]
    for i in range ( MinCol , int ( Maxcol ) + 1 ) :
        s . add ( ( MidRow , i ) )
    for i in range ( 3 ) :
        if ( v [ i ] [ 0 ] == MidRow ) :
            continue ;
        for j in range ( min ( v [ i ] [ 0 ] , MidRow ) , max ( v [ i ] [ 0 ] , MidRow ) + 1 ) :
            s . add ( ( j , v [ i ] [ 1 ] ) ) ;
    return len ( s )
if __name__ == " _ _ main _ _ " :
    v = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 2 , 2 ) ]
    print ( Minimum_Cells ( v ) )

Index/key variables: i
Accumulator variables: s
Iterator variables: i, j
Python program:
def sortWithSetbits ( arr , n , k ) :
    v1 = [ ]
    v2 = [ ]
    for i in range ( 0 , n , 1 ) :
        if ( bin ( arr [ i ] ) . count ( '1' ) == k ) :
            v1 . append ( i )
            v2 . append ( arr [ i ] )
    v2 . sort ( reverse = False )
    for i in range ( 0 , len ( v1 ) , 1 ) :
        arr [ v1 [ i ] ] = v2 [ i ]
    for i in range ( 0 , n , 1 ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 14 , 255 , 1 , 7 , 13 ]
    n = len ( arr )
    k = 3
    sortWithSetbits ( arr , n , k )

Index/key variables: i
Accumulator variables: v1, v2
Iterator variables: i
Python program:
def numBoxex ( A , n , K ) :
    A . sort ( )
    i = 0
    j = n - 1
    ans = 0
    while i <= j :
        ans += 1
        if A [ i ] + A [ j ] <= K :
            i += 1
        j -= 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    A = [ 3 , 2 , 2 , 1 ]
    K = 3
    n = len ( A )
    print ( numBoxex ( A , n , K ) )

Index/key variables: i, j
Accumulator variables: ans
Python program:
def solve ( N , M , cp , sp ) :
    profit = [ ]
    for i in range ( N ) :
        profit . append ( sp [ i ] - cp [ i ] )
    profit . sort ( reverse = True )
    sum = 0
    for i in range ( M ) :
        if profit [ i ] > 0 :
            sum += profit [ i ]
        else :
            break
    return sum
if __name__ == " _ _ main _ _ " :
    N , M = 5 , 3
    CP = [ 5 , 10 , 35 , 7 , 23 ]
    SP = [ 11 , 10 , 0 , 9 , 19 ]
    print ( solve ( N , M , CP , SP ) )

Index/key variables: i
Accumulator variables: profit, sum
Iterator variables: i
Python program:
def minimumSets ( arr , n , key ) :
    arr . sort ( reverse = False )
    j = n - 1
    for i in range ( 0 , j + 1 , 1 ) :
        if ( arr [ i ] + arr [ j ] <= key ) :
            j -= 1
    return i + 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 5 , 3 , 4 ]
    n = len ( arr )
    key = 5
    print ( minimumSets ( arr , n , key ) )

Index/key variables: i, j
Iterator variables: i
Python program:
def substringSort ( arr , n , maxLen ) :
    count = [ 0 ] * ( maxLen )
    sortedArr = [ " " ] * ( maxLen )
    for i in range ( n ) :
        s = arr [ i ]
        Len = len ( s )
        if ( count [ Len - 1 ] == 0 ) :
            sortedArr [ Len - 1 ] = s
            count [ Len - 1 ] = 1
        elif ( sortedArr [ Len - 1 ] == s ) :
            count [ Len - 1 ] += 1
        else :
            print ( " Cannot ▁ be ▁ sorted " )
            return
    index = 0
    while ( count [ index ] == 0 ) :
        index += 1
    prev = index
    prevString = sortedArr [ prev ]
    index += 1
    while index < maxLen :
        if ( count [ index ] != 0 ) :
            current = sortedArr [ index ]
            if ( prevString in current ) :
                prev = index
                prevString = current
            else :
                print ( " Cannot ▁ be ▁ sorted " )
                return
        index += 1
    for i in range ( maxLen ) :
        s = sortedArr [ i ]
        for j in range ( count [ i ] ) :
            print ( s )
maxLen = 100
arr1 = [ " d " , " zddsaaz " , " ds " , " ddsaa " , " dds " , " dds " ]
substringSort ( arr1 , len ( arr1 ) , maxLen )
arr2 = [ " for " , " rof " ]
substringSort ( arr2 , len ( arr2 ) , maxLen )

Index/key variables: i, index, prev
Iterator variables: i, j
Python program:
from collections import deque
MAXN = 1000005
v = [ [ ] for i in range ( MAXN ) ]
def add_edge ( a , b , fre ) :
    v [ a ] . append ( b )
    fre [ b ] += 1
def topological_sorting ( fre , n ) :
    q = deque ( )
    for i in range ( n ) :
        if ( not fre [ i ] ) :
            q . append ( i )
    l = [ ]
    while ( len ( q ) > 0 ) :
        u = q . popleft ( )
        l . append ( u )
        for i in range ( len ( v [ u ] ) ) :
            fre [ v [ u ] [ i ] ] -= 1
            if ( not fre [ v [ u ] [ i ] ] ) :
                q . append ( v [ u ] [ i ] )
    return l
def numberofPaths ( source , destination , n , fre ) :
    s = topological_sorting ( fre , n )
    dp = [ 0 ] * n
    dp [ destination ] = 1
    for i in range ( len ( s ) - 1 , - 1 , - 1 ) :
        for j in range ( len ( v [ s [ i ] ] ) ) :
            dp [ s [ i ] ] += dp [ v [ s [ i ] ] [ j ] ]
    return dp
if __name__ == ' _ _ main _ _ ' :
    n = 5
    source , destination = 0 , 4
    fre = [ 0 ] * n
    add_edge ( 0 , 1 , fre )
    add_edge ( 0 , 2 , fre )
    add_edge ( 0 , 3 , fre )
    add_edge ( 0 , 4 , fre )
    add_edge ( 2 , 3 , fre )
    add_edge ( 3 , 4 , fre )
    print ( numberofPaths ( source , destination , n , fre ) )

Index/key variables: a, b, destination, i, j, u
Accumulator variables: l, q
Iterator variables: i, j
Python program:
def binary_search ( a , x , lo = 0 , hi = None ) :
    if hi is None :
        hi = len ( a )
    while lo < hi :
        mid = ( lo + hi ) // 2
        midval = a [ mid ]
        if midval < x :
            lo = mid + 1
        elif midval > x :
            hi = mid
        else :
            return mid
    return - 1
def printPairs ( arr , n ) :
    pair_exists = Fals
    arr . sort ( )
    for i in range ( n ) :
        if ( arr [ i ] < 0 ) :
            if ( binary_search ( arr , - arr [ i ] ) ) :
                print ( arr [ i ] , " , ▁ " , - arr [ i ] )
                pair_exists = True
        else :
            break
    if ( pair_exists == False ) :
        print ( " No ▁ such ▁ pair ▁ exists " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ]
    n = len ( arr )
    printPairs ( arr , n )

Index/key variables: i, mid
Iterator variables: i
Boolean variables: pair_exists
Python program:
import queue
def minEdgeBFS ( edges , u , v , n ) :
    visited = [ 0 ] * n
    distance = [ 0 ] * n
    Q = queue . Queue ( )
    distance [ u ] = 0
    Q . put ( u )
    visited [ u ] = True
    while ( not Q . empty ( ) ) :
        x = Q . get ( )
        for i in range ( len ( edges [ x ] ) ) :
            if ( visited [ edges [ x ] [ i ] ] ) :
                continue
            distance [ edges [ x ] [ i ] ] = distance [ x ] + 1
            Q . put ( edges [ x ] [ i ] )
            visited [ edges [ x ] [ i ] ] = 1
    return distance [ v ]
def addEdge ( edges , u , v ) :
    edges [ u ] . append ( v )
    edges [ v ] . append ( u )
if __name__ == ' _ _ main _ _ ' :
    n = 9
    edges = [ [ ] for i in range ( n ) ]
    addEdge ( edges , 0 , 1 )
    addEdge ( edges , 0 , 7 )
    addEdge ( edges , 1 , 7 )
    addEdge ( edges , 1 , 2 )
    addEdge ( edges , 2 , 3 )
    addEdge ( edges , 2 , 5 )
    addEdge ( edges , 2 , 8 )
    addEdge ( edges , 3 , 4 )
    addEdge ( edges , 3 , 5 )
    addEdge ( edges , 4 , 5 )
    addEdge ( edges , 5 , 6 )
    addEdge ( edges , 6 , 7 )
    addEdge ( edges , 7 , 8 )
    u = 0
    v = 5
    print ( minEdgeBFS ( edges , u , v , n ) )

Index/key variables: i, u, v, x
Iterator variables: i
Python program:
def findMinimumSwaps ( arr , n , k ) :
    num_min = num_max = need_minimum = 0
    need_maximum = swaps = 0
    for i in range ( n ) :
        if ( arr [ i ] < k ) :
            num_min += 1
        elif ( arr [ i ] > k ) :
            num_max += 1
    for i in range ( n ) :
        if ( arr [ i ] == k ) :
            pos = i
            break
    left = 0
    right = n - 1
    while ( left <= right ) :
        mid = ( left + right ) // 2
        if ( arr [ mid ] == k ) :
            break
        elif ( arr [ mid ] > k ) :
            if ( pos > mid ) :
                need_minimum += 1
            else :
                num_min -= 1
            left = mid + 1
        else :
            if ( pos < mid ) :
                need_maximum += 1
            else :
                num_max -= 1
            right = mid - 1
    if ( need_minimum > need_maximum ) :
        swaps = swaps + need_maximum
        num_min = num_min - need_maximum
        need_minimum = ( need_minimum - need_maximum )
        need_maximum = 0
    else :
        swaps = swaps + need_minimum
        num_max = num_max - need_minimum
        need_maximum = ( need_maximum - need_minimum )
        need_minimum = 0
    if ( need_maximum > num_max or need_minimum > num_min ) :
        return - 1
    else :
        return ( swaps + need_maximum + need_minimum )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 10 , 6 , 7 , 2 , 5 , 4 ]
    k = 4
    n = len ( arr )
    print ( findMinimumSwaps ( arr , n , k ) )

Index/key variables: i, mid
Accumulator variables: need_maximum, need_minimum, num_max, num_min
Iterator variables: i
Python program:
def function ( st ) :
    st = list ( st )
    l = len ( st )
    st [ : l // 2 ] = sorted ( st [ : l // 2 ] )
    st [ l // 2 : ] = sorted ( st [ l // 2 : ] )
    for i in range ( l // 2 ) :
        if ( st [ i ] != st [ l // 2 + 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:
inputArray = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ]
outputArray = [ ]
indexOfOne = 0
for item in inputArray :
    if item == 2 :
        outputArray . append ( item )
    elif item == 1 :
        outputArray . insert ( indexOfOne , item )
        indexOfOne += 1
    elif item == 0 :
        outputArray . insert ( 0 , item )
        indexOfOne += 1
    else :
        print ( " ▁ wrong ▁ value ▁ - ▁ Aborting ▁ " )
        continue
print ( outputArray )

Accumulator variables: indexOfOne, outputArray
Iterator variables: item
Python program:
import collections
def minimumBox ( arr , n ) :
    q = collections . deque ( [ ] )
    arr . sort ( )
    q . append ( arr [ 0 ] )
    for i in range ( 1 , n ) :
        now = q [ 0 ]
        if ( arr [ i ] >= 2 * now ) :
            q . popleft ( )
        q . append ( arr [ i ] )
    return len ( q )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 1 , 2 , 8 ]
    n = len ( arr )
    print ( minimumBox ( arr , n ) )

Index/key variables: i
Accumulator variables: q
Iterator variables: i
Python program:
def sortBinaryArray ( a , n ) :
    j = - 1
    for i in range ( n ) :
        if a [ i ] < 1 :
            j = j + 1
            a [ i ] , a [ j ] = a [ j ] , a [ i ]
a = [ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ]
n = len ( a )
sortBinaryArray ( a , n )
for i in range ( n ) :
    print ( a [ i ] , end = " ▁ " )

Index/key variables: i, j
Iterator variables: i
Python program:
def longestCommonPrefix ( a ) :
    size = len ( a )
    if ( size == 0 ) :
        return " "
    if ( size == 1 ) :
        return a [ 0 ]
    a . sort ( )
    end = min ( len ( a [ 0 ] ) , len ( a [ size - 1 ] ) )
    i = 0
    while ( i < end and a [ 0 ] [ i ] == a [ size - 1 ] [ i ] ) :
        i += 1
    pre = a [ 0 ] [ 0 : i ]
    return pre
if __name__ == " _ _ main _ _ " :
    input = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ]
    print ( " The ▁ longest ▁ Common ▁ Prefix ▁ is ▁ : " , longestCommonPrefix ( input ) )

Index/key variables: i
Python program:
def checkForSorting ( arr , n ) :
    for i in range ( 0 , n - 1 ) :
        if ( arr [ i ] > arr [ i + 1 ] ) :
            if ( arr [ i ] - arr [ i + 1 ] == 1 ) :
                arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ]
            else :
                return False
    return True
arr = [ 1 , 0 , 3 , 2 ]
n = len ( arr )
if ( checkForSorting ( arr , n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Iterator variables: i
Python program:
def printArraystring ( string , n ) :
    for i in range ( n ) :
        print ( string [ i ] , end = " ▁ " )
def sort ( s , n ) :
    for i in range ( 1 , n ) :
        temp = s [ i ]
        j = i - 1
        while j >= 0 and len ( temp ) < len ( s [ j ] ) :
            s [ j + 1 ] = s [ j ]
            j -= 1
        s [ j + 1 ] = temp
if __name__ == " _ _ main _ _ " :
    arr = [ " GeeksforGeeks " , " I " , " from " , " am " ]
    n = len ( arr )
    sort ( arr , n )
    printArraystring ( arr , n )

Index/key variables: i, j
Iterator variables: i
Python program:
def partition ( arr , low , high ) :
    pivot = arr [ low ]
    i = low - 1
    j = high + 1
    while ( True ) :
        i += 1
        while ( arr [ i ] < pivot ) :
            i += 1
        j -= 1
        while ( arr [ j ] > pivot ) :
            j -= 1
        if ( i >= j ) :
            return j
        arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
def quickSort ( arr , low , high ) :
    if ( low < high ) :
        pi = partition ( arr , low , high )
        quickSort ( arr , low , pi )
        quickSort ( arr , pi + 1 , high )
def printArray ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )
    print ( )
arr = [ 10 , 7 , 8 , 9 , 1 , 5 ]
n = len ( arr )
quickSort ( arr , 0 , n - 1 )
print ( " Sorted ▁ array : " )
printArray ( arr , n )

Index/key variables: i, j, low
Iterator variables: i
Python program:
def ksmallest ( arr , n , k ) :
    arr . sort ( ) ;
    if ( k < arr [ 0 ] ) :
        return k ;
    if ( k == arr [ 0 ] ) :
        return arr [ 0 ] + 1 ;
    if ( k > arr [ n - 1 ] ) :
        return k + n ;
    if ( arr [ 0 ] == 1 ) :
        k -= 1 ;
    else :
        k -= ( arr [ 0 ] - 1 ) ;
    for i in range ( 1 , n ) :
        c = arr [ i ] - arr [ i - 1 ] - 1 ;
        if ( k <= c ) :
            return arr [ i - 1 ] + k ;
        else :
            k -= c ;
    return arr [ n - 1 ] + k ;
k = 1 ;
arr = [ 1 ] ;
n = len ( arr ) ;
print ( ksmallest ( arr , n , k ) ) ;

Index/key variables: i
Accumulator variables: k
Iterator variables: i
Python program:
import queue
def bfsWithDistance ( g , mark , u , dis ) :
    lastMarked = 0
    q = queue . Queue ( )
    q . put ( u )
    dis [ u ] = 0
    while ( not q . empty ( ) ) :
        u = q . get ( )
        if ( mark [ u ] ) :
            lastMarked = u
        for i in range ( len ( g [ u ] ) ) :
            v = g [ u ] [ i ]
            if ( dis [ v ] == - 1 ) :
                dis [ v ] = dis [ u ] + 1
                q . put ( v )
    return lastMarked
def nodesKDistanceFromMarked ( edges , V , marked , N , K ) :
    V = V + 1
    g = [ [ ] for i in range ( V ) ]
    u , v = 0 , 0
    for i in range ( V - 1 ) :
        u = edges [ i ] [ 0 ]
        v = edges [ i ] [ 1 ]
        g [ u ] . append ( v )
        g [ v ] . append ( u )
    mark = [ False ] * V
    for i in range ( N ) :
        mark [ marked [ i ] ] = True
    tmp = [ - 1 ] * V
    dl = [ - 1 ] * V
    dr = [ - 1 ] * V
    u = bfsWithDistance ( g , mark , 0 , tmp )
    u = bfsWithDistance ( g , mark , u , dl )
    bfsWithDistance ( g , mark , u , dr )
    res = 0
    for i in range ( V ) :
        if ( dl [ i ] <= K and dr [ i ] <= K ) :
            res += 1
    return res
if __name__ == ' _ _ main _ _ ' :
    edges = [ [ 1 , 0 ] , [ 0 , 3 ] , [ 0 , 8 ] , [ 2 , 3 ] , [ 3 , 5 ] , [ 3 , 6 ] , [ 3 , 7 ] , [ 4 , 5 ] , [ 5 , 9 ] ]
    V = len ( edges )
    marked = [ 1 , 2 , 4 ]
    N = len ( marked )
    K = 3
    print ( nodesKDistanceFromMarked ( edges , V , marked , N , K ) )

Index/key variables: i, u, v
Accumulator variables: res
Iterator variables: i
Python program:
def findSubsequenceCount ( S , T ) :
    m = len ( T )
    n = len ( S )
    if m > n :
        return 0
    mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ]
    for i in range ( 1 , m + 1 ) :
        mat [ i ] [ 0 ] = 0
    for j in range ( n + 1 ) :
        mat [ 0 ] [ j ] = 1
    for i in range ( 1 , m + 1 ) :
        for j in range ( 1 , n + 1 ) :
            if T [ i - 1 ] != S [ j - 1 ] :
                mat [ i ] [ j ] = mat [ i ] [ j - 1 ]
            else :
                mat [ i ] [ j ] = ( mat [ i ] [ j - 1 ] + mat [ i - 1 ] [ j - 1 ] )
    return mat [ m ] [ n ]
if __name__ == " _ _ main _ _ " :
    T = " ge "
    S = " geeksforgeeks "
    print ( findSubsequenceCount ( S , T ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
import math
def maximumUniqueSquares ( N ) :
    ans = 0
    if N < 4 :
        return 0
    len = int ( math . sqrt ( N ) )
    N -= len * len
    for i in range ( 1 , len ) :
        ans += i * i
    if ( N >= len ) :
        N -= len
        for i in range ( 1 , len ) :
            ans += i
    for i in range ( 1 , N ) :
        ans += i
    return ans
if __name__ == " _ _ main _ _ " :
    N = 9
    print ( maximumUniqueSquares ( N ) )

Accumulator variables: ans
Iterator variables: i
Python program:
def findPermutation ( K , arr ) :
    missing = [ ]
    visited = [ 0 ] * ( K + 1 )
    for i in range ( 4 ) :
        visited [ arr [ i ] ] = 1
    for i in range ( 1 , K + 1 ) :
        if ( not visited [ i ] ) :
            missing . append ( i )
    INT_MAX = 2147483647
    arr . append ( INT_MAX )
    missing . append ( INT_MAX )
    p1 = 0
    p2 = 0
    ans = [ ]
    while ( len ( ans ) < K ) :
        if ( arr [ p1 ] < missing [ p2 ] ) :
            ans . append ( arr [ p1 ] )
            p1 = p1 + 1
        else :
            ans . append ( missing [ p2 ] )
            p2 = p2 + 1
    for i in range ( 0 , K ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    K = 7
    arr = [ 6 , 4 , 2 , 1 ]
    findPermutation ( K , arr )

Index/key variables: i, p1, p2
Accumulator variables: ans, missing
Iterator variables: i
Python program:
from math import sqrt
def findRemainingIndex ( N ) :
    ans = N
    while ( N > 1 ) :
        discard = int ( sqrt ( N ) )
        if ( discard * discard == N ) :
            ans -= 1
        N -= discard
    return ans
def findRemainingElement ( arr , N ) :
    remainingIndex = findRemainingIndex ( N )
    print ( arr [ remainingIndex - 1 ] )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 4 , 2 , 4 , - 3 , 1 , 1 ]
    N = len ( arr )
    findRemainingElement ( arr , N )

Accumulator variables: N, ans
Python program:
from queue import Queue
def minEdge ( ) :
    Q = Queue ( )
    Q . put ( [ 1 , 0 ] )
    V = [ False for _ in range ( N + 1 ) ]
    while 1 :
        crnt , c = Q . get ( )
        if crnt == N :
            break
        for _ in X [ crnt ] :
            if not V [ _ ] :
                Q . put ( [ _ , c + 1 ] )
                V [ _ ] = True
    return c
def findTime ( c ) :
    ans = 0
    for _ in range ( c ) :
        if ( ans // T ) % 2 :
            ans = ( ans // T + 1 ) * T + C
        else :
            ans += C
    return ans
if __name__ == " _ _ main _ _ " :
    N = 5
    M = 5
    T = 3
    C = 5
    X = [ [ ] , [ 2 , 3 , 4 ] , [ 4 , 5 ] , [ 1 ] , [ 1 , 2 ] , [ 2 ] ]
    c = minEdge ( )
    ans = findTime ( c )
    print ( ans )

Index/key variables: _, crnt
Accumulator variables: ans
Python program:
def shortestDistance ( S , N ) :
    dic = { }
    dis = [ ]
    for i in range ( N ) :
        if S [ i ] in dic :
            var = i - dic [ S [ i ] ]
            dic [ S [ i ] ] = i
            dis . append ( var )
        else :
            dic [ S [ i ] ] = i
    if ( len ( dis ) == 0 ) :
        return - 1
    else :
        return min ( dis ) - 1
S = " geeksforgeeks "
N = 13
print ( shortestDistance ( S , N ) )

Index/key variables: i
Accumulator variables: dis
Iterator variables: i
Python program:
def countCandies ( arr , n ) :
    sum = 0
    ans = [ 1 ] * n
    if ( n == 1 ) :
        return 1
    for i in range ( n - 1 ) :
        if ( arr [ i + 1 ] > arr [ i ] and ans [ i + 1 ] <= ans [ i ] ) :
            ans [ i + 1 ] = ans [ i ] + 1
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( arr [ i ] > arr [ i + 1 ] and ans [ i ] <= ans [ i + 1 ] ) :
            ans [ i ] = ans [ i + 1 ] + 1
        elif ( arr [ i ] == arr [ i + 1 ] and ans [ i ] < ans [ i + 1 ] ) :
            ans [ i ] = ans [ i + 1 ] + 1
        sum += ans [ i ]
    sum += ans [ n - 1 ]
    return sum
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 0 , 2 ]
    N = len ( arr )
    print ( countCandies ( arr , N ) )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def findMaximumProfit ( arr , M , N ) :
    max_heap = [ ]
    maxProfit = 0
    for i in range ( 0 , N ) :
        max_heap . append ( arr [ i ] )
    max_heap . sort ( )
    max_heap . reverse ( )
    while ( M > 0 ) :
        M -= 1
        X = max_heap [ 0 ]
        max_heap . pop ( 0 )
        maxProfit += X
        max_heap . append ( X - 1 )
        max_heap . sort ( )
        max_heap . reverse ( )
    print ( maxProfit )
arr = [ 4 , 6 ]
M = 4
N = len ( arr )
findMaximumProfit ( arr , M , N )

Index/key variables: i
Accumulator variables: M, maxProfit, max_heap
Iterator variables: i
Python program:
def findSequence ( n , k ) :
    arr = [ 0 ] * n
    sumPos = 0
    sumNeg = 0
    for i in range ( 0 , n - k ) :
        arr [ i ] = - ( i + 1 )
        sumNeg += arr [ i ]
    for i in range ( n - k , n ) :
        arr [ i ] = i + 1
        sumPos += arr [ i ]
    if ( abs ( sumNeg ) >= sumPos ) :
        print ( - 1 )
        return
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )
N = 10
K = 6
findSequence ( N , K )

Index/key variables: i
Accumulator variables: sumNeg, sumPos
Iterator variables: i
Python program:
def findPath ( arr , M , N ) :
    for row in range ( M ) :
        dir = ' L '
        i = row
        j = 0
        while ( j < N ) :
            if ( arr [ i ] [ j ] == 1 ) :
                if ( dir == ' L ' ) :
                    i -= 1
                    dir = ' D '
                elif ( dir == ' U ' ) :
                    j -= 1
                    dir = ' R '
                elif ( dir == ' R ' ) :
                    i += 1
                    dir = ' U '
                elif ( dir == ' D ' ) :
                    j += 1
                    dir = ' L '
            else :
                if ( dir == ' L ' ) :
                    i += 1
                    dir = ' U '
                elif ( dir == ' U ' ) :
                    j += 1
                    dir = ' L '
                elif ( dir == ' R ' ) :
                    i -= 1
                    dir = ' D '
                elif ( dir == ' D ' ) :
                    j -= 1
                    dir = ' R '
            if ( i < 0 or i == M or j < 0 or j == N ) :
                break
        if ( j == N ) :
            print ( i , end = " ▁ " )
        else :
            print ( - 1 , end = " ▁ " )
arr = [ [ 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ]
M = len ( arr )
N = len ( arr [ 0 ] )
findPath ( arr , M , N )

Index/key variables: i, j
Iterator variables: row
Python program:
def getSum ( n ) :
    ans = 0
    for i in str ( n ) :
        ans += int ( i )
    return ans
def getMax ( arr ) :
    maxRem0 = 0
    rem1 = 0
    rem2 = 0
    for i in arr :
        digitSum = getSum ( i )
        if digitSum % 3 == 0 :
            maxRem0 = max ( maxRem0 , i )
        elif digitSum % 3 == 1 :
            rem1 += i
        else :
            rem2 += i
    print ( maxRem0 + max ( rem1 , rem2 ) )
arr = [ 23 , 12 , 43 , 3 , 56 ]
getMax ( arr )

Accumulator variables: ans, rem1, rem2
Iterator variables: i