dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def merge ( indices , a , l , mid , r ) :
    temp_ind = [ 0 ] * ( r - l + 1 )
    j = mid + 1
    i = 0
    temp_l = l
    while ( l <= mid and j <= r ) :
        if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) :
            temp_ind [ i ] = indices [ l ]
            i += 1
            l += 1
        else :
            temp_ind [ i ] = indices [ j ]
            i += 1
            j += 1
    while ( l <= mid ) :
        temp_ind [ i ] = indices [ l ]
        i += 1
        l += 1
    while ( j <= r ) :
        temp_ind [ i ] = indices [ j ]
        i += 1
        j += 1
    for k in range ( i ) :
        indices [ temp_l ] = temp_ind [ k ]
        temp_l += 1
def divide ( indices , a , l , r ) :
    if ( l >= r ) :
        return
    mid = l // 2 + r // 2
    divide ( indices , a , l , mid )
    divide ( indices , a , mid + 1 , r )
    merge ( indices , a , l , mid , r )
def noOfSubsequences ( arr , N ) :
    indices = N * [ 0 ]
    for i in range ( N ) :
        indices [ i ] = i
    divide ( indices , arr , 0 , N - 1 )
    B = [ 0 ] * N
    subseq = 1
    for i in range ( N ) :
        B [ indices [ i ] ] = subseq
        subseq *= 2
    for i in range ( N ) :
        print ( B [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 3 , 1 ]
    N = len ( arr )
    noOfSubsequences ( arr , N )

Index/key variables: i, j, k, l, temp_l
Accumulator variables: subseq
Iterator variables: i, k
Python program:
def maxCostToRemove ( arr , N , K ) :
    maxCost = 0
    arr = sorted ( arr )
    for i in range ( 0 , N , K ) :
        maxCost += arr [ i + 1 ]
    return maxCost
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 ]
    N = len ( arr )
    K = 4
    print ( maxCostToRemove ( arr , N , K ) )

Accumulator variables: maxCost
Iterator variables: i
Python program:
import sys
def minimumCost ( binary , n , a , b ) :
    groupOfZeros = [ ]
    length = 0
    i = 0
    increment_need = True
    while ( i < n ) :
        increment_need = True
        while ( i < n and binary [ i ] == 0 ) :
            length += 1
            i += 1
            increment_need = False
        if ( increment_need == True ) :
            i += 1
        if ( length != 0 ) :
            groupOfZeros . append ( length )
        length = 0
    groupOfZeros . sort ( )
    i = 0
    found_ones = False
    NumOfOnes = 0
    while ( i < n ) :
        found_ones = False
        while ( i < n and binary [ i ] == 1 ) :
            i += 1
            found_ones = True
        if ( found_ones == False ) :
            i += 1
        else :
            NumOfOnes += 1
    ans = sys . maxsize
    for i in range ( n ) :
        curr = 0
        totalOnes = NumOfOnes
        if ( i == 0 ) :
            curr = totalOnes * a
        else :
            mark = i
            num_of_changes = 0
            for x in groupOfZeros :
                if ( mark >= x ) :
                    totalOnes -= 1
                    mark -= x
                    num_of_changes += x
                else :
                    break
            curr = ( ( num_of_changes * b ) + ( totalOnes * a ) )
        ans = min ( ans , curr )
    print ( ans )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 1 , 0 , 1 , 1 ]
    N = len ( arr )
    X = 10
    Y = 4
    minimumCost ( arr , N , X , Y )

Index/key variables: i
Accumulator variables: NumOfOnes, groupOfZeros, length, mark, num_of_changes, totalOnes
Iterator variables: i, x
Boolean variables: found_ones, increment_need
Python program:
def isSortedparitions ( arr , i , j ) :
    for k in range ( i + 1 , j + 1 ) :
        if ( arr [ k ] < arr [ k - 1 ] ) :
            return 0
    return 1
