dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import heapq
def Comparator ( p1 ) :
    return p1 [ 1 ]
def FindMaxRating ( N , Point , Upvote , K ) :
    vec = [ ]
    for i in range ( N ) :
        vec . append ( [ Point [ i ] , Upvote [ i ] ] )
    vec . sort ( reverse = True , key = Comparator )
    pq = [ ]
    heapq . heapify ( pq )
    total_points , max_rating = 0 , 0
    for i in range ( K ) :
        total_points = ( total_points + vec [ i ] [ 0 ] )
        max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] )
        heapq . heappush ( pq , vec [ i ] [ 0 ] )
    for i in range ( K , N ) :
        if pq [ 0 ] < vec [ i ] [ 0 ] :
            total_points = ( total_points - pq [ 0 ] + vec [ i ] [ 0 ] )
            max_rating = max ( max_rating , total_points * vec [ i ] [ 1 ] )
            heapq . heappop ( pq )
            heapq . heappush ( pq , vec [ i ] [ 0 ] )
    return max_rating
Point = [ 2 , 10 , 3 , 1 , 5 , 8 ]
Upvote = [ 5 , 4 , 3 , 9 , 7 , 2 ]
N = len ( Point )
K = 2
print ( " Maximum ▁ Rating ▁ of ▁ Coding ▁ Contest ▁ is : " , FindMaxRating ( N , Point , Upvote , K ) )

Index/key variables: i
Accumulator variables: vec
Iterator variables: i
Python program:
def uniqueSum ( A , R , N , M ) :
    for i in range ( 0 , M ) :
        l = R [ i ] [ 0 ]
        r = R [ i ] [ 1 ] + 1
        l -= 1
        r -= 1
        A [ l ] += 1
        if ( r < N ) :
            A [ r ] -= 1
    for i in range ( 1 , N ) :
        A [ i ] += A [ i - 1 ]
    ans = 0
    s = { chr }
    for i in range ( 0 , N ) :
        if ( A [ i ] not in s ) :
            ans += A [ i ]
        s . add ( A [ i ] )
    return ans
A = [ 0 , 0 , 0 , 0 , 0 , 0 ]
R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] , [ 3 , 3 ] ]
N = len ( A )
M = len ( R )
print ( uniqueSum ( A , R , N , M ) )

