dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def longestSequence ( arr , N ) :
    if ( N == 0 ) :
        print ( 0 ) ;
        return ;
    arr . sort ( ) ;
    maxLen = 1 ;
    len = 1 ;
    for i in range ( 1 , N ) :
        if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i - 1 ] + 1 ) :
            len += 1 ;
            maxLen = max ( maxLen , len ) ;
        else :
            len = 1 ;
    print ( maxLen ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 8 , 5 , 4 , 8 , 4 ] ;
    N = len ( arr ) ;
    longestSequence ( arr , N ) ;

Index/key variables: i
Accumulator variables: len
Iterator variables: i
Python program:
def minSum ( arr , N , Q , M ) :
    for i in range ( M ) :
        val = Q [ i ]
        front , rear = 0 , 0
        for j in range ( N ) :
            front += arr [ j ]
            if ( arr [ j ] == val ) :
                break
        for j in range ( N - 1 , - 1 , - 1 ) :
            rear += arr [ j ]
            if ( arr [ j ] == val ) :
                break
        print ( min ( front , rear ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ]
    N = len ( arr )
    Q = [ 7 , 6 ]
    M = len ( Q )
    minSum ( arr , N , Q , M )

Index/key variables: i, j
Accumulator variables: front, rear
Iterator variables: i, j
Python program:
def KthCharacter ( S , N , K ) :
    strarray = [ char for char in S ] ;
    strarray . sort ( ) ;
    ch = strarray [ K - 1 ] ;
    count = 0 ;
    for c in strarray :
        if ( c == ch ) :
            count += 1 ;
    print ( count ) ;
if __name__ == ' _ _ main _ _ ' :
    S = " geeksforgeeks " ;
    N = len ( S ) ;
    K = 3 ;
    KthCharacter ( S , N , K ) ;

Accumulator variables: count
Iterator variables: c
Python program:
def findSubstring ( S ) :
    T = " "
    ans = " "
    l = 0
    T += S [ 0 ]
    for i in range ( 1 , len ( S ) ) :
        if ( abs ( ord ( S [ i ] ) - ord ( S [ i - 1 ] ) ) == 1 ) :
            l = len ( T )
            if ( l > len ( ans ) ) :
                ans = T
            T = " "
            T += S [ i ]
        else :
            T += S [ i ]
    l = len ( T )
    if ( l > len ( ans ) ) :
        ans = T
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    S = " aabdml "
    findSubstring ( S )

Index/key variables: i
Accumulator variables: T
Iterator variables: i
Python program:
def findMinLength ( arr , N ) :
    front = 0
    back = N - 1
    while ( front < back ) :
        x = arr [ front ]
        if arr [ front ] != arr [ back ] :
            break
        while ( arr [ front ] == x and front <= back ) :
            front += 1
        while ( arr [ back ] == x and front <= back ) :
            back -= 1
    print ( back - front + 1 )
arr = [ 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 ]
N = len ( arr )
findMinLength ( arr , N )

Index/key variables: back, front
Python program:
def prefixArr ( arr , prefix , N ) :
    for i in range ( N ) :
        if ( i == 0 ) :
            prefix [ i ] [ 0 ] = arr [ i ]
            prefix [ i ] [ 1 ] = arr [ i ]
        else :
            prefix [ i ] [ 0 ] = max ( prefix [ i - 1 ] [ 0 ] , arr [ i ] )
            prefix [ i ] [ 1 ] = min ( prefix [ i - 1 ] [ 1 ] , arr [ i ] )
    return prefix
def suffixArr ( arr , suffix , N ) :
    for i in range ( N - 1 , - 1 , - 1 ) :
        if ( i == N - 1 ) :
            suffix [ i ] [ 0 ] = arr [ i ]
            suffix [ i ] [ 1 ] = arr [ i ]
        else :
            suffix [ i ] [ 0 ] = max ( suffix [ i + 1 ] [ 0 ] , arr [ i ] )
            suffix [ i ] [ 1 ] = min ( suffix [ i + 1 ] [ 1 ] , arr [ i ] )
    return suffix
def maxAndmin ( prefix , suffix , N , L , R ) :
    maximum , minimum = 0 , 0
    if ( L == 0 and R == N - 1 ) :
        print ( " No ▁ maximum ▁ and ▁ minimum ▁ value " )
        return
    elif ( L == 0 ) :
        maximum = suffix [ R + 1 ] [ 0 ]
        minimum = suffix [ R + 1 ] [ 1 ]
    elif ( R == N - 1 ) :
        maximum = prefix [ L - 1 ] [ 0 ]
        minimum = prefix [ R - 1 ] [ 1 ]
    else :
        maximum = max ( prefix [ L - 1 ] [ 0 ] , suffix [ R + 1 ] [ 0 ] )
        minimum = min ( prefix [ L - 1 ] [ 1 ] , suffix [ R + 1 ] [ 1 ] )
    print ( maximum , minimum )
def MinMaxQueries ( a , queries ) :
    N = len ( arr )
    q = len ( queries )
    prefix = [ [ 0 for i in range ( 2 ) ] for i in range ( N ) ]
    suffix = [ [ 0 for i in range ( 2 ) ] for i in range ( N ) ]
    prefix = prefixArr ( arr , prefix , N )
    suffix = suffixArr ( arr , suffix , N )
    for i in range ( q ) :
        L = queries [ i ] [ 0 ]
        R = queries [ i ] [ 1 ]
        maxAndmin ( prefix , suffix , N , L , R )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 1 , 8 , 3 , 5 , 7 , 4 ]
    queries = [ [ 4 , 6 ] , [ 0 , 4 ] , [ 3 , 7 ] , [ 2 , 5 ] ]
    MinMaxQueries ( arr , queries )

