dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def findSubArray ( arr , n ) :
    min_sum = sys . maxsize
    for i in range ( n ) :
        curr_sum = arr [ i ]
        if ( min_sum > abs ( curr_sum ) ) :
            min_sum = abs ( curr_sum )
            start = i
            end = i
        for j in range ( i + 1 , n , 1 ) :
            curr_sum = curr_sum + arr [ j ]
            if ( min_sum > abs ( curr_sum ) ) :
                min_sum = abs ( curr_sum )
                start = i
                end = j
    p = [ start , end ]
    return p
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , - 5 , 4 , - 6 , - 3 ]
    n = len ( arr )
    point = findSubArray ( arr , n )
    print ( " Subarray ▁ starting ▁ from ▁ " , end = " " )
    print ( point [ 0 ] , " to " , point [ 1 ] )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def uniquePrefix ( a ) :
    size = len ( a )
    res = [ 0 ] * ( size )
    a = sorted ( a )
    j = 0
    while ( j < min ( len ( a [ 0 ] ) - 1 , len ( a [ 1 ] ) - 1 ) ) :
        if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) :
            j += 1
        else :
            break
    ind = 0
    res [ ind ] = a [ 0 ] [ 0 : j + 1 ]
    ind += 1
    temp_prefix = a [ 1 ] [ 0 : j + 1 ]
    for i in range ( 1 , size - 1 ) :
        j = 0
        while ( j < min ( len ( a [ i ] ) - 1 , len ( a [ i + 1 ] ) - 1 ) ) :
            if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) :
                j += 1
            else :
                break
        new_prefix = a [ i ] [ 0 : j + 1 ]
        if ( len ( temp_prefix ) > len ( new_prefix ) ) :
            res [ ind ] = temp_prefix
            ind += 1
        else :
            res [ ind ] = new_prefix
            ind += 1
        temp_prefix = a [ i + 1 ] [ 0 : j + 1 ]
    j = 0
    sec_last = a [ size - 2 ]
    last = a [ size - 1 ]
    while ( j < min ( len ( sec_last ) - 1 , len ( last ) - 1 ) ) :
        if ( sec_last [ j ] == last [ j ] ) :
            j += 1
        else :
            break
    res [ ind ] = last [ 0 : j + 1 ]
    return res
if __name__ == ' _ _ main _ _ ' :
    input = [ " zebra " , " dog " , " duck " , " dove " ]
    output = uniquePrefix ( input )
    print ( " The ▁ shortest ▁ unique ▁ prefixes ▁ " + " in ▁ sorted ▁ order ▁ are ▁ : ▁ " )
    for i in output :
        print ( i , end = " ▁ " )

Index/key variables: i, ind, j
Iterator variables: i
Python program:
def findMinimum ( arr , n , k ) :
    res = 0
    i = 0
    while ( n ) :
        res += arr [ i ]
        n = n - k
        i += 1
    return res
def findMaximum ( arr , n , k ) :
    res = 0
    index = 0
    i = n - 1
    while ( i >= index ) :
        res += arr [ i ]
        index += k
        i -= 1
    return res
arr = [ 3 , 2 , 1 , 4 ]
n = len ( arr )
k = 2
arr . sort ( )
print ( findMinimum ( arr , n , k ) , " ▁ " , findMaximum ( arr , n , k ) )

Index/key variables: i
Accumulator variables: index, res
Python program:
def maxLevel ( boxes , n ) :
    boxes . sort ( )
    prev_width = boxes [ 0 ]
    prev_count = 1
    curr_count = 0
    curr_width = 0
    for i in range ( 1 , n ) :
        curr_width += boxes [ i ]
        curr_count += 1
        if ( curr_width > prev_width and curr_count > prev_count ) :
            prev_width = curr_width
            prev_count = curr_count
            curr_count = 0
            curr_width = 0
            ans += 1
    return ans
if __name__ == " _ _ main _ _ " :
    boxes = [ 10 , 20 , 30 , 50 , 60 , 70 ]
    n = len ( boxes )
    print ( maxLevel ( boxes , n ) )

