dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def binarySearch ( searchSpace , s , e , num ) :
    while ( s <= e ) :
        mid = ( s + e ) // 2
        if searchSpace [ mid ] >= num :
            ans = mid
            e = mid - 1
        else :
            s = mid + 1
    return ans
def longestSubArr ( arr , n ) :
    searchSpace = [ None ] * n
    index = [ None ] * n
    j = 0
    ans = 0
    for i in range ( n ) :
        if ( j == 0 or searchSpace [ j - 1 ] < arr [ i ] ) :
            searchSpace [ j ] = arr [ i ]
            index [ j ] = i
            j += 1
        idx = binarySearch ( searchSpace , 0 , j - 1 , arr [ i ] )
        ans = max ( ans , i - index [ idx ] + 1 )
    return ans
if __name__ == " _ _ main _ _ " :
    arr = [ - 5 , - 1 , 7 , 5 , 1 , - 2 ]
    n = len ( arr )
    print ( longestSubArr ( arr , n ) )

Index/key variables: i, idx, j, mid
Iterator variables: i
Python program:
def pairedElements ( arr , sum ) :
    low = 0 ;
    high = len ( arr ) - 1 ;
    while ( low < high ) :
        if ( arr [ low ] + arr [ high ] == sum ) :
            print ( " The ▁ pair ▁ is ▁ : ▁ ( " , arr [ low ] , " , ▁ " , arr [ high ] , " ) " ) ;
        if ( arr [ low ] + arr [ high ] > sum ) :
            high -= 1 ;
        else :
            low += 1 ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 ] ;
    arr . sort ( ) ;
    pairedElements ( arr , 6 ) ;

Index/key variables: high, low
Python program:
def isSuffix ( s1 , s2 ) :
    n1 = len ( s1 )
    n2 = len ( s2 )
    if ( n1 > n2 ) :
        return False
    for i in range ( n1 ) :
        if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) :
            return False
    return True
if __name__ == " _ _ main _ _ " :
    s1 = " geeks "
    s2 = " geeksforgeeks "
    result = isSuffix ( s1 , s2 )
    if ( result ) :
        print ( " Yes " )
    else :
        print ( " No " )

Iterator variables: i
Python program:
def checkIfAllTogether ( s , c ) :
    oneSeen = False
    i = 0
    n = len ( s )
    while ( i < n ) :
        if ( s [ i ] == c ) :
            if ( oneSeen == True ) :
                return False
            while ( i < n and s [ i ] == c ) :
                i = i + 1
            oneSeen = True
        else :
            i = i + 1
    return True
