dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
MAXN = 1000005 ;
even = [ 0 ] * MAXN ;
odd = [ 0 ] * MAXN ;
def precompute ( arr , n ) :
    for i in range ( n ) :
        if ( arr [ i ] % 2 == 1 ) :
            odd [ i ] = 1 ;
        if ( arr [ i ] % 2 == 0 ) :
            even [ i ] = 1 ;
    for i in range ( 1 , n ) :
        even [ i ] = even [ i ] + even [ i - 1 ] ;
        odd [ i ] = odd [ i ] + odd [ i - 1 ] ;
def isOdd ( L , R ) :
    cnt = odd [ R ] ;
    if ( L > 0 ) :
        cnt -= odd [ L - 1 ] ;
    if ( cnt == R - L + 1 ) :
        return True ;
    return False ;
def performQueries ( a , n , q , m ) :
    precompute ( a , n ) ;
    for i in range ( m ) :
        L = q [ i ] [ 0 ] ;
        R = q [ i ] [ 1 ] ;
        if ( isOdd ( L , R ) ) :
            print ( " Odd " ) ;
        else :
            print ( " Even " ) ;
if __name__ == ' _ _ main _ _ ' :
    a = [ 2 , 1 , 5 , 7 , 6 , 8 , 9 ] ;
    n = len ( a ) ;
    q = [ [ 0 , 2 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 6 ] ] ;
    m = len ( q ) ;
    performQueries ( a , n , q , m ) ;