def partitionsArr ( arr , i , j ) :
    if ( i >= j ) :
        return 1
    flag = int ( isSortedparitions ( arr , i , j ) )
    if ( flag != 0 ) :
        return ( j - i + 1 )
    mid = ( i + j ) // 2
    X = partitionsArr ( arr , i , mid ) ;
    Y = partitionsArr ( arr , mid + 1 , j )
    return max ( X , Y )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 ]
    N = len ( arr )
    print ( partitionsArr ( arr , 0 , N - 1 ) )

Index/key variables: k
Iterator variables: k
Python program:
def checkIfPossibleMerge ( A , B , N ) :
    i = 0
    j = 0
    prev = - 1
    flag = 1
    while ( i < N and j < N ) :
        if ( A [ i ] < B [ j ] and prev != 0 ) :
            prev = 0
            i += 1
        elif ( B [ j ] < A [ i ] and prev != 1 ) :
            prev = 1
            j += 1
        elif ( A [ i ] == B [ j ] ) :
            if ( prev != 1 ) :
                prev = 1
                j += 1
            else :
                prev = 0
                i += 1
        else :
            flag = 0
            break
    return flag
if __name__ == ' _ _ main _ _ ' :
    A = [ 3 , 5 , 8 ]
    B = [ 2 , 4 , 6 ]
    N = len ( A )
    if ( checkIfPossibleMerge ( A , B , N ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, j
Python program:
def minimum_possible_sum ( arr , n , k ) :
    if ( n == 0 ) :
        return 0
    if ( n == 1 ) :
        return arr [ 0 ]
    for i in range ( k ) :
        smallest_element = arr [ 0 ]
        smallest_pos = 0
        largest_element = arr [ 0 ]
        largest_pos = 0
        for i in range ( 1 , n ) :
            if ( arr [ i ] >= largest_element ) :
                largest_element = arr [ i ]
                largest_pos = i
            if ( arr [ i ] < smallest_element ) :
                smallest_element = arr [ i ]
                smallest_pos = i
        a = smallest_element * 2
        b = largest_element // 2
        if ( a + b < smallest_element + largest_element ) :
            arr [ smallest_pos ] = a
            arr [ largest_pos ] = b
    ans = 0
    for i in range ( n ) :
        ans += arr [ i ]
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 50 , 1 , 100 , 100 , 1 ]
    K = 2
    n = len ( arr )
    print ( minimum_possible_sum ( arr , n , K ) )

Index/key variables: i, largest_pos, smallest_pos
Accumulator variables: ans
Iterator variables: i
Python program:
def maxSum ( n , a , l , q ) :
    v = [ ]
    d = [ 0 ] * n
    for i in range ( q ) :
        for x in range ( l [ i ] [ 0 ] , l [ i ] [ 1 ] + 1 ) :
            if ( d [ x ] == 0 ) :
                d [ x ] = 1
    st = set ( [ ] )
    for i in range ( n ) :
        if ( d [ i ] == 0 ) :
            v . append ( a [ i ] )
            st . add ( i )
    v . sort ( reverse = True )
    c = 0
    for it in st :
        a [ it ] = v
        c += 1
    pref_sum = 0
    temp_sum = 0
    for i in range ( n ) :
        temp_sum += a [ i ]
        pref_sum += temp_sum
    return pref_sum
if __name__ == " _ _ main _ _ " :
    arr = [ - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 ]
    N = len ( arr )
    q = [ [ 0 , 0 ] , [ 4 , 5 ] ]
    queries = len ( q )
    print ( maxSum ( N , arr , q , queries ) )

Index/key variables: i, it, x
Accumulator variables: c, pref_sum, st, temp_sum, v
Iterator variables: i, it, x
Python program:
import math
def countPairs ( A , N ) :
    count = 0
    A . sort ( )
    bits = [ 0 ] * 32
    for i in range ( 0 , N ) :
        if ( A [ i ] == 0 ) :
            continue
        for j in range ( 0 , 32 ) :
            if ( ( ( 1 << j ) & A [ i ] ) == 0 ) :
                count += bits [ j ]
        bits [ ( int ) ( math . log ( A [ i ] , 2 ) ) ] += 1
    print ( count )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 4 , 3 ]
    N = len ( arr )
    countPairs ( arr , N )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def mostvisitedsector ( N , A ) :
    maxVisited = 0
    mp = { }
    for i in range ( 0 , len ( A ) - 1 ) :
        start = A [ i ] % N
        end = A [ i + 1 ] % N
        while ( start != end ) :
            if ( start == 0 ) :
                if N in mp :
                    mp [ N ] = mp [ N ] + 1
                else :
                    mp [ N ] = 1
                if ( mp [ N ] > maxVisited ) :
                    maxVisited = mp [ N ]
            else :
                if start in mp :
                    mp [ start ] = mp [ start ] + 1
                else :
                    mp [ start ] = 1
                if ( mp [ start ] > maxVisited ) :
                    maxVisited = mp [ start ]
            start = ( start + 1 ) % N
    if A [ - 1 ] in mp :
        mp [ A [ - 1 ] ] = mp [ A [ - 1 ] ] + 1
    if ( mp [ A [ - 1 ] ] > maxVisited ) :
        maxVisited = mp [ A [ - 1 ] ]
    for x in mp :
        if ( mp [ x ] == maxVisited ) :
            print ( x , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    arr = [ 1 , 2 , 1 , 2 ]
    mostvisitedsector ( N , arr )

Index/key variables: N, i, start, x
Iterator variables: i, x
Python program:
def maxRectangle ( sequence , size ) :
    X_Cord = [ 0 ] * size
    Y_Cord = [ 0 ] * size
    for i in range ( size ) :
        X_Cord [ i ] = sequence [ i ] [ 0 ]
        Y_Cord [ i ] = sequence [ i ] [ 1 ]
    X_Cord . sort ( )
    Y_Cord . sort ( )
    X_Max = 0
    Y_Max = 0
    for i in range ( size - 1 ) :
        X_Max = max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] )
        Y_Max = max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] )
    return X_Max * Y_Max