s = "110029" ;
if ( checkIfAllTogether ( s , '1' ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i
Boolean variables: oneSeen
Python program:
def search ( arr , n , x ) :
    front = 0 ; back = n - 1
    while ( front <= back ) :
        if ( arr [ front ] == x or arr [ back ] == x ) :
            return True
        front += 1
        back -= 1
    return False
arr = [ 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ]
x = 130
n = len ( arr )
if ( search ( arr , n , x ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: back, front
Python program:
def countCrossLine ( arr , n ) :
    count_crossline = 0 ;
    i , key , j = 0 , 0 , 0 ;
    for i in range ( 1 , n ) :
        key = arr [ i ] ;
        j = i - 1 ;
        while ( j >= 0 and arr [ j ] > key ) :
            arr [ j + 1 ] = arr [ j ] ;
            j = j - 1 ;
            count_crossline += 1 ;
        arr [ j + 1 ] = key ;
    return count_crossline ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 3 , 1 , 2 ] ;
    n = len ( arr ) ;
    print ( countCrossLine ( arr , n ) ) ;

Index/key variables: i, j
Accumulator variables: count_crossline
Iterator variables: i
Python program:
def minSwapsAsc ( arr , n ) :
    arrPos = [ [ arr [ i ] , i ] for i in range ( n ) ]
    arrPos = sorted ( arrPos )
    vis = [ False ] * ( n )
    ans = 0
    for i in range ( n ) :
        if ( vis [ i ] or arrPos [ i ] [ 1 ] == i ) :
            continue
        cycle_size = 0
        j = i
        while ( not vis [ j ] ) :
            vis [ j ] = 1
            j = arrPos [ j ] [ 1 ]
            cycle_size += 1
        if ( cycle_size > 0 ) :
            ans += ( cycle_size - 1 )
    return ans
def minSwapsDes ( arr , n ) :
    arrPos = [ [ 0 , 0 ] for i in range ( n ) ]
    for i in range ( n ) :
        arrPos [ i ] [ 0 ] = arr [ i ]
        arrPos [ i ] [ 1 ] = i
    arrPos = sorted ( arrPos ) [ : : - 1 ]
    vis = [ False ] * n
    ans = 0
    for i in range ( n ) :
        if ( vis [ i ] or arrPos [ i ] [ 1 ] == i ) :
            continue
        cycle_size = 0
        j = i
        while ( not vis [ j ] ) :
            vis [ j ] = 1
            j = arrPos [ j ] [ 1 ]
            cycle_size += 1
        if ( cycle_size > 0 ) :
            ans += ( cycle_size - 1 )
    return ans
def minimumSwaps ( arr ) :
    S1 = minSwapsAsc ( arr , len ( arr ) )
    S2 = minSwapsDes ( arr , len ( arr ) )
    return min ( S1 , S2 )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 4 , 2 , 5 , 1 ]
    print ( minimumSwaps ( arr ) )

Index/key variables: i, j
Accumulator variables: ans, cycle_size
Iterator variables: i
Python program:
def sortArray ( arr , N ) :
    i = 0
    j = N - 1
    mid = 0
    while ( mid <= j ) :
        if ( arr [ mid ] == 0 ) :
            arr [ i ] , arr [ mid ] = arr [ mid ] , arr [ i ]
            i += 1
            mid += 1
        elif ( arr [ mid ] == 3 ) :
            arr [ mid ] , arr [ j ] = arr [ j ] , arr [ mid ]
            j -= 1
        elif ( arr [ mid ] == 1 or arr [ mid ] == 2 ) :
            mid += 1
    while ( i <= j ) :
        if ( arr [ i ] == 2 ) :
            arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
            j -= 1
        else :
            i += 1
    for i in range ( N ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ]
    N = len ( arr )
    sortArray ( arr , N )

Index/key variables: i, j, mid
Iterator variables: i
Python program:
def check ( arr , n ) :
    count = 0
    index = - 1
    for i in range ( 1 , n ) :
        if ( arr [ i - 1 ] >= arr [ i ] ) :
            count += 1
            index = i
    if ( count > 1 ) :
        return False
    if ( count == 0 ) :
        return True
    if ( index == n - 1 or index == 1 ) :
        return True
    if ( arr [ index - 1 ] < arr [ index + 1 ] ) :
        return True
    if ( arr [ index - 2 ] < arr [ index ] ) :
        return True
    return False
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 5 , 3 , 5 ]
    N = len ( arr )
    if ( check ( arr , N ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, index
Accumulator variables: count
Iterator variables: i
Python program:
def sortVowels ( S ) :
    n = len ( S ) ;
    vow = " " ;
    for i in range ( n ) :
        if ( S [ i ] == ' a ' or S [ i ] == ' e ' or S [ i ] == ' i ' or S [ i ] == ' o ' or S [ i ] == ' u ' ) :
            vow += S [ i ] ;
    if len ( vow ) == 0 :
        print ( S , end = " " ) ;
        return ;
    vow = list ( vow ) ;
    vow . sort ( ) ;
    j = 0 ;
    for i in range ( n ) :
        if ( S [ i ] == ' a ' or S [ i ] == ' e ' or S [ i ] == ' i ' or S [ i ] == ' o ' or S [ i ] == ' u ' ) :
            S [ i ] = vow [ j ] ;
            j += 1 ;
    print ( " " . join ( S ) , end = " " ) ;
if __name__ == " _ _ main _ _ " :
    S = " geeksforgeeks " ;
    sortVowels ( list ( S ) ) ;

Index/key variables: i, j
Accumulator variables: vow
Iterator variables: i
Python program:
def getBuckets ( arr , N ) :
    availableWater = N * ( N - 1 ) // 2
    arr . sort ( )
    i , Sum = 0 , 0
    while ( Sum <= availableWater ) :
        Sum += arr [ i ]
        i += 1
    print ( i - 1 , end = " " )
arr = [ 1 , 5 , 3 , 4 , 7 , 9 ]
N = len ( arr )
getBuckets ( arr , N ) ;

Index/key variables: i
Accumulator variables: Sum
Python program:
def findMaxDifference ( arr , n ) :
    arr = sorted ( arr )
    Leftsum = 0
    Totalsum = 0
    Min , Max = 10 ** 8 , - 10 ** 8
    for i in range ( n ) :
        Totalsum += arr [ i ]
    for i in range ( n ) :
        leftNumbers = i
        rightNumbers = n - i - 1
        Totalsum = Totalsum - arr [ i ]
        sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] )
        Min = min ( Min , sum )
        Max = max ( Max , sum )
        Leftsum += arr [ i ]
    print ( Max - Min )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 4 , 7 ]
    N = len ( arr )
    findMaxDifference ( arr , N )

