dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def maximumSum ( S , M , N , stacks ) :
    dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( S + 1 ) ]
    for i in range ( S ) :
        for j in range ( N + 1 ) :
            for k in range ( min ( j , M ) + 1 ) :
                dp [ i + 1 ] [ j ] = max ( dp [ i + 1 ] [ j ] , stacks [ i ] [ k ] + dp [ i ] [ j - k ] )
    result = - sys . maxsize - 1
    for i in range ( N + 1 ) :
        result = max ( result , dp [ S ] [ i ] )
    return result
if __name__ == " _ _ main _ _ " :
    S = 2
    M = 4
    stacks = [ [ 2 , 6 , 4 , 5 ] , [ 1 , 6 , 15 , 10 ] ]
    N = 3
    print ( maximumSum ( S , M , N , stacks ) )

Index/key variables: S, i, j, k
Iterator variables: i, j, k
Python program:
import sys
def findMinSum ( arr , n ) :
    for i in range ( 1 , n ) :
        if ( not abs ( arr [ i - 1 ] ) < abs ( arr [ i ] ) ) :
            arr [ i - 1 ] , arr [ i ] = arr [ i ] , arr [ i - 1 ]
    Min = sys . maxsize
    x = 0
    y = 0
    for i in range ( 1 , n ) :
        if ( abs ( arr [ i - 1 ] + arr [ i ] ) <= Min ) :
            Min = abs ( arr [ i - 1 ] + arr [ i ] )
            x = i - 1
            y = i
    print ( " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are " , arr [ x ] , " and " , arr [ y ] )
arr = [ 1 , 60 , - 10 , 70 , - 80 , 85 ]
n = len ( arr )
findMinSum ( arr , n )

Index/key variables: i, x, y
Iterator variables: i
Python program:
ans = [ ]
flag = 1 ;
def dfs ( adj , order , bridge_detect , mark , v , l ) :
    global flag
    mark [ v ] = 1 ;
    order [ v ] = order [ l ] + 1 ;
    bridge_detect [ v ] = order [ v ] ;
    for i in range ( len ( adj [ v ] ) ) :
        u = adj [ v ] [ i ] ;
        if ( u == l ) :
            continue ;
        if ( order [ v ] < order [ u ] ) :
            continue ;
        if ( mark [ u ] ) :
            bridge_detect [ v ] = min ( order [ u ] , bridge_detect [ v ] ) ;
        else :
            dfs ( adj , order , bridge_detect , mark , u , v ) ;
        bridge_detect [ v ] = min ( bridge_detect [ u ] , bridge_detect [ v ] ) ;
        ans . append ( [ v , u ] ) ;
    if ( bridge_detect [ v ] == order [ v ] and l != 0 ) :
        flag = 0 ;
    return flag ;
def convert ( adj , n ) :
    order = [ 0 for i in range ( n ) ]
    bridge_detect = [ 0 for i in range ( n ) ]
    mark = [ False for i in range ( n ) ]
    flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ;
    if ( flag == 0 ) :
        print ( - 1 )
    else :
        for it in ans :
            print ( " { } ▁ - > ▁ { } " . format ( it [ 0 ] , it [ 1 ] ) )
def createGraph ( Edges , adj , M ) :
    for i in range ( M ) :
        u = Edges [ i ] [ 0 ] ;
        v = Edges [ i ] [ 1 ] ;
        adj [ u ] . append ( v ) ;
        adj [ v ] . append ( u ) ;
if __name__ == " _ _ main _ _ " :
    N = 5
    M = 6 ;
    Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ;
    adj = [ [ ] for i in range ( N ) ]
    createGraph ( Edges , adj , M ) ;
    convert ( adj , N ) ;

Index/key variables: i, l, u, v
Accumulator variables: ans
Iterator variables: i, it
Python program:
def increasing ( a , n ) :
    for i in range ( 0 , n - 1 ) :
        if ( a [ i ] >= a [ i + 1 ] ) :
            return False
    return True
def decreasing ( a , n ) :
    for i in range ( 0 , n - 1 ) :
        if ( a [ i ] < a [ i + 1 ] ) :
            return False
    return True