Index/key variables: i
Accumulator variables: ans, curr_count, curr_width
Iterator variables: i
Python program:
def twoWaySort ( arr , n ) :
    for i in range ( 0 , n ) :
        if ( arr [ i ] & 1 ) :
            arr [ i ] *= - 1
    arr . sort ( )
    for i in range ( 0 , n ) :
        if ( arr [ i ] & 1 ) :
            arr [ i ] *= - 1
arr = [ 1 , 3 , 2 , 7 , 5 , 4 ]
n = len ( arr )
twoWaySort ( arr , n ) ;
for i in range ( 0 , n ) :
    print ( arr [ i ] , end = " ▁ " )

Index/key variables: i
Iterator variables: i
Python program:
def isPossibleTriangle ( arr , N ) :
    if N < 3 :
        return False
    arr . sort ( )
    for i in range ( N - 2 ) :
        if arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] :
            return True
arr = [ 5 , 4 , 3 , 1 , 2 ]
N = len ( arr )
print ( " Yes " if isPossibleTriangle ( arr , N ) else " No " )

Index/key variables: i
Iterator variables: i
Python program:
MAX = 1000000
def ksmallest ( arr , n , k ) :
    b = [ 0 ] * MAX ;
    for i in range ( n ) :
        b [ arr [ i ] ] = 1 ;
    for j in range ( 1 , MAX ) :
        if ( b [ j ] != 1 ) :
            k -= 1 ;
        if ( k is not 1 ) :
            return j ;
k = 1 ;
arr = [ 1 ] ;
n = len ( arr ) ;
print ( ksmallest ( arr , n , k ) ) ;

Index/key variables: i, j
Accumulator variables: k
Iterator variables: i, j
Python program:
def mergeTwoHalf ( A , n ) :
    A . sort ( )
if __name__ == ' _ _ main _ _ ' :
    A = [ 2 , 3 , 8 , - 1 , 7 , 10 ]
    n = len ( A )
    mergeTwoHalf ( A , n )
    for i in range ( n ) :
        print ( A [ i ] , end = " ▁ " )

Index/key variables: i
Iterator variables: i
Python program:
def minDiff ( arr , n , k ) :
    result = + 2147483647
    arr . sort ( )
    for i in range ( n - k + 1 ) :
        result = int ( min ( result , arr [ i + k - 1 ] - arr [ i ] ) )
    return result
arr = [ 10 , 100 , 300 , 200 , 1000 , 20 , 30 ]
n = len ( arr )
k = 3
print ( minDiff ( arr , n , k ) )

Index/key variables: i
Iterator variables: i
Python program:
def bitonicGenerator ( arr , n ) :
    evenArr = [ ]
    oddArr = [ ]
    for i in range ( n ) :
        if ( ( i % 2 ) == 0 ) :
            evenArr . append ( arr [ i ] )
        else :
            oddArr . append ( arr [ i ] )
    evenArr = sorted ( evenArr )
    oddArr = sorted ( oddArr )
    oddArr = oddArr [ : : - 1 ]
    i = 0
    for j in range ( len ( evenArr ) ) :
        arr [ i ] = evenArr [ j ]
        i += 1
    for j in range ( len ( oddArr ) ) :
        arr [ i ] = oddArr [ j ]
        i += 1
arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ]
n = len ( arr )
bitonicGenerator ( arr , n )
for i in arr :
    print ( i , end = " ▁ " )

Index/key variables: i, j
Accumulator variables: evenArr, oddArr
Iterator variables: i, j
Python program:
def findSurpasser ( arr , n ) :
    for i in range ( 0 , n ) :
        count = 0 ;
        for j in range ( i + 1 , n ) :
            if ( arr [ j ] > arr [ i ] ) :
                count += 1
        print ( count , end = " ▁ " )
def printArray ( arr , n ) :
    for i in range ( 0 , n ) :
        print ( arr [ i ] , end = " ▁ " )