Index/key variables: i, l, r
Accumulator variables: ans, s
Iterator variables: i
Python program:
def kthpair ( n , k , arr ) :
    arr . sort ( )
    k -= 1
    i = 0
    while ( i < n ) :
        t = 1
        while ( arr [ i ] == arr [ i + t ] ) :
            t += 1
        if ( t * n > k ) :
            break
        k = k - t * n
        i += t
    print ( arr [ i ] , " ▁ " , arr [ k // t ] )
if __name__ == " _ _ main _ _ " :
    n , k = 3 , 2
    arr = [ 3 , 1 , 5 ]
    kthpair ( n , k , arr )

Index/key variables: i
Accumulator variables: t
Python program:
def minOperations ( s ) :
    freq = [ 0 ] * 26
    n = len ( s )
    for i in range ( n ) :
        freq [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1
    freq . sort ( reverse = True )
    answer = n
    for i in range ( 1 , 27 ) :
        if ( n % i == 0 ) :
            x = n // i
            y = 0
            for j in range ( i ) :
                y += min ( freq [ j ] , x )
            answer = min ( answer , n - y )
    return answer
if __name__ == " _ _ main _ _ " :
    s = " BBC "
    print ( minOperations ( s ) )

Index/key variables: i, j
Accumulator variables: y
Iterator variables: i, j
Python program:
def fun ( arr , n , k ) :
    v = [ ]
    for i in range ( k ) :
        for j in range ( i , n , k ) :
            v . append ( arr [ j ] ) ;
        v . sort ( ) ;
        x = 0
        for j in range ( i , n , k ) :
            arr [ j ] = v [ x ] ;
            x += 1
        v = [ ]
    for i in range ( n - 1 ) :
        if ( arr [ i ] > arr [ i + 1 ] ) :
            return False
    return True
arr = [ 4 , 2 , 3 , 7 , 6 ]
K = 2 ;
n = len ( arr )
if ( fun ( arr , n , K ) ) :
    print ( " yes " )
else :
    print ( " no " )

Index/key variables: i, j, x
Accumulator variables: v
Iterator variables: i, j
Python program:
def sortedAdjacentDifferences ( arr , n ) :
    ans = [ 0 ] * n
    arr = sorted ( arr )
    l = 0
    r = n - 1
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( i % 2 ) :
            ans [ i ] = arr [ l ]
            l += 1
        else :
            ans [ i ] = arr [ r ]
            r -= 1
    for i in range ( n ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ]
    n = len ( arr )
    sortedAdjacentDifferences ( arr , n )

Index/key variables: i, l, r
Iterator variables: i
Python program:
def get_permutation ( arr , n ) :
    arr = sorted ( arr )
    result = 0
    for i in range ( n ) :
        result += abs ( arr [ i ] - ( i + 1 ) )
    return result
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ]
    n = len ( arr )
    print ( get_permutation ( arr , n ) )

Index/key variables: i
Accumulator variables: result
Iterator variables: i
Python program:
import sys
dp = [ [ 0 for x in range ( 1000 ) ] for y in range ( 1000 ) ]
def minimumIncDec ( arr , N , maxE , minE ) :
    if ( N == 0 ) :
        return 0
    if ( dp [ N ] [ maxE ] ) :
        return dp [ N ] [ maxE ]
    ans = sys . maxsize
    for k in range ( minE , maxE + 1 ) :
        x = minimumIncDec ( arr , N - 1 , k , minE )
        ans = min ( ans , x + abs ( arr [ N - 1 ] - k ) )
    dp [ N ] [ maxE ] = ans
    return dp [ N ] [ maxE ]
if __name__ == " _ _ main _ _ " :
    arr = [ 5 , 4 , 3 , 2 , 1 ]
    N = len ( arr )
    minE = min ( arr )
    maxE = max ( arr )
    print ( minimumIncDec ( arr , N , maxE , minE ) )

Index/key variables: N, maxE
Iterator variables: k
Python program:
def sortDiagonal ( a , M , N ) :
    for i in range ( M ) :
        sm = a [ i ] [ i ]
        pos = i
        for j in range ( i + 1 , N ) :
            if ( sm > a [ j ] [ j ] ) :
                sm = a [ j ] [ j ]
                pos = j
        a [ i ] [ i ] , a [ pos ] [ pos ] = a [ pos ] [ pos ] , a [ i ] [ i ]
    for i in range ( M ) :
        for j in range ( N ) :
            print ( a [ i ] [ j ] , end = " ▁ " )
        print ( )
a = [ [ 4 , 2 ] , [ 3 , 1 ] ]
sortDiagonal ( a , 2 , 2 )

Index/key variables: i, j, pos
Iterator variables: i, j
Python program:
def make_permutation ( arr , n ) :
    arr . sort ( ) ;
    ans = 0 ;
    for i in range ( n ) :
        ans += abs ( i + 1 - arr [ i ] ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 5 , 3 , 8 , 1 , 1 ] ;
    n = len ( arr ) ;
    print ( make_permutation ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
def maxSum ( V , n , m ) :
    ans = 0
    for i in range ( n - 2 , - 1 , - 1 ) :
        aux = [ ]
        for j in range ( m ) :
            aux . append ( [ V [ i + 1 ] [ j ] , j ] )
        aux = sorted ( aux )
        aux = aux [ : : - 1 ]
        for j in range ( m ) :
            for k in range ( m ) :
                if ( aux [ k ] [ 1 ] - j == 0 or abs ( aux [ k ] [ 1 ] - j ) > 1 ) :
                    V [ i ] [ j ] += aux [ k ] [ 0 ]
                    break
    for i in range ( m ) :
        ans = max ( ans , V [ 0 ] [ i ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    V = [ [ 1 , 2 , 3 , 4 ] , [ 8 , 7 , 6 , 5 ] , [ 10 , 11 , 12 , 13 ] ]
    n = len ( V )
    m = len ( V [ 0 ] )
    print ( maxSum ( V , n , m ) )

Index/key variables: i, j, k
Accumulator variables: aux
Iterator variables: i, j, k
Python program:
def SumofDigits ( digit ) :
    sum = 0
    while ( digit != 0 ) :
        rem = digit % 10
        sum += rem
        digit //= 10
    return sum
def findCountofNumbers ( arr , n , M ) :
    SumDigits = [ ]
    for i in range ( n ) :
        s = SumofDigits ( arr [ i ] )
        SumDigits . append ( s )
    SumDigits . sort ( )
    ip = list ( set ( SumDigits ) )
    count = 0
    sum = 0
    for i in range ( len ( SumDigits ) ) :
        if ( sum > M ) :
            break
        sum += SumDigits [ i ]
        if ( sum <= M ) :
            count += 1
    return count
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 45 , 16 , 17 , 219 , 32 , 22 ]
    M = 10
    n = len ( arr )
    print ( findCountofNumbers ( arr , n , M ) )

Index/key variables: i
Accumulator variables: SumDigits, count, digit, sum
Iterator variables: i
Python program:
def minimumProduct ( arr , n ) :
    arr = sorted ( arr )
    product = 1
    for i in range ( 0 , n , 2 ) :
        product *= ( arr [ i ] + arr [ i + 1 ] )
    return product
arr = [ 1 , 6 , 3 , 1 , 7 , 8 ]
n = len ( arr )
print ( minimumProduct ( arr , n ) )

Index/key variables: i
Accumulator variables: product
Iterator variables: i
Python program:
def minRemovels ( ranges ) :
    size = len ( ranges )
    rem = 0
    ranges . sort ( )
    end = ranges [ 0 ] [ 1 ]
    for i in range ( 1 , size ) :
        if ( ranges [ i ] [ 0 ] < end ) :
            rem += 1
            end = min ( ranges [ i ] [ 1 ] , end )
        else :
            end = ranges [ i ] [ 1 ]
    return rem
if __name__ == ' _ _ main _ _ ' :
    Input = [ [ 19 , 25 ] , [ 10 , 20 ] , [ 16 , 20 ] ]
    print ( minRemovels ( Input ) )

Index/key variables: i
Accumulator variables: rem
Iterator variables: i
Python program:
def checkIsAP ( arr , n ) :
    if ( n == 1 ) :
        return True
    arr . sort ( ) ;
    d = arr [ 1 ] - arr [ 0 ]
    for i in range ( 2 , n ) :
        if ( arr [ i ] - arr [ i - 1 ] != d ) :
            return False
    return True
def checkIsGP ( arr , n ) :
    if ( n == 1 ) :
        return True
    arr . sort ( )
    r = arr [ 1 ] / arr [ 0 ]
    for i in range ( 2 , n ) :
        if ( arr [ i ] / arr [ i - 1 ] != r ) :
            return False
    return True
def checkIsHP ( arr , n ) :
    if ( n == 1 ) :
        return True
    rec = [ ]
    for i in range ( 0 , n ) :
        rec . append ( ( 1 / arr [ i ] ) )
    if ( checkIsAP ( rec , n ) ) :
        return True
    else :
        return False
arr = [ 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 ]
n = len ( arr )
flag = 0
if ( checkIsAP ( arr , n ) ) :
    print ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " , end =   ' ' )
    flag = 1
if ( checkIsGP ( arr , n ) ) :
    print ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " , end =   ' ' )
    flag = 1
if ( checkIsHP ( arr , n ) ) :
    print ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " , end =   ' ' )
    flag = 1
elif ( flag == 0 ) :
    print ( " No " , end =   ' ' )

Index/key variables: i
Accumulator variables: rec
Iterator variables: i
Python program:
def reversDigits ( num ) :
    rev_num = 0 ;
    while ( num > 0 ) :
        rev_num = rev_num * 10 + num % 10 ;
        num = num // 10 ;
    return rev_num ;
def sortArr ( arr , n ) :
    vp = [ ] ;
    for i in range ( n ) :
        vp . append ( ( reversDigits ( arr [ i ] ) , arr [ i ] ) ) ;
    vp . sort ( )
    for i in range ( len ( vp ) ) :
        print ( vp [ i ] [ 1 ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 12 , 10 , 102 , 31 , 15 ] ;
    n = len ( arr ) ;
    sortArr ( arr , n ) ;

Index/key variables: i
Accumulator variables: vp
Iterator variables: i
Python program:
def CountPairs ( arr , n ) :
    arr . sort ( )
    count = 0 ;
    l = 0 ; r = n - 1 ;
    while ( l < r ) :
        if ( arr [ l ] + arr [ r ] > 0 ) :
            count += ( r - l ) ;
            r -= 1 ;
        else :
            l += 1 ;
    return count ;
if __name__ == " _ _ main _ _ " :
    arr = [ - 7 , - 1 , 3 , 2 ] ;
    n = len ( arr ) ;
    print ( CountPairs ( arr , n ) ) ;

Index/key variables: l, r
Accumulator variables: count
Python program:
def sortMat ( data , row , col ) :
    size = row * col
    for i in range ( 0 , size ) :
        for j in range ( 0 , size - 1 ) :
            if ( data [ j // col ] [ j % col ] > data [ ( j + 1 ) // col ] [ ( j + 1 ) % col ] ) :
                temp = data [ j // col ] [ j % col ]
                data [ j // col ] [ j % col ] = data [ ( j + 1 ) // col ] [ ( j + 1 ) % col ]
                data [ ( j + 1 ) // col ] [ ( j + 1 ) % col ] = temp
def printMat ( mat , row , col ) :
    for i in range ( row ) :
        for j in range ( col ) :
            print ( mat [ i ] [ j ] , end = " ▁ " )
        print ( )
if __name__ == " _ _ main _ _ " :
    mat = [ [ 5 , 4 , 7 ] , [ 1 , 3 , 8 ] , [ 2 , 9 , 6 ] ]
    row = len ( mat )
    col = len ( mat [ 0 ] )
    sortMat ( mat , row , col )
    printMat ( mat , row , col )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def overlap ( v ) :
    ans = 0
    count = 0
    data = [ ]
    for i in range ( len ( v ) ) :
        data . append ( [ v [ i ] [ 0 ] , ' x ' ] )
        data . append ( [ v [ i ] [ 1 ] , ' y ' ] )
    data = sorted ( data )
    for i in range ( len ( data ) ) :
        if ( data [ i ] [ 1 ] == ' x ' ) :
            count += 1
        if ( data [ i ] [ 1 ] == ' y ' ) :
            count -= 1
        ans = max ( ans , count )
    print ( ans )
v = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 6 ] ]
overlap ( v )

Index/key variables: i
Accumulator variables: count, data
Iterator variables: i
Python program:
def orgazineInOrder ( vec , op , n ) :
    result = [ 0 ] * n ;
    vec . sort ( ) ;
    i = 0 ;
    j = n - 1 ;
    k = 0 ;
    while ( i <= j and k <= n - 2 ) :
        if ( op [ k ] == ' < ' ) :
            result [ k ] = vec [ i ] ;
            i += 1 ;
        else :
            result [ k ] = vec [ j ] ;
            j -= 1 ;
        k += 1 ;
    result [ n - 1 ] = vec [ i ] ;
    return result ;
if __name__ == " _ _ main _ _ " :
    vec = [ 8 , 2 , 7 , 1 , 5 , 9 ] ;
    op = [ ' > ' , ' > ' , ' < ' , ' > ' , ' < ' ] ;
    result = orgazineInOrder ( vec , op , len ( vec ) ) ;
    for i in range ( len ( result ) ) :
        print ( result [ i ] , end = " ▁ " ) ;

Index/key variables: i, j, k
Iterator variables: i
Python program:
def getDistance ( arr , n , a , b ) :
    vp = [ ]
    for i in range ( n ) :
        vp . append ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] )
    vp = sorted ( vp )
    pos = [ 0 for i in range ( n ) ]
    for i in range ( len ( vp ) ) :
        height = vp [ i ] [ 0 ]
        k = vp [ i ] [ 1 ]
        pos [ k ] = height
    first = - 1
    second = - 1
    for i in range ( n ) :
        if ( pos [ i ] == a ) :
            first = i
        if ( pos [ i ] == b ) :
            second = i
    return abs ( first - second )
arr = [ [ 5 , 0 ] , [ 3 , 0 ] , [ 2 , 0 ] , [ 6 , 4 ] , [ 1 , 0 ] , [ 4 , 3 ] ]
n = len ( arr )
a = 6
b = 5
print ( getDistance ( arr , n , a , b ) )

Index/key variables: i, k
Accumulator variables: vp
Iterator variables: i
Python program:
def findSum ( arr , n , k ) :
    ans = arr [ n - k - 1 ] - arr [ 0 ] ;
    for i in range ( 1 , k + 1 ) :
        ans = min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 100 , 120 , 140 ] ;
    k = 2 ;
    n = len ( arr ) ;
    print ( findSum ( arr , n , k ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def findMin ( arr , n ) :
    ans = 1
    for i in range ( n ) :
        j = i + 1
        while ( j < n and arr [ j ] >= arr [ j - 1 ] ) :
            j += 1
        ans = max ( ans , j - i )
        i = j - 1
    return n - ans
arr = [ 3 , 2 , 1 ]
n = len ( arr )
print ( findMin ( arr , n ) )

Index/key variables: j
Iterator variables: i
Python program:
def countSubSeq ( a , n , k ) :
    a . sort ( reverse = False )
    arr = [ ]
    i = 0
    while ( i < n ) :
        count = 1
        x = a [ i ]
        i += 1
        while ( i < n and a [ i ] == x ) :
            count += 1
            i += 1
        arr . append ( count )
    m = len ( arr )
    n = min ( m , k )
    count = 1
    dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ]
    for i in range ( m + 1 ) :
        dp [ 0 ] [ i ] = 1
    for i in range ( 1 , n + 1 , 1 ) :
        j = m
        while ( j >= 0 ) :
            if ( j > m - i ) :
                dp [ i ] [ j ] = 0
            else :
                dp [ i ] [ j ] = dp [ i ] [ j + 1 ] + arr [ j ] * dp [ i - 1 ] [ j + 1 ]
            j -= 1
        count = count + dp [ i ] [ 0 ]
    return count
if __name__ == ' _ _ main _ _ ' :
    a = [ 2 , 2 , 3 , 3 , 5 ]
    n = len ( a )
    k = 3
    print ( countSubSeq ( a , n , k ) )

Index/key variables: i, j
Accumulator variables: arr, count
Iterator variables: i
Python program:
def countUnvisited ( n , m ) :
    i = 0
    X = ( m * n ) - m - n
    queue = [ ]
    queue . append ( X )
    count = 0
    while ( len ( queue ) > 0 ) :
        curr = queue [ 0 ]
        queue . remove ( queue [ 0 ] )
        count += 1
        if ( curr - m > 0 ) :
            queue . append ( curr - m )
        if ( curr - n > 0 ) :
            queue . append ( curr - n )
    return count
if __name__ == ' _ _ main _ _ ' :
    n = 2
    m = 5
    print ( countUnvisited ( n , m ) )

Accumulator variables: count, queue
Python program:
def partOdd ( s ) :
    odd = [ ]
    for i in range ( len ( s ) ) :
        if i % 2 != 0 :
            odd . append ( s [ i ] )
    return odd
def partEven ( s ) :
    even = [ ]
    for i in range ( len ( s ) ) :
        if i % 2 == 0 :
            even . append ( s [ i ] )
    return even
def canBeMadeEqual ( s1 , s2 ) :
    even_s1 = partEven ( s1 )
    even_s2 = partEven ( s2 )
    odd_s1 = partOdd ( s1 )
    odd_s2 = partOdd ( s2 )
    even_s1 . sort ( )
    even_s2 . sort ( )
    odd_s1 . sort ( )
    odd_s2 . sort ( )
    if even_s1 == even_s2 and odd_s1 == odd_s2 :
        return True
    return False
s1 = " cdab "
s2 = " abcd "
if canBeMadeEqual ( s1 , s2 ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Accumulator variables: even, odd
Iterator variables: i
Python program:
def sortStack ( input1 , k ) :
    tmpStack = [ ]
    while ( len ( input1 ) != 0 ) :
        tmp = input1 [ - 1 ]
        input1 . pop ( )
        while ( len ( tmpStack ) != 0 ) :
            tmpStackMod = tmpStack [ - 1 ] % k
            tmpMod = tmp % k
            if ( ( tmpStackMod > tmpMod ) or ( tmpStackMod == tmpMod and tmpStack [ - 1 ] > tmp ) ) :
                input1 . append ( tmpStack [ - 1 ] )
                tmpStack . pop ( )
            else :
                break
        tmpStack . append ( tmp )
    while ( len ( tmpStack ) != 0 ) :
        input1 . append ( tmpStack [ - 1 ] )
        tmpStack . pop ( )
    while ( len ( input1 ) != 0 ) :
        print ( input1 [ - 1 ] , end = " ▁ " )
        input1 . pop ( )
if __name__ == " _ _ main _ _ " :
    input1 = [ ]
    input1 . append ( 10 )
    input1 . append ( 3 )
    input1 . append ( 2 )
    input1 . append ( 6 )
    input1 . append ( 12 )
    k = 4
    sortStack ( input1 , k )

Accumulator variables: input1, tmpStack
Python program:
MAX = 1000 ;
def lineExists ( x , y , v , n ) :
    size = ( 2 * MAX ) + 1 ;
    arr = [ 0 ] * size ;
    for i in range ( n ) :
        arr [ x [ i ] + MAX ] += v [ i ] ;
    for i in range ( 1 , size ) :
        arr [ i ] += arr [ i - 1 ] ;
    if ( arr [ size - 1 ] == 0 ) :
        return True ;
    if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) :
        return True ;
    for i in range ( 1 , size - 1 ) :
        if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) :
            return True ;
        if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) :
            return True ;
        if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) :
            return True ;
    if ( arr [ size - 2 ] == 0 ) :
        return True ;
    return False ;
if __name__ == " _ _ main _ _ " :
    x = [ - 3 , 5 , 8 ] ;
    y = [ 8 , 7 , 9 ] ;
    v = [ 8 , 2 , 10 ] ;
    n = len ( x ) ;
    if ( lineExists ( x , y , v , n ) ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;

Index/key variables: i
Iterator variables: i
Python program:
def maxSum ( a , n ) :
    a . sort ( ) ;
    sum = 0 ;
    for i in range ( 0 , n - 1 , 2 ) :
        sum += a [ i ] ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 2 , 1 , 4 , 5 ] ;
    n = len ( arr ) ;
    print ( maxSum ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def printArr ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = ' ▁ ' )
def sortWithRemainder ( arr , n , k ) :
    v = [ [ ] for i in range ( k ) ]
    for i in range ( n ) :
        v [ arr [ i ] % k ] . append ( arr [ i ] )
    for i in range ( k ) :
        v [ i ] . sort ( )
    j = 0
    for i in range ( k ) :
        for it in v [ i ] :
            arr [ j ] = it
            j += 1
    printArr ( arr , n )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 ]
    n = len ( arr )
    k = 4
    sortWithRemainder ( arr , n , k )

Index/key variables: i, j
Iterator variables: i, it
Python program:
def minOperations ( ar , k ) :
    ar = sorted ( ar )
    opsNeeded = 0
    for i in range ( k ) :
        opsNeeded += ar [ k - 1 ] - ar [ i ]
    ans = opsNeeded
    for i in range ( k , len ( ar ) ) :
        opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] )
        opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] )
        ans = min ( ans , opsNeeded )
    return ans