def shortestUnsorted ( a , n ) :
    if ( increasing ( a , n ) == True or decreasing ( a , n ) == True ) :
        return 0
    else :
        return 3
ar = [ 7 , 9 , 10 , 8 , 11 ]
n = len ( ar )
print ( shortestUnsorted ( ar , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def minSwaps ( arr ) :
    n = len ( arr )
    arrpos = [ * enumerate ( arr ) ]
    arrpos . sort ( key = lambda it : it [ 1 ] )
    vis = { k : False for k in range ( n ) }
    ans = 0
    for i in range ( n ) :
        if vis [ i ] or arrpos [ i ] [ 0 ] == i :
            continue
        cycle_size = 0
        j = i
        while not vis [ j ] :
            vis [ j ] = True
            j = arrpos [ j ] [ 0 ]
            cycle_size += 1
        if cycle_size > 0 :
            ans += ( cycle_size - 1 )
    return ans
arr = [ 1 , 5 , 4 , 3 , 2 ]
print ( minSwaps ( arr ) )

Index/key variables: i, j
Accumulator variables: ans, cycle_size
Iterator variables: i
Python program:
def swap ( arr , i , j ) :
    temp = arr [ i ]
    arr [ i ] = arr [ j ]
    arr [ j ] = temp
def indexOf ( arr , ele ) :
    for i in range ( len ( arr ) ) :
        if ( arr [ i ] == ele ) :
            return i
    return - 1
def minSwaps ( arr , N ) :
    ans = 0
    temp = arr . copy ( )
    temp . sort ( )
    for i in range ( N ) :
        if ( arr [ i ] != temp [ i ] ) :
            ans += 1
            swap ( arr , i , indexOf ( arr , temp [ i ] ) )
    return ans
if __name__ == " _ _ main _ _ " :
    a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ]
    n = len ( a )
    print ( minSwaps ( a , n ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i
Python program:
def minSwap ( arr , n ) :
    ans = 0
    temp = arr . copy ( )
    h = { }
    temp . sort ( )
    for i in range ( n ) :
        h [ arr [ i ] ] = i
    init = 0
    for i in range ( n ) :
        if ( arr [ i ] != temp [ i ] ) :
            ans += 1
            init = arr [ i ]
            arr [ i ] , arr [ h [ temp [ i ] ] ] = arr [ h [ temp [ i ] ] ] , arr [ i ]
            h [ init ] = h [ temp [ i ] ]
            h [ temp [ i ] ] = i
    return ans
a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ]
n = len ( a )
print ( minSwap ( a , n ) )

Index/key variables: i, init
Accumulator variables: ans
Iterator variables: i
Python program:
def printUnion ( arr1 , arr2 , m , n ) :
    i , j = 0 , 0
    while i < m and j < n :
        if arr1 [ i ] < arr2 [ j ] :
            print ( arr1 [ i ] )
            i += 1
        elif arr2 [ j ] < arr1 [ i ] :
            print ( arr2 [ j ] )
            j += 1
        else :
            print ( arr2 [ j ] )
            j += 1
            i += 1
    while i < m :
        print ( arr1 [ i ] )
        i += 1
    while j < n :
        print ( arr2 [ j ] )
        j += 1
arr1 = [ 1 , 2 , 4 , 5 , 6 ]
arr2 = [ 2 , 3 , 5 , 7 ]
m = len ( arr1 )
n = len ( arr2 )
printUnion ( arr1 , arr2 , m , n )

Index/key variables: i, j
Python program:
def UnionArray ( arr1 , arr2 ) :
    m = arr1 [ - 1 ]
    n = arr2 [ - 1 ]
    ans = 0
    if m > n :
        ans = m
    else :
        ans = n
    newtable = [ 0 ] * ( ans + 1 )
    print ( arr1 [ 0 ] , end = " ▁ " )
    newtable [ arr1 [ 0 ] ] += 1
    for i in range ( 1 , len ( arr1 ) ) :
        if arr1 [ i ] != arr1 [ i - 1 ] :
            print ( arr1 [ i ] , end = " ▁ " )
            newtable [ arr1 [ i ] ] += 1
    for j in range ( 0 , len ( arr2 ) ) :
        if newtable [ arr2 [ j ] ] == 0 :
            print ( arr2 [ j ] , end = " ▁ " )
            newtable [ arr2 [ j ] ] += 1
if __name__ == " _ _ main _ _ " :
    arr1 = [ 1 , 2 , 2 , 2 , 3 ]
    arr2 = [ 2 , 3 , 4 , 5 ]
    UnionArray ( arr1 , arr2 )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def printIntersection ( arr1 , arr2 , m , n ) :
    i , j = 0 , 0
    while i < m and j < n :
        if arr1 [ i ] < arr2 [ j ] :
            i += 1
        elif arr2 [ j ] < arr1 [ i ] :
            j += 1
        else :
            print ( arr2 [ j ] )
            j += 1
            i += 1
arr1 = [ 1 , 2 , 4 , 5 , 6 ]
arr2 = [ 2 , 3 , 5 , 7 ]
m = len ( arr1 )
n = len ( arr2 )
printIntersection ( arr1 , arr2 , m , n )

Index/key variables: i, j
Python program:
import numpy as np ;
dp = np . ones ( ( 15 , 2 ) ) * - 1 ;
def convertToString ( num ) :
    return str ( num ) ;
def calculate ( pos , tight , D , sz , num ) :
    if ( pos == len ( num ) ) :
        return 1 ;
    if ( dp [ pos ] [ tight ] != - 1 ) :
        return dp [ pos ] [ tight ] ;
    val = 0 ;
    if ( tight == 0 ) :
        for i in range ( sz ) :
            if ( D [ i ] < ( ord ( num [ pos ] ) - ord ( '0' ) ) ) :
                val += calculate ( pos + 1 , 1 , D , sz , num ) ;
            elif ( D [ i ] == ord ( num [ pos ] ) - ord ( '0' ) ) :
                val += calculate ( pos + 1 , tight , D , sz , num ) ;
    else :
        for i in range ( sz ) :
            val += calculate ( pos + 1 , tight , D , sz , num ) ;
    dp [ pos ] [ tight ] = val ;
    return dp [ pos ] [ tight ] ;
def countNumbers ( D , N , sz ) :
    num = convertToString ( N ) ;
    length = len ( num ) ;
    ans = calculate ( 0 , 0 , D , sz , num ) ;
    for i in range ( 1 , length ) :
        ans += calculate ( i , 1 , D , sz , num ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    sz = 3 ;
    D = [ 1 , 4 , 9 ] ;
    N = 10 ;
    print ( countNumbers ( D , N , sz ) ) ;

Index/key variables: i, pos, tight
Accumulator variables: ans, val
Iterator variables: i
Python program:
dp = [ [ - 1 for i in range ( 1001 ) ] for j in range ( 1001 ) ]
def find_max ( i , sum , v , k ) :
    if ( i == len ( v ) ) :
        return 0
    if ( dp [ i ] [ sum ] != - 1 ) :
        return dp [ i ] [ sum ]
    ans = 0
    if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) :
        ans = find_max ( i + 1 , sum , v , k )
    if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) :
        ans = max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) )
    dp [ i ] [ sum ] = ans
    return dp [ i ] [ sum ]