arr = [ 2 , 7 , 5 , 3 , 0 , 8 , 1 ]
n = len ( arr )
print ( " Given ▁ array ▁ is " )
printArray ( arr , n )
print ( " Surpasser Count of array is " ) ;
findSurpasser ( arr , n )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def solve ( arr , n ) :
    arr . sort ( )
    a = 0 ; b = 0
    for i in range ( n ) :
        if ( i % 2 != 0 ) :
            a = a * 10 + arr [ i ]
        else :
            b = b * 10 + arr [ i ]
    return a + b
arr = [ 6 , 8 , 4 , 5 , 2 , 3 ]
n = len ( arr )
print ( " Sum ▁ is ▁ " , solve ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
import sys
def maxProduct ( arr , n ) :
    if n < 3 :
        return - 1
    max_product = - ( sys . maxsize - 1 )
    for i in range ( 0 , n - 2 ) :
        for j in range ( i + 1 , n - 1 ) :
            for k in range ( j + 1 , n ) :
                max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] )
    return max_product
arr = [ 10 , 3 , 5 , 6 , 20 ]
n = len ( arr )
max = maxProduct ( arr , n )
if max == - 1 :
    print ( " No ▁ Tripplet ▁ Exits " )
else :
    print ( " Maximum ▁ product ▁ is " , max )

Index/key variables: i, j, k
Iterator variables: i, j, k
Python program:
import sys
def maxProduct ( arr , n ) :
    if ( n < 3 ) :
        return - 1
    leftMin = [ - 1 for i in range ( n ) ]
    rightMin = [ - 1 for i in range ( n ) ]
    leftMax = [ - 1 for i in range ( n ) ]
    rightMax = [ - 1 for i in range ( n ) ]
    max_product = - sys . maxsize - 1
    max_sum = arr [ 0 ]
    min_sum = arr [ 0 ]
    for i in range ( 1 , n ) :
        leftMax [ i ] = max_sum
        if ( arr [ i ] > max_sum ) :
            max_sum = arr [ i ]
        leftMin [ i ] = min_sum
        if ( arr [ i ] < min_sum ) :
            min_sum = arr [ i ]
    max_sum = arr [ n - 1 ]
    min_sum = arr [ n - 1 ]
    for j in range ( n - 2 , - 1 , - 1 ) :
        rightMax [ j ] = max_sum
        if ( arr [ j ] > max_sum ) :
            max_sum = arr [ j ]
        rightMin [ j ] = min_sum
        if ( arr [ j ] < min_sum ) :
            min_sum = arr [ j ]
    for i in range ( 1 , n - 1 ) :
        max1 = max ( arr [ i ] * leftMax [ i ] * rightMax [ i ] , arr [ i ] * leftMin [ i ] * rightMin [ i ] )
        max2 = max ( arr [ i ] * leftMax [ i ] * rightMin [ i ] , arr [ i ] * leftMin [ i ] * rightMax [ i ] )
        max_product = max ( max_product , max ( max1 , max2 ) )
    return max_product
arr = [ 1 , 4 , 3 , - 6 , - 7 , 0 ]
n = len ( arr )
Max = maxProduct ( arr , n )
if ( Max == - 1 ) :
    print ( " No ▁ Triplet ▁ Exists " )
else :
    print ( " Maximum ▁ product ▁ is " , Max )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import sys
def maxProduct ( arr , n ) :
    if ( n < 3 ) :
        return - 1
    maxA = - sys . maxsize - 1
    maxB = - sys . maxsize - 1
    maxC = - sys . maxsize - 1
    minA = sys . maxsize
    minB = sys . maxsize
    for i in range ( n ) :
        if ( arr [ i ] > maxA ) :
            maxC = maxB
            maxB = maxA
            maxA = arr [ i ]
        elif ( arr [ i ] > maxB ) :
            maxC = maxB
            maxB = arr [ i ]
        elif ( arr [ i ] > maxC ) :
            maxC = arr [ i ]
        if ( arr [ i ] < minA ) :
            minB = minA
            minA = arr [ i ]
        elif ( arr [ i ] < minB ) :
            minB = arr [ i ]
    return max ( minA * minB * maxA , maxA * maxB * maxC )