if __name__ == " _ _ main _ _ " :
    point = [ [ - 2 , 0 ] , [ 2 , 0 ] , [ 4 , 0 ] , [ 4 , 2 ] ]
    n = len ( point )
    print ( maxRectangle ( point , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def checkArrangement ( A1 , A2 , n , k ) :
    A1 = sorted ( A1 )
    A2 = sorted ( A2 )
    A2 = A2 [ : : - 1 ]
    flag = 0
    for i in range ( n ) :
        if ( ( A1 [ i ] + A2 [ i ] > k ) or ( A1 [ i ] + A2 [ i ] < k // 2 ) ) :
            flag = 1
            break
    if ( flag == 1 ) :
        print ( " No " )
    else :
        print ( " Yes " )
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 1 , 3 , 4 , 5 ]
    arr2 = [ 2 , 0 , 1 , 1 ]
    K = 6
    N = len ( arr1 )
    checkArrangement ( arr1 , arr2 , N , K )

Index/key variables: i
Iterator variables: i
Python program:
def MaxRearrngeSum ( A , B , N ) :
    A . sort ( )
    B . sort ( reverse = True )
    maxSum = 0
    for i in range ( N ) :
        maxSum += abs ( A [ i ] - B [ i ] )
    return maxSum
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 2 , 2 , 4 , 5 ]
    B = [ 5 , 5 , 5 , 6 , 6 ]
    N = len ( A )
    print ( MaxRearrngeSum ( A , B , N ) )

Index/key variables: i
Accumulator variables: maxSum
Iterator variables: i
Python program:
def largestArea ( arr1 , n , arr2 , m ) :
    end = 0
    start = 0
    i = 0
    j = 0
    arr1 . sort ( reverse = False )
    arr2 . sort ( reverse = False )
    while ( i < n and j < m ) :
        if ( arr1 [ i ] == arr2 [ j ] ) :
            if ( start == 0 ) :
                start = arr1 [ i ]
            else :
                end = arr1 [ i ]
            i += 1
            j += 1
        elif ( arr1 [ i ] > arr2 [ j ] ) :
            j += 1
        else :
            i += 1
    if ( end == 0 or start == 0 ) :
        return 0
    else :
        return ( end - start )
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 1 , 2 , 4 ]
    N = len ( arr1 )
    arr2 = [ 1 , 3 , 4 ]
    M = len ( arr2 )
    print ( largestArea ( arr1 , N , arr2 , M ) )

Index/key variables: i, j
Python program:
def count_pairs ( arr , brr , N , M , K ) :
    i = 0
    j = 0
    cntPairs = 0
    arr = sorted ( arr )
    brr = sorted ( brr )
    while ( i < N and j < M ) :
        if ( brr [ j ] - arr [ i ] > K ) :
            cntPairs += ( M - j )
            i += 1
        else :
            j += 1
    return cntPairs
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 9 , 1 , 8 ]
    brr = [ 10 , 12 , 7 , 4 , 2 , 3 ]
    K = 3
    N = len ( arr )
    M = len ( brr )
    print ( count_pairs ( arr , brr , N , M , K ) )