if __name__ == ' _ _ main _ _ ' :
    arr = [ 43 , 1 , 17 , 26 , 15 ]
    k = 16
    print ( find_max ( 0 , 0 , arr , k ) )

Index/key variables: i, sum
Python program:
def printUnion ( arr1 , arr2 , m , n ) :
    if ( m > n ) :
        tempp = arr1
        arr1 = arr2
        arr2 = tempp
        temp = m
        m = n
        n = temp
    arr1 . sort ( )
    for i in range ( 0 , m ) :
        print ( arr1 [ i ] , end = " ▁ " )
    for i in range ( 0 , n ) :
        if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) :
            print ( arr2 [ i ] , end = " ▁ " )
def printIntersection ( arr1 , arr2 , m , n ) :
    if ( m > n ) :
        tempp = arr1
        arr1 = arr2
        arr2 = tempp
        temp = m
        m = n
        n = temp
    arr1 . sort ( )
    for i in range ( 0 , n ) :
        if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) :
            print ( arr2 [ i ] , end = " ▁ " )
def binarySearch ( arr , l , r , x ) :
    if ( r >= l ) :
        mid = int ( l + ( r - l ) / 2 )
        if ( arr [ mid ] == x ) :
            return mid
        if ( arr [ mid ] > x ) :
            return binarySearch ( arr , l , mid - 1 , x )
        return binarySearch ( arr , mid + 1 , r , x )
    return - 1