arr = [ 1 , - 4 , 3 , - 6 , 7 , 0 ]
n = len ( arr )
Max = maxProduct ( arr , n )
if ( Max == - 1 ) :
    print ( " No ▁ Triplet ▁ Exists " )
else :
    print ( " Maximum ▁ product ▁ is " , Max )

Index/key variables: i
Iterator variables: i
Python program:
def minMoves ( a , b , K ) :
    n = len ( a )
    m = len ( a [ 0 ] )
    cntOperations = 0
    for i in range ( 0 , n ) :
        for j in range ( 0 , m ) :
            if ( a [ i ] [ j ] != b [ i ] [ j ] ) :
                cntOperations += 1
                if ( i + K - 1 >= n or j + K - 1 >= m ) :
                    return - 1
                for p in range ( 0 , K ) :
                    for q in range ( 0 , K ) :
                        if ( a [ i + p ] [ j + q ] == '0' ) :
                            a [ i + p ] [ j + q ] = '1'
                        else :
                            a [ i + p ] [ j + q ] = '0'
    return cntOperations
if __name__ == " _ _ main _ _ " :
    A = [ [ '1' , '0' , '0' ] , [ '0' , '0' , '0' ] , [ '0' , '0' , '0' ] ]
    B = [ [ '0' , '0' , '0' ] , [ '0' , '0' , '0' ] , [ '0' , '0' , '0' ] ]
    K = 3
    print ( minMoves ( A , B , K ) )

Index/key variables: i, j
Accumulator variables: cntOperations
Iterator variables: i, j, p, q
Python program:
from typing import Deque
def largestPermutation ( arr , N ) :
    p = Deque ( )
    mx = arr [ 0 ]
    p . append ( arr [ 0 ] )
    for i in range ( 1 , N ) :
        if ( arr [ i ] < mx ) :
            p . append ( arr [ i ] )
        else :
            p . appendleft ( arr [ i ] )
            mx = arr [ i ]
    for i in p :
        print ( i , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 1 , 2 , 4 ]
    N = len ( arr )
    largestPermutation ( arr , N )

Index/key variables: i
Accumulator variables: p
Iterator variables: i
Python program:
def maxConsecutiveCnt ( arr ) :
    X = 0
    arr . sort ( )
    for i in range ( 0 , len ( arr ) ) :
        if ( arr [ i ] <= ( X + 1 ) ) :
            X = X + arr [ i ]
        else :
            break
    return X + 1
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 1 , 4 ]
    print ( maxConsecutiveCnt ( arr ) )

Index/key variables: i
Iterator variables: i
Python program:
def maxLength ( A , B ) :
    n = len ( A )
    maxSize = 0
    pre = { }
    diff = 0
    pre [ 0 ] = 0
    for i in range ( 0 , n ) :
        diff += ( A [ i ] - B [ i ] )
        if ( not ( diff in pre ) ) :
            pre = i + 1
        else :
            maxSize = max ( maxSize , i - pre + 1 )
    return maxSize
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 2 , 3 , 4 ]
    B = [ 4 , 3 , 2 , 1 ]
    print ( maxLength ( A , B ) )

Index/key variables: i
Accumulator variables: diff
Iterator variables: i
Python program:
def minimumPossibleProduct ( K ) :
    res = 1
    r = ( 1 << K ) - 1
    for i in range ( 0 , K ) :
        res *= ( r - 1 )
    res *= r
    return res
K = 3
print ( minimumPossibleProduct ( K ) )

Accumulator variables: res
Iterator variables: i
Python program:
def maximizeSubset ( arr , N ) :
    arr . sort ( )
    maxSize = 0
    i = 0
    j = i + 2
    while ( i < N - 2 ) :
        while ( j < N and arr [ i ] + arr [ i + 1 ] > arr [ j ] ) :
            j = j + 1
        maxSize = max ( maxSize , j - i )
        i += 1
        j = max ( j , i + 2 )
    return maxSize
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 ]
    N = len ( arr )
    print ( maximizeSubset ( arr , N ) )