Index/key variables: i, j
Accumulator variables: cntPairs
Python program:
def minHeapify ( brr , i , M ) :
    left = 2 * i + 1
    right = 2 * i + 2
    smallest = i
    if ( left < M and brr [ left ] < brr [ smallest ] ) :
        smallest = left
    if ( right < M and brr [ right ] < brr [ smallest ] ) :
        smallest = right
    if ( smallest != i ) :
        brr [ i ] , brr [ smallest ] = brr [ smallest ] , brr [ i ]
        minHeapify ( brr , smallest , M )
def merge ( arr , brr , N , M ) :
    for i in range ( N ) :
        if ( arr [ i ] > brr [ 0 ] ) :
            arr [ i ] , brr [ 0 ] = brr [ 0 ] , arr [ i ]
            minHeapify ( brr , 0 , M )
    brr . sort ( )
def printArray ( arr , N ) :
    for i in range ( N ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 23 , 35 , 235 , 2335 ]
    brr = [ 3 , 5 ]
    N = len ( arr )
    M = len ( brr )
    merge ( arr , brr , N , M )
    printArray ( arr , N )
    printArray ( brr , M )

Index/key variables: i, left, right, smallest
Iterator variables: i
Python program:
def partition ( arr , N , brr , M , Pivot ) :
    l = N - 1
    r = 0
    while ( l >= 0 and r < M ) :
        if ( arr [ l ] < Pivot ) :
            l -= 1
        elif ( brr [ r ] > Pivot ) :
            r += 1
        else :
            arr [ l ] , brr [ r ] = brr [ r ] , arr [ l ]
            l -= 1
            r += 1
def Merge ( arr , N , brr , M ) :
    l = 0
    r = 0
    index = - 1
    Pivot = 0
    while ( index < N and l < N and r < M ) :
        if ( arr [ l ] < brr [ r ] ) :
            Pivot = arr [ l ]
            l += 1
        else :
            Pivot = brr [ r ]
            r += 1
        index += 1
    while ( index < N and l < N ) :
        Pivot = arr [ l ]
        l += 1
        index += 1
    while ( index < N and r < M ) :
        Pivot = brr [ r ]
        r += 1
        index += 1
    partition ( arr , N , brr , M , Pivot )
    arr = sorted ( arr )
    brr = sorted ( brr )
    for i in range ( N ) :
        print ( arr [ i ] , end = " ▁ " )
    for i in range ( M ) :
        print ( brr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 5 , 9 ]
    brr = [ 2 , 4 , 7 , 10 ]
    N = len ( arr )
    M = len ( brr )
    Merge ( arr , N , brr , M )