arr1 = [ 7 , 1 , 5 , 2 , 3 , 6 ]
arr2 = [ 3 , 8 , 6 , 20 , 7 ]
m = len ( arr1 )
n = len ( arr2 )
print ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " )
printUnion ( arr1 , arr2 , m , n )
print ( " Intersection of two arrays is   " )
printIntersection ( arr1 , arr2 , m , n )

Index/key variables: i, mid
Iterator variables: i
Python program:
class node :
    def __init__ ( self ) :
        self . data = 0
        self . left = None
        self . right = None
def printPath ( root , target_leaf ) :
    if ( root == None ) :
        return False
    if ( root == target_leaf or printPath ( root . left , target_leaf ) or printPath ( root . right , target_leaf ) ) :
        print ( root . data , end = " ▁ " )
        return True
    return False
max_sum_ref = 0
target_leaf_ref = None
def getTargetLeaf ( Node , curr_sum ) :
    global max_sum_ref
    global target_leaf_ref
    if ( Node == None ) :
        return
    curr_sum = curr_sum + Node . data
    if ( Node . left == None and Node . right == None ) :
        if ( curr_sum > max_sum_ref ) :
            max_sum_ref = curr_sum
            target_leaf_ref = Node
    getTargetLeaf ( Node . left , curr_sum )
    getTargetLeaf ( Node . right , curr_sum )
def maxSumPath ( Node ) :
    global max_sum_ref
    global target_leaf_ref
    if ( Node == None ) :
        return 0
    target_leaf_ref = None
    max_sum_ref = - 32676
    getTargetLeaf ( Node , 0 )
    printPath ( Node , target_leaf_ref ) ;
    return max_sum_ref ;
def newNode ( data ) :
    temp = node ( ) ;
    temp . data = data ;
    temp . left = None ;
    temp . right = None ;
    return temp ;
root = None ;
root = newNode ( 10 ) ;
root . left = newNode ( - 2 ) ;
root . right = newNode ( 7 ) ;
root . left . left = newNode ( 8 ) ;
root . left . right = newNode ( - 4 ) ;
print ( " Following ▁ are ▁ the ▁ nodes ▁ on ▁ the ▁ maximum ▁ sum ▁ path ▁ " ) ;
sum = maxSumPath ( root ) ;
print (   " Sum of the nodes is   " , sum ) ;

Class/struct names: node
Python program:
def intersection ( a , b , n , m ) :
    i = 0
    j = 0
    while ( i < n and j < m ) :
        if ( a [ i ] > b [ j ] ) :
            j += 1
        else :
            if ( b [ j ] > a [ i ] ) :
                i += 1
            else :
                print ( a [ i ] , end = " ▁ " )
                i += 1
                j += 1
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 ]
    b = [ 3 , 3 , 5 ]
    n = len ( a )
    m = len ( b )
    a . sort ( )
    b . sort ( )
    intersection ( a , b , n , m )

Index/key variables: i, j
Python program:
class Node :
    def __init__ ( self ) :
        self . data = 0
        self . left = None
        self . right = None
def newNode ( data ) :
    newNode = Node ( )
    newNode . data = data
    newNode . left = newNode . right = None
    return ( newNode )
res = 0
def countUntil ( root ) :
    global res
    if ( root == None ) :
        return 0
    l = countUntil ( root . left )
    r = countUntil ( root . right )
    maxCount = 0
    if ( root . data == 1 ) :
        maxCount = l + r + 1
    else :
        maxCount = l + r
    res = max ( res , maxCount )
    if ( root . data == 1 ) :
        return max ( l , r ) + 1
    else :
        return max ( l , r )
def findMaxCount ( root ) :
    global res
    res = - 999999
    countUntil ( root )
    return res
root = newNode ( 1 )
root . left = newNode ( 0 )
root . right = newNode ( 1 )
root . left . left = newNode ( 1 )
root . left . right = newNode ( 1 )
root . left . right . left = newNode ( 1 )
root . left . right . right = newNode ( 0 )
print ( findMaxCount ( root ) )