Index/key variables: i, j
Python program:
def maxShiftArrayValue ( arr , cap , N ) :
    sumVals = 0
    for i in range ( N ) :
        sumVals += arr [ i ]
    maxCapacity = 0
    for i in range ( N ) :
        maxCapacity = max ( cap [ i ] , maxCapacity )
    return min ( maxCapacity , sumVals )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 ]
    cap = [ 5 , 6 ]
    N = len ( arr )
    print ( maxShiftArrayValue ( arr , cap , N ) )

Index/key variables: i
Accumulator variables: sumVals
Iterator variables: i
Python program:
import sys
def Find ( parent , a ) :
    if parent [ parent [ a ] ] != parent [ a ] :
        parent [ a ] = Find ( parent , parent [ a ] )
    return parent [ a ]
def Union ( parent , rank , minVal , a , b ) :
    a = Find ( parent , a )
    b = Find ( parent , b )
    if ( rank [ a ] == rank [ b ] ) :
        rank [ a ] += 1
    if ( rank [ a ] > rank [ b ] ) :
        parent [ b ] = a
        if ( minVal [ a ] >= 0 and minVal [ b ] >= 0 ) :
            minVal [ a ] = min ( minVal [ a ] , minVal [ b ] )
        elif ( minVal [ a ] >= 0 and minVal [ b ] < 0 ) :
            minVal [ a ] = minVal [ a ]
        elif ( minVal [ a ] < 0 and minVal [ b ] >= 0 ) :
            minVal [ a ] = minVal [ b ]
        else :
            minVal [ a ] = max ( minVal [ a ] , minVal [ b ] )
    else :
        parent [ a ] = b
        if ( minVal [ a ] >= 0 and minVal [ b ] >= 0 ) :
            minVal [ b ] = min ( minVal [ a ] , minVal [ b ] )
        elif ( minVal [ a ] >= 0 and minVal [ b ] < 0 ) :
            minVal [ b ] = minVal [ a ]
        elif ( minVal [ a ] < 0 and minVal [ b ] >= 0 ) :
            minVal [ b ] = minVal [ b ]
        else :
            minVal [ b ] = max ( minVal [ a ] , minVal [ b ] )
def findMinCost ( G , cost , N , M ) :
    parent = [ 0 for i in range ( N + 1 ) ]
    rank = [ 0 for i in range ( N + 1 ) ]
    minVal = [ 0 for i in range ( N + 1 ) ]
    for i in range ( 1 , N + 1 , 1 ) :
        parent [ i ] = i
        minVal [ i ] = cost [ i - 1 ]
    for i in G :
        Union ( parent , rank , minVal , i [ 0 ] , i [ 1 ] )
    s = set ( )
    for i in range ( 1 , N + 1 , 1 ) :
        s . add ( Find ( parent , i ) )
    min1 = [ 0 , sys . maxsize ]
    flag = False
    for i in s :
        if ( minVal [ i ] < 0 ) :
            flag = True
        if ( min1 [ 1 ] > minVal [ i ] ) :
            min1 = [ i , minVal [ i ] ]
    minCost = 0
    if ( flag == False or ( flag and len ( s ) == 1 ) ) :
        for i in s :
            if ( i != min1 [ 0 ] ) :
                minCost += ( minVal [ i ] + min1 [ 1 ] )
        print ( minCost )
    else :
        print ( - 1 )
if __name__ == ' _ _ main _ _ ' :
    N = 6
    G = [ [ 3 , 1 ] , [ 2 , 3 ] , [ 2 , 1 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 4 ] ]
    cost = [ 2 , 1 , 5 , 3 , 2 , 9 ]
    M = len ( G )
    findMinCost ( G , cost , N , M )

Index/key variables: a, b, i
Accumulator variables: minCost, s
Iterator variables: i
Boolean variables: flag
Python program:
def minimumSizeArray ( S , P ) :
    if ( S == P ) :
        return 1
    for i in range ( 2 , S + 1 ) :
        d = i
        if ( ( S / d ) >= pow ( P , 1.0 / d ) ) :
            return i
    return - 1