Index/key variables: R, i
Iterator variables: i
Python program:
from math import sqrt
def findTriplets ( x ) :
    fact = [ ] ;
    factors = set ( ) ;
    for i in range ( 2 , int ( sqrt ( x ) ) ) :
        if ( x % i == 0 ) :
            fact . append ( i ) ;
            if ( x / i != i ) :
                fact . append ( x // i ) ;
            factors . add ( i ) ;
            factors . add ( x // i ) ;
    found = False ;
    k = len ( fact ) ;
    for i in range ( k ) :
        a = fact [ i ] ;
        for j in range ( k ) :
            b = fact [ j ] ;
            if ( ( a != b ) and ( x % ( a * b ) == 0 ) and ( x / ( a * b ) != a ) and ( x / ( a * b ) != b ) and ( x / ( a * b ) != 1 ) ) :
                print ( a , b , x // ( a * b ) ) ;
                found = True ;
                break ;
        if ( found ) :
            break ;
    if ( not found ) :
        print ( " - 1" ) ;
if __name__ == " _ _ main _ _ " :
    x = 105 ;
    findTriplets ( x ) ;

Index/key variables: i, j
Accumulator variables: fact, factors
Iterator variables: i, j
Boolean variables: found
Python program:
def countPair ( a , b ) :
    s = str ( b )
    i = 0
    while i < ( len ( s ) ) :
        if ( s [ i ] != '9' ) :
            break
        i += 1
    result = 0
    if ( i == len ( s ) ) :
        result = a * len ( s )
    else :
        result = a * ( len ( s ) - 1 )
    return result
a = 5
b = 101
print ( countPair ( a , b ) )

Index/key variables: i
Python program:
def makeSeq ( s , a ) :
    seq = [ ] ;
    for i in range ( len ( s ) ) :
        if ( s [ i ] == ' A ' ) :
            seq . append ( a [ i ] ) ;
    for i in range ( len ( s ) ) :
        if ( s [ i ] == ' B ' ) :
            seq . append ( a [ i ] ) ;
    return seq ;
def checkSeq ( v ) :
    check = True ;
    for i in range ( 1 , len ( v ) ) :
        if ( v [ i ] < v [ i - 1 ] ) :
            check = False ;
    return check ;
def digitPartition ( X ) :
    num = str ( X ) ;
    l = len ( num ) ;
    a = [ 0 ] * l ;
    for i in range ( l ) :
        a [ i ] = ( ord ( num [ i ] ) - ord ( '0' ) ) ;
    for D in range ( 10 ) :
        res = " " ;
        for i in range ( l ) :
            if ( a [ i ] < D ) :
                res += ' A ' ;
            elif ( a [ i ] > D ) :
                res += ' B ' ;
            else :
                res += ' C ' ;
        flag = False ;
        for i in range ( l ) :
            if ( res [ i ] == ' B ' ) :
                flag = True ;
            res = list ( res ) ;
            if ( res [ i ] == ' C ' ) :
                res [ i ] = ' A ' if flag else ' B ' ;
        seq = makeSeq ( res , a ) ;
        if ( checkSeq ( seq ) ) :
            return " " . join ( res ) ;
    return " - 1" ;
if __name__ == " _ _ main _ _ " :
    X = 777147777 ;
    print ( digitPartition ( X ) ) ;

Index/key variables: i
Accumulator variables: res, seq
Iterator variables: D, i
Boolean variables: check, flag
Python program:
def flipsPossible ( a , n ) :
    count_odd = 0 ; count_even = 0 ;
    for i in range ( n ) :
        if ( a [ i ] & 1 ) :
            count_odd += 1 ;
        else :
            count_even += 1 ;
    if ( count_odd % 2 and count_even % 2 ) :
        return False ;
    else :
        return True ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 0 , 1 , 1 , 0 , 1 ] ;
    n = len ( a ) ;
    if ( flipsPossible ( a , n ) ) :
        print ( " Yes " ) ;
    else :
        print ( " No " ) ;

Index/key variables: i
Accumulator variables: count_even, count_odd
Iterator variables: i
Python program:
def PerformQueries ( a , vec ) :
    ans = [ ] ;
    n = len ( a ) - 1 ;
    q = len ( vec ) ;
    for i in range ( q ) :
        t = vec [ i ] [ 0 ] ;
        m = vec [ i ] [ 1 ] ;
        if ( m > n ) :
            ans . append ( - 1 ) ;
            continue ;
        turn = t // n ;
        rem = t % n ;
        if ( rem == 0 and turn % 2 == 1 ) :
            ans . append ( - 1 ) ;
            continue ;
        if ( rem == 0 and turn % 2 == 0 ) :
            ans . append ( a [ m ] ) ;
            continue ;
        if ( turn % 2 == 0 ) :
            cursize = n - rem ;
            if ( cursize < m ) :
                ans . append ( - 1 ) ;
                continue ;
            ans . append ( a [ m + rem ] ) ;
        else :
            cursize = rem ;
            if ( cursize < m ) :
                ans . append ( - 1 ) ;
                continue ;
            ans . append ( a [ m ] ) ;
    for i in ans :
        print ( i ) ;
if __name__ == " _ _ main _ _ " :
    a = [ - 1 , 1 , 2 , 3 , 4 , 5 ] ;
    vec = [ [ 1 , 4 ] , [ 6 , 1 ] , [ 3 , 5 ] ] ;
    PerformQueries ( a , vec ) ;

Index/key variables: i, m
Accumulator variables: ans
Iterator variables: i
Python program:
def sumEqualProduct ( a , n ) :
    zero = 0
    two = 0
    for i in range ( n ) :
        if a [ i ] == 0 :
            zero += 1
        if a [ i ] == 2 :
            two += 1
    cnt = ( zero * ( zero - 1 ) ) // 2 + ( two * ( two - 1 ) ) // 2
    return cnt
a = [ 2 , 2 , 3 , 4 , 2 , 6 ]
n = len ( a )
print ( sumEqualProduct ( a , n ) )

Index/key variables: i
Accumulator variables: two, zero
Iterator variables: i
Python program:
def minSum ( n ) :
    sum = 0 ;
    while ( n > 0 ) :
        sum += ( n % 10 ) ;
        n //= 10 ;
    if ( sum == 1 ) :
        return 10 ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    n = 1884 ;
    print ( minSum ( n ) ) ;

Accumulator variables: n, sum
Python program:
def initialize ( mul , div , size ) :
    for i in range ( 1 , size ) :
        mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ;
def update ( l , r , x , mul , div ) :
    mul [ l ] *= x ;
    div [ r + 1 ] *= x ;
def printArray ( ar , mul , div , n ) :
    for i in range ( n ) :
        ar [ i ] = ar [ i ] * mul [ i ] ;
        print ( int ( ar [ i ] ) , end = " ▁ " ) ;
if __name__ == ' _ _ main _ _ ' :
    ar = [ 10 , 5 , 20 , 40 ] ;
    n = len ( ar ) ;
    mul = [ 0 ] * ( n + 1 ) ;
    div = [ 0 ] * ( n + 1 ) ;
    for i in range ( n + 1 ) :
        mul [ i ] = div [ i ] = 1 ;
    update ( 0 , 1 , 10 , mul , div ) ;
    update ( 1 , 3 , 20 , mul , div ) ;
    update ( 2 , 2 , 2 , mul , div ) ;
    initialize ( mul , div , n + 1 ) ;
    printArray ( ar , mul , div , n ) ;

Index/key variables: i, l
Iterator variables: i
Python program:
def getWinner ( string , length ) :
    total = 0 ;
    for i in range ( length ) :
        if ( string [ i ] == ' S ' ) :
            total += 1 ;
    if ( ( total % 2 ) == 1 ) :
        return " First " ;
    return " Second " ;
if __name__ == " _ _ main _ _ " :
    string = " GSSS " ;
    length = len ( string ) ;
    print ( getWinner ( string , length ) ) ;

Index/key variables: i
Accumulator variables: total
Iterator variables: i
Python program:
def printArr ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " ) ;
def findArrangement ( arr , n ) :
    if ( n < 2 ) :
        print ( " - 1" , end = " " ) ;
        return ;
    minVal = min ( arr ) ;
    for i in range ( n ) :
        if ( arr [ i ] == minVal ) :
            arr [ i ] , arr [ n - 1 ] = arr [ n - 1 ] , arr [ i ] ;
            break ;
    andVal = arr [ 0 ] ;
    for i in range ( 1 , n - 1 ) :
        andVal &= arr [ i ] ;
    if ( andVal == arr [ n - 1 ] ) :
        printArr ( arr , n ) ;
    else :
        print ( " - 1" ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 5 , 3 , 3 ] ;
    n = len ( arr ) ;
    findArrangement ( arr , n ) ;

Index/key variables: i
Accumulator variables: andVal
Iterator variables: i
Python program:
DIGITS = 10 ;
MAX = 10000 ;
MOD = 1000000007 ;
dp = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ;
cnt = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ;
def precompute ( ) :
    for i in range ( DIGITS ) :
        dp [ 1 ] [ i ] = i ;
        cnt [ 1 ] [ i ] = 1 ;
    for i in range ( 2 , MAX ) :
        for j in range ( DIGITS ) :
            for k in range ( DIGITS ) :
                if ( j != k ) :
                    dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ;
                    dp [ i ] [ j ] %= MOD ;
                    cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ;
                    cnt [ i ] [ j ] %= MOD ;
x = 6 ; y = 4 ;
precompute ( ) ;
print ( dp [ x ] [ y ] ) ;

Index/key variables: i, j, k, x, y
Iterator variables: i, j, k
Python program:
N = 10000 ;
MOD = 1000000007 ;
F = [ 0 ] * N ;
def precompute ( ) :
    F [ 1 ] = 2 ;
    F [ 2 ] = 3 ;
    F [ 3 ] = 4 ;
    for i in range ( 4 , N ) :
        F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ;
n = 8 ;
precompute ( ) ;
print ( F [ n ] ) ;

Index/key variables: i, n
Iterator variables: i
Python program:
import sys
def maxSubArraySum ( a , size ) :
    max_so_far = - ( sys . maxsize - 1 ) ;
    max_ending_here = 0 ;
    for i in range ( size ) :
        max_ending_here = max_ending_here + a [ i ] ;
        if ( max_so_far < max_ending_here ) :
            max_so_far = max_ending_here ;
        if ( max_ending_here < 0 ) :
            max_ending_here = 0 ;
    return max_so_far ;
def minimizedSum ( a , n , K ) :
    sum = maxSubArraySum ( a , n ) ;
    totalSum = 0 ;
    for i in range ( n ) :
        totalSum += a [ i ] ;
    if ( sum < 0 ) :
        return totalSum ;
    totalSum = totalSum - sum + sum / K ;
    return totalSum ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , - 2 , 3 ] ;
    n = len ( a ) ;
    K = 2 ;
    print ( minimizedSum ( a , n , K ) ) ;

Index/key variables: i
Accumulator variables: totalSum
Iterator variables: i
Python program:
from random import randint
def printArr ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " ) ;
def randomList ( m , n ) :
    arr = [ 0 ] * m ;
    for i in range ( n ) :
        arr [ randint ( 0 , n ) % m ] += 1 ;
    printArr ( arr , m ) ;
if __name__ == " _ _ main _ _ " :
    m = 4 ; n = 8 ;
    randomList ( m , n ) ;

Index/key variables: i
Iterator variables: i
Python program:
def maxPartition ( s ) :
    n = len ( s )
    P = 0
    current = " "
    previous = " "
    for i in range ( n ) :
        current += s [ i ]
        if ( current != previous ) :
            previous = current
            current = " "
            P += 1
    return P
s = " geeksforgeeks "
ans = maxPartition ( s )
print ( ans )

Index/key variables: i
Accumulator variables: P, current
Iterator variables: i
Python program:
N = 4 ;
print ( " Value ▁ of ▁ N : ▁ " , N ) ;
for i in range ( 1 , N + 1 ) :
    for j in range ( 1 , N + 1 ) :
        min = i if i < j else j ;
        print ( N - min + 1 , end = " " ) ;
    for j in range ( N - 1 , 0 , - 1 ) :
        min = i if i < j else j ;
        print ( N - min + 1 , end = " " ) ;
    print ( ) ;

Iterator variables: i, j
Python program:
def findMissingPositive ( arr , n ) :
    m = 1
    x = [ ]
    for i in range ( n ) :
        if ( m < arr [ i ] ) :
            x . append ( arr [ i ] )
        elif ( m == arr [ i ] ) :
            m = m + 1
            while ( x . count ( m ) ) :
                x . remove ( m )
                m = m + 1
    return m
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 ]
    n = len ( arr )
    print ( findMissingPositive ( arr , n ) )

