dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def longestSubsequence ( N , Q , arr , Queries ) :
    count = 1
    for i in range ( 1 , N ) :
        if ( arr [ i ] != arr [ i - 1 ] ) :
            count += 1
    for i in range ( Q ) :
        x = Queries [ i ] [ 0 ]
        y = Queries [ i ] [ 1 ]
        if ( x > 1 ) :
            if ( arr [ x - 1 ] != arr [ x - 2 ] ) :
                count -= 1
            if ( arr [ x - 2 ] != y ) :
                count += 1
        if ( x < N ) :
            if ( arr [ x ] != arr [ x - 1 ] ) :
                count -= 1
            if ( y != arr [ x ] ) :
                count += 1
        print ( count , end = ' ▁ ' )
        arr [ x - 1 ] = y
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 2 , 5 , 2 ]
    N = len ( arr )
    Q = 2
    Queries = [ [ 1 , 3 ] , [ 4 , 2 ] ]
    longestSubsequence ( N , Q , arr , Queries )

Index/key variables: i, x
Accumulator variables: count
Iterator variables: i
Python program:
from collections import defaultdict
def sum_i ( arr , n ) :
    mp = defaultdict ( lambda : [ ] )
    for i in range ( n ) :
        mp [ arr [ i ] ] . append ( i )
    ans = [ 0 ] * n
    for i in range ( n ) :
        sum = 0
        for it in mp [ arr [ i ] ] :
            sum += abs ( it - i )
            ans [ i ] = sum
    for i in range ( n ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 1 , 1 , 2 ]
    n = len ( arr )
    sum_i ( arr , n )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i, it
Python program:
def conVowUpp ( str ) :
    N = len ( str )
    str1 = " "
    for i in range ( N ) :
        if ( str [ i ] == ' a ' or str [ i ] == ' e ' or str [ i ] == ' i ' or str [ i ] == ' o ' or str [ i ] == ' u ' ) :
            c = ( str [ i ] ) . upper ( )
            str1 += c
        else :
            str1 += str [ i ]
    print ( str1 )
if __name__ == ' _ _ main _ _ ' :
    str = " eutopia "
    conVowUpp ( str )

Index/key variables: i
Accumulator variables: str1
Iterator variables: i
Python program:
mp = { }
N , P = 0 , 0
def helper ( mid ) :
    cnt = 0 ;
    for i in mp :
        temp = mp [ i ]
        while ( temp >= mid ) :
            temp -= mid
            cnt += 1
    return cnt >= N
