dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
ways = [ 0 for i in range ( 1010 ) ] ;
def calculateWays ( arr1 , arr2 , N ) :
    A = arr2 [ 0 ] ; B = arr2 [ 1 ] ;
    C = arr2 [ 2 ] ; D = arr2 [ 3 ] ;
    ans = 0 ;
    for b in range ( 0 , B + 1 ) :
        if ( b * 5 > N ) :
            break ;
        for a in range ( 0 , A + 1 ) :
            if ( a + b * 5 > N ) :
                break ;
            ways [ a + b * 5 ] += 5 ;
    for c in range ( 0 , C ) :
        if ( c * 10 > N ) :
            break ;
        for d in range ( 0 , D ) :
            if ( c * 10 + d * 20 > N ) :
                break ;
            ans += ways [ N - c * 10 - d * 20 ] ;
    return ans ;
if __name__ == ' _ _ main _ _ ' :
    N = 123 ;
    arr1 = [ 1 , 5 , 10 , 20 ] ;
    arr2 = [ 6 , 4 , 3 , 5 ] ;
    print ( calculateWays ( arr1 , arr2 , N ) ) ;

Accumulator variables: ans
Iterator variables: a, b, c, d
Python program:
def maxXorSubsequence ( arr , n ) :
    odd = 0
    even = 0
    for i in range ( 0 , n ) :
        if arr [ i ] % 2 != 0 :
            odd += 1
        else :
            even += 1
    if odd == n :
        if odd % 2 == 0 :
            maxlen = n - 1
        else :
            maxlen = n
    elif even == n :
        maxlen = 0
    else :
        if odd % 2 == 0 :
            maxlen = even + odd - 1
        else :
            maxlen = even + odd
    return maxlen
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 4 , 5 , 6 , 7 ]
    n = len ( arr )
    print ( maxXorSubsequence ( arr , n ) )

Index/key variables: i
Accumulator variables: even, odd
Iterator variables: i
Python program:
def good_pair ( st , N ) :
    countStr = 0
    for i in range ( N ) :
        for j in range ( i + 1 , N ) :
            res = st [ i ] + st [ j ]
            vowel = [ 0 ] * 5
            for k in range ( len ( res ) ) :
                if ( res [ k ] == ' a ' ) :
                    vowel [ 0 ] = 1
                elif ( res [ k ] == ' e ' ) :
                    vowel [ 1 ] = 1
                elif ( res [ k ] == ' i ' ) :
                    vowel [ 2 ] = 1
                elif ( res [ k ] == ' o ' ) :
                    vowel [ 3 ] = 1
                elif ( res [ k ] == ' u ' ) :
                    vowel [ 4 ] = 1
            temp = 0
            for ind in range ( 5 ) :
                if ( vowel [ ind ] == 1 ) :
                    temp += 1
            if ( temp == 5 ) :
                countStr += 1
    return countStr
if __name__ == " _ _ main _ _ " :
    arr = [ " aaweiolkju " , " oxdfgujkmi " ]
    N = len ( arr )
    print ( good_pair ( arr , N ) )

Index/key variables: i, ind, j, k
Accumulator variables: countStr, temp
Iterator variables: i, ind, j, k
Python program:
def sieveOfEratosthenes ( N , s ) :
    prime = [ False ] * ( N + 1 )
    for i in range ( 2 , N + 1 , 2 ) :
        s [ i ] = 2
    for i in range ( 3 , N + 1 , 2 ) :
        if ( prime [ i ] == False ) :
            s [ i ] = i
            j = i
            while ( j * i <= N ) :
                if ( prime [ i * j ] == False ) :
                    prime [ i * j ] = True
                    s [ i * j ] = i
                j += 2
def generatePrimeFactors ( N ) :
    s = [ 0 ] * ( N + 1 )
    sum = 0
    sieveOfEratosthenes ( N , s )
    curr = s [ N ]
    cnt = 1
    while ( N > 1 ) :
        N //= s [ N ]
        if ( curr == s [ N ] ) :
            cnt += 1
            continue
        sum = sum + cnt
        curr = s [ N ]
        cnt = 1
    return sum