Index/key variables: i
Accumulator variables: x
Iterator variables: i
Python program:
from math import sqrt
def prime ( n ) :
    if ( n == 1 ) :
        return False
    for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) :
        if ( n % i == 0 ) :
            return False
    return True
def Min_Sum ( n ) :
    if ( prime ( n ) ) :
        return 1
    if ( n % 2 == 0 ) :
        return 2
    else :
        if ( prime ( n - 2 ) ) :
            return 2
        else :
            return 3
if __name__ == ' _ _ main _ _ ' :
    n = 27
    print ( Min_Sum ( n ) )

Iterator variables: i
Python program:
def find_maximum_value ( a , n ) :
    sum = 0
    minimum = 10 ** 9
    pos = 0
    neg = 0
    for i in range ( n ) :
        minimum = min ( minimum , abs ( a [ i ] ) )
        sum += abs ( a [ i ] )
        if ( a [ i ] >= 0 ) :
            pos += 1
        else :
            neg += 1
    if ( pos > 0 and neg > 0 ) :
        return sum
    return ( sum - 2 * minimum )
a = [ 5 , 4 , 6 , 2 ]
n = len ( a )
print ( find_maximum_value ( a , n ) )

Index/key variables: i
Accumulator variables: neg, pos, sum
Iterator variables: i
Python program:
def find_number ( N , K ) :
    r = " "
    if ( N % 100 == 0 ) :
        N += 1 ;
        r = str ( N )
    else :
        r = str ( N )
    for i in range ( 1 , K + 1 ) :
        r += "00"
    return r
