dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
M = 100
def mxSubSum ( i , arr , j ) :
    global dp
    if ( i == 0 ) :
        dp [ i ] [ j ] = max ( 0 , arr [ i ] )
        return dp [ i ] [ j ]
    if ( dp [ i ] [ j ] != - 1 ) :
        return dp [ i ] [ j ]
    X = max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j ) )
    if ( j == 0 ) :
        dp [ i ] [ j ] = X
        return X
    Y = mxSubSum ( i - 1 , arr , j - 1 )
    dp [ i ] [ j ] = max ( X , Y )
    return dp [ i ] [ j ]
def MaximumSubarraySum ( n , arr , k ) :
    mxSubSum ( n - 1 , arr , k )
    res = 0
    for i in range ( n ) :
        for j in range ( k + 1 ) :
            res = max ( res , dp [ i ] [ j ] )
    if ( max ( arr ) < 0 ) :
        res = max ( arr )
    return res
if __name__ == ' _ _ main _ _ ' :
    dp = [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ]
    arr = [ - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 ]
    K = 1
    N = len ( arr )
    print ( MaximumSubarraySum ( N , arr , K ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
INF = 10000
def minCost ( arr , n ) :
    arr = sorted ( arr )
    m = 2 * n
    cost = [ [ INF for i in range ( m + 1 ) ] for i in range ( n + 1 ) ]
    cost [ 0 ] [ 0 ] = 0
    prev = 0
    for i in range ( 1 , n + 1 ) :
        prev = cost [ i - 1 ] [ 0 ]
        for j in range ( 1 , m + 1 ) :
            cost [ i ] [ j ] = min ( cost [ i ] [ j ] , prev + abs ( j - arr [ i - 1 ] ) )
            prev = min ( prev , cost [ i - 1 ] [ j ] )
    minCost = INF
    for j in range ( 1 , m + 1 ) :
        minCost = min ( minCost , cost [ n ] [ j ] )
    print ( minCost )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 2 , 4 , 4 , 5 , 2 ]
    N = len ( arr )
    minCost ( arr , N )

Index/key variables: i, j, n
Iterator variables: i, j
Python program:
import math
def checkbit ( array , index ) :
    return array [ index >> 5 ] & ( 1 << ( index & 31 ) )
def setbit ( array , index ) :
    array [ index >> 5 ] |= ( 1 << ( index & 31 ) )
a = 2
b = 10
size = abs ( b - a )
size = math . ceil ( size / 32 )
array = [ 0 for i in range ( size ) ]
for i in range ( a , b + 1 ) :
    if ( i % 2 == 0 or i % 5 == 0 ) :
        setbit ( array , i - a )
print ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " )
for i in range ( a , b + 1 ) :
    if ( checkbit ( array , i - a ) ) :
        print ( i , end = " ▁ " )

Iterator variables: i
Python program:
def longestCommonSum ( arr1 , arr2 , n ) :
    maxLen = 0
    for i in range ( 0 , n ) :
        sum1 = 0
        sum2 = 0
        for j in range ( i , n ) :
            sum1 += arr1 [ j ]
            sum2 += arr2 [ j ]
            if ( sum1 == sum2 ) :
                len = j - i + 1
                if ( len > maxLen ) :
                    maxLen = len
    return maxLen
arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ]
arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ]
n = len ( arr1 )
print ( " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ with ▁ same ▁ " " sum ▁ is " , longestCommonSum ( arr1 , arr2 , n ) )

Index/key variables: j
Accumulator variables: sum1, sum2
Iterator variables: i, j
Python program:
def findNumberOfLIS ( nums ) :
    if not nums :
        return 0
    n = len ( nums )
    dp_l = [ 1 ] * n
    dp_c = [ 1 ] * n
    for i , num in enumerate ( nums ) :
        for j in range ( i ) :
            if nums [ i ] <= nums [ j ] :
                continue
            if dp_l [ j ] + 1 > dp_l [ i ] :
                dp_l [ i ] = dp_l [ j ] + 1
                dp_c [ i ] = dp_c [ j ]
            elif dp_l [ j ] + 1 == dp_l [ i ] :
                dp_c [ i ] += dp_c [ j ]
    max_length = max ( x for x in dp_l )
    count = 0
    for l , c in zip ( dp_l , dp_c ) :
        if l == max_length :
            count += c
    return count