def findSum ( N ) :
    sum = 0
    for i in range ( 2 , N + 1 ) :
        sum += generatePrimeFactors ( i )
    print ( sum )
if __name__ == ' _ _ main _ _ ' :
    N = 4
    findSum ( N )

Index/key variables: N, i
Accumulator variables: cnt, j, sum
Iterator variables: i
Python program:
import sys
def LCM ( a , b ) :
    lcm = a if a > b else b
    while ( True ) :
        if ( lcm % a == 0 and lcm % b == 0 ) :
            break
        else :
            lcm += 1
    return lcm
def findMinLCM ( arr , n ) :
    minLCM = sys . maxsize
    for i in range ( n - 1 ) :
        val = LCM ( arr [ i ] , arr [ i + 1 ] )
        if ( val < minLCM ) :
            minLCM = val
    print ( minLCM )
arr = [ 4 , 8 , 12 , 16 , 20 , 24 ]
n = len ( arr )
findMinLCM ( arr , n )

Index/key variables: i
Accumulator variables: lcm
Iterator variables: i
Python program:
def find_max ( n , k ) :
    X = [ 0 ] * 32
    cnt = 0
    i = 31
    while ( i >= 0 and cnt != k ) :
        if ( ( n & ( 1 << i ) ) != 0 ) :
            X [ i ] = 1
            cnt += 1
        i -= 1
    s = " "
    for i in range ( 31 , - 1 , - 1 ) :
        if X [ i ] == 0 :
            s += '0'
        else :
            s += '1'
    return int ( s , 2 )
n , k = 10 , 2
print ( find_max ( n , k ) )

Index/key variables: i
Accumulator variables: cnt, s
Iterator variables: i
Python program:
def convert ( s ) :
    num = 0
    n = len ( s )
    for i in s :
        num = num * 10 + ( ord ( i ) - 48 )
    print ( num )
if __name__ == ' _ _ main _ _ ' :
    s = "123"
    convert ( s )

Iterator variables: i
Python program:
hell = 1000000007
N = 100005
c_0 = 0
c_1 = 0
c_2 = 0
def count_k ( arr , ones , twos , zeros ) :
    global c_0 , c_1 , c_2
    for i in range ( len ( arr ) ) :
        if ( arr [ i ] % 3 == 0 ) :
            c_0 += 1
            zeros . append ( arr [ i ] )
        elif ( arr [ i ] % 3 == 1 ) :
            c_1 += 1
            ones . append ( arr [ i ] )
        else :
            c_2 += 1
            twos . append ( arr [ i ] )
def printArrangement ( arr , ones , twos , zeros ) :
    global c_0 , c_1 , c_2
    if ( ( c_0 == 0 and c_1 != 0 and c_2 != 0 ) or c_0 > c_1 + c_2 + 1 ) :
        print ( " - 1" , end = " " )
        return
    if ( c_0 == 0 ) :
        for i in range ( len ( arr ) ) :
            print ( arr [ i ] , end = " ▁ " )
        return
    ans = [ - 1 ] * N
    i , j = 1 , 0
    while ( j < c_1 ) :
        ans [ i ] = ones [ - 1 ]
        ones . pop ( )
        i += 2
        j += 1
    ans [ i - 1 ] = zeros [ - 1 ]
    zeros . pop ( )
    c_0 -= 1
    j = 0
    while ( j < c_2 ) :
        ans [ i ] = twos [ - 1 ]
        twos . pop ( )
        j += 1
        i += 2
    k = 0
    while c_0 > 0 :
        if ( ans [ k ] == - 1 ) :
            ans [ k ] = zeros [ - 1 ]
            c_0 -= 1
        k += 2
    for i1 in range ( N ) :
        if ( ans [ i1 ] != - 1 ) :
            print ( ans [ i1 ] , end = " ▁ " )
    return