N = 1000
K = 2 ;
ans = find_number ( N , K )
print ( ans )

Accumulator variables: r
Iterator variables: i
Python program:
def countPairs ( s ) :
    st = set ( ) ;
    st . add ( ' a ' ) ;
    st . add ( ' e ' ) ;
    st . add ( ' i ' ) ;
    st . add ( ' o ' ) ;
    st . add ( ' u ' ) ;
    count = 0 ;
    n = len ( s ) ;
    for i in range ( n - 1 ) :
        if ( s [ i ] not in st and s [ i + 1 ] in st ) :
            count += 1 ;
    return count ;
if __name__ == " _ _ main _ _ " :
    s = " geeksforgeeks " ;
    print ( countPairs ( s ) ) ;

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def maxSize ( v , n ) :
    v . append ( 0 )
    v . append ( 100001 )
    n += 2
    v = sorted ( v )
    mx = 0
    for i in range ( 1 , n - 1 ) :
        L = v [ i - 1 ] + 1
        R = v [ i + 1 ] - 1
        cnt = R - L + 1
        mx = max ( mx , cnt )
    return mx
v = [ 200 , 10 , 5 ]
n = len ( v )
print ( maxSize ( v , n ) )

Iterator variables: i
Python program:
def maxLength ( a , n ) :
    Sum = 0
    Len = 0
    for i in range ( n ) :
        Sum += a [ i ]
        return n
    for i in range ( n ) :
        if ( a [ i ] % 2 == 1 ) :
            Len = max ( Len , max ( n - i - 1 , i ) )
    return Len
