dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
class TreeNode :
    def __init__ ( self , val = ' ' , left = None , right = None ) :
        self . val = val
        self . left = left
        self . right = right
def evalComp ( arr ) :
    ans = 0
    getBit = 1
    for i in range ( 32 ) :
        S = 0
        NS = 0
        for j in arr :
            if getBit & j :
                S += 1
            else :
                NS += 1
        if S > NS :
            ans += 2 ** i
        getBit <<= 1
    print ( ans , end = " ▁ " )
def compressTree ( root ) :
    mp = { }
    def Trav ( root , hd ) :
        if not root :
            return
        if hd not in mp :
            mp [ hd ] = [ root . val ]
        else :
            mp [ hd ] . append ( root . val )
        Trav ( root . left , hd - 1 )
        Trav ( root . right , hd + 1 )
    Trav ( root , 0 )
    lower = min ( mp . keys ( ) )
    upper = max ( mp . keys ( ) )
    for i in range ( lower , upper + 1 ) :
        evalComp ( mp [ i ] )
if __name__ == ' _ _ main _ _ ' :
    root = TreeNode ( 5 )
    root . left = TreeNode ( 3 )
    root . right = TreeNode ( 2 )
    root . left . left = TreeNode ( 1 )
    root . left . right = TreeNode ( 4 )
    root . right . left = TreeNode ( 1 )
    root . right . right = TreeNode ( 2 )
    compressTree ( root )

Index/key variables: hd, i
Accumulator variables: NS, S, ans, getBit
Iterator variables: i, j
Class/struct names: TreeNode
Python program:
def Or_of_Ands_for_each_query ( arr , n , queries , q ) :
    bits = [ 0 for x in range ( 32 ) ]
    for i in range ( 0 , 32 ) :
        for j in range ( 0 , n ) :
            if ( ( 1 << i ) & arr [ j ] ) :
                bits [ i ] += 1
    for p in range ( 0 , q ) :
        for i in range ( 0 , 32 ) :
            if ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) :
                bits [ i ] -= 1
            if ( queries [ p ] [ 1 ] & ( 1 << i ) ) :
                bits [ i ] += 1
        arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ]
        ans = 0
        for i in range ( 0 , 32 ) :
            if ( bits [ i ] != 0 ) :
                ans |= ( 1 << i )
        print ( ans )
n = 3
q = 2
arr = [ 3 , 5 , 7 ]
queries = [ [ 1 , 2 ] , [ 2 , 1 ] ]
Or_of_Ands_for_each_query ( arr , n , queries , q )