Index/key variables: i
Accumulator variables: Leftsum, Totalsum
Iterator variables: i
Python program:
def maxcntPairsSumKRemoved ( arr , k ) :
    cntPairs = 0
    if not arr or len ( arr ) == 1 :
        return cntPairs
    arr . sort ( )
    left = 0
    right = len ( arr ) - 1
    while left < right :
        s = arr [ left ] + arr [ right ]
        if s == k :
            cntPairs += 1
            left += 1
            right -= 1
        elif s > k :
            right -= 1
        else :
            left += 1
    return cntPairs
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 ]
    K = 5
    print ( maxcntPairsSumKRemoved ( arr , K ) )

Index/key variables: left, right
Accumulator variables: cntPairs
Python program:
def getMinCost ( arr , n , k ) :
    arr . sort ( reverse = True )
    min_cost = 0 ;
    X = 0 ;
    for i in range ( 0 , n , k ) :
        for j in range ( i , n , 1 ) :
            if ( j < i + k ) :
                min_cost += arr [ j ] * ( X + 1 ) ;
        X += 1 ;
    return min_cost ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 9 , 20 , 7 , 8 ] ;
    K = 2 ;
    N = len ( arr ) ;
    print ( getMinCost ( arr , N , K ) ) ;

Index/key variables: j
Accumulator variables: X, min_cost
Iterator variables: i, j
Python program:
def minDiff ( A , K ) :
    A . sort ( ) ;
    n = len ( A ) ;
    if ( n <= K ) :
        return 0 ;
    mindiff = A [ n - 1 ] - A [ 0 ] ;
    if ( K == 0 ) :
        return mindiff ;
    i = 0 ;
    for j in range ( n - 1 - K , n ) :
        mindiff = min ( mindiff , A [ j ] - A [ i ] ) ;
        i += 1 ;
        j += 1 ;
    return mindiff ;
if __name__ == ' _ _ main _ _ ' :
    A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ] ;
    K = 3 ;
    print ( minDiff ( A , K ) ) ;