a = [ 1 , 2 , 3 , 2 ]
n = len ( a )
print ( maxLength ( a , n ) )

Index/key variables: i
Accumulator variables: Sum
Iterator variables: i
Python program:
def findPerm ( n , differences ) :
    ans = [ ]
    ans . append ( 0 )
    x = 0
    for i in range ( n - 1 ) :
        diff = differences [ i ]
        x = x + diff
        ans . append ( x )
    anss = ans
    ans = sorted ( ans )
    flag = - 1
    for i in range ( 1 , n ) :
        res = ans [ i ] - ans [ i - 1 ]
        if ( res != 1 ) :
            flag = 0
    if ( flag == 0 ) :
        print ( " - 1" )
        return
    else :
        mpp = dict ( )
        j = 1
        value_at_index = [ ]
        for x in ans :
            mpp [ x ] = j
            j += 1
        for x in anss :
            value_at_index . append ( mpp [ x ] )
        for x in value_at_index :
            print ( x , end = " ▁ " )
        print ( )
differences = [ ]
differences . append ( 2 )
differences . append ( - 3 )
differences . append ( 2 )
n = len ( differences ) + 1
findPerm ( n , differences )

Index/key variables: i, x
Accumulator variables: ans, j, value_at_index
Iterator variables: i, x
Python program:
def sumOfDigit ( n ) :
    sum = 0 ;
    while ( n > 0 ) :
        sum += n % 10 ;
        n = n // 10 ;
    return sum ;