Index/key variables: i, j, p
Accumulator variables: ans
Iterator variables: i, j, p
Python program:
def XorSum ( A , B , N , M ) :
    ans = - 1
    for i in range ( N ) :
        for j in range ( M ) :
            if ( ans == - 1 ) :
                ans = ( A [ i ] & B [ j ] )
            else :
                ans ^= ( A [ i ] & B [ j ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    A = [ 3 , 5 ]
    B = [ 2 , 3 ]
    N = len ( A )
    M = len ( B )
    print ( XorSum ( A , B , N , M ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def countbitdiff ( xo ) :
    count = 0
    while ( xo ) :
        if ( xo % 2 == 1 ) :
            count += 1
        xo = xo // 2
    return count
def shortestPath ( n , a , b ) :
    xorVal = a ^ b
    cnt = countbitdiff ( xorVal )
    if ( cnt % 2 == 0 ) :
        print ( cnt // 2 )
    else :
        print ( " - 1" )
if __name__ == ' _ _ main _ _ ' :
    n = 15
    a , b = 15 , 3
    shortestPath ( n , a , b )

Accumulator variables: count
Python program:
M = 3
N = 3
def findXOR ( X ) :
    ans = 0
    while ( X ) :
        ans ^= ( X % 10 )
        X //= 10
    return ans
def printXORmatrix ( arr ) :
    for i in range ( 3 ) :
        for j in range ( 3 ) :
            print ( arr [ i ] [ j ] , end = " ▁ " )
        print ( )
def convertXOR ( arr ) :
    for i in range ( 3 ) :
        for j in range ( 3 ) :
            X = arr [ i ] [ j ]
            temp = findXOR ( X )
            arr [ i ] [ j ] = temp
    printXORmatrix ( arr )
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ]
    convertXOR ( arr )

Index/key variables: i, j
Accumulator variables: X, ans
Iterator variables: i, j
Python program:
M = 1000000007
from bisect import bisect_left
def findSum ( A , N ) :
    ans = 0
    for i in range ( 30 ) :
        vec = [ ]
        for j in range ( N ) :
            if ( ( A [ j ] >> i ) & 1 ) :
                X = ( A [ j ] >> ( i + 1 ) )
                X = X * ( 1 << ( i + 1 ) )
                X %= M
                vec . append ( A [ j ] - X )
        vec . sort ( reverse = False )
        for j in range ( len ( vec ) ) :
            Y = ( 1 << ( i + 1 ) ) + ( 1 << i ) - vec [ j ]
            temp = vec [ j + 1 : ]
            idx = int ( bisect_left ( temp , Y ) )
            ans += ( ( len ( vec ) - idx ) * ( 1 << i ) )
            ans %= M
    ans /= 7
    print ( int ( ans % M ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 3 ]
    N = len ( arr )
    findSum ( arr , N )

Index/key variables: j
Accumulator variables: X, ans, vec
Iterator variables: i, j
Python program:
def countSet ( N ) :
    ans = 0
    while N :
        ans += N & 1
        N >>= 1
    return ans
def countSub ( arr , k ) :
    ans = 0
    setK = 0
    for i in arr :
        if countSet ( i ) == k :
            setK += 1
        else :
            setK = 0
        ans += setK
    return ans
arr = [ 4 , 2 , 1 , 5 , 6 ]
K = 2
print ( countSub ( arr , K ) )

Accumulator variables: N, ans, setK
Iterator variables: i
Python program:
def oddXorSubarray ( a , n ) :
    odd = 0
    c_odd = 0
    result = 0
    for i in range ( n ) :
        if ( a [ i ] & 1 ) :
            odd = not odd
        if ( odd ) :
            c_odd += 1
    for i in range ( n ) :
        result += c_odd
        if ( a [ i ] & 1 ) :
            c_odd = ( n - i - c_odd )
    print ( result )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 4 , 7 , 9 , 10 ]
    N = len ( arr )
    oddXorSubarray ( arr , N )

Index/key variables: i
Accumulator variables: c_odd, result
Iterator variables: i
Python program:
def Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) :
    frequency = [ 0 ] * 32
    for i in range ( N ) :
        bit_position = 0
        num = arr1 [ i ]
        while ( num ) :
            if ( num & 1 != 0 ) :
                frequency [ bit_position ] += 1
            bit_position += 1
            num >>= 1
    for i in range ( M ) :
        num = arr2 [ i ]
        value_at_that_bit = 1
        bitwise_OR_sum = 0
        for bit_position in range ( 32 ) :
            if ( num & 1 != 0 ) :
                bitwise_OR_sum += N * value_at_that_bit
            else :
                bitwise_OR_sum += ( frequency [ bit_position ] * value_at_that_bit )
            num >>= 1
            value_at_that_bit <<= 1
        print ( bitwise_OR_sum , end = " ▁ " )
    return
arr1 = [ 1 , 2 , 3 ]
arr2 = [ 1 , 2 , 3 ]
N = len ( arr1 )
M = len ( arr2 )
Bitwise_OR_sum_i ( arr1 , arr2 , M , N )

Index/key variables: bit_position, i
Accumulator variables: bitwise_OR_sum, num, value_at_that_bit
Iterator variables: bit_position, i
Python program:
def countPairs ( arr , k , size ) :
    count = 0
    for i in range ( size - 1 ) :
        for j in range ( i + 1 , size ) :
            x = arr [ i ] | arr [ j ]
            if ( x == k ) :
                count += 1
    print ( count )
arr = [ 2 , 38 , 44 , 29 , 62 ]
K = 46
N = len ( arr )
countPairs ( arr , K , N )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
adj = [ [ ] for i in range ( 100005 ) ]
visited = [ 0 ] * 100005
ans = 0
def dfs ( node , xorr , k ) :
    global ans
    visited [ node ] = 1
    if ( node != 1 and xorr == k ) :
        ans += 1
    for x in adj [ node ] :
        if ( not visited [ x [ 0 ] ] ) :
            xorr1 = xorr ^ x [ 1 ]
            dfs ( x [ 0 ] , xorr1 , k )
def countNodes ( N , K , R , edges ) :
    for i in range ( N - 1 ) :
        u = edges [ i ] [ 0 ]
        v = edges [ i ] [ 1 ]
        w = edges [ i ] [ 2 ]
        adj [ u ] . append ( [ v , w ] )
        adj [ v ] . append ( [ u , w ] )
    dfs ( R , 0 , K )
    print ( ans )
if __name__ == ' _ _ main _ _ ' :
    K = 0
    R = 1
    edges = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 1 ] , [ 2 , 4 , 3 ] , [ 2 , 5 , 4 ] , [ 3 , 6 , 1 ] , [ 3 , 7 , 2 ] ]
    N = len ( edges )
    countNodes ( N , K , R , edges )

Index/key variables: i, node, u, v
Iterator variables: i, x
Python program:
def findMinXORSubarray ( arr , n , k ) :
    if ( n < k ) :
        return ;
    res_index = 0 ;
    curr_xor = 0 ;
    for i in range ( k ) :
        curr_xor ^= arr [ i ] ;
    min_xor = curr_xor ;
    for i in range ( k , n ) :
        curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ;
        if ( curr_xor < min_xor ) :
            min_xor = curr_xor ;
            res_index = ( i - k + 1 ) ;
    print ( min_xor ) ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] ;
    k = 3 ;
    n = len ( arr ) ;
    findMinXORSubarray ( arr , n , k ) ;