Class/struct names: Node
Python program:
def printUnion ( arr1 , arr2 , n1 , n2 ) :
    hs = set ( )
    for i in range ( 0 , n1 ) :
        hs . add ( arr1 [ i ] )
    for i in range ( 0 , n2 ) :
        hs . add ( arr2 [ i ] )
    print ( " Union : " )
    for i in hs :
        print ( i , end = " ▁ " )
    print ( " " )
def printIntersection ( arr1 , arr2 , n1 , n2 ) :
    hs = set ( )
    for i in range ( 0 , n1 ) :
        hs . add ( arr1 [ i ] )
    print ( " Intersection : " )
    for i in range ( 0 , n2 ) :
        if arr2 [ i ] in hs :
            print ( arr2 [ i ] , end = " ▁ " )
arr1 = [ 7 , 1 , 5 , 2 , 3 , 6 ]
arr2 = [ 3 , 8 , 6 , 20 , 7 ]
n1 = len ( arr1 )
n2 = len ( arr2 )
printUnion ( arr1 , arr2 , n1 , n2 )
printIntersection ( arr1 , arr2 , n1 , n2 )

Index/key variables: i
Accumulator variables: hs
Iterator variables: i
Python program:
def findPosition ( a , b ) :
    v = len ( a ) + len ( b ) ;
    ans = [ 0 ] * v ;
    zero1 = zero2 = 0 ;
    print ( " Intersection ▁ : " , end = " ▁ " ) ;
    for i in range ( len ( a ) ) :
        zero1 = iterateArray ( a , v , ans , i ) ;
    for j in range ( len ( b ) ) :
        zero2 = iterateArray ( b , v , ans , j ) ;
    zero = zero1 + zero2 ;
    placeZeros ( v , ans , zero ) ;
    printUnion ( v , ans , zero ) ;
def printUnion ( v , ans , zero ) :
    zero1 = 0 ;
    print ( " Union : " , end = "   " ) ;
    for i in range ( v ) :
        if ( ( zero == 0 and ans [ i ] == 0 ) or ( ans [ i ] == 0 and zero1 > 0 ) ) :
            continue ;
        if ( ans [ i ] == 0 ) :
            zero1 += 1 ;
        print ( ans [ i ] , end = " , " ) ;
def placeZeros ( v , ans , zero ) :
    if ( zero == 2 ) :
        print ( "0" ) ;
        d = [ 0 ] ;
        placeValue ( d , ans , 0 , 0 , v ) ;
    if ( zero == 1 ) :
        d = [ 0 ] ;
        placeValue ( d , ans , 0 , 0 , v ) ;
def iterateArray ( a , v , ans , i ) :
    if ( a [ i ] != 0 ) :
        p = a [ i ] % v ;
        placeValue ( a , ans , i , p , v ) ;
    else :
        return 1 ;
    return 0 ;
def placeValue ( a , ans , i , p , v ) :
    p = p % v ;
    if ( ans [ p ] == 0 ) :
        ans [ p ] = a [ i ] ;
    else :
        if ( ans [ p ] == a [ i ] ) :
            print ( a [ i ] , end = " , " ) ;
        else :
            p = p + 1 ;
            placeValue ( a , ans , i , p , v ) ;
a = [ 7 , 1 , 5 , 2 , 3 , 6 ] ;
b = [ 3 , 8 , 6 , 20 , 7 ] ;
findPosition ( a , b ) ;

Index/key variables: i, p
Accumulator variables: zero1
Iterator variables: i, j
Python program:
def findCount ( N ) :
    list = [ ]
    sum = 0
    for i in range ( 0 , 20 ) :
        sum += 2 ** i
        list . append ( sum )
    for value in list :
        if ( value >= N ) :
            return ( list . index ( value ) + 1 )
N = 10
print ( findCount ( N ) )