arr = [ 3 , 1 , 9 , 100 ]
n = len ( arr )
k = 3
print ( minOperations ( arr , k ) )

Index/key variables: i
Accumulator variables: opsNeeded
Iterator variables: i
Python program:
x = - 1 ;
heap = [ 0 ] * 1000 ;
def heapForm ( k ) :
    global x ;
    x += 1 ;
    heap [ x ] = k ;
    child = x ;
    index = x // 2 ;
    while ( index >= 0 ) :
        if ( heap [ index ] > heap [ child ] ) :
            tmp = heap [ index ] ;
            heap [ index ] = heap [ child ] ;
            heap [ child ] = tmp ;
            child = index ;
            index = index // 2 ;
        else :
            break ;
def heapSort ( ) :
    global x ;
    while ( x >= 0 ) :
        k = heap [ 0 ] ;
        print ( k , end = " ▁ " ) ;
        heap [ 0 ] = heap [ x ] ;
        x = x - 1 ;
        tmp = - 1 ;
        index = 0 ;
        length = x ;
        left1 = 1 ;
        right1 = left1 + 1 ;
        while ( left1 <= length ) :
            if ( heap [ index ] <= heap [ left1 ] and heap [ index ] <= heap [ right1 ] ) :
                break ;
            else :
                if ( heap [ left1 ] < heap [ right1 ] ) :
                    tmp = heap [ index ] ;
                    heap [ index ] = heap [ left1 ] ;
                    heap [ left1 ] = tmp ;
                    index = left1 ;
                else :
                    tmp = heap [ index ] ;
                    heap [ index ] = heap [ right1 ] ;
                    heap [ right1 ] = tmp ;
                    index = right1 ;
            left1 = 2 * left1 ;
            right1 = left1 + 1 ;