def sortArr ( arr , n ) :
    vp = [ ] ;
    for i in range ( n ) :
        vp . append ( [ sumOfDigit ( arr [ i ] ) , arr [ i ] ] ) ;
    vp . sort ( )
    for i in range ( len ( vp ) ) :
        print ( vp [ i ] [ 1 ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 14 , 1101 , 10 , 35 , 0 ] ;
    n = len ( arr ) ;
    sortArr ( arr , n ) ;

Index/key variables: i
Accumulator variables: sum, vp
Iterator variables: i
Python program:
import math as mt
def isPalindrome ( str1 , i , j ) :
    while ( i < j ) :
        if ( str1 [ i ] != str1 [ j ] ) :
            return False
        i += 1
        j -= 1
    return True
def countPrimePalindrome ( str1 , Len ) :
    prime = [ True for i in range ( Len + 1 ) ]
    prime [ 0 ] , prime [ 1 ] = False , False
    for p in range ( 2 , mt . ceil ( mt . sqrt ( Len + 1 ) ) ) :
        if ( prime [ p ] ) :
            for i in range ( 2 * p , Len + 1 , p ) :
                prime [ i ] = False
    count = 0
    for j in range ( 2 , Len + 1 ) :
        if ( prime [ j ] ) :
            for i in range ( Len + 1 - j ) :
                if ( isPalindrome ( str1 , i , i + j - 1 ) ) :
                    count += 1
    return count
s = " geeksforgeeks "
Len = len ( s )
print ( countPrimePalindrome ( s , Len ) )

Index/key variables: i, j, p
Accumulator variables: count
Iterator variables: i, j, p
Python program:
def check ( arr , n ) :
    for i in range ( n ) :
        if arr [ i ] & 1 :
            return True
    return False
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 9 , 12 , 13 , 15 ]
    n = len ( arr )
    if check ( arr , n ) :
        print ( " Odd ▁ Bit - wise ▁ OR " )
    else :
        print ( " Even ▁ Bit - wise ▁ OR " )

Index/key variables: i
Iterator variables: i
Python program:
from timeit import default_timer as clock
if __name__ == " _ _ main _ _ " :
    n = 800000 ;
    t = clock ( )
    sum = 0
    for i in range ( 1 , n + 1 ) :
        sum += i
    t = clock ( ) - t
    print ( " sum ▁ is : ▁ " + str ( sum ) )
    print ( " time ▁ taken ▁ by ▁ normal ▁ " + " loops : " + str ( t ) )
    t = clock ( )
    sum = 0
    for i in range ( 1 , n + 1 , 8 ) :
        sum += i
        sum += ( i + 1 )
        sum += ( i + 2 )
        sum += ( i + 3 )
        sum += ( i + 4 )
        sum += ( i + 5 )
        sum += ( i + 6 )
        sum += ( i + 7 )
    t = clock ( ) - t
    print ( " Sum ▁ is : ▁ " + str ( sum ) )
    print ( " Time ▁ taken ▁ by ▁ unrolling : ▁ " + str ( t ) )

Accumulator variables: sum
Iterator variables: i
Python program:
def findMaximum ( arr , size ) :
    i = 0
    j = size - 1
    while ( i <= j ) :
        mid = i + ( j - i ) // 2
        if ( arr [ mid ] < 0 ) :
            i = mid + 1
        elif ( arr [ mid ] > 0 ) :
            j = mid - 1
    return max ( i , size - i )
if __name__ == " _ _ main _ _ " :
    arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ]
    N = len ( arr )
    print ( findMaximum ( arr , N ) )

Index/key variables: mid
Python program:
def solve ( n , m , queries ) :
    countE1 = 0
    removed = set ( )
    for i in range ( 0 , m ) :
        if ( queries [ i ] [ 0 ] == 1 ) :
            countE1 += 1
        elif ( queries [ i ] [ 0 ] == 2 ) :
            removed . add ( queries [ i ] [ 1 ] )
        else :
            position = queries [ i ] [ 1 ] - countE1
            if ( queries [ i ] [ 1 ] in removed or position <= 0 ) :
                print ( " - 1" )
            else :
                for it in removed :
                    if ( it > queries [ i ] [ 1 ] ) :
                        break
                    position -= 1
                print ( position )
if __name__ == " _ _ main _ _ " :
    N = 5
    Q = 3
    queries = [ [ 0 for _ in range ( 2 ) ] for _ in range ( Q ) ]
    queries [ 0 ] [ 0 ] = 1
    queries [ 0 ] [ 1 ] = 0
    queries [ 1 ] [ 0 ] = 3
    queries [ 1 ] [ 1 ] = 3
    queries [ 2 ] [ 0 ] = 2
    queries [ 2 ] [ 1 ] = 2
    solve ( N , Q , queries )