Index/key variables: i, l, r
Accumulator variables: index
Iterator variables: i
Python program:
def rankLessThanK ( arr , k , n ) :
    rank = 1 ;
    position = 1 ;
    arr = sorted ( arr )
    for i in range ( n - 1 , - 1 , - 1 ) :
        if ( i == n - 1 or arr [ i ] != arr [ i + 1 ] ) :
            rank = position ;
            if ( rank > k ) :
                return position - 1 ;
        position += 1 ;
    return n ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 2 , 3 , 4 , 5 ] ;
    N = len ( arr ) ;
    K = 4 ;
    print ( rankLessThanK ( arr , K , N ) ) ;

Index/key variables: i
Accumulator variables: position
Iterator variables: i
Python program:
def eraseSubdirectory ( dir ) :
    res = [ ]
    dir . sort ( )
    res . append ( dir [ 0 ] )
    print ( " { " , dir [ 0 ] , end = " , ▁ " )
    for i in range ( 1 , len ( dir ) ) :
        curr = dir [ i ]
        prev = res [ len ( res ) - 1 ]
        l = len ( prev )
        if ( len ( curr ) > l and curr [ l ] == ' / ' and prev == curr [ : l ] ) :
            continue
        res . append ( curr )
        print ( curr , end = " , ▁ " )
    print ( " } " )
if __name__ == ' _ _ main _ _ ' :
    dir = [ " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ]
    eraseSubdirectory ( dir )

Index/key variables: i, l
Accumulator variables: res
Iterator variables: i
Python program:
def numberOfPairs ( arr1 , n , arr2 , m ) :
    pq = [ ]
    arr1 . sort ( reverse = False )
    for j in range ( m ) :
        pq . append ( arr2 [ j ] )
    ans = 2
    i = n - 1
    while ( i >= 0 ) :
        pq . sort ( reverse = False )
        if ( pq [ 0 ] >= 2 * arr1 [ i ] ) :
            ans += 1
            print ( pq [ 0 ] )
            pq . remove ( pq [ 0 ] )
        i -= 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 3 , 2 , 1 ]
    arr2 = [ 3 , 4 , 2 , 1 ]
    N = len ( arr1 )
    M = len ( arr2 )
    print ( numberOfPairs ( arr1 , N , arr2 , M ) )

Index/key variables: i, j
Accumulator variables: ans, pq
Iterator variables: j
Python program:
import sys
def minCost ( arr , n ) :
    if ( n == 1 ) :
        print ( 0 )
        return
    arr = sorted ( arr )
    raised = 1 / ( n - 1 )
    temp = pow ( arr [ n - 1 ] , raised )
    r = round ( temp ) + 1
    min_cost = sys . maxsize
    common_ratio = 1
    for j in range ( 1 , r + 1 ) :
        curr_cost = 0
        prod = 1
        i = 0
        while i < n :
            curr_cost += abs ( arr [ i ] - prod )
            prod *= j
            if ( curr_cost >= min_cost ) :
                break
            i += 1
        if ( i == n ) :
            min_cost = min ( min_cost , curr_cost )
            common_ratio = j
    print ( min_cost , common_ratio )
if __name__ == ' _ _ main _ _ ' :
    N = 6
    arr = [ 1 , 11 , 4 , 27 , 15 , 33 ]
    minCost ( arr , N )

Index/key variables: i
Accumulator variables: curr_cost, prod
Iterator variables: j
Python program:
maxN = 10
def calculateFib ( fib , n ) :
    fib [ 0 ] = fib [ 1 ] = 1
    for x in range ( 2 , n ) :
        fib [ x ] = ( fib [ x - 1 ] + fib [ x - 2 ] )