Index/key variables: i
Iterator variables: i
Python program:
def finddigits ( s ) :
    num = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " ]
    arr = [ 0 ] * ( 10 )
    ans = " "
    n = len ( s )
    for i in range ( n ) :
        if ( s [ i ] == ' z ' ) :
            arr [ 0 ] += 1
        if ( s [ i ] == ' w ' ) :
            arr [ 2 ] += 1
        if ( s [ i ] == ' g ' ) :
            arr [ 8 ] += 1
        if ( s [ i ] == ' x ' ) :
            arr [ 6 ] += 1
        if ( s [ i ] == ' v ' ) :
            arr [ 5 ] += 1
        if ( s [ i ] == ' o ' ) :
            arr [ 1 ] += 1
        if ( s [ i ] == ' s ' ) :
            arr [ 7 ] += 1
        if ( s [ i ] == ' f ' ) :
            arr [ 4 ] += 1
        if ( s [ i ] == ' h ' ) :
            arr [ 3 ] += 1
        if ( s [ i ] == ' i ' ) :
            arr [ 9 ] += 1
    arr [ 7 ] -= arr [ 6 ]
    arr [ 5 ] -= arr [ 7 ]
    arr [ 4 ] -= arr [ 5 ]
    arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] )
    arr [ 3 ] -= arr [ 8 ]
    arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] )
    for i in range ( 10 ) :
        for j in range ( arr [ i ] ) :
            ans += chr ( ( i ) + ord ( '0' ) )
    return ans
if __name__ == ' _ _ main _ _ ' :
    s = " owoftnuoer "
    print ( finddigits ( s ) )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i, j
Python program:
def noOfValidKbers ( K , arr ) :
    st = { }
    while ( K != 0 ) :
        if ( K % 10 in st ) :
            st [ K % 10 ] = 1
        else :
            st [ K % 10 ] = st . get ( K % 10 , 0 ) + 1
        K = K // 10
    count = 0
    for i in range ( len ( arr ) ) :
        no = arr [ i ]
        flag = True
        while ( no != 0 ) :
            digit = no % 10
            if ( digit not in st ) :
                flag = False
                break
            no = no // 10
        if ( flag == True ) :
            count += 1
    return count