def findMaximumDays ( arr ) :
    for i in range ( P ) :
        mp [ arr [ i ] ] = mp . get ( arr [ i ] , 0 ) + 1
    start = 0
    end = P
    ans = 0
    while ( start <= end ) :
        mid = start + ( ( end - start ) // 2 )
        if ( mid != 0 and helper ( mid ) ) :
            ans = mid
            start = mid + 1
        elif ( mid == 0 ) :
            start = mid + 1
        else :
            end = mid - 1
    return ans
if __name__ == ' _ _ main _ _ ' :
    N = 3
    P = 10
    arr = [ 1 , 2 , 2 , 1 , 1 , 3 , 3 , 3 , 2 , 4 ]
    print ( findMaximumDays ( arr ) )

Index/key variables: i
Accumulator variables: cnt, temp
Iterator variables: i
Python program:
def countSubarrays ( a , n , k ) :
    ans = 0
    pref = [ ]
    pref . append ( 0 )
    for i in range ( n ) :
        pref . append ( ( a [ i ] + pref [ i ] ) % k )
    for i in range ( 1 , n + 1 , 1 ) :
        for j in range ( i , n + 1 , 1 ) :
            if ( ( pref [ j ] - pref [ i - 1 ] + k ) % k == j - i + 1 ) :
                ans += 1
    print ( ans , end = ' ▁ ' )
arr = [ 2 , 3 , 5 , 3 , 1 , 5 ]
N = len ( arr )
K = 4
countSubarrays ( arr , N , K )

Index/key variables: i, j
Accumulator variables: ans, pref
Iterator variables: i, j
Python program:
def countSubarrays ( a , n , k ) :
    cnt = { }
    ans = 0
    pref = [ ]
    pref . append ( 0 )
    for i in range ( n ) :
        pref . append ( ( a [ i ] + pref [ i ] ) % k )
    cnt [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        remIdx = i - k
        if ( remIdx >= 0 ) :
            if ( ( pref [ remIdx ] - remIdx % k + k ) % k in cnt ) :
                cnt [ ( pref [ remIdx ] - remIdx % k + k ) % k ] -= 1
            else :
                cnt [ ( pref [ remIdx ] - remIdx % k + k ) % k ] = - 1
        if ( pref [ i ] - i % k + k ) % k in cnt :
            ans += cnt [ ( pref [ i ] - i % k + k ) % k ]
        if ( pref [ i ] - i % k + k ) % k in cnt :
            cnt [ ( pref [ i ] - i % k + k ) % k ] += 1
        else :
            cnt [ ( pref [ i ] - i % k + k ) % k ] = 1
    print ( ans , end = ' ▁ ' )
arr = [ 2 , 3 , 5 , 3 , 1 , 5 ]
N = len ( arr )
K = 4
countSubarrays ( arr , N , K )

Index/key variables: i, remIdx
Accumulator variables: ans, pref
Iterator variables: i
Python program:
def check ( s , k ) :
    n = len ( s )
    for i in range ( k ) :
        for j in range ( i , n , k ) :
            if ( s [ i ] != s [ j ] ) :
                return False
    c = 0
    for i in range ( k ) :
        if ( s [ i ] == '0' ) :
            c += 1
        else :
            c -= 1
    if ( c == 0 ) :
        return True
    else :
        return False
s = "101010"
k = 2
if ( check ( s , k ) != 0 ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j
Accumulator variables: c
Iterator variables: i, j
Python program:
from collections import defaultdict
def isSame ( str , n ) :
    mp = defaultdict ( lambda : 0 )
    for i in range ( len ( str ) ) :
        mp [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1
    for it in mp . keys ( ) :
        if ( mp [ it ] >= n ) :
            return True
    return False
str = " ccabcba "
n = 4
if ( isSame ( str , n ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, it
Iterator variables: i, it
Python program:
def possible ( mid , a ) :
    n = len ( a ) ;
    total = ( n * ( n - 1 ) ) // 2 ;
    need = ( total + 1 ) // 2 ;
    count = 0 ;
    start = 0 ; end = 1 ;
    while ( end < n ) :
        if ( a [ end ] - a [ start ] <= mid ) :
            end += 1 ;
        else :
            count += ( end - start - 1 ) ;
            start += 1 ;
    if ( end == n and start < end and a [ end - 1 ] - a [ start ] <= mid ) :
        t = end - start - 1 ;
        count += ( t * ( t + 1 ) // 2 ) ;
    if ( count >= need ) :
        return True ;
    else :
        return False ;
def findMedian ( a ) :
    n = len ( a ) ;
    low = 0 ; high = a [ n - 1 ] - a [ 0 ] ;
    while ( low <= high ) :
        mid = ( low + high ) // 2 ;
        if ( possible ( mid , a ) ) :
            high = mid - 1 ;
        else :
            low = mid + 1 ;
    return high + 1 ;
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 7 , 5 , 2 ] ;
    a . sort ( )
    print ( findMedian ( a ) ) ;

Index/key variables: end, start
Accumulator variables: count
Python program:
def UniversalSubset ( A , B ) :
    n1 = len ( A )
    n2 = len ( B )
    res = [ ]
    A_freq = [ [ 0 for x in range ( 26 ) ] for y in range ( n1 ) ]
    for i in range ( n1 ) :
        for j in range ( len ( A [ i ] ) ) :
            A_freq [ i ] [ ord ( A [ i ] [ j ] ) - ord ( ' a ' ) ] += 1
    B_freq = [ 0 ] * 26
    for i in range ( n2 ) :
        arr = [ 0 ] * 26
        for j in range ( len ( B [ i ] ) ) :
            arr [ ord ( B [ i ] [ j ] ) - ord ( ' a ' ) ] += 1
            B_freq [ ord ( B [ i ] [ j ] ) - ord ( ' a ' ) ] = max ( B_freq [ ord ( B [ i ] [ j ] ) - ord ( ' a ' ) ] , arr [ ord ( B [ i ] [ j ] ) - ord ( ' a ' ) ] )
    for i in range ( n1 ) :
        flag = 0
        for j in range ( 26 ) :
            if ( A_freq [ i ] [ j ] < B_freq [ j ] ) :
                flag = 1
                break
        if ( flag == 0 ) :
            res . append ( A [ i ] )
    if ( len ( res ) ) :
        for i in range ( len ( res ) ) :
            for j in range ( len ( res [ i ] ) ) :
                print ( res [ i ] [ j ] , end = " " )
    else :
        print ( - 1 , end = " " )
if __name__ == ' _ _ main _ _ ' :
    A = [ " geeksforgeeks " , " topcoder " , " leetcode " ]
    B = [ " geek " , " ee " ]
    UniversalSubset ( A , B )

Index/key variables: i, j
Accumulator variables: res
Iterator variables: i, j
Python program:
import sys
def findPair ( a , n ) :
    min_dist = sys . maxsize
    index_a = - 1
    index_b = - 1
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( j - i < min_dist ) :
                if ( ( a [ i ] % a [ j ] == 0 ) or ( a [ j ] % a [ i ] == 0 ) ) :
                    min_dist = j - i
                    index_a = i
                    index_b = j
    if ( index_a == - 1 ) :
        print ( " - 1" )
    else :
        print ( " ( " , a [ index_a ] , " , ▁ " , a [ index_b ] , " ) " )
a = [ 2 , 3 , 4 , 5 , 6 ]
n = len ( a )
findPair ( a , n )

Index/key variables: i, index_a, index_b, j
Iterator variables: i, j
Python program:
def printNum ( L , R ) :
    for i in range ( L , R + 1 ) :
        temp = i
        c = 10
        flag = 0
        while ( temp > 0 ) :
            if ( temp % 10 >= c ) :
                flag = 1
                break
            c = temp % 10
            temp //= 10
        if ( flag == 0 ) :
            print ( i , end = " ▁ " )
L = 10
R = 15
printNum ( L , R )

Accumulator variables: temp
Iterator variables: i
Python program:
import sys
def findMissing ( arr , left , right , diff ) :
    if ( right <= left ) :
        return sys . maxsize
    mid = left + ( right - left ) // 2
    if ( arr [ mid + 1 ] - arr [ mid ] != diff ) :
        return ( arr [ mid ] + diff )
    if ( mid > 0 and arr [ mid ] - arr [ mid - 1 ] != diff ) :
        return ( arr [ mid - 1 ] + diff )
    if ( arr [ mid ] == arr [ 0 ] + mid * diff ) :
        return findMissing ( arr , mid + 1 , right , diff )
    return findMissing ( arr , left , mid - 1 , diff )
def missingElement ( arr , n ) :
    arr . sort ( )
    diff = ( arr [ n - 1 ] - arr [ 0 ] ) // n
    return findMissing ( arr , 0 , n - 1 , diff )
arr = [ 2 , 8 , 6 , 10 ]
n = len ( arr )
print ( missingElement ( arr , n ) )

Index/key variables: mid
Python program:
def get_subset_count ( arr , K , N ) :
    arr . sort ( )
    left = 0 ;
    right = N - 1 ;
    ans = 0 ;
    while ( left <= right ) :
        if ( arr [ left ] + arr [ right ] < K ) :
            ans += 1 << ( right - left ) ;
            left += 1 ;
        else :
            right -= 1 ;
    return ans ;
arr = [ 2 , 4 , 5 , 7 ] ;
K = 8 ;
print ( get_subset_count ( arr , K , 4 ) )

Index/key variables: left, right
Accumulator variables: ans
Python program:
def minMaxDiff ( arr , n , k ) :
    max_adj_dif = float ( ' - inf ' ) ;
    for i in range ( n - 1 ) :
        max_adj_dif = max ( max_adj_dif , abs ( arr [ i ] - arr [ i + 1 ] ) ) ;
    if ( max_adj_dif == 0 ) :
        return 0 ;
    best = 1 ;
    worst = max_adj_dif ;
    while ( best < worst ) :
        mid = ( best + worst ) // 2 ;
        required = 0
        for i in range ( n - 1 ) :
            required += ( abs ( arr [ i ] - arr [ i + 1 ] ) - 1 ) // mid
        if ( required > k ) :
            best = mid + 1 ;
        else :
            worst = mid
    return worst
arr = [ 3 , 12 , 25 , 50 ]
n = len ( arr )
k = 7
print ( minMaxDiff ( arr , n , k ) )

Index/key variables: i
Accumulator variables: required
Iterator variables: i
Python program:
import math
def checkMin ( arr , n ) :
    smallest = math . inf
    secondSmallest = math . inf
    for i in range ( n ) :
        if ( arr [ i ] < smallest ) :
            secondSmallest = smallest
            smallest = arr [ i ]
        elif ( arr [ i ] < secondSmallest ) :
            secondSmallest = arr [ i ]
    if ( 2 * smallest <= secondSmallest ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 5 ]
    n = len ( arr )
    checkMin ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
import sys
def createHash ( hash , maxElement ) :
    prev = 0
    curr = 1
    hash . add ( prev )
    hash . add ( curr )
    while ( curr <= maxElement ) :
        temp = curr + prev
        hash . add ( temp )
        prev = curr
        curr = temp
def fibonacci ( arr , n ) :
    max_val = max ( arr )
    hash = set ( )
    createHash ( hash , max_val )
    minimum = sys . maxsize
    maximum = - sys . maxsize - 1
    for i in range ( n ) :
        if ( arr [ i ] in hash ) :
            minimum = min ( minimum , arr [ i ] )
            maximum = max ( maximum , arr [ i ] )
    print ( minimum , end = " , ▁ " )
    print ( maximum )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
    n = len ( arr )
    fibonacci ( arr , n )

Index/key variables: i
Accumulator variables: hash
Iterator variables: i
Python program:
def isValidLen ( s , lenn , k ) :
    n = len ( s )
    mp = dict ( )
    right = 0
    while ( right < lenn ) :
        mp [ s [ right ] ] = mp . get ( s [ right ] , 0 ) + 1
        right += 1
    if ( len ( mp ) <= k ) :
        return True
    while ( right < n ) :
        mp [ s [ right ] ] = mp . get ( s [ right ] , 0 ) + 1
        mp [ s [ right - lenn ] ] -= 1
        if ( mp [ s [ right - lenn ] ] == 0 ) :
            del mp [ s [ right - lenn ] ]
        if ( len ( mp ) <= k ) :
            return True
        right += 1
    return len ( mp ) <= k
def maxLenSubStr ( s , k ) :
    uni = dict ( )
    for x in s :
        uni [ x ] = 1
    if ( len ( uni ) < k ) :
        return - 1
    n = len ( s )
    lo = - 1
    hi = n + 1
    while ( hi - lo > 1 ) :
        mid = lo + hi >> 1
        if ( isValidLen ( s , mid , k ) ) :
            lo = mid
        else :
            hi = mid
    return lo
s = " aabacbebebe "
k = 3
print ( maxLenSubStr ( s , k ) )

Index/key variables: right, x
Iterator variables: x
Python program:
def isSquarePossible ( arr , n , l ) :
    cnt = 0
    for i in range ( n ) :
        if arr [ i ] >= l :
            cnt += 1
        if cnt >= l :
            return True
    return False
def maxArea ( arr , n ) :
    l , r = 0 , n
    len = 0
    while l <= r :
        m = l + ( ( r - l ) // 2 )
        if isSquarePossible ( arr , n , m ) :
            len = m
            l = m + 1
        else :
            r = m - 1
    return ( len * len )
arr = [ 1 , 3 , 4 , 5 , 5 ]
n = len ( arr )
print ( maxArea ( arr , n ) )

Index/key variables: i
Accumulator variables: cnt
Iterator variables: i
Python program:
def insertNames ( arr , n ) :
    string = set ( ) ;
    for i in range ( n ) :
        if arr [ i ] not in string :
            print ( " No " ) ;
            string . add ( arr [ i ] ) ;
        else :
            print ( " Yes " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ " geeks " , " for " , " geeks " ] ;
    n = len ( arr ) ;
    insertNames ( arr , n ) ;

Index/key variables: i
Accumulator variables: string
Iterator variables: i
Python program:
def countLessThan ( arr , n , key ) :
    l = 0
    r = n - 1
    index = - 1
    while ( l <= r ) :
        m = ( l + r ) // 2
        if ( arr [ m ] < key ) :
            l = m + 1
            index = m
        else :
            r = m - 1
    return ( index + 1 )
def countGreaterThan ( arr , n , key ) :
    l = 0
    r = n - 1
    index = - 1
    while ( l <= r ) :
        m = ( l + r ) // 2
        if ( arr [ m ] <= key ) :
            l = m + 1
        else :
            r = m - 1
            index = m
    if ( index == - 1 ) :
        return 0
    return ( n - index )
def countTriplets ( n , a , b , c ) :
    a . sort
    b . sort ( )
    c . sort ( )
    count = 0
    for i in range ( n ) :
        current = b [ i ]
        a_index = - 1
        c_index = - 1
        low = countLessThan ( a , n , current )
        high = countGreaterThan ( c , n , current )
        count += ( low * high )
    return count
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 5 ]
    b = [ 2 , 4 ]
    c = [ 3 , 6 ]
    size = len ( a )
    print ( countTriplets ( size , a , b , c ) )

Index/key variables: i, m
Accumulator variables: count
Iterator variables: i
Python program:
def missing4 ( arr ) :
    helper = [ 0 ] * 4
    for i in range ( 0 , len ( arr ) ) :
        temp = abs ( arr [ i ] )
        if ( temp <= len ( arr ) ) :
            arr [ temp - 1 ] = arr [ temp - 1 ] * ( - 1 )
        elif ( temp > len ( arr ) ) :
            if ( temp % len ( arr ) ) :
                helper [ temp % len ( arr ) - 1 ] = - 1
            else :
                helper [ ( temp % len ( arr ) ) + len ( arr ) - 1 ] = - 1
    for i in range ( 0 , len ( arr ) ) :
        if ( arr [ i ] > 0 ) :
            print ( ( i + 1 ) , end = " ▁ " )
    for i in range ( 0 , len ( helper ) ) :
        if ( helper [ i ] >= 0 ) :
            print ( ( len ( arr ) + i + 1 ) , end = " ▁ " )
arr = [ 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ]
missing4 ( arr )

Index/key variables: i
Iterator variables: i
Python program:
def lexiMiddleSmallest ( K , N ) :
    if ( K % 2 == 0 ) :
        print ( K // 2 , end = " ▁ " )
        for i in range ( N - 1 ) :
            print ( K , end = " ▁ " )
        print ( )
        return
    a = [ ( K + 1 ) // 2 ] * ( N )
    for i in range ( N // 2 ) :
        if ( a [ - 1 ] == 1 ) :
            del a [ - 1 ]
        else :
            a [ - 1 ] -= 1
            while ( len ( a ) < N ) :
                a . append ( K )
    for i in a :
        print ( i , end = " ▁ " )
    print ( )
if __name__ == ' _ _ main _ _ ' :
    K , N = 2 , 4
    lexiMiddleSmallest ( K , N )

Accumulator variables: a
Iterator variables: i
Python program:
def maxDivisions ( arr , N , X ) :
    arr . sort ( reverse = True )
    maxSub = 0 ;
    size = 0 ;
    for i in range ( N ) :
        size += 1 ;
        if ( arr [ i ] * size >= X ) :
            maxSub += 1 ;
            size = 0 ;
    print ( maxSub ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 3 , 3 , 7 ] ;
    N = len ( arr ) ;
    X = 3 ;
    maxDivisions ( arr , N , X ) ;

Index/key variables: i
Accumulator variables: maxSub, size
Iterator variables: i
Python program:
def maxPossibleSum ( arr , N ) :
    arr . sort ( )
    sum = 0
    j = N - 3
    while ( j >= 0 ) :
        sum += arr [ j ]
        j -= 3
    print ( sum )
if __name__ == " _ _ main _ _ " :
    arr = [ 7 , 4 , 5 , 2 , 3 , 1 , 5 , 9 ]
    N = 8
    maxPossibleSum ( arr , N )

Index/key variables: j
Accumulator variables: sum
Python program:
def getPairs ( arr , N , K ) :
    count = 0
    for i in range ( N ) :
        for j in range ( i + 1 , N ) :
            if ( arr [ i ] > K * arr [ i + 1 ] ) :
                count += 1
    print ( count )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 5 , 6 , 2 , 1 ]
    N = len ( arr )
    K = 2
    getPairs ( arr , N , K )

Index/key variables: i
Accumulator variables: count
Iterator variables: i, j
Python program:
def merge ( arr , temp , l , m , r , K ) :
    i = l
    j = m + 1
    cnt = 0
    for l in range ( m + 1 ) :
        found = False
        while ( j <= r ) :
            if ( arr [ i ] >= K * arr [ j ] ) :
                found = True
            else :
                break
            j += 1
        if ( found ) :
            cnt += j - ( m + 1 )
            j -= 1
    k = l
    i = l
    j = m + 1
    while ( i <= m and j <= r ) :
        if ( arr [ i ] <= arr [ j ] ) :
            temp [ k ] = arr [ i ]
            k += 1
            i += 1
        else :
            temp [ k ] = arr [ j ]
            k += 1
            j += 1
    while ( i <= m ) :
        temp [ k ] = arr [ i ]
        k += 1
        i += 1
    while ( j <= r ) :
        temp [ k ] = arr [ j ]
        k += 1
        j += 1
    for i in range ( l , r + 1 ) :
        arr [ i ] = temp [ i ]
    return cnt
def mergeSortUtil ( arr , temp , l , r , K ) :
    cnt = 0
    if ( l < r ) :
        m = ( l + r ) // 2
        cnt += mergeSortUtil ( arr , temp , l , m , K )
        cnt += mergeSortUtil ( arr , temp , m + 1 , r , K )
        cnt += merge ( arr , temp , l , m , r , K )
    return cnt
def mergeSort ( arr , N , K ) :
    temp = [ 0 ] * N
    print ( mergeSortUtil ( arr , temp , 0 , N - 1 , K ) )
arr = [ 5 , 6 , 2 , 5 ]
N = len ( arr )
K = 2
mergeSort ( arr , N , K )

Index/key variables: i, j, k
Accumulator variables: cnt
Iterator variables: i, l
Boolean variables: found
Python program:
def minRemovals ( A , N ) :
    A . sort ( )
    mx = A [ N - 1 ]
    sum = 1
    for i in range ( 0 , N ) :
        sum += A [ i ]
    if ( ( sum - mx ) >= mx ) :
        print ( 0 , end = " " )
    else :
        print ( 2 * mx - sum , end = " " )
if __name__ == " _ _ main _ _ " :
    A = [ 3 , 3 , 2 ]
    N = len ( A )
    minRemovals ( A , N )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def rearrangeArray ( a , n ) :
    a = sorted ( a )
    for i in range ( n - 1 ) :
        if ( a [ i ] == i + 1 ) :
            a [ i ] , a [ i + 1 ] = a [ i + 1 ] , a [ i ]
    if ( a [ n - 1 ] == n ) :
        a [ n - 1 ] , a [ n - 2 ] = a [ n - 2 ] , a [ n - 1 ]
    for i in range ( n ) :
        print ( a [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 5 , 3 , 2 , 4 ]
    N = len ( arr )
    rearrangeArray ( arr , N )

Index/key variables: i
Iterator variables: i
Python program:
def minOperations ( arr1 , arr2 , i , j ) :
    if arr1 == arr2 :
        return 0
    if i >= len ( arr1 ) or j >= len ( arr2 ) :
        return 0
    if arr1 [ i ] < arr2 [ j ] :
        return 1 + minOperations ( arr1 , arr2 , i + 1 , j + 1 )
    return max ( minOperations ( arr1 , arr2 , i , j + 1 ) , minOperations ( arr1 , arr2 , i + 1 , j ) )
def minOperationsUtil ( arr ) :
    brr = sorted ( arr ) ;
    if ( arr == brr ) :
        print ( "0" )
    else :
        print ( minOperations ( arr , brr , 0 , 0 ) )
arr = [ 4 , 7 , 2 , 3 , 9 ]
minOperationsUtil ( arr )

Index/key variables: i, j
Python program:
def canTransform ( s , t ) :
    n = len ( s )
    occur = [ [ ] for i in range ( 26 ) ]
    for x in range ( n ) :
        ch = ord ( s [ x ] ) - ord ( ' a ' )
        occur [ ch ] . append ( x )
    idx = [ 0 ] * ( 26 )
    poss = True
    for x in range ( n ) :
        ch = ord ( t [ x ] ) - ord ( ' a ' )
        if ( idx [ ch ] >= len ( occur [ ch ] ) ) :
            poss = False
            break
        for small in range ( ch ) :
            if ( idx [ small ] < len ( occur [ small ] ) and occur [ small ] [ idx [ small ] ] < occur [ ch ] [ idx [ ch ] ] ) :
                poss = False
                break
        idx [ ch ] += 1
    if ( poss ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    s = " hdecb "
    t = " cdheb "
    canTransform ( s , t )

Index/key variables: ch, small, x
Iterator variables: small, x
Boolean variables: poss
Python program:
def inversionCount ( s ) :
    freq = [ 0 for _ in range ( 26 ) ]
    inv = 0
    for i in range ( len ( s ) ) :
        temp = 0
        for j in range ( ord ( s [ i ] ) - ord ( ' a ' ) ) :
            temp += freq [ j ]
        inv += ( i - temp )
        freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1
    return inv
def haveRepeated ( S1 , S2 ) :
    freq = [ 0 for _ in range ( 26 ) ]
    for i in range ( len ( S1 ) ) :
        if freq [ ord ( S1 [ i ] ) - ord ( ' a ' ) ] > 0 :
            return 1
        freq [ ord ( S1 [ i ] ) - ord ( ' a ' ) ] += 1
    for i in range ( 26 ) :
        freq [ i ] = 0
    for i in range ( len ( S2 ) ) :
        if freq [ ord ( S2 [ i ] ) - ord ( ' a ' ) ] > 0 :
            return 1
        freq [ ord ( S2 [ i ] ) - ord ( ' a ' ) ] += 1
    return 0
def checkToMakeEqual ( S1 , S2 ) :
    freq = [ 0 for _ in range ( 26 ) ]
    for i in range ( len ( S1 ) ) :
        freq [ ord ( S1 [ i ] ) - ord ( ' a ' ) ] += 1
    flag = 0
    for i in range ( len ( S2 ) ) :
        if freq [ ord ( S2 [ i ] ) - ord ( ' a ' ) ] == 0 :
            flag = 1
            break
        freq [ ord ( S2 [ i ] ) - ord ( ' a ' ) ] -= 1
    if flag == 1 :
        print ( " No " )
        return
    invCount1 = inversionCount ( S1 )
    invCount2 = inversionCount ( S2 )
    if ( ( invCount1 == invCount2 ) or ( ( invCount1 % 2 ) == ( invCount2 % 2 ) ) or haveRepeated ( S1 , S2 ) == 1 ) :
        print ( " Yes " )
    else :
        print ( " No " )
S1 = " abbca "
S2 = " acabb "
checkToMakeEqual ( S1 , S2 )

Index/key variables: i, j
Accumulator variables: inv, temp
Iterator variables: i, j