Index/key variables: i, j
Iterator variables: j
Python program:
import sys
def minDiff ( A , K ) :
    if ( len ( A ) <= K + 1 ) :
        return 0
    maxHeap = [ ]
    minHeap = [ ]
    for n in A :
        maxHeap . append ( n )
        maxHeap . sort ( )
        if ( len ( maxHeap ) > K + 1 ) :
            del maxHeap [ 0 ]
        minHeap . append ( n )
        minHeap . sort ( )
        minHeap . reverse ( )
        if ( len ( minHeap ) > K + 1 ) :
            del minHeap [ 0 ]
    maxList = [ ]
    while ( len ( maxHeap ) > 0 ) :
        maxList . append ( maxHeap [ 0 ] )
        del maxHeap [ 0 ]
    minList = [ ]
    while ( len ( minHeap ) > 0 ) :
        minList . append ( minHeap [ 0 ] )
        del minHeap [ 0 ]
    mindiff = sys . maxsize
    for i in range ( K ) :
        mindiff = min ( mindiff , maxList [ i ] - minList [ K - i ] )
    return mindiff
A = [ - 1 , 3 , - 1 , 8 , 5 , 4 ]
K = 3
print ( minDiff ( A , K ) )

Index/key variables: i
Accumulator variables: maxHeap, maxList, minHeap, minList
Iterator variables: i, n
Python program:
def sortArrayInDescAsc ( arr , N , K ) :
    arr = sorted ( arr )
    arr = arr [ : : - 1 ]
    for i in arr [ : K ] :
        print ( i , end = " ▁ " )
    for i in reversed ( arr [ K : ] ) :
        print ( i , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 ]
    N = len ( arr )
    K = 6
    sortArrayInDescAsc ( arr , N , K )

Iterator variables: i
Python program:
def findMedianOfsubSum ( arr , N ) :
    sum = 0
    for i in range ( N ) :
        sum += arr [ i ]
    arr . sort ( reverse = False )
    dp = [ [ 0 for i in range ( sum + 1 ) ] for j in range ( N ) ]
    for i in range ( N ) :
        dp [ i ] [ 0 ] = 1
    dp [ 0 ] [ arr [ 0 ] ] = 1
    for i in range ( 1 , N , 1 ) :
        for j in range ( 1 , sum + 1 , 1 ) :
            if ( j >= arr [ i ] ) :
                dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] )
            else :
                dp [ i ] [ j ] = dp [ i - 1 ] [ j ]
    sumSub = [ ]
    for j in range ( 1 , sum + 1 , 1 ) :
        M = dp [ N - 1 ] [ j ]
        for i in range ( 1 , M + 1 , 1 ) :
            sumSub . append ( j )
    mid = sumSub [ len ( sumSub ) // 2 ]
    return mid
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 3 ]
    N = len ( arr )
    print ( findMedianOfsubSum ( arr , N ) )

Index/key variables: i, j
Accumulator variables: sum, sumSub
Iterator variables: i, j
Python program:
def printIndex ( arr , N ) :
    B = [ 0 ] * ( N )
    save = - 1
    for i in range ( N ) :
        B [ i ] = arr [ i ]
    B = sorted ( B )
    for i in range ( N - 1 , 1 , - 1 ) :
        if ( B [ i ] != B [ i - 1 ] ) :
            save = B [ i ]
            break
    if ( save == - 1 ) :
        print ( - 1 + " " )
        return
    if ( save == arr [ 0 ] and save != arr [ 1 ] ) :
        print ( 1 )
    elif ( save == arr [ N - 1 ] and save != arr [ N - 2 ] ) :
        print ( N )
    for i in range ( 1 , N - 1 ) :
        if ( save == arr [ i ] and ( save != arr [ i - 1 ] or save != arr [ i + 1 ] ) ) :
            print ( i + 1 )
            break
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 3 , 4 , 4 , 5 ]
    N = len ( arr )
    printIndex ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def firstSubsequence ( s ) :
    allsubseq = [ ]
    k = [ ]
    for i in range ( len ( s ) ) :
        k = [ i for i in s ]
        del k [ i ]
        allsubseq . append ( " " . join ( k ) )
    allsubseq = sorted ( allsubseq )
    print ( allsubseq [ 0 ] )