if __name__ == ' _ _ main _ _ ' :
    K = 12
    arr = [ 1 , 12 , 1222 , 13 , 2 ]
    print ( noOfValidKbers ( K , arr ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Boolean variables: flag
Python program:
maxN = 100
Tree = [ 0 for i in range ( 4 * maxN ) ]
def build ( arr , index , s , e ) :
    global Tree
    global max
    if ( s == e ) :
        Tree [ index ] = arr [ s ]
    else :
        m = ( s + e ) // 2
        build ( arr , 2 * index , s , m )
        build ( arr , 2 * index + 1 , m + 1 , e )
        Tree [ index ] = max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] )
def atleast_x ( index , s , e , ql , qr , x ) :
    global Tree
    global max
    if ( ql > e or qr < s ) :
        return - 1
    if ( s <= ql and e <= qr ) :
        if ( Tree [ index ] < x ) :
            return - 1 ;
        while ( s != e ) :
            m = ( s + e ) // 2
            if ( Tree [ 2 * index ] >= x ) :
                e = m
                index = 2 * index
            else :
                s = m + 1
                index = 2 * index + 1
        return s
    m = ( s + e ) // 2
    val = atleast_x ( 2 * index , s , m , ql , qr , x )
    if ( val != - 1 ) :
        return val
    return atleast_x ( 2 * index + 1 , m + 1 , e , ql , qr , x )
def update ( index , s , e , new_val , pos ) :
    global Tree
    global max
    if ( s == e ) :
        Tree [ index ] = new_val
    else :
        m = ( s + e ) // 2
        if ( pos <= m ) :
            update ( 2 * index , s , m , new_val , pos )
        else :
            update ( 2 * index + 1 , m + 1 , e , new_val , pos )
        Tree [ index ] = max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] )
def printAnswer ( arr , n ) :
    global Tree
    global max
    build ( arr , 1 , 0 , n - 1 )
    print ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) )
    arr [ 2 ] = 5
    update ( 1 , 0 , n - 1 , 5 , 2 )
    print ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) )
    print ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 2 , 4 , 6 ]
    N = len ( arr )
    printAnswer ( arr , N )

Index/key variables: index, s
Python program:
class TreeNode :
    def __init__ ( self , x ) :
        self . val = x
        self . left = None
        self . right = None
def find_ht ( root ) :
    if ( not root ) :
        return 0
    if ( root . left == None and root . right == None ) :
        return 1
    return max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1
def find_node ( root ) :
    global req_node
    if ( not root ) :
        return
    left_ht = find_ht ( root . left )
    right_ht = find_ht ( root . right )
    if ( left_ht > right_ht ) :
        find_node ( root . left )
    elif ( right_ht > left_ht ) :
        find_node ( root . right )
    else :
        req_node = root
        return
if __name__ == ' _ _ main _ _ ' :
    root = TreeNode ( 1 )
    root . left = TreeNode ( 2 )
    root . right = TreeNode ( 3 )
    req_node = None
    find_node ( root )
    print ( req_node . val )

Class/struct names: TreeNode
Python program:
def maxEvenIntegers ( arr , N , K ) :
    ans = 0
    for i in range ( N - K + 1 ) :
        cnt = 0
        for j in range ( 0 , K ) :
            if arr [ i + j ] % 2 == 0 :
                cnt += 1
        ans = max ( cnt , ans )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 5 , 4 , 7 , 6 ]
    K = 3
    N = len ( arr )
    print ( maxEvenIntegers ( arr , N , K ) )