Accumulator variables: list, sum
Iterator variables: i, value
Python program:
def findSetBits ( n ) :
    dp = [ 0 ] * ( n + 1 ) ;
    print ( dp [ 0 ] , end = " ▁ " ) ;
    for i in range ( 1 , n + 1 ) :
        if ( i % 2 == 0 ) :
            dp [ i ] = dp [ i // 2 ] ;
        else :
            dp [ i ] = dp [ i // 2 ] + 1 ;
        print ( dp [ i ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    n = 5 ;
    findSetBits ( n ) ;

Index/key variables: i
Iterator variables: i
Python program:
def printArr ( arr , n ) :
    for i in range ( n ) :
        print ( arr [ i ] , end = " ▁ " )
def sortArr ( arr , n ) :
    cnt0 = 0
    cnt1 = 0
    cnt2 = 0
    for i in range ( n ) :
        if arr [ i ] == 0 :
            cnt0 += 1
        elif arr [ i ] == 1 :
            cnt1 += 1
        elif arr [ i ] == 2 :
            cnt2 += 1
    i = 0
    while ( cnt0 > 0 ) :
        arr [ i ] = 0
        i += 1
        cnt0 -= 1
    while ( cnt1 > 0 ) :
        arr [ i ] = 1
        i += 1
        cnt1 -= 1
    while ( cnt2 > 0 ) :
        arr [ i ] = 2
        i += 1
        cnt2 -= 1
    printArr ( arr , n )
arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ]
n = len ( arr )
sortArr ( arr , n )

Index/key variables: i
Accumulator variables: cnt0, cnt1, cnt2
Iterator variables: i
Python program:
def printUnsorted ( arr , n ) :
    e = n - 1
    for s in range ( 0 , n - 1 ) :
        if arr [ s ] > arr [ s + 1 ] :
            break
    if s == n - 1 :
        print ( " The ▁ complete ▁ array ▁ is ▁ sorted " )
        exit ( )
    e = n - 1
    while e > 0 :
        if arr [ e ] < arr [ e - 1 ] :
            break
        e -= 1
    max = arr [ s ]
    min = arr [ s ]
    for i in range ( s + 1 , e + 1 ) :
        if arr [ i ] > max :
            max = arr [ i ]
        if arr [ i ] < min :
            min = arr [ i ]
    for i in range ( s ) :
        if arr [ i ] > min :
            s = i
            break
    i = n - 1
    while i >= e + 1 :
        if arr [ i ] < max :
            e = i
            break
        i -= 1
    print ( " The ▁ unsorted ▁ subarray ▁ which ▁ makes ▁ the ▁ given ▁ array " )
    print ( " sorted ▁ lies ▁ between ▁ the ▁ indexes ▁ % d ▁ and ▁ % d " % ( s , e ) )
arr = [ 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ]
arr_size = len ( arr )
printUnsorted ( arr , arr_size )

Index/key variables: e, i, s
Iterator variables: i, s
Python program:
def findNumberOfTriangles ( arr , n ) :
    count = 0
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            for k in range ( j + 1 , n ) :
                if ( arr [ i ] + arr [ j ] > arr [ k ] and arr [ i ] + arr [ k ] > arr [ j ] and arr [ k ] + arr [ j ] > arr [ i ] ) :
                    count += 1
    return count
arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ]
size = len ( arr )
print ( " Total ▁ number ▁ of ▁ triangles ▁ possible ▁ is " , findNumberOfTriangles ( arr , size ) )

Index/key variables: i, j, k
Accumulator variables: count
Iterator variables: i, j, k
Python program:
def CountTriangles ( A ) :
    n = len ( A ) ;
    A . sort ( ) ;
    count = 0 ;
    for i in range ( n - 1 , 0 , - 1 ) :
        l = 0 ;
        r = i - 1 ;
        while ( l < r ) :
            if ( A [ l ] + A [ r ] > A [ i ] ) :
                count += r - l ;
                r -= 1 ;
            else :
                l += 1 ;
    print ( " No ▁ of ▁ possible ▁ solutions : ▁ " , count ) ;
if __name__ == ' _ _ main _ _ ' :
    A = [ 4 , 3 , 5 , 7 , 6 ] ;
    CountTriangles ( A ) ;

Index/key variables: i, l, r
Accumulator variables: count
Iterator variables: i
Python program:
class Node :
    def __init__ ( self , data ) :
        self . data = data
        self . left = None
        self . right = None
def newNode ( data ) :
    temp = Node ( data )
    return temp ;
def sumOfGrandChildren ( node , mp ) :
    sum = 0 ;
    if ( node . left ) :
        sum += ( getMaxSumUtil ( node . left . left , mp ) + getMaxSumUtil ( node . left . right , mp ) ) ;
    if ( node . right ) :
        sum += ( getMaxSumUtil ( node . right . left , mp ) + getMaxSumUtil ( node . right . right , mp ) ) ;
    return sum ;
def getMaxSumUtil ( node , mp ) :
    if ( node == None ) :
        return 0 ;
    if node in mp :
        return mp [ node ] ;
    incl = ( node . data + sumOfGrandChildren ( node , mp ) ) ;
    excl = ( getMaxSumUtil ( node . left , mp ) + getMaxSumUtil ( node . right , mp ) ) ;
    mp [ node ] = max ( incl , excl ) ;
    return mp [ node ] ;
def getMaxSum ( node ) :
    if ( node == None ) :
        return 0 ;
    mp = dict ( )
    return getMaxSumUtil ( node , mp ) ;
if __name__ == " _ _ main _ _ " :
    root = newNode ( 1 ) ;
    root . left = newNode ( 2 ) ;
    root . right = newNode ( 3 ) ;
    root . right . left = newNode ( 4 ) ;
    root . right . right = newNode ( 5 ) ;
    root . left . left = newNode ( 1 ) ;
    print ( getMaxSum ( root ) )

Index/key variables: node
Class/struct names: Node
Python program:
def solve ( A , n ) :
    dp = [ [ 0 for i in range ( 2000 ) ] for i in range ( 2000 ) ]
    flag = 1
    sum = 0
    for i in range ( n ) :
        sum += A [ i ]
    for i in range ( - sum , sum + 1 ) :
        dp [ 0 ] [ i ] = 10 ** 9
    dp [ 0 ] [ 0 ] = 0
    for i in range ( 1 , n + 1 ) :
        for j in range ( - sum , sum + 1 ) :
            dp [ flag ] [ j ] = 10 ** 9
            if ( j - A [ i - 1 ] <= sum and j - A [ i - 1 ] >= - sum ) :
                dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ]
            if ( j + A [ i - 1 ] <= sum and j + A [ i - 1 ] >= - sum and dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != 10 ** 9 ) :
                dp [ flag ] [ j ] = min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 )
        flag = flag ^ 1
    for i in range ( sum + 1 ) :
        if ( dp [ flag ^ 1 ] [ i ] != 10 ** 9 ) :
            return dp [ flag ^ 1 ] [ i ]
    return n - 1
arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ]
n = len ( arr )
print ( solve ( arr , n ) )

Index/key variables: flag, i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def countPairsBruteForce ( X , Y , m , n ) :
    ans = 0
    for i in range ( m ) :
        for j in range ( n ) :
            if ( pow ( X [ i ] , Y [ j ] ) > pow ( Y [ j ] , X [ i ] ) ) :
                ans += 1
    return ans

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
import bisect
def count ( x , Y , n , NoOfY ) :
    if x == 0 :
        return 0
    if x == 1 :
        return NoOfY [ 0 ]
    idx = bisect . bisect_right ( Y , x )
    ans = n - idx
    ans += NoOfY [ 0 ] + NoOfY [ 1 ]
    if x == 2 :
        ans -= NoOfY [ 3 ] + NoOfY [ 4 ]
    if x == 3 :
        ans += NoOfY [ 2 ]
    return ans
def count_pairs ( X , Y , m , n ) :
    NoOfY = [ 0 ] * 5
    for i in range ( n ) :
        if Y [ i ] < 5 :
            NoOfY [ Y [ i ] ] += 1
    Y . sort ( )
    total_pairs = 0
    for x in X :
        total_pairs += count ( x , Y , n , NoOfY )
    return total_pairs
if __name__ == ' _ _ main _ _ ' :
    X = [ 2 , 1 , 6 ]
    Y = [ 1 , 5 ]
    print ( " Total ▁ pairs ▁ = ▁ " , count_pairs ( X , Y , len ( X ) , len ( Y ) ) )

Index/key variables: i
Accumulator variables: total_pairs
Iterator variables: i, x