Index/key variables: i
Accumulator variables: curr_xor
Iterator variables: i
Python program:
def findmax ( arr , n , k ) :
    trav , i = 0 , 0
    c = 0
    maximum = 0
    while i < n - k + 1 :
        trav = i - 1
        c = 0
        while trav >= 0 and arr [ trav ] == 1 :
            trav -= 1
            c += 1
        trav = i + k
        while ( trav < n and arr [ trav ] == 1 ) :
            trav += 1
            c += 1
        c += k
        if ( c > maximum ) :
            maximum = c
        i += 1
    return maximum
if __name__ == ' _ _ main _ _ ' :
    k = 3
    arr = [ 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ]
    n = len ( arr )
    ans = findmax ( arr , n , k )
    print ( ans )

Index/key variables: trav
Accumulator variables: c, i
Python program:
from collections import defaultdict
def powerOfTwo ( a , n ) :
    mp = defaultdict ( int )
    for i in range ( n ) :
        mp [ a [ i ] ] += 1
    count = 0
    for i in range ( n ) :
        f = False
        for j in range ( 31 ) :
            s = ( 1 << j ) - a [ i ]
            if ( s in mp and ( mp [ s ] > 1 or mp [ s ] == 1 and s != a [ i ] ) ) :
                f = True
        if ( f == False ) :
            count += 1
    return count
if __name__ == " _ _ main _ _ " :
    a = [ 6 , 2 , 11 ]
    n = len ( a )
    print ( powerOfTwo ( a , n ) )