arr = [ 1 , 3 , 5 , 4 , 7 ]
print ( findNumberOfLIS ( arr ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: c, i, j, l, num
Python program:
class Node :
    def __init__ ( self , data ) :
        self . left = None
        self . right = None
        self . val = data
def newNode ( data ) :
    temp = Node ( data )
    return temp
def checkEvenOddLevel ( root ) :
    if ( root == None ) :
        return True
    q = [ ]
    q . append ( root )
    level = 0
    while ( len ( q ) != 0 ) :
        l = [ ]
        size = len ( q )
        for i in range ( size ) :
            node = q [ 0 ]
            q . pop ( 0 )
            if ( node . left != None ) :
                q . append ( node . left ) ;
            if ( node . right != None ) :
                q . append ( node . right ) ;
            if ( level % 2 == 0 ) :
                for i in range ( len ( l ) - 1 ) :
                    if ( l [ i + 1 ] > l [ i ] ) :
                        continue
                    return False
            elif ( level % 2 == 1 ) :
                for i in range ( len ( l ) - 1 ) :
                    if ( l [ i + 1 ] < l [ i ] ) :
                        continue
                    return False
            level += 1
        return True
if __name__ == " _ _ main _ _ " :
    root = None
    root = newNode ( 2 )
    root . left = newNode ( 6 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 7 )
    root . right . right = newNode ( 11 )
    root . left . left . left = newNode ( 10 )
    root . left . left . right = newNode ( 5 )
    root . left . right . right = newNode ( 1 )
    if ( checkEvenOddLevel ( root ) ) :
        print ( " YES " )
    else :
        print ( " NO " )

Index/key variables: i
Accumulator variables: level, q
Iterator variables: i
Class/struct names: Node
Python program:
def longestCommonSum ( arr1 , arr2 , n ) :
    arr = [ 0 for i in range ( n ) ]
    for i in range ( n ) :
        arr [ i ] = arr1 [ i ] - arr2 [ i ] ;
    hm = { }
    sum = 0
    max_len = 0
    for i in range ( n ) :
        sum += arr [ i ]
        if ( sum == 0 ) :
            max_len = i + 1
        if sum in hm :
            max_len = max ( max_len , i - hm [ sum ] )
        else :
            hm [ sum ] = i
    return max_len
arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ]
arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ]
n = len ( arr1 )
print ( longestCommonSum ( arr1 , arr2 , n ) )

Index/key variables: i, sum
Iterator variables: i
Python program:
maxN = 20
dp = [ [ [ [ 0 for i in range ( maxN ) ] for j in range ( 27 ) ] for k in range ( 27 ) ] for l in range ( maxN ) ]
def solve ( s , n , idx , k , last , count ) :
    if ( k < 0 ) :
        return n + 1
    if ( idx == n ) :
        return 0
    ans = dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ]
    if ( ans != - 1 ) :
        return ans
    ans = n + 1
    ans = min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) )
    inc = 0
    if ( count == 1 or count == 9 or count == 99 ) :
        inc = 1
    if ( last == s [ idx ] ) :
        ans = min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] , count + 1 ) )
    else :
        ans = max ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] , 1 ) )
    dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ] = ans
    print ( ans )
    return dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ]
def MinRunLengthEncoding ( s , n , k ) :
    for i in range ( maxN ) :
        for j in range ( 27 ) :
            for k in range ( 27 ) :
                for l in range ( maxN ) :
                    dp [ i ] [ j ] [ k ] [ l ] = - 1
    return solve ( s , n , 0 , k , chr ( 123 ) , 0 ) - 1
if __name__ == ' _ _ main _ _ ' :
    S = " abbbcdcdd "
    N = 9
    K = 2
    print ( MinRunLengthEncoding ( S , N , K ) )