if __name__ == ' _ _ main _ _ ' :
    S = " geeksforgeeks "
    firstSubsequence ( S )

Index/key variables: i
Accumulator variables: allsubseq
Iterator variables: i
Python program:
def evenSumK ( arr , N , K ) :
    if ( K > N ) :
        return - 1
    maxSum = 0
    Even = [ ]
    Odd = [ ]
    for i in range ( N ) :
        if ( arr [ i ] % 2 ) :
            Odd . append ( arr [ i ] )
        else :
            Even . append ( arr [ i ] )
    Odd . sort ( reverse = False )
    Even . sort ( reverse = False )
    i = len ( Even ) - 1
    j = len ( Odd ) - 1
    while ( K > 0 ) :
        if ( K % 2 == 1 ) :
            if ( i >= 0 ) :
                maxSum += Even [ i ]
                i -= 1
            else :
                return - 1
            K -= 1
        elif ( i >= 1 and j >= 1 ) :
            if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) :
                maxSum += Odd [ j ] + Odd [ j - 1 ]
                j -= 2
            else :
                maxSum += Even [ i ] + Even [ i - 1 ]
                i -= 2
            K -= 2
        elif ( i >= 1 ) :
            maxSum += Even [ i ] + Even [ i - 1 ]
            i -= 2
            K -= 2
        elif ( j >= 1 ) :
            maxSum += Odd [ j ] + Odd [ j - 1 ]
            j -= 2
            K -= 2
    return maxSum
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 4 , 10 , 3 , 5 ]
    N = len ( arr )
    K = 3
    print ( evenSumK ( arr , N , K ) )

Index/key variables: i, j
Accumulator variables: Even, K, Odd, maxSum
Iterator variables: i
Python program:
def weightedMedian ( arr , W ) :
    pairs = [ ]
    for index in range ( len ( arr ) ) :
        pairs . append ( [ arr [ index ] , W [ index ] ] )
    pairs . sort ( key = lambda p : p [ 0 ] )
    if len ( arr ) % 2 != 0 :
        sums = 0
        for element , weight in pairs :
            sums += weight
            if sums > 0.5 :
                print ( " The ▁ Weighted ▁ Median " , end = ' ▁ ' )
                print ( " is ▁ element ▁ { } " . format ( element ) )
    else :
        sums = 0
        for element , weight in pairs :
            sums += weight
            if sums >= 0.5 :
                print ( " Lower ▁ Weighted ▁ Median " , end = ' ▁ ' )
                print ( " is ▁ element ▁ { } " . format ( element ) )
                break
        sums = 0
        for index in range ( len ( pairs ) - 1 , - 1 , - 1 ) :
            element = pairs [ index ] [ 0 ]
            weight = pairs [ index ] [ 1 ]
            sums += weight
            if sums >= 0.5 :
                print ( " Upper ▁ Weighted ▁ Median " , end = ' ▁ ' )
                print ( " is ▁ element ▁ { } " . format ( element ) )
                break
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 1 , 3 , 2 ]
    W = [ 0.25 , 0.49 , 0.25 , 0.01 ]
    weightedMedian ( arr , W )

Index/key variables: index
Accumulator variables: pairs, sums
Iterator variables: element, index, weight
Python program:
def KthSmallestNum ( arr , n , k ) :
    pq = [ ]
    for i in range ( n ) :
        pq . append ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] )
    cnt = 1
    while ( cnt < k ) :
        pq . sort ( reverse = True )
        interval = pq [ 0 ]
        pq . remove ( pq [ 0 ] )
        if ( interval [ 0 ] < interval [ 1 ] ) :
            pq . append ( [ interval [ 0 ] + 1 , interval [ 1 ] ] )
        cnt += 1
    pq . sort ( reverse = True )
    return pq [ 0 ] [ 0 ] + 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 5 , 11 ] , [ 10 , 15 ] , [ 12 , 20 ] ]
    n = len ( arr )
    k = 12
    print ( KthSmallestNum ( arr , n , k ) )