def sort ( k , n ) :
    for i in range ( n ) :
        heapForm ( k [ i ] ) ;
    heapSort ( ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " ] ;
    n = len ( arr ) ;
    sort ( arr , n ) ;

Index/key variables: child, i, index, left1, right1, x
Iterator variables: i
Python program:
def getNumber ( n , k ) :
    arr = [ 0 ] * n ;
    i = 0 ;
    odd = 1 ;
    while ( odd <= n ) :
        arr [ i ] = odd ;
        i += 1 ;
        odd += 2 ;
    even = 2 ;
    while ( even <= n ) :
        arr [ i ] = even ;
        i += 1 ;
        even += 2 ;
    return arr [ k - 1 ] ;
if __name__ == ' _ _ main _ _ ' :
    n = 8 ;
    k = 5 ;
    print ( getNumber ( n , k ) ) ;

Index/key variables: i
Accumulator variables: even, odd
Python program:
def printArr ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " ) ;
def UpdateArr ( arr , n ) :
    i = 0 ;
    j = n - 1 ;
    while ( i < j ) :
        temp = arr [ i ] ;
        arr [ i ] = arr [ j ] ;
        arr [ j ] = temp ;
        i += 2 ;
        j -= 2 ;
    printArr ( arr , n ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
    n = len ( arr ) ;
    UpdateArr ( arr , n ) ;

Index/key variables: i, j
Iterator variables: i
Python program:
N = 20
fact = [ 0 ] * N ;
def pre ( ) :
    fact [ 0 ] = 1 ;
    for i in range ( 1 , N ) :
        fact [ i ] = i * fact [ i - 1 ] ;
def CountPermutation ( a , n ) :
    ways = 1 ;
    a . sort ( ) ;
    size = 1 ;
    for i in range ( 1 , n ) :
        if ( a [ i ] == a [ i - 1 ] ) :
            size += 1 ;
        else :
            ways *= fact [ size ] ;
            size = 1 ;
    ways *= fact [ size ] ;
    return ways ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ;
    n = len ( a ) ;
    pre ( ) ;
    print ( CountPermutation ( a , n ) ) ;

Index/key variables: i, size
Accumulator variables: ways
Iterator variables: i