def solve ( n , arr ) :
    ones = [ ]
    zeros = [ ]
    twos = [ ]
    count_k ( arr , ones , twos , zeros )
    printArrangement ( arr , ones , twos , zeros )
n = 5
arr = [ 1 , 2 , 3 , 4 , 5 ]
solve ( n , arr )

Index/key variables: i, i1, k
Accumulator variables: c_0, c_1, c_2, j, ones, twos, zeros
Iterator variables: i, i1
Python program:
def countUniqueDigits ( N ) :
    res = 0
    cnt = [ 0 ] * 10
    while ( N > 0 ) :
        rem = N % 10
        cnt [ rem ] += 1
        N = N // 10
    for i in range ( 10 ) :
        if ( cnt [ i ] == 1 ) :
            res += 1
    return res
N = 2234262
print ( countUniqueDigits ( N ) )

Index/key variables: i, rem
Accumulator variables: res
Iterator variables: i
Python program:
from collections import defaultdict
def checkUniqueFrequency ( arr , n ) :
    freq = defaultdict ( int )
    for i in range ( n ) :
        freq [ arr [ i ] ] += 1
    uniqueFreq = set ( [ ] )
    for i in freq :
        if ( freq [ i ] in uniqueFreq ) :
            return False
        else :
            uniqueFreq . add ( freq [ i ] )
    return True
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 , 2 , 5 , 5 ]
    n = len ( arr )
    res = checkUniqueFrequency ( arr , n )
    if ( res ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: uniqueFreq
Iterator variables: i
Python program:
def Permute ( arr , n ) :
    odd = 0
    four = 0
    non_four = 0
    ODD , FOUR , NON_FOUR = [ ] , [ ] , [ ]
    for x in arr :
        if ( x & 1 ) :
            odd += 1
            ODD . append ( x )
        elif ( x % 4 == 0 ) :
            four += 1
            FOUR . append ( x )
        else :
            non_four += 1
            NON_FOUR . append ( x )
    if ( non_four == 0 and four >= odd - 1 ) :
        x = len ( ODD )
        y = len ( FOUR )
        i = 0
        while i < x :
            print ( ODD [ i ] , end = " ▁ " )
            if ( i < y ) :
                print ( FOUR [ i ] , end = " ▁ " )
        while ( i < y ) :
            print ( FOUR [ i ] , end = " ▁ " )
            i += 1
        print ( )
    elif ( non_four > 0 and four >= odd ) :
        x = len ( ODD )
        y = len ( FOUR )
        i = 0
        while i < x :
            print ( ODD [ i ] , end = " ▁ " )
            if ( i < y ) :
                print ( FOUR [ i ] , end = " ▁ " )
            i += 1
        while ( i < y ) :
            print ( FOUR [ i ] , end = " ▁ " )
            i += 1
        for j in NON_FOUR :
            print ( j , end = " ▁ " )
    else :
        print ( " Not ▁ Possible " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 7 , 1 , 8 , 2 , 8 ]
    N = len ( arr )
    Permute ( arr , N )

Index/key variables: i
Accumulator variables: FOUR, NON_FOUR, ODD, four, non_four, odd
Iterator variables: j, x
Python program:
def Kperiodicinteger ( X , N , K ) :
    X = list ( X )
    temp = X . copy ( )
    for i in range ( K ) :
        j = i
        while ( j < N ) :
            X [ j ] = X [ i ]
            j += K
    if ( X >= temp ) :
        return X
    POS = 0
    for i in range ( K - 1 , - 1 , - 1 ) :
        if ( X [ i ] != '9' ) :
            X [ i ] = str ( int ( X [ i ] ) + 1 )
            POS = i
            break
    for i in range ( POS + 1 , K ) :
        X [ i ] = '0'
    for i in range ( K ) :
        j = i
        while ( j < N ) :
            X [ j ] = X [ i ]
            j += K
    return X
N = 4
K = 2
X = "1215"
print ( * Kperiodicinteger ( X , N , K ) , sep = ' ' )

Index/key variables: i, j
Iterator variables: i
Python program:
N = 100000
canTake = 0
best = 0
dp = [ 0 for i in range ( N ) ]
vis = [ 0 for i in range ( N ) ]
def dfs ( g , cost , u , pre ) :
    global canTake , best
    vis [ u ] = True
    dp [ u ] = cost [ u ]
    check = 1
    cur = cost [ u ]
    for x in g [ u ] :
        if ( vis [ x ] and x != pre ) :
            check = 0
        elif ( not vis [ x ] ) :
            check &= dfs ( g , cost , x , u )
            cur = max ( cur , cost [ u ] + dp [ x ] )
    dp [ u ] = cur
    if ( not check ) :
        canTake += cost [ u ]
    else :
        best = max ( best , dp [ u ] )
    return check
def FindMaxCost ( g , cost , source ) :
    dfs ( g , cost , source , - 1 )
    print ( canTake + best )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    m = 5
    cost = [ 2 , 2 , 8 , 6 , 9 ]
    g = [ [ ] for i in range ( n ) ]
    g [ 0 ] . append ( 1 )
    g [ 1 ] . append ( 0 )
    g [ 0 ] . append ( 2 )
    g [ 2 ] . append ( 0 )
    g [ 0 ] . append ( 3 )
    g [ 3 ] . append ( 0 )
    g [ 1 ] . append ( 2 )
    g [ 2 ] . append ( 1 )
    g [ 1 ] . append ( 4 )
    g [ 4 ] . append ( 1 )
    source = 1
    FindMaxCost ( g , cost , source )

Index/key variables: u, x
Accumulator variables: check
Iterator variables: x
Python program:
def after_rev ( v ) :
    mini = 0
    count = 0
    for i in range ( len ( v ) ) :
        count += v [ i ]
        if ( count > 0 ) :
            count = 0
        if ( mini > count ) :
            mini = count
    return mini
def print_f ( arr ) :
    sum = 0
    for i in range ( 0 , len ( arr ) , 2 ) :
        sum += arr [ i ]
    v1 , v2 = [ ] , [ ]
    i = 1
    while i + 1 < len ( arr ) :
        v1 . append ( arr [ i + 1 ] - arr [ i ] )
        i += 2
    i = 1
    while i + 1 < len ( arr ) :
        v2 . append ( arr [ i ] - arr [ i + 1 ] )
        i += 2
    change = min ( after_rev ( v1 ) , after_rev ( v2 ) )
    if ( change < 0 ) :
        sum += change
    print ( sum )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 0 , 1 , 4 , 3 ]
    print_f ( arr )