Index/key variables: i, s
Accumulator variables: count
Iterator variables: i, j
Boolean variables: f
Python program:
import sys
def findArray ( n , a , b , c ) :
    arr = [ 0 ] * ( n + 1 )
    for bit in range ( 30 , - 1 , - 1 ) :
        set = a & ( 1 << bit )
        if ( set ) :
            for i in range ( n ) :
                arr [ i ] |= set
        else :
            if ( b & ( 1 << bit ) ) :
                arr [ 0 ] |= ( 1 << bit )
                if ( not ( c & ( 1 << bit ) ) ) :
                    arr [ 1 ] |= ( 1 << bit )
    aa = sys . maxsize
    bb = 0
    cc = 0
    for i in range ( n ) :
        aa &= arr [ i ]
        bb |= arr [ i ]
        cc ^= arr [ i ]
    if ( a == aa and b == bb and c == cc ) :
        for i in range ( n ) :
            print ( arr [ i ] , end = " ▁ " )
    else :
        print ( " - 1" )
if __name__ == " _ _ main _ _ " :
    n = 3
    a = 4
    b = 6
    c = 6
    findArray ( n , a , b , c )

Index/key variables: i
Accumulator variables: aa, bb, cc
Iterator variables: bit, i
Python program:
def maxAndXor ( arr , n ) :
    ans = float ( ' inf ' )
    arr . sort ( )
    for i in range ( n - 1 ) :
        ans = min ( ans , arr [ i ] ^ arr [ i + 1 ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 , 4 , 5 ]
    N = len ( arr )
    print ( maxAndXor ( arr , N ) )

Index/key variables: i
Iterator variables: i
Python program:
def countSubarray ( arr , K , N ) :
    if ( K % 2 != 0 ) :
        return 0
    if ( N < K ) :
        return 0
    start = 0
    i = 0
    count = 0
    currXor = arr [ i ]
    i += 1
    while ( i < K ) :
        currXor ^= arr [ i ]
        i += 1
    if ( currXor == 0 ) :
        count += 1
    currXor ^= arr [ start ]
    start += 1
    while ( i < N ) :
        currXor ^= arr [ i ]
        i += 1
        if ( currXor == 0 ) :
            count += 1
        currXor ^= arr [ start ]
        start += 1
    return count
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 4 , 4 , 2 , 2 , 4 ]
    K = 4
    N = len ( arr )
    print ( countSubarray ( arr , K , N ) )

Index/key variables: i, start
Accumulator variables: count, currXor
Python program:
even = 0
odd = 0
def keep_count ( arr , N ) :
    global even
    global odd
    for i in range ( N ) :
        count = 0
        while ( arr [ i ] != 0 ) :
            arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ]
            count += 1
        if ( count % 2 == 0 ) :
            even += 1
        else :
            odd += 1
    return
def solveQueries ( arr , n , q , m ) :
    global even
    global odd
    keep_count ( arr , n )
    for i in range ( m ) :
        X = q [ i ]
        count = 0
        while ( X != 0 ) :
            X = ( X - 1 ) & X
            count += 1
        if ( count % 2 == 0 ) :
            print ( even , odd )
        else :
            print ( odd , even )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 7 , 4 , 5 , 3 ]
    n = len ( arr )
    q = [ 3 , 4 , 12 , 6 ]
    m = len ( q )
    solveQueries ( arr , n , q , m )

Index/key variables: i
Accumulator variables: count, even, odd
Iterator variables: i
Python program:
def findLargestNumber ( arr ) :
    k = max ( arr ) ;
    m = [ 0 ] * ( k + 1 ) ;
    for n in arr :
        m [ n ] += 1 ;
    for n in range ( len ( arr ) - 1 , 0 , - 1 ) :
        if ( n == m [ n ] ) :
            return n ;
    return - 1 ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 5 , 2 , 4 , 5 ] ;
    print ( findLargestNumber ( arr ) ) ;