Index/key variables: i
Accumulator variables: cnt, pq
Iterator variables: i
Python program:
import sys
def MaxDist ( A , N ) :
    maximum = - sys . maxsize
    for i in range ( N ) :
        sum = 0
        for j in range ( i + 1 , N ) :
            Sum = ( abs ( A [ i ] [ 0 ] - A [ j ] [ 0 ] ) + abs ( A [ i ] [ 1 ] - A [ j ] [ 1 ] ) )
            maximum = max ( maximum , Sum )
    print ( maximum )
N = 3
A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ]
MaxDist ( A , N )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def findMatrix ( N , K ) :
    mat = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ;
    element = 0 ;
    for i in range ( 0 , N ) :
        for j in range ( 0 , K - 1 ) :
            element += 1 ;
            mat [ i ] [ j ] = element ;
    for i in range ( 0 , N ) :
        for j in range ( K - 1 , N ) :
            element += 1 ;
            mat [ i ] [ j ] = element ;
    return mat ;
def printMatrix ( mat , N ) :
    for i in range ( 0 , N ) :
        for j in range ( 0 , N ) :
            print ( mat [ i ] [ j ] , end = " ▁ " ) ;
        print ( ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 3 ; K = 2 ;
    mat = findMatrix ( N , K ) ;
    printMatrix ( mat , N ) ;

Index/key variables: i, j
Accumulator variables: element
Iterator variables: i, j
Python program:
def range_sum ( arr , a , b ) :
    sum = 0
    if ( a - 2 < 0 ) :
        sum = arr [ b - 1 ]
    else :
        sum = ( arr [ b - 1 ] - arr [ a - 2 ] )
    return sum
def precompute_sum ( arr , brr ) :
    N = len ( arr )
    for i in range ( 1 , N ) :
        arr [ i ] = arr [ i ] + arr [ i - 1 ]
        brr [ i ] = brr [ i ] + brr [ i - 1 ]
def find_sum ( arr , q , Queries ) :
    brr = arr . copy ( )
    N = len ( arr )
    brr . sort ( )
    precompute_sum ( arr , brr )
    for i in range ( q ) :
        m = Queries [ i ] [ 0 ]
        a = Queries [ i ] [ 1 ]
        b = Queries [ i ] [ 2 ]
        if ( m == 1 ) :
            print ( range_sum ( arr , a , b ) , end = ' ▁ ' )
        elif ( m == 2 ) :
            print ( range_sum ( brr , a , b ) , end = ' ▁ ' )
if __name__ == " _ _ main _ _ " :
    arr = [ 0 , 6 , 4 , 2 , 7 , 2 , 7 ]
    Q = 1
    Queries = [ [ 2 , 3 , 6 ] ]
    find_sum ( arr , Q , Queries )

Index/key variables: i
Iterator variables: i
Python program:
from bisect import bisect , bisect_left , bisect_right
def XorSum ( A , B , N ) :
    maxBit = 29
    ans = 0
    for k in range ( maxBit ) :
        C = [ 0 ] * N
        for i in range ( N ) :
            C [ i ] = B [ i ] % ( 1 << ( k + 1 ) )
        C = sorted ( C )
        count = 0
        l , r = 0 , 0
        for i in range ( N ) :
            x = A [ i ] % ( 1 << ( k + 1 ) )
            l = bisect_left ( C , ( 1 << k ) - x )
            r = bisect_left ( C , ( 1 << k ) * 2 - x )
            count += ( r - l )
            l = bisect_left ( C , ( 1 << k ) * 3 - x )
            r = bisect_left ( C , ( 1 << k ) * 4 - x )
            count += ( r - l )
        if ( count & 1 ) :
            ans += ( 1 << k )
    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
Accumulator variables: ans, count
Iterator variables: i, k
Python program:
def findCount ( arr , N , K ) :
    arr . sort ( )
    result = 1
    cur_max = arr [ 0 ]
    cur_min = arr [ 0 ]
    for i in range ( 1 , N ) :
        cur_max = arr [ i ]
        if ( cur_max - cur_min > K ) :
            result += 1
            cur_max = arr [ i ]
            cur_min = arr [ i ]
    return result
arr = [ 1 , 10 , 8 , 3 , 9 ]
K = 3
N = len ( arr )
print ( findCount ( arr , N , K ) )

Index/key variables: i
Accumulator variables: result
Iterator variables: i
Python program:
mod = 1e9 + 7
def findMinValue ( a , b ) :
    n = len ( a )
    pro = [ 0 ] * ( n )
    for i in range ( n ) :
        pro [ i ] = ( ( i + 1 ) * ( n - i ) )
        pro [ i ] *= ( a [ i ] )
    b . sort ( reverse = True )
    pro . sort ( )
    ans = 0
    for i in range ( n ) :
        ans += ( pro [ i ] % mod * b [ i ] ) % mod
        ans %= mod
    return ans
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 2 , 3 ]
    b = [ 2 , 3 , 2 ]
    print ( int ( findMinValue ( a , b ) ) )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