if __name__ == " _ _ main _ _ " :
    S = 5
    P = 6
    print ( minimumSizeArray ( S , P ) )

Iterator variables: i
Python program:
def findVal ( A , B , N ) :
    s = set ( )
    for i in range ( N ) :
        s . add ( A [ i ] )
    A . sort ( )
    B . sort ( )
    X = B [ 0 ] - A [ 1 ]
    if ( X <= 0 ) :
        X = B [ 0 ] - A [ 0 ]
    else :
        for i in range ( N - 1 ) :
            if ( B [ i ] - X not in s ) :
                X = B [ 0 ] - A [ 0 ]
                break
    print ( X )
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 4 , 3 , 8 ]
    B = [ 15 , 8 , 11 ]
    N = len ( A )
    findVal ( A , B , N )

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
from math import sqrt
def countPoints ( D ) :
    count = 0
    for x in range ( 1 , int ( sqrt ( D * D ) ) , 1 ) :
        y = int ( sqrt ( ( D * D - x * x ) ) )
        if ( x * x + y * y == D * D ) :
            count += 4
    count += 4
    return count
if __name__ == ' _ _ main _ _ ' :
    D = 5
    print ( countPoints ( D ) )

Accumulator variables: count
Iterator variables: x
Python program:
INT_MAX = 2147483647
def findTotalDistance ( S , N ) :
    prefixDistance = [ 0 for _ in range ( N ) ]
    suffixDistance = [ 0 for _ in range ( N ) ]
    cnt = 0
    haveOne = False
    for i in range ( 0 , N ) :
        if ( S [ i ] == '1' ) :
            haveOne = True
            cnt = 0
            prefixDistance [ i ] = 0
        elif ( haveOne ) :
            cnt = cnt + 1
            prefixDistance [ i ] = cnt
        else :
            prefixDistance [ i ] = INT_MAX
    haveOne = False
    for i in range ( N - 1 , - 1 , - 1 ) :
        if ( S [ i ] == '1' ) :
            haveOne = True
            cnt = 0
            suffixDistance [ i ] = 0
        elif ( haveOne ) :
            cnt = cnt + 1
            suffixDistance [ i ] = cnt
        else :
            suffixDistance [ i ] = INT_MAX
    sum = 0
    for i in range ( 0 , N ) :
        if ( S [ i ] == '0' ) :
            sum += min ( prefixDistance [ i ] , suffixDistance [ i ] )
    print ( sum )
if __name__ == " _ _ main _ _ " :
    S = "100100"
    N = len ( S )
    findTotalDistance ( S , N )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Boolean variables: haveOne
Python program:
def minimizeCost ( A , B , C ) :
    ans = A + B + C
    for i in range ( 1 , 2 * C + 1 ) :
        j = 0
        while ( i * j <= 2 * C ) :
            ans = min ( ans , abs ( A - i ) + abs ( B - j ) + abs ( i * j - C ) )
            j += 1
    return ans
A = 19
B = 28
C = 522
print ( minimizeCost ( A , B , C ) )

Accumulator variables: j
Iterator variables: i
Python program:
def averageSetBits ( N , K , arr ) :
    p = N
    q = 0
    for i in range ( K ) :
        _p = p
        _q = q
        p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N
        q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N
    return p
if __name__ == " _ _ main _ _ " :
    N = 5
    arr = [ 1 , 2 , 3 ]
    K = len ( arr )
    print ( " % .2f " % averageSetBits ( N , K , arr ) )

Index/key variables: i
Iterator variables: i
Python program:
def maxSubseq ( arr , N , K ) :
    sum = 0
    arr . sort ( )
    for i in range ( N ) :
        if ( K == 0 ) :
            break
        if ( arr [ i ] < 0 ) :
            arr [ i ] = - arr [ i ]
            K -= 1
    for i in range ( N ) :
        if ( arr [ i ] > 0 ) :
            sum += arr [ i ]
    return sum
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , - 10 , - 1 , 0 , - 4 , 2 ]
    K = 2
    N = len ( arr )
    print ( maxSubseq ( arr , N , K ) )