Index/key variables: i
Accumulator variables: countE1, position, removed
Iterator variables: i, it
Python program:
from bisect import bisect_left
def maxIndex ( N , B ) :
    maximumIndexReached = 0
    Ans = [ ]
    for i in range ( 1 , N + 1 ) :
        maximumIndexReached += i
        Ans . append ( i )
    Ans . reverse ( )
    for i in range ( len ( Ans ) ) :
        Ans [ i ] += Ans [ i - 1 ]
    while ( maximumIndexReached ) :
        it = bisect_left ( Ans , maximumIndexReached - B )
        if ( it not in Ans ) :
            break
        if ( it == maximumIndexReached - B ) :
            maximumIndexReached -= 1
        else :
            break
    return maximumIndexReached
if __name__ == " _ _ main _ _ " :
    N = 3
    B = 2
    print ( maxIndex ( N , B ) )

Index/key variables: i
Accumulator variables: Ans, maximumIndexReached
Iterator variables: i
Python program:
def maxKth ( arr , N , C , K ) :
    if ( N + C < K ) :
        return - 1
    iter = 300
    l = 0
    r = 1000000000.0
    while ( iter ) :
        iter = iter - 1
        mid = ( l + r ) * 0.5
        a = 0
        b = 0
        for i in range ( N ) :
            a += arr [ i ] // mid
            if ( arr [ i ] >= mid ) :
                b += 1
        if ( a >= K and b + C >= K ) :
            l = mid
        else :
            r = mid
    return int ( l )
arr = [ 5 , 8 ]
K = 1
C = 1
N = len ( arr )
print ( maxKth ( arr , N , C , K ) )

Index/key variables: i
Accumulator variables: a, b
Iterator variables: i
Python program:
def isPossibleToSelect ( arr , N , dif , K ) :
    count = 1
    prev = arr [ 0 ]
    for i in range ( 1 , N ) :
        if ( arr [ i ] >= ( prev + dif ) ) :
            count += 1
            if ( count == K ) :
                return True
            prev = arr [ i ]
    return False
def binarySearch ( arr , left , right , K , N ) :
    ans = 1
    while ( left <= right ) :
        dif = left + ( right - left ) // 2
        if ( isPossibleToSelect ( arr , N , dif , K ) ) :
            ans = max ( ans , dif )
            left = dif + 1
        else :
            right = dif - 1
    return ans
if __name__ == " _ _ main _ _ " :
    N = 7
    K = 4
    arr = [ 1 , 4 , 9 , 0 , 2 , 13 , 3 ]
    arr . sort ( )
    print ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def cal ( arr , mid ) :
    chocolate = 0
    for i in arr :
        if i >= mid :
            chocolate += i - mid
    return chocolate
def maximumCut ( arr , K ) :
    low = 0
    high = max ( arr )
    while low <= high :
        mid = ( low + high ) // 2
        chocolate = cal ( arr , mid )
        if chocolate == K :
            return mid
        elif chocolate < K :
            high = mid - 1
        else :
            low = mid + 1
            if mid > high :
                high = mid
    return high
N , K = 4 , 7
arr = [ 15 , 20 , 8 , 17 ]
print ( maximumCut ( arr , K ) )

Accumulator variables: chocolate
Iterator variables: i
Python program:
import sys
def minSumPair ( arr , N ) :
    if ( N < 5 ) :
        return - 1
    prefixMin = [ 0 for i in range ( N ) ]
    prefixMin [ 0 ] = arr [ 0 ]
    for i in range ( 1 , N - 1 , 1 ) :
        prefixMin [ i ] = min ( arr [ i ] , prefixMin [ i - 1 ] )
    ans = sys . maxsize
    for i in range ( 3 , N - 1 , 1 ) :
        ans = min ( ans , arr [ i ] + prefixMin [ i - 2 ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 2 , 4 , 6 , 3 , 7 ]
    N = len ( arr )
    print ( minSumPair ( arr , N ) )

Index/key variables: i
Iterator variables: i