def find_mth_bit ( n , m , fib ) :
    if ( n <= 1 ) :
        return n
    len_left = fib [ n - 2 ]
    len_right = fib [ n - 1 ]
    if ( m <= len_left ) :
        return find_mth_bit ( n - 2 , len_left + 1 - m , fib )
    else :
        return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib )
def find_mth_bitUtil ( n , m ) :
    fib = [ 0 for i in range ( maxN ) ]
    calculateFib ( fib , maxN )
    ans = find_mth_bit ( n , m , fib )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    m = 3
    find_mth_bitUtil ( n , m )

Index/key variables: x
Iterator variables: x
Python program:
def minAdjSwaps ( mat ) :
    N = len ( mat )
    cntZero = [ 0 ] * ( N )
    for i in range ( N ) :
        for j in range ( N - 1 , - 1 , - 1 ) :
            if mat [ i ] [ j ] != 0 :
                break
            cntZero [ i ] += 1
    cntSwaps = 0
    for i in range ( N ) :
        if ( cntZero [ i ] < ( N - i - 1 ) ) :
            First = i
            while ( First < N and cntZero [ First ] < ( N - i - 1 ) ) :
                First += 1
            if ( First == N ) :
                return - 1
            while ( First > i ) :
                cntZero [ First ] = cntZero [ First - 1 ]
                cntZero [ First - 1 ] = cntZero [ First ]
                First -= 1
                cntSwaps += 1
    return cntSwaps
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 0 , 0 , 2 ] , [ 3 , 1 , 0 ] , [ 4 , 0 , 0 ] ]
    print ( minAdjSwaps ( mat ) )

Index/key variables: First, i, j
Accumulator variables: cntSwaps
Iterator variables: i, j
Python program:
def maxArea ( point_x , point_y , n , length , width ) :
    point_x . sort ( )
    point_y . sort ( )
    dx = point_x [ 0 ]
    dy = point_y [ 0 ]
    for i in range ( 1 , n ) :
        dx = max ( dx , point_x [ i ] - point_x [ i - 1 ] )
        dy = max ( dy , point_y [ i ] - point_y [ i - 1 ] )
    dx = max ( dx , ( length + 1 ) - point_x [ n - 1 ] )
    dy = max ( dy , ( width + 1 ) - point_y [ n - 1 ] )
    print ( ( dx - 1 ) * ( dy - 1 ) )
if __name__ == " _ _ main _ _ " :
    length = 15
    width = 8
    n = 3
    point_x = [ 3 , 11 , 8 ]
    point_y = [ 8 , 2 , 6 ]
    maxArea ( point_x , point_y , n , length , width )

Index/key variables: i
Iterator variables: i
Python program:
def make_cuts ( arr , n , K ) :
    ans = 0
    poss = [ ]
    ce = 0
    co = 0
    for x in range ( n - 1 ) :
        if ( arr [ x ] % 2 == 0 ) :
            ce += 1
        else :
            co += 1
        if ( ce == co and co > 0 and ce > 0 ) :
            poss . append ( abs ( arr [ x ] - arr [ x + 1 ] ) )
    poss . sort ( )
    for x in poss :
        if ( K >= x ) :
            ans += 1
            K -= x
        else :
            break
    return ans
N = 6
K = 4
arr = [ 1 , 2 , 5 , 10 , 15 , 20 ]
print ( make_cuts ( arr , N , K ) )

Index/key variables: x
Accumulator variables: K, ans, ce, co, poss
Iterator variables: x
Python program:
def Arrange ( arr , n ) :
    cost = 0
    index = { }
    for i in range ( n ) :
        index [ arr [ i ] ] = i
    arr . sort ( )
    for i in range ( n ) :
        if ( i == 0 ) :
            if ( arr [ i ] > arr [ i + 1 ] + arr [ - 1 ] ) :
                print ( " - 1" )
                return
            else :
                cost += abs ( index [ arr [ i ] ] - i )
        elif ( i == n - 1 ) :
            if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) :
                print ( " - 1" )
                return
            else :
                cost += abs ( index [ arr [ i ] ] - i )
        else :
            if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) :
                print ( " - 1" )
                return
            else :
                cost += abs ( index [ arr [ i ] ] - i )
    print ( cost )
    return