Index/key variables: i
Accumulator variables: K, sum
Iterator variables: i
Python program:
def minimizeSegment ( A , X ) :
    A . sort ( )
    INT_MAX = 2147483647
    A . append ( [ INT_MAX , INT_MAX ] )
    start = X [ 0 ]
    end = X [ 0 ] - 1
    cnt = 0
    for i in range ( 0 , len ( A ) ) :
        if ( A [ i ] [ 0 ] <= start ) :
            end = max ( A [ i ] [ 1 ] , end )
            i = i + 1
        else :
            start = end
            cnt = cnt + 1
            if ( A [ i ] [ 0 ] > end or end >= X [ 1 ] ) :
                break
    if ( end < X [ 1 ] ) :
        return - 1
    return cnt
if __name__ == " _ _ main _ _ " :
    A = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 2 , 10 ] , [ 2 , 3 ] , [ 1 , 1 ] ]
    X = [ 1 , 10 ]
    print ( minimizeSegment ( A , X ) )

Index/key variables: i
Iterator variables: i
Python program:
def KthSolution ( X , K ) :
    ans = 0
    for i in range ( 0 , 64 ) :
        if ( not ( X & ( 1 << i ) ) ) :
            if ( K & 1 ) :
                ans |= ( 1 << i )
            K >>= 1
            if ( not K ) :
                break
    return ans
if __name__ == " _ _ main _ _ " :
    X = 5
    K = 5
    print ( KthSolution ( X , K ) )

Accumulator variables: K, ans
Iterator variables: i
Python program:
def printArray ( arr , n ) :
    for i in range ( N ) :
        print ( arr [ i ] , end = " ▁ " )
    print ( " " )
def rearrange ( arr , N ) :
    if ( N & 1 ) :
        N -= 1
    odd_idx = 1
    even_idx = 0
    max_elem = arr [ N - 1 ] + 1
    for i in range ( N // 2 ) :
        arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem
        odd_idx += 2
    for i in range ( N // 2 , N ) :
        arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem
        even_idx += 2
    for i in range ( N ) :
        arr [ i ] = arr [ i ] // max_elem
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 ]
    N = len ( arr )
    rearrange ( arr , N )
    printArray ( arr , N ) ;

Index/key variables: even_idx, i, odd_idx
Iterator variables: i
Python program:
from math import ceil , floor
import sys
def findRange ( arr , N , K ) :
    L = - sys . maxsize - 1
    R = sys . maxsize
    for i in range ( N ) :
        l = ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) )
        r = ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) - 1 )
        L = max ( L , l )
        R = min ( R , r )
    print ( L , R )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 4 , 6 , 9 , 11 ]
    K = 10
    N = len ( arr )
    findRange ( arr , N , K )

Index/key variables: i
Iterator variables: i
Python program:
def findAnagram ( s ) :
    check = s
    st = list ( s )
    i = 0
    j = len ( st ) - 1
    while ( i < len ( st ) and j >= 0 ) :
        if ( st [ i ] != st [ j ] and check [ i ] != st [ j ] and check [ j ] != st [ i ] ) :
            st [ i ] , st [ j ] = st [ j ] , st [ i ]
            i += 1
            j = len ( st ) - 1
        else :
            j -= 1
    if ( len ( st ) % 2 != 0 ) :
        mid = len ( st ) / 2
        if ( check [ mid ] == st [ mid ] ) :
            for i in range ( len ( st ) ) :
                if ( check [ i ] != st [ mid ] and st [ i ] != st [ mid ] ) :
                    st [ i ] , st [ mid ] = st [ mid ] , st [ i ]
                    break
    ok = True
    for i in range ( len ( st ) ) :
        if ( check [ i ] == st [ i ] ) :
            ok = False
            break
    if ( ok ) :
        print ( " " . join ( st ) )
    else :
        print ( - 1 )
if __name__ == " _ _ main _ _ " :
    S = " geek "
    findAnagram ( S )

Index/key variables: i, j, mid
Iterator variables: i
Boolean variables: ok