Index/key variables: i
Accumulator variables: count, sum, v1, v2
Iterator variables: i
Python program:
import sys
from collections import defaultdict
def moduloEquality ( A , B , n , m ) :
    mapA = defaultdict ( int )
    mapB = defaultdict ( int )
    for i in range ( n ) :
        mapA [ A [ i ] ] += 1
        mapB [ B [ i ] ] += 1
    possibleValues = set ( )
    FirstElement = B [ 0 ]
    for i in range ( n ) :
        cur = A [ i ]
        if cur > FirstElement :
            possibleValues . add ( m - cur + FirstElement )
        else :
            possibleValues . add ( FirstElement - cur )
    ans = sys . maxsize
    for it in possibleValues :
        possible = True
        for it2 in mapA :
            if ( mapA [ it2 ] != mapB [ ( it2 + it ) % m ] ) :
                possible = False
                break
        if ( possible ) :
            ans = min ( ans , it )
    return ans
if __name__ == " _ _ main _ _ " :
    n = 4
    m = 3
    A = [ 0 , 0 , 2 , 1 ]
    B = [ 2 , 0 , 1 , 1 ]
    print ( moduloEquality ( A , B , n , m ) )

Index/key variables: i, it2
Accumulator variables: possibleValues
Iterator variables: i, it, it2
Boolean variables: possible
Python program:
def countCyclicShifts ( S , n ) :
    aux = [ 0 for i in range ( n ) ]
    for i in range ( 0 , n ) :
        if ( S [ i ] == ' ( ' ) :
            aux [ i ] = 1
        else :
            aux [ i ] = - 1
    mn = aux [ 0 ]
    for i in range ( 1 , n ) :
        aux [ i ] += aux [ i - 1 ]
        mn = min ( mn , aux [ i ] )
    if ( aux [ n - 1 ] != 0 ) :
        return 0
    count = 0
    for i in range ( 0 , n ) :
        if ( aux [ i ] == mn ) :
            count += 1
    return count