Index/key variables: n
Iterator variables: n
Python program:
def findLargestNumber ( arr , n ) :
    for i in range ( n ) :
        arr [ i ] &= 0xFFFF ;
        if ( arr [ i ] <= n ) :
            arr [ i ] += 0x10000 ;
    for i in range ( n - 1 , 0 , - 1 ) :
        if ( ( arr [ i ] >> 16 ) == i ) :
            return i + 1 ;
    return - 1 ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 5 , 5 , 2 , 4 , 5 ] ;
    n = len ( arr ) ;
    print ( findLargestNumber ( arr , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def CheckBits ( N , K ) :
    check = 0
    count = 0
    for i in range ( 31 , - 1 , - 1 ) :
        if ( ( 1 << i ) & N ) :
            if ( check == 0 ) :
                check = 1
            else :
                if ( count < K ) :
                    return False
            count = 0
        else :
            count += 1
    return True
if __name__ == " _ _ main _ _ " :
    N = 5
    K = 1
    if ( CheckBits ( N , K ) ) :
        print ( " YES " )
    else :
        print ( " NO " )

Accumulator variables: count
Iterator variables: i
Python program:
def minimumFlips ( X , Y , Z ) :
    res = 0
    while ( X > 0 or Y > 0 or Z > 0 ) :
        if ( ( ( X & 1 ) or ( Y & 1 ) ) and ( Z & 1 ) ) :
            X = X >> 1
            Y = Y >> 1
            Z = Z >> 1
            continue
        elif ( not ( X & 1 ) and not ( Y & 1 ) and ( Z & 1 ) ) :
            res += 1
        elif ( ( X & 1 ) or ( Y & 1 ) == 1 ) :
            if ( ( X & 1 ) and ( Y & 1 ) and not ( Z & 1 ) ) :
                res += 2
            elif ( ( ( X & 1 ) or ( Y & 1 ) ) and not ( Z & 1 ) ) :
                res += 1
        X = X >> 1
        Y = Y >> 1
        Z = Z >> 1
    return res
if __name__ == " _ _ main _ _ " :
    X = 5
    Y = 8
    Z = 6
    print ( minimumFlips ( X , Y , Z ) )

Accumulator variables: res
Python program:
def countSubseq ( arr , n ) :
    count = 0
    mp = { }
    for x in arr :
        if x in mp . keys ( ) :
            mp [ x ] += 1
        else :
            mp [ x ] = 1
    for i in mp . keys ( ) :
        if ( i == 0 ) :
            count += pow ( 2 , mp [ i ] ) - 1
        else :
            count += pow ( 2 , mp [ i ] - 1 )
    return count
arr = [ 2 , 2 , 2 , 5 , 6 ]
n = len ( arr )
print ( countSubseq ( arr , n ) )

Index/key variables: i, x
Accumulator variables: count
Iterator variables: i, x
Python program:
def calculate ( section , pos ) :
    if ( pos < 0 ) :
        return 0
    on_section = [ ]
    off_section = [ ]
    for el in section :
        if ( ( ( el >> pos ) & 1 ) == 0 ) :
            off_section . append ( el )
        else :
            on_section . append ( el )
    if ( len ( off_section ) == 0 ) :
        return calculate ( on_section , pos - 1 )
    if ( len ( on_section ) == 0 ) :
        return calculate ( off_section , pos - 1 )
    return min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos )
def minXorValue ( a , n ) :
    section = [ ]
    for i in range ( n ) :
        section . append ( a [ i ] ) ;
    return calculate ( section , 30 )
if __name__ == " _ _ main _ _ " :
    N = 4
    A = [ 3 , 2 , 5 , 6 ]
    print ( minXorValue ( A , N ) )

Index/key variables: i
Accumulator variables: off_section, on_section, section
Iterator variables: el, i
Python program:
def findNthNum ( N ) :
    bit_L = 1 ;
    last_num = 0 ;
    while ( bit_L * ( bit_L + 1 ) / 2 < N ) :
        last_num = last_num + bit_L ;
        bit_L += 1 ;
    bit_R = N - last_num - 1 ;
    print ( ( 1 << bit_L ) + ( 1 << bit_R ) ) ;
if __name__ == ' _ _ main _ _ ' :
    N = 13 ;
    findNthNum ( N ) ;

Accumulator variables: bit_L
Python program:
MAX = 1000005
def SieveOfEratosthenes ( prime ) :
    prime [ 1 ] = False ;
    prime [ 0 ] = False ;
    for p in range ( 2 , int ( MAX ** ( 1 / 2 ) ) ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * 2 , MAX , p ) :
                prime [ i ] = False ;
def prime_xor ( arr , n , k ) :
    prime = [ True ] * MAX ;
    SieveOfEratosthenes ( prime ) ;
    ans = 0 ;
    for i in range ( n ) :
        if ( prime [ arr [ i ] ] ) :
            if ( ( i + 1 ) % k == 0 ) :
                ans ^= arr [ i ] ;
    print ( ans ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 2 , 3 , 5 , 7 , 11 , 8 ] ;
    n = len ( arr ) ;
    K = 2 ;
    prime_xor ( arr , n , K ) ;

Index/key variables: i, p
Accumulator variables: ans
Iterator variables: i, p
Python program:
def xorOfArray ( arr , n ) :
    xor_arr = 0
    for i in range ( n ) :
        xor_arr = xor_arr ^ arr [ i ]
    return xor_arr
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 9 , 12 , 13 , 15 ]
    n = len ( arr )
    print ( xorOfArray ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def countEvenBit ( A , B , n , m ) :
    i , j , cntOdd = 0 , 0 , 0
    cntEven = 0
    for i in range ( n ) :
        x = bin ( A [ i ] ) [ 2 : ] . count ( '1' )
        if ( x & 1 ) :
            cntEven += 1
        else :
            cntOdd += 1
    CountB = [ 0 ] * m
    for i in range ( m ) :
        x = bin ( B [ i ] ) [ 2 : ] . count ( '1' )
        if ( x & 1 ) :
            CountB [ i ] = cntEven
        else :
            CountB [ i ] = cntOdd
    for i in range ( m ) :
        print ( CountB [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    A = [ 4 , 2 , 15 , 9 , 8 , 8 ]
    B = [ 3 , 4 , 22 ]
    countEvenBit ( A , B , 6 , 3 )

Index/key variables: i
Accumulator variables: cntEven, cntOdd
Iterator variables: i
Python program:
def bitwiseOrTillN ( n ) :
    result = 2 ;
    for i in range ( 4 , n + 1 , 2 ) :
        result = result | i
    return result
n = 10 ;
print ( bitwiseOrTillN ( n ) ) ;

Iterator variables: i
Python program:
def genSequence ( n , val ) :
    for i in range ( 1 << n ) :
        x = i ^ ( i >> 1 ) ^ val ;
        print ( x , end = " ▁ " ) ;
if __name__ == " _ _ main _ _ " :
    n = 3 ; k = 2 ;
    genSequence ( n , k ) ;

Iterator variables: i
Python program:
def winner ( moves ) :
    data = dict ( )
    data [ ' R ' ] = 0
    data [ ' P ' ] = 1
    data [ ' S ' ] = 2
    if ( moves [ 0 ] == moves [ 1 ] ) :
        return " Draw "
    if ( ( ( data [ moves [ 0 ] ] | 1 << ( 2 ) ) - ( data [ moves [ 1 ] ] | 0 << ( 2 ) ) ) % 3 ) :
        return " A "
    return " B "
def performQueries ( arr , n ) :
    for i in range ( n ) :
        print ( winner ( arr [ i ] ) )
arr = [ " RS " , " SR " , " SP " , " PP " ]
n = len ( arr )
performQueries ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
graph = [ [ [ 0 , 0 ] for i in range ( 18 ) ] for j in range ( 18 ) ]
dp = [ [ [ 0 , 0 ] for i in range ( 18 ) ] for j in range ( 1 << 15 ) ]
def minCost ( n : int , m : int , mask : int , prev : int , col : int ) -> int :
    global dp
    if mask == ( ( 1 << n ) - 1 ) :
        return 0
    if dp [ mask ] [ prev ] [ col == 1 ] != 0 :
        return dp [ mask ] [ prev ] [ col == 1 ]
    ans = int ( 1e9 )
    for i in range ( n ) :
        for j in range ( 2 ) :
            if graph [ prev ] [ i ] [ j ] and not ( mask & ( 1 << i ) ) and ( j != col ) :
                z = graph [ prev ] [ i ] [ j ] + minCost ( n , m , mask | ( 1 << i ) , i , j )
                ans = min ( z , ans )
    dp [ mask ] [ prev ] [ col == 1 ] = ans
    return dp [ mask ] [ prev ] [ col == 1 ]
def makeGraph ( vp : list , m : int ) :
    global graph
    for i in range ( m ) :
        a = vp [ i ] [ 0 ] [ 0 ] - 1
        b = vp [ i ] [ 0 ] [ 1 ] - 1
        cost = vp [ i ] [ 1 ] [ 0 ]
        color = vp [ i ] [ 1 ] [ 1 ]
        graph [ a ] [ b ] [ color == ' W ' ] = cost
        graph [ b ] [ a ] [ color == ' W ' ] = cost
def getCost ( n : int , m : int ) -> int :
    ans = int ( 1e9 )
    for i in range ( n ) :
        ans = min ( ans , minCost ( n , m , 1 << i , i , 2 ) )
    if ans != int ( 1e9 ) :
        return ans
    else :
        return - 1
if __name__ == " _ _ main _ _ " :
    n = 3
    m = 4
    vp = [ [ [ 1 , 2 ] , [ 2 , ' B ' ] ] , [ [ 1 , 2 ] , [ 3 , ' W ' ] ] , [ [ 2 , 3 ] , [ 4 , ' W ' ] ] , [ [ 2 , 3 ] , [ 5 , ' B ' ] ] ]
    makeGraph ( vp , m )
    print ( getCost ( n , m ) )

Index/key variables: a, b, i, j, mask, prev
Iterator variables: i, j
Python program:
def maxConsecutiveOnes ( x ) :
    count = 0 ;
    while ( x != 0 ) :
        x = ( x & ( x << 1 ) ) ;
        count += 1 ;
    return count ;
def maxOnes ( arr , n ) :
    ans = 0 ;
    for i in range ( n ) :
        currMax = maxConsecutiveOnes ( arr [ i ] ) ;
        ans = max ( ans , currMax ) ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 2 , 3 , 4 ] ;
    n = len ( arr ) ;
    print ( maxOnes ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
MAX = 64 ;
def maxOR ( L , R ) :
    if ( L == R ) :
        return L ;
    ans = 0 ;
    for i in range ( MAX - 1 , - 1 , - 1 ) :
        p = 1 << i ;
        if ( ( rbit == 1 ) and ( lbit == 0 ) ) :
            ans += ( p << 1 ) - 1 ;
            break ;
        if ( rbit == 1 ) :
            ans += p ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    L = 4 ; R = 5 ;
    print ( maxOR ( L , R ) ) ;

Accumulator variables: ans
Iterator variables: i
Python program:
def findLen ( arr , n , m ) :
    filter = [ ] ;
    for i in range ( n ) :
        if ( ( arr [ i ] & m ) == m ) :
            filter . append ( arr [ i ] ) ;
    if ( len ( filter ) == 0 ) :
        return 0 ;
    c_and = filter [ 0 ] ;
    for i in range ( 1 , len ( filter ) ) :
        c_and &= filter [ i ] ;
    if ( c_and == m ) :
        return len ( filter ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 7 , 3 , 3 , 1 , 3 ] ;
    n = len ( arr ) ;
    m = 3 ;
    print ( findLen ( arr , n , m ) ) ;

Index/key variables: i
Accumulator variables: c_and, filter
Iterator variables: i
Python program:
MAX = 32
def maximizeExpression ( a , b ) :
    result = a
    for bit in range ( MAX - 1 , - 1 , - 1 ) :
        bitOfD = 1 << bit
        x = b & bitOfD
        if ( x == bitOfD ) :
            y = result & bitOfD
            if ( y == 0 ) :
                result = result ^ bitOfD
    return result
a = 11
b = 14
print ( maximizeExpression ( a , b ) )

Iterator variables: bit