Index/key variables: count, i, idx, j, k, l
Iterator variables: i, j, k, l
Python program:
import math
countPaths = 0 ;
def CountUniquePaths ( a , i , j , m , n , ans ) :
    if ( i >= m or j >= n ) :
        return ;
    global countPaths ;
    if ( i == m - 1 and j == n - 1 ) :
        sr = math . sqrt ( ans * a [ i ] [ j ] ) ;
        if ( ( sr - math . floor ( sr ) ) == 0 ) :
            countPaths += 1 ;
    CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i ] [ j ] ) ;
    CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i ] [ j ] ) ;
if __name__ == ' _ _ main _ _ ' :
    M = 3 ; N = 2 ;
    mat = [ [ 1 , 1 ] , [ 3 , 1 ] , [ 3 , 1 ] ] ;
    CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ;
    print ( countPaths ) ;

Index/key variables: i, j
Python program:
def mElementsWithMaxSum ( matrix , M , block , dp ) :
    if block == len ( matrix ) :
        return 0
    if ( dp [ block ] [ M ] != - 1 ) :
        return dp [ block ] [ M ]
    ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp )
    for i in range ( len ( matrix [ block ] ) ) :
        for j in range ( i , len ( matrix [ block ] ) ) :
            if ( j - i + 1 <= M ) :
                x = 0
                if i - 1 >= 0 :
                    x = matrix [ block ] [ i - 1 ]
                ans = max ( ans , matrix [ block ] [ j ] - x + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) )
    dp [ block ] [ M ] = ans
    return ans
def preComputing ( matrix , N ) :
    for i in range ( N ) :
        for j in range ( len ( matrix [ i ] ) ) :
            if j > 0 :
                matrix [ i ] [ j ] = matrix [ i ] [ j - 1 ]
    return matrix
def mElementsWithMaxSumUtil ( matrix , M , N ) :
    matrix = preComputing ( matrix , N )
    sum = 20
    dp = [ [ - 1 for i in range ( M + 5 ) ] for i in range ( N + 5 ) ]
    sum += mElementsWithMaxSum ( matrix , M , 0 , dp )
    print ( sum )
if __name__ == ' _ _ main _ _ ' :
    N = 3
    M = 4
    matrix = [ [ 2 , 3 , 5 ] , [ - 1 , 7 ] , [ 8 , 10 ] ]
    mElementsWithMaxSumUtil ( matrix , M , N )

Index/key variables: M, block, i, j
Iterator variables: i, j
Python program:
def lastRemaining ( n , dp ) :
    if n in dp :
        return dp [ n ]
    if n == 1 :
        return 1
    else :
        dp [ n ] = 2 * ( 1 + n // 2 - lastRemaining ( n // 2 , dp ) )
    return dp [ n ]
N = 5
dp = { }
print ( lastRemaining ( N , dp ) )

Index/key variables: n
Python program:
dp = [ [ - 1 for x in range ( 1005 ) ] for y in range ( 1005 ) ]
def maximumSumUtil ( a , index , count , n ) :
    if ( index > n or count > n + 1 ) :
        return 0
    if ( dp [ index ] [ count ] != - 1 ) :
        return dp [ index ] [ count ]
    ans1 = ( maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count )
    ans2 = maximumSumUtil ( a , index + 1 , count , n )
    dp [ index ] [ count ] = max ( ans1 , ans2 )
    return dp [ index ] [ count ]
def maximumSum ( arr , N ) :
    print ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) )
arr = [ - 1 , 2 , - 10 , 4 , - 20 ]
N = len ( arr )
maximumSum ( arr , N )

Index/key variables: count, index
Python program:
def rearrange ( arr , n , x ) :
    m = { }
    for i in range ( n ) :
        m [ arr [ i ] ] = abs ( x - arr [ i ] )
    m = { k : v for k , v in sorted ( m . items ( ) , key = lambda item : item [ 1 ] ) }
    i = 0
    for it in m . keys ( ) :
        arr [ i ] = it
        i += 1