S = " ) ( ) ( "
N = len ( S )
print ( countCyclicShifts ( S , N ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
def maxProfit ( prices , n ) :
    profit = 0
    currentDay = n - 1
    while ( currentDay > 0 ) :
        day = currentDay - 1
        while ( day >= 0 and ( prices [ currentDay ] > prices [ day ] ) ) :
            profit += ( prices [ currentDay ] - prices [ day ] )
            day -= 1
        currentDay = day ;
    return profit ;
prices = [ 2 , 3 , 5 ]
N = len ( prices )
print ( maxProfit ( prices , N ) )

Index/key variables: currentDay, day
Accumulator variables: profit
Python program:
def isOddStringPalindrome ( str , n ) :
    oddStringSize = n // 2 ;
    lengthOdd = True if ( oddStringSize % 2 == 1 ) else False
    s = [ ]
    i = 1
    c = 0
    while ( i < n and c < oddStringSize // 2 ) :
        s . append ( str [ i ] )
        i += 2
        c += 1
    if ( lengthOdd ) :
        i = i + 2
    while ( i < n and len ( s ) > 0 ) :
        if ( s [ len ( s ) - 1 ] == str [ i ] ) :
            s . pop ( )
        else :
            break
        i = i + 2
    if ( len ( s ) == 0 ) :
        return True
    return False ;
if __name__ == " _ _ main _ _ " :
    N = 10
    s = " aeafacafae "
    if ( isOddStringPalindrome ( s , N ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: c, s
Python program:
def isOddStringPalindrome ( Str , n ) :
    left , right = 0 , 0
    if ( n % 2 == 0 ) :
        left = 1
        right = n - 1
    else :
        left = 1
        right = n - 2
    while ( left < n and right >= 0 and left < right ) :
        if ( Str [ left ] != Str [ right ] ) :
            return False
        left += 2
        right -= 2
    return True
if __name__ == ' _ _ main _ _ ' :
    n = 10
    Str = " aeafacafae "
    if ( isOddStringPalindrome ( Str , n ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: left, right
Python program:
import sys
def min_remove ( st ) :
    N = len ( st )
    prefix_a = [ 0 ] * ( N + 1 )
    prefix_b = [ 0 ] * ( N + 1 )
    prefix_c = [ 0 ] * ( N + 1 )
    prefix_a [ 0 ] = 0
    prefix_b [ 0 ] = 0
    prefix_c [ 0 ] = 0
    for i in range ( 1 , N + 1 ) :
        if ( st [ i - 1 ] == ' a ' ) :
            prefix_a [ i ] = ( prefix_a [ i - 1 ] + 1 )
        else :
            prefix_a [ i ] = prefix_a [ i - 1 ]
        if ( st [ i - 1 ] == ' b ' ) :
            prefix_b [ i ] = ( prefix_b [ i - 1 ] + 1 )
        else :
            prefix_b [ i ] = prefix_b [ i - 1 ]
        if ( st [ i - 1 ] == ' c ' ) :
            prefix_c [ i ] = ( prefix_c [ i - 1 ] + 1 )
        else :
            prefix_c [ i ] = prefix_c [ i - 1 ]
    maxi = - sys . maxsize - 1 ;
    for i in range ( N + 1 ) :
        for j in range ( i , N + 1 ) :
            maxi = max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) )
    print ( ( N - maxi ) )
if __name__ == " _ _ main _ _ " :
    st = " aaaabaaxccac "
    min_remove ( st )

Index/key variables: N, i, j
Iterator variables: i, j
Python program:
def createArray ( n , s ) :
    if ( 2 * n <= s ) :
        for i in range ( n - 1 ) :
            print ( 2 , end = " ▁ " )
            s -= 2
        print ( s )
        print ( 1 )
    else :
        print ( ' - 1' )
N = 1
S = 4
createArray ( N , S )

Accumulator variables: s
Iterator variables: i
Python program:
def maximum_partition ( strr ) :
    i = 0
    j = 0
    k = 0
    c = 0
    r = 0
    m = { }
    for i in range ( len ( strr ) - 1 , - 1 , - 1 ) :
        if ( strr [ i ] not in m ) :
            m [ strr [ i ] ] = i
    i = 0
    k = m [ strr [ i ] ]
    for i in range ( len ( strr ) ) :
        if ( i <= k ) :
            c = c + 1
            k = max ( k , m [ strr [ i ] ] )
        else :
            r = r + 1
            c = 1
            k = max ( k , m [ strr [ i ] ] )
    if ( c != 0 ) :
        r = r + 1
    return r
if __name__ == ' _ _ main _ _ ' :
    strr = " ababcbacadefegdehijhklij "
    print ( maximum_partition ( strr ) )

Index/key variables: i
Iterator variables: i
Python program:
def findAns ( mat , x , y , n , m ) :
    ans = 0
    for i in range ( n ) :
        for j in range ( m ) :
            if ( mat [ i ] [ j ] == 1 ) :
                ans += ( abs ( x - i ) + abs ( y - j ) )
    return ans
mat = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 1 ] ]
x = 0
y = 2
print ( findAns ( mat , x , y , len ( mat ) , len ( mat [ 0 ] ) ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def countReversebitonic ( arr , n ) :
    c = 0 ;
    for i in range ( n ) :
        for j in range ( i , n ) :
            temp = arr [ i ]
            f = 0 ;
            if ( j == i ) :
                c += 1 ;
                continue ;
            k = i + 1 ;
            while ( k <= j and temp > arr [ k ] ) :
                temp = arr [ k ] ;
                k += 1 ;
            if ( k > j ) :
                c += 1 ;
                f = 2 ;
            while ( k <= j and temp < arr [ k ] and f != 2 ) :
                temp = arr [ k ] ;
                k += 1 ;
            if ( k > j and f != 2 ) :
                c += 1 ;
                f = 0 ;
    print ( c )
arr = [ 2 , 3 , 1 , 4 ] ;
countReversebitonic ( arr , len ( arr ) ) ;

Index/key variables: i, k
Accumulator variables: c
Iterator variables: i, j
Python program:
import sys
def longestSequence ( n , arr ) :
    l = 0
    r = n - 1
    prev = - sys . maxsize - 1
    ans = 0
    while ( l <= r ) :
        if ( arr [ l ] > prev and arr [ r ] > prev ) :
            if ( arr [ l ] < arr [ r ] ) :
                ans += 1
                prev = arr [ l ]
                l += 1
            else :
                ans += 1
                prev = arr [ r ]
                r -= 1
        elif ( arr [ l ] > prev ) :
            ans += 1
            prev = arr [ l ]
            l += 1
        elif ( arr [ r ] > prev ) :
            ans += 1
            prev = arr [ r ]
            r -= 1
        else :
            break
    return ans
arr = [ 3 , 5 , 1 , 4 , 2 ]
n = len ( arr )
print ( longestSequence ( n , arr ) )

Index/key variables: l, r
Accumulator variables: ans
Python program:
def getSubsequence ( arr , n ) :
    req = [ ]
    req . append ( arr [ 0 ] )
    for i in range ( 1 , n - 1 ) :
        if ( arr [ i ] > arr [ i + 1 ] and arr [ i ] > arr [ i - 1 ] ) :
            req . append ( arr [ i ] )
        elif ( arr [ i ] < arr [ i + 1 ] and arr [ i ] < arr [ i - 1 ] ) :
            req . append ( arr [ i ] ) ;
    req . append ( arr [ n - 1 ] ) ;
    print ( len ( req ) )
    for x in req :
        print ( x , end = ' ▁ ' )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 5 , 3 , 6 , 7 , 4 ]
    n = len ( arr )
    getSubsequence ( arr , n )

Index/key variables: i
Accumulator variables: req
Iterator variables: i, x
Python program:
def CountLongestSubarrays ( arr , n , k ) :
    s = 0
    for i in range ( n ) :
        s += arr [ i ]
    if ( s % k ) :
        return 1
    else :
        ini = 0
        while ( ini < n and arr [ ini ] % k == 0 ) :
            ini += 1
        final = n - 1
        while ( final >= 0 and arr [ final ] % k == 0 ) :
            final -= 1
        sum , count = 0 , 0
        if ( ini == n ) :
            return - 1
        else :
            length = max ( n - 1 - ini , final )
        for i in range ( length ) :
            sum += arr [ i ]
        if ( sum % k != 0 ) :
            count += 1
        for i in range ( length , n ) :
            sum = sum + arr [ i ]
            sum = sum + arr [ i - length ]
            if ( sum % k != 0 ) :
                count += 1
        return count
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 2 , 2 , 3 ]
    n = len ( arr )
    k = 3
    print ( CountLongestSubarrays ( arr , n , k ) )

Index/key variables: final, i, ini
Accumulator variables: count, s, sum
Iterator variables: i
Python program:
MAXN = int ( 1e6 + 1 )
freq = [ 0 ] * MAXN
mul = [ 0 ] * MAXN
cnt = [ 0 ] * MAXN
def nC3 ( n ) :
    if ( n < 3 ) :
        return 0
    return ( n * ( n - 1 ) * ( n - 2 ) ) / 6
def count_triplet ( arr , N , K ) :
    for i in range ( N ) :
        freq [ arr [ i ] ] += 1
    for i in range ( 1 , 1000000 + 1 ) :
        for j in range ( i , 1000000 + 1 , i ) :
            mul [ i ] += freq [ j ]
        cnt [ i ] = nC3 ( mul [ i ] )
    for i in range ( 1000000 , 0 , - 1 ) :
        for j in range ( 2 * i , 1000000 + 1 , i ) :
            cnt [ i ] -= cnt [ j ]
    print ( " Number ▁ of ▁ triplets ▁ with ▁ GCD " " ▁ { 0 } ▁ are ▁ { 1 } " . format ( K , int ( cnt [ K ] ) ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 7 , 12 , 6 , 15 , 9 ]
    N = 6
    K = 3
    count_triplet ( arr , N , K )

Index/key variables: K, i, j
Iterator variables: i, j
Python program:
def isSubseqPossible ( arr , N , K ) :
    i = 0
    odd = 0
    even = 0
    for i in range ( N ) :
        if ( arr [ i ] % 2 == 1 ) :
            odd += 1
        else :
            even += 1
    if ( odd == 0 or ( even == 0 and K % 2 == 0 ) ) :
        return False
    return True
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 3 , 5 , 7 , 4 ]
    N = len ( arr )
    K = 3
    print ( " Yes " if isSubseqPossible ( arr , N , K ) else " No " )

Index/key variables: i
Accumulator variables: even, odd
Iterator variables: i
Python program:
def print_path ( N , jump , coin ) :
    if ( jump > coin or jump * ( N - 1 ) < coin ) :
        print ( " - 1" )
    else :
        pos = 1 ;
        while ( jump > 0 ) :
            tmp = min ( N - 1 , coin - ( jump - 1 ) ) ;
            if ( pos + tmp <= N ) :
                pos += tmp ;
            else :
                pos -= tmp ;
            print ( pos , end = " ▁ " )
            coin -= tmp ;
            jump -= 1 ;
N = 5
K = 4
M = 12
print_path ( N , K , M ) ;

Accumulator variables: coin, jump, pos