def rearrange ( a , N ) :
    i = 0
    j = N - 1
    min = 0
    x = 0
    while ( i < j ) :
        for k in range ( i , j + 1 ) :
            if ( a [ k ] < a [ min ] ) :
                min = k
        if ( x % 2 == 0 ) :
            temp = a [ i ]
            a [ i ] = a [ min ]
            a [ min ] = temp
            i += 1
            min = i
        else :
            temp = a [ j ]
            a [ j ] = a [ min ]
            a [ min ] = temp
            j -= 1
            min = j
        x += 1
    for i in range ( N ) :
        print ( a [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 3 , 4 , 5 ]
    N = len ( arr )
    rearrange ( arr , N )

Index/key variables: i, j, k, min
Accumulator variables: x
Iterator variables: i, k
Python program:
def redistribute ( arr , n , x ) :
    arr . sort ( reverse = True )
    sum = 0
    for i in range ( n ) :
        sum += arr [ i ]
        if ( sum / ( i + 1 ) < x ) :
            print ( i )
            break
    if ( i == n ) :
        print ( n )
arr = [ 5 , 1 , 2 , 1 ]
x = 3
redistribute ( arr , 4 , x )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
from queue import PriorityQueue
def final_element ( arr , n ) :
    heap = PriorityQueue ( )
    for i in range ( n ) :
        heap . put ( - 1 * arr [ i ] )
    while ( heap . qsize ( ) > 1 ) :
        X = - 1 * heap . get ( )
        Y = - 1 * heap . get ( )
        if ( X != Y ) :
            diff = abs ( X - Y )
            heap . put ( - 1 * diff )
    if ( heap . qsize ( ) == 1 ) :
        print ( - 1 * heap . get ( ) )
    else :
        print ( " - 1" )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 5 , 2 , 7 ]
    n = len ( arr )
    final_element ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
def countFrequency ( string , ch ) :
    count = 0 ;
    for i in range ( len ( string ) ) :
        if ( string [ i ] == ch ) :
            count += 1 ;
    return count ;
def sortArr ( string ) :
    n = len ( string ) ;
    vp = [ ] ;
    for i in range ( n ) :
        vp . append ( ( countFrequency ( string , string [ i ] ) , string [ i ] ) ) ;
    vp . sort ( ) ;
    for i in range ( len ( vp ) ) :
        print ( vp [ i ] [ 1 ] , end = " " ) ;
if __name__ == " _ _ main _ _ " :
    string = " geeksforgeeks " ;
    sortArr ( string ) ;

Index/key variables: i
Accumulator variables: count, vp
Iterator variables: i