def printArray ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 5 , 3 , 9 , 2 ]
    n = len ( arr )
    x = 7
    rearrange ( arr , n , x )
    printArray ( arr , n )

Index/key variables: i
Iterator variables: i, it
Python program:
def find_sequence ( n ) :
    if n == 1 :
        return 1 , - 1
    ans = ( find_sequence ( n - 1 ) [ 0 ] + 1 , n - 1 )
    if n % 2 == 0 :
        div_by_2 = find_sequence ( n // 2 )
        if div_by_2 [ 0 ] < ans [ 0 ] :
            ans = ( div_by_2 [ 0 ] + 1 , n // 2 )
    if n % 3 == 0 :
        div_by_3 = find_sequence ( n // 3 )
        if div_by_3 [ 0 ] < ans [ 0 ] :
            ans = ( div_by_3 [ 0 ] + 1 , n // 3 )
    return ans
def find_solution ( n ) :
    a , b = find_sequence ( n )
    print ( a )
    sequence = [ ]
    sequence . append ( n )
    while b != - 1 :
        sequence . append ( b )
        _ , b = find_sequence ( b )
    return sequence [ : : - 1 ]
n = 5
print ( * find_solution ( n ) )

Accumulator variables: sequence
Python program:
def FindNoOfFullVessels ( n , t ) :
    Matrix = [ [ 0 for i in range ( n ) ] for j in range ( n ) ]
    Matrix [ 0 ] [ 0 ] = t * 1.0
    ans = 0
    for i in range ( n ) :
        for j in range ( i + 1 ) :
            exceededwater = Matrix [ i ] [ j ] - 1.0
            if ( exceededwater < 0 ) :
                continue
            ans += 1
            if ( i + 1 < n ) :
                Matrix [ i + 1 ] [ j ] += exceededwater / 2
            if ( i + 1 < n and j + 1 < n ) :
                Matrix [ i + 1 ] [ j + 1 ] += exceededwater / 2
    return ans
N = 3
T = 4
print ( FindNoOfFullVessels ( N , T ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def findMaxTimes ( S ) :
    arr = [ 0 ] * 26
    dp = [ [ 0 for x in range ( 26 ) ] for y in range ( 26 ) ]
    for i in range ( len ( S ) ) :
        now = ord ( S [ i ] ) - ord ( ' a ' )
        for j in range ( 26 ) :
            dp [ j ] [ now ] += arr [ j ]
        arr [ now ] += 1
    ans = 0
    for i in range ( 26 ) :
        ans = max ( ans , arr [ i ] )
    for i in range ( 26 ) :
        for j in range ( 26 ) :
            ans = max ( ans , dp [ i ] [ j ] )
    return ans
S = " ddee "
print ( findMaxTimes ( S ) )

Index/key variables: i, j, now
Iterator variables: i, j
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def inorder ( root ) :
    if root is None :
        return
    inorder ( root . left )
    print ( root . data , " " , end = " " )
    inorder ( root . right )
def prune ( root , sum ) :
    if root is None :
        return None
    root . left = prune ( root . left , sum - root . data )
    root . right = prune ( root . right , sum - root . data )
    if root . left is None and root . right is None :
        if sum > root . data :
            return None
    return root
root = Node ( 1 )
root . left = Node ( 2 )
root . right = Node ( 3 )
root . left . left = Node ( 4 )
root . left . right = Node ( 5 )
root . right . left = Node ( 6 )
root . right . right = Node ( 7 )
root . left . left . left = Node ( 8 )
root . left . left . right = Node ( 9 )
root . left . right . left = Node ( 12 )
root . right . right . left = Node ( 10 )
root . right . right . left . right = Node ( 11 )
root . left . left . right . left = Node ( 13 )
root . left . left . right . right = Node ( 14 )
root . left . left . right . right . left = Node ( 15 )
print ( " Tree ▁ before ▁ truncation " )
inorder ( root )
prune ( root , 45 )
print ( " Tree after truncation " )
inorder ( root )

Class/struct names: Node
Python program:
def count_unique_sum ( n ) :
    ar = [ 0 ] * ( n + 1 )
    fsum = [ 0 ] * ( n + 1 )
    rsum = [ 0 ] * ( n + 1 )
    ans = 1
    for i in range ( 0 , n + 1 ) :
        ar [ i ] = i
    fsum [ 0 ] = ar [ 0 ]
    rsum [ n ] = ar [ n ]
    for i in range ( 1 , n + 1 ) :
        fsum [ i ] = ar [ i ] + fsum [ i - 1 ]
    for i in range ( n - 1 , - 1 , - 1 ) :
        rsum [ i ] = ar [ i ] + rsum [ i + 1 ]
    for k in range ( 2 , n + 1 ) :
        ans += ( 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] )
    return ans
N = 4
print ( count_unique_sum ( N ) )

Index/key variables: i, n
Accumulator variables: ans
Iterator variables: i, k
Python program:
NA = - 1
def moveToEnd ( mPlusN , size ) :
    i = 0
    j = size - 1
    for i in range ( size - 1 , - 1 , - 1 ) :
        if ( mPlusN [ i ] != NA ) :
            mPlusN [ j ] = mPlusN [ i ]
            j -= 1
def merge ( mPlusN , N , m , n ) :
    i = n
    j = 0
    k = 0
    while ( k < ( m + n ) ) :
        if ( ( j == n ) or ( i < ( m + n ) and mPlusN [ i ] <= N [ j ] ) ) :
            mPlusN [ k ] = mPlusN [ i ]
            k += 1
            i += 1
        else :
            mPlusN [ k ] = N [ j ]
            k += 1
            j += 1
def printArray ( arr , size ) :
    for i in range ( size ) :
        print ( arr [ i ] , " ▁ " , end = " " )
    print ( )
mPlusN = [ 2 , 8 , NA , NA , NA , 13 , NA , 15 , 20 ]
N = [ 5 , 7 , 9 , 25 ]
n = len ( N )
m = len ( mPlusN ) - n
moveToEnd ( mPlusN , m + n )
merge ( mPlusN , N , m , n )
printArray ( mPlusN , m + n )

Index/key variables: i, j, k
Iterator variables: i
Python program:
def getCount ( n , K ) :
    if ( n == 1 ) :
        return 10
    dp = [ 0 ] * 11
    next = [ 0 ] * 11
    for i in range ( 1 , 9 + 1 ) :
        dp [ i ] = 1
    for i in range ( 2 , n + 1 ) :
        for j in range ( 9 + 1 ) :
            l = max ( 0 , j - k )
            r = min ( 9 , j + k )
            next [ l ] += dp [ j ]
            next [ r + 1 ] -= dp [ j ]
        for j in range ( 1 , 9 + 1 ) :
            next [ j ] += next [ j - 1 ]
        for j in range ( 10 ) :
            dp [ j ] = next [ j ]
            next [ j ] = 0
    count = 0
    for i in range ( 9 + 1 ) :
        count += dp [ i ]
    return count
if __name__ == ' _ _ main _ _ ' :
    n = 2
    k = 1
    print ( getCount ( n , k ) )

Index/key variables: i, j, l
Accumulator variables: count
Iterator variables: i, j
Python program:
def sortedAfterSwap ( A , B , n ) :
    for i in range ( 0 , n - 1 ) :
        if ( B [ i ] == 1 ) :
            j = i
            while ( B [ j ] == 1 ) :
                j = j + 1
            A = A [ 0 : i ] + sorted ( A [ i : j + 1 ] ) + A [ j + 1 : ]
            i = j
    for i in range ( 0 , n ) :
        if ( A [ i ] != i + 1 ) :
            return False
    return True
A = [ 1 , 2 , 5 , 3 , 4 , 6 ]
B = [ 0 , 1 , 1 , 1 , 0 ]
n = len ( A )
if ( sortedAfterSwap ( A , B , n ) ) :
    print ( " A ▁ can ▁ be ▁ sorted " )
else :
    print ( " A ▁ can ▁ not ▁ be ▁ sorted " )

Index/key variables: i, j
Iterator variables: i
Python program:
class Edge :
    def __init__ ( self ) :
        self . src = 0
        self . dest = 0
        self . weight = 0
class Graph :
    def __init__ ( self ) :
        self . V = 0
        self . E = 0
        self . edge = [ ]
def createGraph ( V , E ) :
    graph = Graph ( ) ;
    graph . V = V ;
    graph . E = E ;
    graph . edge = [ Edge ( ) for i in range ( graph . E ) ]
    return graph ;
def NegCycleBellmanFord ( graph , src ) :
    V = graph . V ;
    E = graph . E ;
    dist = [ 1000000 for i in range ( V ) ]
    parent = [ - 1 for i in range ( V ) ]
    dist [ src ] = 0 ;
    for i in range ( 1 , V ) :
        for j in range ( E ) :
            u = graph . edge [ j ] . src ;
            v = graph . edge [ j ] . dest ;
            weight = graph . edge [ j ] . weight ;
            if ( dist [ u ] != 1000000 and dist [ u ] + weight < dist [ v ] ) :
                dist [ v ] = dist [ u ] + weight ;
                parent [ v ] = u ;
    C = - 1 ;
    for i in range ( E ) :
        u = graph . edge [ i ] . src ;
        v = graph . edge [ i ] . dest ;
        weight = graph . edge [ i ] . weight ;
        if ( dist [ u ] != 1000000 and dist [ u ] + weight < dist [ v ] ) :
            C = v ;
            break ;
    if ( C != - 1 ) :
        for i in range ( V ) :
            C = parent [ C ] ;
        cycle = [ ]
        v = C
        while ( True ) :
            cycle . append ( v )
            if ( v == C and len ( cycle ) > 1 ) :
                break ;
            v = parent [ v ]
        cycle . reverse ( )
        for v in cycle :
            print ( v , end = " ▁ " ) ;
        print ( )
    else :
        print ( - 1 ) ;
if __name__ == ' _ _ main _ _ ' :
    V = 5 ;
    E = 5 ;
    graph = createGraph ( V , E ) ;
    graph . edge [ 0 ] . src = 0 ;
    graph . edge [ 0 ] . dest = 1 ;
    graph . edge [ 0 ] . weight = 1 ;
    graph . edge [ 1 ] . src = 1 ;
    graph . edge [ 1 ] . dest = 2 ;
    graph . edge [ 1 ] . weight = 2 ;
    graph . edge [ 2 ] . src = 2 ;
    graph . edge [ 2 ] . dest = 3 ;
    graph . edge [ 2 ] . weight = 3 ;
    graph . edge [ 3 ] . src = 3 ;
    graph . edge [ 3 ] . dest = 4 ;
    graph . edge [ 3 ] . weight = - 3 ;
    graph . edge [ 4 ] . src = 4 ;
    graph . edge [ 4 ] . dest = 1 ;
    graph . edge [ 4 ] . weight = - 3 ;
    NegCycleBellmanFord ( graph , 0 ) ;

Index/key variables: C, i, j, src, u, v
Accumulator variables: cycle
Iterator variables: i, j, v
Class/struct names: Edge, Graph
Python program:
def sortedAfterSwap ( A , B , n ) :
    for i in range ( 0 , n - 1 ) :
        if B [ i ] :
            if A [ i ] != i + 1 :
                A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ]
    for i in range ( n ) :
        if A [ i ] != i + 1 :
            return False
    return True
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 2 , 5 , 3 , 4 , 6 ]
    B = [ 0 , 1 , 1 , 1 , 0 ]
    n = len ( A )
    if ( sortedAfterSwap ( A , B , n ) ) :
        print ( " A ▁ can ▁ be ▁ sorted " )
    else :
        print ( " A ▁ can ▁ not ▁ be ▁ sorted " )

Index/key variables: i
Iterator variables: i
Python program:
def segregate0and1 ( arr , n ) :
    type0 = 0 ; type1 = n - 1
    while ( type0 < type1 ) :
        if ( arr [ type0 ] == 1 ) :
            arr [ type0 ] , arr [ type1 ] = arr [ type1 ] , arr [ type0 ]
            type1 -= 1
        else :
            type0 += 1
arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ]
n = len ( arr )
segregate0and1 ( arr , n )
for i in range ( 0 , n ) :
    print ( arr [ i ] , end = " ▁ " )

Index/key variables: i, type0, type1
Iterator variables: i
Python program:
def getInvCount ( arr , n ) :
    inv_count = 0
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            if ( arr [ i ] > arr [ j ] ) :
                inv_count += 1
    return inv_count
arr = [ 1 , 20 , 6 , 4 , 5 ]
n = len ( arr )
print ( " Number ▁ of ▁ inversions ▁ are " , getInvCount ( arr , n ) )

Index/key variables: i, j
Accumulator variables: inv_count
Iterator variables: i, j
Python program:
from collections import deque
def addEdge ( v , w ) :
    global adj
    adj [ v ] . append ( w )
    adj [ w ] . append ( v )
def isReachable ( s , d ) :
    if ( s == d ) :
        return True
    visited = [ False for i in range ( V ) ]
    queue = deque ( )
    visited [ s ] = True
    queue . append ( s )
    while ( len ( queue ) > 0 ) :
        s = queue . popleft ( )
        for i in adj [ s ] :
            if ( i == d ) :
                return True
            if ( not visited [ i ] ) :
                visited [ i ] = True
                queue . append ( i )
    return False
if __name__ == ' _ _ main _ _ ' :
    V = 4
    adj = [ [ ] for i in range ( V + 1 ) ]
    addEdge ( 0 , 1 )
    addEdge ( 0 , 2 )
    addEdge ( 1 , 2 )
    addEdge ( 2 , 0 )
    addEdge ( 2 , 3 )
    addEdge ( 3 , 3 )
    u , v = 1 , 3
    if ( isReachable ( u , v ) ) :
        print ( " There ▁ is ▁ a ▁ path ▁ from " , u , " to " , v )
    else :
        print ( " There ▁ is ▁ no ▁ path ▁ from " , u , " to " , v )

Index/key variables: i, s, v, w
Accumulator variables: queue
Iterator variables: i
Python program:
def findResultUtil ( arr , L , R ) :
    result = 0
    flag = False
    for i in range ( L , R + 1 ) :
        if ( flag == False ) :
            result = result + arr [ i ]
            flag = True
        else :
            result = result - arr [ i ]
            flag = False
    return result
def findResult ( arr , n , q , m ) :
    for i in range ( m ) :
        print ( findResultUtil ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 ]
    n = len ( arr )
    q = [ [ 2 , 5 ] , [ 6 , 8 ] , [ 1 , 7 ] , [ 4 , 8 ] , [ 0 , 5 ] ]
    m = len ( q )
    findResult ( arr , n , q , m )

Index/key variables: i
Iterator variables: i
Boolean variables: flag
Python program:
def minAbsSumPair ( arr , arr_size ) :
    inv_count = 0
    if arr_size < 2 :
        print ( " Invalid ▁ Input " )
        return
    min_l = 0
    min_r = 1
    min_sum = arr [ 0 ] + arr [ 1 ]
    for l in range ( 0 , arr_size - 1 ) :
        for r in range ( l + 1 , arr_size ) :
            sum = arr [ l ] + arr [ r ]
            if abs ( min_sum ) > abs ( sum ) :
                min_sum = sum
                min_l = l
                min_r = r
    print ( " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are " , arr [ min_l ] , " and ▁ " , arr [ min_r ] )
arr = [ 1 , 60 , - 10 , 70 , - 80 , 85 ]
minAbsSumPair ( arr , 6 ) ;

Index/key variables: l, min_l, min_r, r
Iterator variables: l, r