Accumulator variables: cnt
Iterator variables: i, j
Python program:
def countDiv ( arr ) :
    rem0 = 0
    rem1 = 0
    rem2 = 0
    for i in arr :
        digitSum = 0
        for digit in str ( i ) :
            digitSum += int ( digit )
        if digitSum % 3 == 0 :
            rem0 += 1
        elif digitSum % 3 == 1 :
            rem1 += 1
        else :
            rem2 += 1
    return ( rem0 // 2 + min ( rem1 , rem2 ) )
arr = [ 5 , 3 , 2 , 8 , 7 ]
print ( countDiv ( arr ) )

Accumulator variables: digitSum, rem0, rem1, rem2
Iterator variables: digit, i
Python program:
def MaxNonEmpSubSeq ( a , n ) :
    sum = 0
    maxm = max ( a )
    if ( maxm <= 0 ) :
        return maxm
    for i in range ( n ) :
        if ( a [ i ] > 0 ) :
            sum += a [ i ]
    return sum
if __name__ == ' _ _ main _ _ ' :
    arr = [ - 2 , 11 , - 4 , 2 , - 3 , - 10 ]
    N = len ( arr )
    print ( MaxNonEmpSubSeq ( arr , N ) )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
def splitString ( S , N ) :
    c = S [ N - 1 ]
    f = 0
    for i in range ( N - 1 ) :
        if ( S [ i ] == c ) :
            f = 1
            break
    if ( f ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == ' _ _ main _ _ ' :
    S = " abcdab "
    N = len ( S )
    splitString ( S , N )

Index/key variables: i
Iterator variables: i
Python program:
import sys
INT_MIN = ( sys . maxsize - 1 )
def longestLastingBulb ( onTime , s ) :
    n = len ( onTime )
    maxDur = INT_MIN
    maxPos = INT_MIN
    currentDiff = 0
    for i in range ( n ) :
        if ( i == 0 ) :
            currentDiff = onTime [ i ]
            maxDur = currentDiff
            maxPos = i
        else :
            currentDiff = onTime [ i ] - onTime [ i - 1 ]
            if ( maxDur < currentDiff ) :
                maxDur = currentDiff
                maxPos = i
            else :
                if ( maxDur == currentDiff ) :
                    one = s [ i ]
                    two = s [ maxPos ]
                    if ( one > two ) :
                        maxDur = currentDiff
                        maxPos = i
    ans = s [ maxPos ]
    return ans
if __name__ == " _ _ main _ _ " :
    S = " spuda "
    arr = [ 12 , 23 , 36 , 46 , 62 ]
    print ( longestLastingBulb ( arr , S ) )

Index/key variables: i, maxPos
Iterator variables: i
Python program:
def findIntersectingRange ( tup , N , ranges ) :
    curr = 0
    currPos = 0
    for i in range ( N ) :
        x = ranges [ i ] [ 0 ]
        y = ranges [ i ] [ 1 ]
        tup . append ( [ [ x , y ] , i + 1 ] )
    tup = sorted ( tup )
    curr = tup [ 0 ] [ 0 ] [ 1 ]
    currPos = tup [ 0 ] [ 1 ]
    for i in range ( 1 , N ) :
        Q = tup [ i - 1 ] [ 0 ] [ 0 ]
        R = tup [ i ] [ 0 ] [ 0 ]
        if ( Q == R ) :
            if ( tup [ i - 1 ] [ 0 ] [ 1 ] < tup [ i ] [ 0 ] [ 1 ] ) :
                print ( tup [ i - 1 ] [ 1 ] , tup [ i ] [ 1 ] )
            else :
                print ( tup [ i ] [ 1 ] , tup [ i - 1 ] [ 1 ] )
            return
        T = tup [ i ] [ 0 ] [ 1 ]
        if ( T <= curr ) :
            print ( tup [ i ] [ 1 ] , currPos )
            return
        else :
            curr = T
            currPos = tup [ i ] [ 1 ]
    print ( " - 1 ▁ - 1" )
if __name__ == ' _ _ main _ _ ' :
    N = 5
    ranges = [ [ 1 , 5 ] , [ 2 , 10 ] , [ 3 , 10 ] , [ 2 , 2 ] , [ 2 , 15 ] ]
    findIntersectingRange ( [ ] , N , ranges )

Index/key variables: i
Accumulator variables: tup
Iterator variables: i
Python program:
def printNGE ( arr , n ) :
    s = [ ] ;
    nge = [ - 1 ] * n ;
    i = 0 ;
    while ( i < 2 * n ) :
        while ( len ( s ) != 0 and arr [ i % n ] > arr [ s [ - 1 ] ] ) :
            nge [ s [ - 1 ] ] = arr [ i % n ] ;
            s . pop ( ) ;
        s . append ( i % n ) ;
        i += 1 ;
    for i in range ( n ) :
        print ( nge [ i ] , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , - 2 , 5 , 8 ] ;
    N = len ( arr ) ;
    printNGE ( arr , N ) ;

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
N = 100005
from math import sqrt
def SieveOfEratosthenes ( prime , p_size ) :
    prime [ 0 ] = False
    prime [ 1 ] = False
    for p in range ( 2 , int ( sqrt ( p_size ) ) , 1 ) :
        if ( prime [ p ] ) :
            for i in range ( p * 2 , p_size + 1 , p ) :
                prime [ i ] = False
def digitProduct ( number ) :
    res = 1
    while ( number > 0 ) :
        res *= ( number % 10 )
        number //= 10
    return res
def DistinctCompositeDigitProduct ( arr , n ) :
    output = set ( )
    prime = [ True for i in range ( N + 1 ) ]
    SieveOfEratosthenes ( prime , N )
    for i in range ( n ) :
        ans = digitProduct ( arr [ i ] )
        if ( ans <= 1 ) :
            continue
        if ( prime [ ans ] == False ) :
            output . add ( ans )
    print ( len ( output ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 ]
    n = len ( arr )
    DistinctCompositeDigitProduct ( arr , n )

Index/key variables: ans, i, p
Accumulator variables: number, output, res
Iterator variables: i, p
Python program:
import math
def simpleSieve ( lmt , prime ) :
    Sieve = [ True ] * ( lmt + 1 )
    Sieve [ 0 ] = Sieve [ 1 ] = False
    for i in range ( 2 , lmt + 1 ) :
        if ( Sieve [ i ] == True ) :
            prime . append ( i )
            for j in range ( i * i , int ( math . sqrt ( lmt ) ) + 1 , i ) :
                Sieve [ j ] = false
    return prime
def SegmentedSieveFn ( low , high ) :
    lmt = int ( math . sqrt ( high ) ) + 1
    prime = [ ]
    prime = simpleSieve ( lmt , prime )
    n = high - low + 1
    segmentedSieve = [ True ] * ( n + 1 )
    for i in range ( 0 , len ( prime ) ) :
        lowLim = int ( low // prime [ i ] ) * prime [ i ]
        if ( lowLim < low ) :
            lowLim += prime [ i ]
            for j in range ( lowLim , high + 1 , prime [ i ] ) :
                if ( j != prime [ i ] ) :
                    segmentedSieve [ j - low ] = False
    return segmentedSieve
def countPairsWhoseSumPrimeL_R ( L , R ) :
    segmentedSieve = SegmentedSieveFn ( L , R )
    cntPairs = 0
    for i in range ( L , R + 1 ) :
        if ( segmentedSieve [ i - L ] == True ) :
            cntPairs += i / 2
    return cntPairs
if __name__ == ' _ _ main _ _ ' :
    L = 1
    R = 5
    print ( countPairsWhoseSumPrimeL_R ( L , R ) )

Index/key variables: i, j
Accumulator variables: cntPairs, lowLim, prime
Iterator variables: i, j
Python program:
from collections import defaultdict
def countSum ( a , n , sum ) :
    count = 0
    m = defaultdict ( int )
    for i in range ( n - 1 ) :
        for j in range ( i + 1 , n ) :
            temp = a [ i ] + a [ j ]
            if ( temp < sum ) :
                count += m [ sum - temp ]
        for j in range ( i ) :
            temp = a [ i ] + a [ j ]
            if ( temp < sum ) :
                m [ temp ] += 1
    return count
if __name__ == " _ _ main _ _ " :
    arr = [ 4 , 5 , 3 , 1 , 2 , 4 ]
    S = 13
    N = len ( arr )
    print ( countSum ( arr , N , S ) )

Index/key variables: i, j, temp
Accumulator variables: count
Iterator variables: i, j
Python program:
from collections import defaultdict
MOD = 1000000007
def modPower ( x , y ) :
    res = 1
    x = x % MOD
    if ( x == 0 ) :
        return 0
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % MOD
        y = y // 2
        x = ( x * x ) % MOD
    return res
def countPairs ( arr , N ) :
    pairCount = 0
    hash1 = defaultdict ( int )
    for i in range ( N ) :
        hash1 [ arr [ i ] ] += 1
    for i in range ( N ) :
        modularInverse = modPower ( arr [ i ] , MOD - 2 )
        pairCount += hash1 [ modularInverse ]
        if ( arr [ i ] == modularInverse ) :
            pairCount -= 1
    return pairCount // 2
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 236426 , 280311812 , 500000004 ]
    N = len ( arr )
    print ( countPairs ( arr , N ) )

Index/key variables: i, modularInverse
Accumulator variables: pairCount
Iterator variables: i
Python program:
def find_index ( arr , n , B ) :
    start = 0
    end = n - 1
    while start <= end :
        mid = ( start + end ) // 2
        if arr [ mid ] == K :
            return mid
        elif arr [ mid ] < K :
            start = mid + 1
        else :
            end = mid - 1
    return end + 1
arr = [ 1 , 3 , 5 , 6 ]
n = len ( arr )
K = 2
print ( find_index ( arr , n , K ) )

Index/key variables: mid
Python program:
def computePos ( arr , n , value ) :
    if ( value < arr [ 0 ] ) :
        return 0
    if ( value > arr [ n - 1 ] ) :
        return n - 1
    start = 0
    end = n - 1
    while ( start < end ) :
        mid = ( start + end + 1 ) // 2
        if ( arr [ mid ] >= value ) :
            end = mid - 1
        else :
            start = mid
    return start
def countShift ( arr , n , queries ) :
    for q in queries :
        index = q [ 0 ]
        update = q [ 1 ]
        newElement = arr [ index ] + update
        newIndex = computePos ( arr , n , newElement )
        print ( abs ( newIndex - index ) , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 5 , 6 ]
    N = len ( arr )
    queries = [ [ 0 , - 1 ] , [ 4 , - 11 ] ]
    countShift ( arr , N , queries )

Index/key variables: index, mid
Iterator variables: q
Python program:
import sys
ans = 0
class Node :
    def __init__ ( self , key ) :
        self . key = key
        self . left = None
        self . right = None
def countReqNodes ( root , minNodeVal ) :
    global ans
    if root == None :
        return
    if root . key <= minNodeVal :
        minNodeVal = root . key
        ans += 1
    countReqNodes ( root . left , minNodeVal )
    countReqNodes ( root . right , minNodeVal )
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 8 )
    root . left = Node ( 6 )
    root . right = Node ( 5 )
    root . left . left = Node ( 6 )
    root . left . right = Node ( 7 )
    root . right . left = Node ( 3 )
    root . right . right = Node ( 9 )
    minNodeVal = sys . maxsize
    countReqNodes ( root , minNodeVal )
    print ( ans )

Class/struct names: Node
Python program:
def isPalindrome ( st ) :
    i = 0
    j = len ( st ) - 1
    while ( i < j ) :
        if ( st [ i ] != st [ j ] ) :
            return False
        i += 1
        j -= 1
    return True
def formPalindrome ( a , b , n ) :
    aa = [ ' ▁ ' ] * ( n + 2 )
    bb = [ ' ▁ ' ] * ( n + 2 )
    for i in range ( 1 , n + 1 ) :
        aa [ i ] = a [ i - 1 ]
        bb [ i ] = b [ i - 1 ]
    ok = False
    for i in range ( n + 2 ) :
        la = " "
        ra = " "
        lb = " "
        rb = " "
        for j in range ( 1 , i ) :
            if ( aa [ j ] == ' ▁ ' ) :
                la += " "
            else :
                la += aa [ j ]
            if ( bb [ j ] == ' ▁ ' ) :
                lb += " "
            else :
                lb += bb [ j ]
        for j in range ( i , n + 2 ) :
            if ( aa [ j ] == ' ▁ ' ) :
                ra += " "
            else :
                ra += aa [ j ]
            if ( bb [ j ] == ' ▁ ' ) :
                rb += " "
            else :
                rb += bb [ j ]
        if ( isPalindrome ( str ( la ) + str ( rb ) ) or isPalindrome ( str ( lb ) + str ( ra ) ) ) :
            ok = True
            break
    if ( ok ) :
        print ( " Yes " )
    else :
        print ( " No " )
if __name__ == " _ _ main _ _ " :
    A = " bdea "
    B = " abbb "
    N = 4
    formPalindrome ( A , B , N )

Index/key variables: i, j
Accumulator variables: la, lb, ra, rb
Iterator variables: i, j
Boolean variables: ok
Python program:
def minElements ( A , B , N , M ) :
    map = { }
    for i in range ( len ( A ) ) :
        map [ A [ i ] ] = i
    subseq = [ ]
    l = 0
    r = - 1
    for i in range ( M ) :
        if B [ i ] in map :
            e = map [ B [ i ] ]
            while ( l <= r ) :
                m = l + ( r - l ) // 2
                if ( subseq [ m ] < e ) :
                    l = m + 1
                else :
                    r = m - 1
            if ( r + 1 < len ( subseq ) ) :
                subseq [ r + 1 ] = e
            else :
                subseq . append ( e )
            l = 0
            r = len ( subseq ) - 1
    return N - len ( subseq )
if __name__ == ' _ _ main _ _ ' :
    A = [ 1 , 2 , 3 , 4 , 5 ]
    B = [ 2 , 5 , 6 , 4 , 9 , 12 ]
    M = len ( A )
    N = len ( B )
    print ( minElements ( A , B , M , N ) )

Index/key variables: i, m
Accumulator variables: subseq
Iterator variables: i
Python program:
import sys
def sfpa ( V , src , Edges , M ) :
    g = [ [ ] for i in range ( V ) ]
    for i in range ( M ) :
        u = Edges [ i ] [ 0 ]
        v = Edges [ i ] [ 1 ]
        w = Edges [ i ] [ 2 ]
        g [ u ] . append ( [ v , w ] )
    dist = [ sys . maxsize for i in range ( V ) ]
    inQueue = [ False for i in range ( V ) ]
    cnt = [ 0 for i in range ( V ) ]
    dist [ src ] = 0
    q = [ ]
    q . append ( src )
    inQueue [ src ] = True
    while ( len ( q ) ) :
        u = q [ 0 ]
        q . remove ( q [ 0 ] )
        inQueue [ u ] = False
        for x in g [ u ] :
            v = x [ 0 ]
            cost = x [ 1 ]
            if ( dist [ v ] > dist [ u ] + cost ) :
                dist [ v ] = dist [ u ] + cost
                if ( inQueue [ v ] == False ) :
                    q . append ( v )
                    inQueue [ v ] = True
                    cnt [ v ] += 1
                    if ( cnt [ v ] >= V ) :
                        return True
    return False
if __name__ == ' _ _ main _ _ ' :
    N = 4
    src = 0
    M = 4
    Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , - 1 ] , [ 2 , 3 , - 1 ] , [ 3 , 0 , - 1 ] ]
    if ( sfpa ( N , src , Edges , M ) == True ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, src, u, v
Accumulator variables: q
Iterator variables: i, x