arr = [ 2 , 4 , 5 , 1 , 3 ]
N = len ( arr )
Arrange ( arr , N )

Index/key variables: i
Accumulator variables: cost
Iterator variables: i
Python program:
def XorSum ( A , B , N ) :
    ans = 0
    for i in range ( N ) :
        for j in range ( N ) :
            ans = ans ^ ( A [ i ] + B [ j ] )
    return ans
if __name__ == " _ _ main _ _ " :
    A = [ 4 , 6 , 0 , 0 , 3 , 3 ]
    B = [ 0 , 5 , 6 , 5 , 0 , 3 ]
    N = len ( A )
    print ( XorSum ( A , B , N ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
from collections import defaultdict
def maxDistinctChar ( s , n , k ) :
    freq = defaultdict ( int )
    for i in range ( n ) :
        freq [ s [ i ] ] += 1
    v = [ ]
    for it in freq . values ( ) :
        v . append ( it )
    v . sort ( )
    for i in range ( len ( v ) ) :
        mn = min ( v [ i ] - 1 , k )
        v [ i ] -= mn
        k -= mn
    if ( k > 0 ) :
        for i in range ( len ( v ) ) :
            mn = min ( v [ i ] , k ) ;
            v [ i ] -= mn
            k -= mn
    res = 0
    for i in range ( len ( v ) ) :
        if ( v [ i ] == 1 ) :
            res += 1
    return res
if __name__ == " _ _ main _ _ " :
    S = " geeksforgeeks "
    N = len ( S )
    K = 1
    print ( maxDistinctChar ( S , N , K ) )

Index/key variables: i
Accumulator variables: k, res, v
Iterator variables: i, it
Python program:
primes = [ ]
def SieveOfEratosthenes ( ) :
    prime = [ True ] * 10005
    p = 2
    while p * p <= 1000 :
        if ( prime [ p ] == True ) :
            for i in range ( p * p , 1001 , p ) :
                prime [ i ] = False
        p += 1
    for p in range ( 2 , 1001 ) :
        if ( prime [ p ] ) :
            primes . append ( p )
def generate ( n , k ) :
    prod = [ ]
    SieveOfEratosthenes ( )
    l = len ( primes )
    for i in range ( l ) :
        for j in range ( i + 1 , l ) :
            if ( primes [ i ] * primes [ j ] > 0 ) :
                prod . append ( primes [ i ] * primes [ j ] )
    prod . sort ( )
    sum = 0
    for i in range ( k - 1 ) :
        sum += prod [ i ]
    if ( sum > n ) :
        print ( " - 1" )
    else :
        for i in range ( k - 1 ) :
            print ( prod [ i ] , end = " , ▁ " )
        print ( n - sum )
if __name__ == " _ _ main _ _ " :
    n = 52
    k = 5
    generate ( n , k )

Index/key variables: i, j, p
Accumulator variables: primes, prod, sum
Iterator variables: i, j, p
Python program:
def findMaxValue ( arr , n ) :
    arr . sort ( )
    ans = arr [ n - 1 ]
    maxPossible = arr [ n - 1 ]
    for i in range ( n - 2 , - 1 , - 1 ) :
        if ( maxPossible > 0 ) :
            if ( arr [ i ] >= maxPossible ) :
                ans += ( maxPossible - 1 )
                maxPossible = maxPossible - 1
            else :
                maxPossible = arr [ i ]
                ans += maxPossible
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 4 , 1 , 5 ]
    n = len ( arr )
    print ( findMaxValue ( arr , n ) )

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