dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
import sys
def maxSum ( arr , n , K ) :
    maximum = - sys . maxsize - 1
    for i in range ( n ) :
        sumk = 0
        for j in range ( i , n , K ) :
            sumk = sumk + arr [ j ]
        maximum = max ( maximum , sumk )
    return maximum
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 6 , 4 , 7 , 2 ]
    n = len ( arr )
    K = 2
    print ( maxSum ( arr , n , K ) )

Index/key variables: j
Iterator variables: i, j
Python program:
def countGreater ( arr , n , k ) :
    l = 0
    r = n - 1
    leftGreater = n
    while ( l <= r ) :
        m = int ( l + ( r - l ) / 2 )
        if ( arr [ m ] > k ) :
            leftGreater = m
            r = m - 1
        else :
            l = m + 1
    return ( n - leftGreater )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 ]
    n = len ( arr )
    k = 7
    print ( countGreater ( arr , n , k ) )

Index/key variables: m
Python program:
def operations ( op , n , k ) :
    i , count = 0 , 0
    nVal = 0
    minimum = 10 ** 9
    for i in range ( n ) :
        nVal += op [ i ]
        minimum = min ( minimum , nVal )
        if ( ( k + nVal ) <= 0 ) :
            return ( i + 1 )
    if ( nVal >= 0 ) :
        return - 1
    times = ( k - abs ( minimum ) ) // abs ( nVal )
    k = ( k - ( times * abs ( nVal ) ) )
    count = ( times * n )
    while ( k > 0 ) :
        for i in range ( n ) :
            k = k + op [ i ]
            count += 1
            if ( k <= 0 ) :
                break
    return count
op = [ - 60 , 65 , - 1 , 14 , - 25 ]
n = len ( op )
k = 100000
print ( operations ( op , n , k ) )

Index/key variables: i
Accumulator variables: count, nVal
Iterator variables: i
Python program:
MAX_SIZE = 1000
lookup_table = [ 0 ] * MAX_SIZE
def create_table ( n ) :
    pow = 1
    co = 0
    while True :
        pow <<= 1
        lookup_table [ co ] = ( n + ( pow >> 1 ) ) // pow
        if lookup_table [ co ] == 0 :
            break
        co += 1
def binary ( arr , v ) :
    index = lookup_table [ 0 ] - 1
    co = 0
    while lookup_table [ co ] != 0 :
        if v == arr [ index ] :
            return index
        elif v < arr [ index ] :
            co += 1
            index -= lookup_table [ co ]
        else :
            co += 1
            index += lookup_table [ co ]
arr = [ 1 , 3 , 5 , 6 , 7 , 8 , 9 ]
n = len ( arr )
create_table ( n )
print ( " Position ▁ of ▁ 3 ▁ in ▁ array ▁ = ▁ " , binary ( arr , 3 ) )

Index/key variables: co, index
Accumulator variables: pow
Python program:
def findNumber ( a , n , K ) :
    for i in range ( 0 , n , 1 ) :
        if ( K >= a [ i ] [ 0 ] and K <= a [ i ] [ 1 ] ) :
            return i
    return - 1
if __name__ == ' _ _ main _ _ ' :
    a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ]
    n = len ( a )
    k = 6
    index = findNumber ( a , n , k )
    if ( index != - 1 ) :
        print ( index , end = " " )
    else :
        print ( - 1 , end = " " )

Index/key variables: i
Iterator variables: i
Python program:
def findNumber ( a , n , K ) :
    low = 0
    high = n - 1
    while ( low <= high ) :
        mid = ( low + high ) >> 1
        if ( K >= a [ mid ] [ 0 ] and K <= a [ mid ] [ 1 ] ) :
            return mid
        elif ( K < a [ mid ] [ 0 ] ) :
            high = mid - 1
        else :
            low = mid + 1
    return - 1
a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ]
n = len ( a )
k = 6
index = findNumber ( a , n , k )
if ( index != - 1 ) :
    print ( index )
else :
    print ( - 1 )

Index/key variables: mid
Python program:
def prefixSum ( p_arr , arr , n ) :
    p_arr [ 0 ] = arr [ 0 ]
    for i in range ( 1 , n ) :
        p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ]
def hashPrefixSum ( p_arr , n , q ) :
    for i in range ( n ) :
        q [ p_arr [ i ] ] = 1
def canDivide ( p_arr , n , q , l , r ) :
    Sum = 0
    if ( l == 0 ) :
        Sum = p_arr [ r ]
    else :
        Sum = p_arr [ r ] - p_arr [ l - 1 ]
    if ( Sum % 2 == 1 ) :
        print ( " No " )
        return
    beg = 0
    if ( l != 0 ) :
        beg = p_arr [ l - 1 ]
    if ( beg + Sum // 2 in q . keys ( ) ) :
        print ( " Yes " )
    else :
        print ( " No " )
arr = [ 1 , 1 , 2 , 3 ]
n = len ( arr )
p_arr = [ 0 for i in range ( n ) ]
prefixSum ( p_arr , arr , n )
q = dict ( )
hashPrefixSum ( p_arr , n , q )
canDivide ( p_arr , n , q , 0 , 1 )
canDivide ( p_arr , n , q , 1 , 3 )
canDivide ( p_arr , n , q , 1 , 2 )

Index/key variables: i, r
Iterator variables: i
Python program:
def findRing ( arr , x ) :
    if arr [ 0 ] [ 0 ] > x :
        return - 1
    l , r = 0 , ( n + 1 ) // 2 - 1
    if n % 2 == 1 and arr [ r ] [ r ] < x :
        return - 1
    if n % 2 == 0 and arr [ r + 1 ] [ r ] < x :
        return - 1
    while l < r :
        mid = ( l + r ) // 2
        if arr [ mid ] [ mid ] <= x :
            if ( mid == ( n + 1 ) // 2 - 1 or arr [ mid + 1 ] [ mid + 1 ] > x ) :
                return mid
            else :
                l = mid + 1
        else :
            r = mid - 1
    return r
def binarySearchRowInc ( arr , row , l , r , x ) :
    while l <= r :
        mid = ( l + r ) // 2
        if arr [ row ] [ mid ] == x :
            return mid
        elif arr [ row ] [ mid ] < x :
            l = mid + 1
        else :
            r = mid - 1
    return - 1
def binarySearchColumnInc ( arr , col , t , b , x ) :
    while t <= b :
        mid = ( t + b ) // 2
        if arr [ mid ] [ col ] == x :
            return mid
        elif arr [ mid ] [ col ] < x :
            t = mid + 1
        else :
            b = mid - 1
    return - 1
def binarySearchRowDec ( arr , row , l , r , x ) :
    while l <= r :
        mid = ( l + r ) // 2
        if arr [ row ] [ mid ] == x :
            return mid
        elif arr [ row ] [ mid ] < x :
            r = mid - 1
        else :
            l = mid + 1
    return - 1
def binarySearchColumnDec ( arr , col , t , b , x ) :
    while t <= b :
        mid = ( t + b ) // 2
        if arr [ mid ] [ col ] == x :
            return mid
        elif arr [ mid ] [ col ] < x :
            b = mid - 1
        else :
            t = mid + 1
    return - 1
def spiralBinary ( arr , x ) :
    f1 = findRing ( arr , x )
    r , c = None , None
    if f1 == - 1 :
        print ( " - 1" )
        return
    if n % 2 == 1 and f1 == ( n + 1 ) // 2 - 1 :
        print ( f1 , f1 )
        return
    if x < arr [ f1 ] [ n - f1 - 1 ] :
        c = binarySearchRowInc ( arr , f1 , f1 , n - f1 - 2 , x )
        r = f1
    elif x < arr [ n - f1 - 1 ] [ n - f1 - 1 ] :
        c = n - f1 - 1
        r = binarySearchColumnInc ( arr , n - f1 - 1 , f1 , n - f1 - 2 , x )
    elif x < arr [ n - f1 - 1 ] [ f1 ] :
        c = binarySearchRowDec ( arr , n - f1 - 1 , f1 + 1 , n - f1 - 1 , x )
        r = n - f1 - 1
    else :
        r = binarySearchColumnDec ( arr , f1 , f1 + 1 , n - f1 - 1 , x )
        c = f1
    if c == - 1 or r == - 1 :
        print ( " - 1" )
    else :
        print ( " { 0 } ▁ { 1 } " . format ( r , c ) )
if __name__ == " _ _ main _ _ " :
    n = 4
    arr = [ [ 1 , 2 , 3 , 4 ] , [ 12 , 13 , 14 , 5 ] , [ 11 , 16 , 15 , 6 ] , [ 10 , 9 , 8 , 7 ] ]
    spiralBinary ( arr , 7 )

Index/key variables: col, f1, mid, r, row
Python program:
def costToPanagram ( string , cost ) :
    n = len ( string )
    occurrences = [ 0 ] * 26
    for i in range ( n ) :
        occurrences [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1
    gain = 0
    for i in range ( 26 ) :
        if occurrences [ i ] == 0 :
            gain -= 2 * cost [ i ]
        elif occurrences [ i ] > 1 :
            gain += cost [ i ] * ( occurrences [ i ] - 1 )
    if gain >= 0 :
        return 0
    return gain * - 1
if __name__ == " _ _ main _ _ " :
    cost = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]
    string = " geeksforgeeks "
    print ( costToPanagram ( string , cost ) )

Index/key variables: i
Accumulator variables: gain
Iterator variables: i
Python program:
def strScore ( string , s , n ) :
    m = { }
    for i in range ( n ) :
        m [ string [ i ] ] = i + 1
    if s not in m . keys ( ) :
        return 0
    score = 0
    for i in range ( len ( s ) ) :
        score += ord ( s [ i ] ) - ord ( ' a ' ) + 1
    score = score * m [ s ]
    return score
if __name__ == " _ _ main _ _ " :
    string = [ " geeksforgeeks " , " algorithms " , " stack " ]
    s = " algorithms "
    n = len ( string )
    score = strScore ( string , s , n ) ;
    print ( score )

Index/key variables: i, s
Accumulator variables: score
Iterator variables: i
Python program:
def countSubArrays ( arr , n , K ) :
    count = 0 ;
    for i in range ( n ) :
        for j in range ( i , n ) :
            bitwise_or = 0
            for k in range ( i , j + 1 ) :
                bitwise_or = bitwise_or | arr [ k ]
            if ( bitwise_or >= K ) :
                count += 1
    return count
if __name__ == " _ _ main _ _ " :
    arr = [ 3 , 4 , 5 ]
    n = len ( arr )
    k = 6
    print ( countSubArrays ( arr , n , k ) )

Index/key variables: k
Accumulator variables: count
Iterator variables: i, j, k
Python program:
N = 100002
tree = [ 0 ] * ( 4 * N )
def build ( arr , node , start , end ) :
    if start == end :
        tree [ node ] = arr [ start ]
        return
    mid = ( start + end ) >> 1
    build ( arr , 2 * node , start , mid )
    build ( arr , 2 * node + 1 , mid + 1 , end )
    tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ]
def query ( node , start , end , l , r ) :
    if start > end or start > r or end < l :
        return 0
    if start >= l and end <= r :
        return tree [ node ]
    mid = ( start + end ) >> 1
    q1 = query ( 2 * node , start , mid , l , r )
    q2 = query ( 2 * node + 1 , mid + 1 , end , l , r )
    return q1 or q2
def countSubArrays ( arr , n , K ) :
    build ( arr , 1 , 0 , n - 1 )
    count = 0
    for i in range ( n ) :
        for j in range ( n ) :
            bitwise_or = query ( 1 , 0 , n - 1 , i , j )
            if bitwise_or >= K :
                count += 1
    return count
arr = [ 3 , 4 , 5 ]
n = len ( arr )
k = 6
print ( countSubArrays ( arr , n , k ) )

Index/key variables: node, start
Accumulator variables: count
Iterator variables: i, j
Python program:
N = 100002
tree = [ 0 for i in range ( 4 * N ) ] ;
def build ( arr , node , start , end ) :
    if ( start == end ) :
        tree [ node ] = arr [ start ] ;
        return ;
    mid = ( start + end ) >> 1 ;
    build ( arr , 2 * node , start , mid ) ;
    build ( arr , 2 * node + 1 , mid + 1 , end ) ;
    tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ;
def query ( node , start , end , l , r ) :
    if ( start > end or start > r or end < l ) :
        return 0 ;
    if ( start >= l and end <= r ) :
        return tree [ node ] ;
    mid = ( start + end ) >> 1 ;
    q1 = query ( 2 * node , start , mid , l , r ) ;
    q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ;
    return q1 | q2 ;
def countSubArrays ( arr , n , K ) :
    count = 0 ;
    for i in range ( n ) :
        low = i
        high = n - 1
        index = 1000000000
        while ( low <= high ) :
            mid = ( low + high ) >> 1 ;
            if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) :
                index = min ( index , mid ) ;
                high = mid - 1 ;
            else :
                low = mid + 1 ;
        if ( index != 1000000000 ) :
            count += n - index ;
    return count ;
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 4 , 5 ]
    n = len ( arr )
    build ( arr , 1 , 0 , n - 1 ) ;
    k = 6 ;
    print ( countSubArrays ( arr , n , k ) )

Index/key variables: node, start
Accumulator variables: count
Iterator variables: i
Python program:
def countPattern ( n , pat ) :
    pattern_int = 0
    power_two = 1
    all_ones = 0
    i = len ( pat ) - 1
    while ( i >= 0 ) :
        current_bit = ord ( pat [ i ] ) - ord ( '0' )
        pattern_int += ( power_two * current_bit )
        all_ones = all_ones + power_two
        power_two = power_two * 2
        i -= 1
    count = 0
    while ( n != 0 and n >= pattern_int ) :
        if ( ( n & all_ones ) == pattern_int ) :
            count += 1
        n = n >> 1
    return count
if __name__ == ' _ _ main _ _ ' :
    n = 500
    pat = "10"
    print ( countPattern ( n , pat ) )

Index/key variables: i
Accumulator variables: count, pattern_int
Python program:
from collections import defaultdict
def findManhattanEuclidPair ( arr , n ) :
    X = defaultdict ( lambda : 0 )
    Y = defaultdict ( lambda : 0 )
    XY = defaultdict ( lambda : 0 )
    for i in range ( 0 , n ) :
        xi = arr [ i ] [ 0 ]
        yi = arr [ i ] [ 1 ]
        X [ xi ] += 1
        Y [ yi ] += 1
        XY [ tuple ( arr [ i ] ) ] += 1
    xAns , yAns , xyAns = 0 , 0 , 0
    for xCoordinatePair in X :
        xFrequency = X [ xCoordinatePair ]
        sameXPairs = ( xFrequency * ( xFrequency - 1 ) ) // 2
        xAns += sameXPairs
    for yCoordinatePair in Y :
        yFrequency = Y [ yCoordinatePair ]
        sameYPairs = ( yFrequency * ( yFrequency - 1 ) ) // 2
        yAns += sameYPairs
    for XYPair in XY :
        xyFrequency = XY [ XYPair ]
        samePointPairs = ( xyFrequency * ( xyFrequency - 1 ) ) // 2
        xyAns += samePointPairs
    return ( xAns + yAns - xyAns )
if __name__ == " _ _ main _ _ " :
    arr = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 1 , 3 ] ]
    n = len ( arr )
    print ( findManhattanEuclidPair ( arr , n ) )

Index/key variables: XYPair, i, xCoordinatePair, xi, yCoordinatePair, yi
Accumulator variables: xAns, xyAns, yAns
Iterator variables: XYPair, i, xCoordinatePair, yCoordinatePair
Python program:
MAX = 1000001
MAX_sqrt = MAX ** ( 0.5 )
primeUpto = [ 0 ] * ( MAX )
def SieveOfEratosthenes ( ) :
    isPrime = [ 1 ] * ( MAX )
    isPrime [ 0 ] , isPrime [ 1 ] = 0 , 0
    for i in range ( 2 , int ( MAX_sqrt ) ) :
        if isPrime [ i ] == 1 :
            for j in range ( i * 2 , MAX , i ) :
                isPrime [ j ] = 0
    for i in range ( 1 , MAX ) :
        primeUpto [ i ] = primeUpto [ i - 1 ]
        if isPrime [ i ] == 1 :
            primeUpto [ i ] += 1
def countOfNumbers ( N , K ) :
    SieveOfEratosthenes ( )
    low , high , ans = 1 , N , 0
    while low <= high :
        mid = ( low + high ) >> 1
        if mid - primeUpto [ mid ] >= K :
            ans = mid
            high = mid - 1
        else :
            low = mid + 1
    return ( N - ans + 1 ) if ans else 0
if __name__ == " _ _ main _ _ " :
    N , K = 10 , 3
    print ( countOfNumbers ( N , K ) )

Index/key variables: i, j, mid
Iterator variables: i, j
Python program:
def minIndex ( arr , n ) :
    sum = 0
    for i in range ( 0 , n ) :
        sum += arr [ i ]
    for i in range ( 0 , n ) :
        if ( 2 * arr [ i ] == sum ) :
            return ( i + 1 )
    return - 1
arr = [ 1 , 3 , - 5 , 3 , 4 ] ;
n = len ( arr ) ;
print ( minIndex ( arr , n ) )

Index/key variables: i
Accumulator variables: sum
Iterator variables: i
Python program:
import bisect
def printPrevGreater ( arr , n ) :
    if n == 1 :
        print ( " - 1" )
        return
    v = list ( arr )
    v . sort ( )
    for i in range ( n ) :
        it = bisect . bisect_right ( v , arr [ i ] )
        if ( it - 1 ) != 0 and v [ it - 2 ] == arr [ i ] :
            print ( arr [ i ] , end = " ▁ " )
        elif it <= n - 1 :
            print ( v [ it ] , end = " ▁ " )
        else :
            print ( - 1 , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 10 , 5 , 11 , 10 , 20 , 12 ]
    n = len ( arr )
    printPrevGreater ( arr , n )

Index/key variables: i, it
Iterator variables: i
Python program:
def printPrevGreater ( arr , n ) :
    v = arr . copy ( )
    v . sort ( )
    for i in range ( n ) :
        if ( arr [ i ] == v [ 0 ] ) :
            if ( arr [ i ] == v [ 1 ] ) :
                print ( arr [ i ] , end = " ▁ " )
            else :
                print ( - 1 , end = " ▁ " )
            continue
        if v . count ( arr [ i ] ) > 0 :
            it = v [ v . index ( arr [ i ] ) ]
        else :
            it = v [ n - 1 ]
        if ( it != v [ n - 1 ] and v [ v . index ( it ) + 1 ] == arr [ i ] ) :
            print ( arr [ i ] , end = " ▁ " )
        else :
            print ( v [ v . index ( it ) - 1 ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , 11 , 7 , 8 , 20 , 12 ]
    n = len ( arr )
    printPrevGreater ( arr , n )

Index/key variables: i
Iterator variables: i
Python program:
n = 5
def diagonalsMinMax ( mat ) :
    if ( n == 0 ) :
        return
    principalMin = mat [ 0 ] [ 0 ]
    principalMax = mat [ 0 ] [ 0 ]
    secondaryMin = mat [ n - 1 ] [ 0 ]
    secondaryMax = mat [ n - 1 ] [ 0 ]
    for i in range ( n ) :
        if ( mat [ i ] [ i ] < principalMin ) :
            principalMin = mat [ i ] [ i ]
        if ( mat [ i ] [ i ] > principalMax ) :
            principalMax = mat [ i ] [ i ]
        if ( mat [ n - 1 - i ] [ i ] < secondaryMin ) :
            secondaryMin = mat [ n - 1 - i ] [ i ]
        if ( mat [ n - 1 - i ] [ i ] > secondaryMax ) :
            secondaryMax = mat [ n - 1 - i ] [ i ]
    print ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , principalMin )
    print ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " , principalMax )
    print ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , secondaryMin )
    print ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " , secondaryMax )
matrix = [ [ 1 , 2 , 3 , 4 , - 10 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 1 , 2 , 11 , 3 , 4 ] , [ 5 , 6 , 70 , 5 , 8 ] , [ 4 , 9 , 7 , 1 , - 5 ] ]
diagonalsMinMax ( matrix )

Index/key variables: i
Iterator variables: i
Python program:
def checkSorted ( n , arr ) :
    first , second = 0 , 0
    count = 0
    for i in range ( 1 , n ) :
        if arr [ i ] < arr [ i - 1 ] :
            count += 1
            if first == 0 :
                first = i
            else :
                second = i
    if count > 2 :
        return False
    if count == 0 :
        return True
    if count == 2 :
        ( arr [ first - 1 ] , arr [ second ] ) = ( arr [ second ] , arr [ first - 1 ] )
    elif count == 1 :
        ( arr [ first - 1 ] , arr [ first ] ) = ( arr [ first ] , arr [ first - 1 ] )
    for i in range ( 1 , n ) :
        if arr [ i ] < arr [ i - 1 ] :
            return False
    return True
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 4 , 3 , 2 ]
    n = len ( arr )
    if checkSorted ( n , arr ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: first, i, second
Accumulator variables: count
Iterator variables: i
Python program:
N , M = 4 , 4
def printIntersection ( A , B ) :
    for i in range ( M ) :
        for j in range ( N ) :
            if ( A [ i ] [ j ] == B [ i ] [ j ] ) :
                print ( A [ i ] [ j ] , end = " ▁ " )
            else :
                print ( " * ▁ " , end = " ▁ " )
        print ( )
if __name__ == " _ _ main _ _ " :
    A = [ [ 2 , 4 , 6 , 8 ] , [ 1 , 3 , 5 , 7 ] , [ 8 , 6 , 4 , 2 ] , [ 7 , 5 , 3 , 1 ] ]
    B = [ [ 2 , 3 , 6 , 8 ] , [ 1 , 3 , 5 , 2 ] , [ 8 , 1 , 4 , 2 ] , [ 3 , 5 , 4 , 1 ] ]
    printIntersection ( A , B )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import math as mt
def countWays ( arr , n ) :
    max_val = 0
    for i in range ( n ) :
        max_val = max ( max_val , arr [ i ] )
    freq = [ 0 for i in range ( max_val + 1 ) ]
    for i in range ( n ) :
        freq [ arr [ i ] ] += 1
    ans += ( freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) // 6 )
    for i in range ( 1 , max_val + 1 ) :
        ans += ( freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) // 2 )
    for i in range ( 1 , ( max_val + 1 ) // 2 ) :
        ans += ( freq [ i ] * ( freq [ i ] - 1 ) // 2 * freq [ 2 * i ] )
    for i in range ( 1 , max_val + 1 ) :
        for j in range ( i + 1 , max_val - i + 1 ) :
            ans += freq [ i ] * freq [ j ] * freq [ i + j ]
    return ans
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len ( arr )
print ( countWays ( arr , n ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def countPairs ( A1 , A2 , n1 , n2 , K ) :
    res = 0
    m = dict ( )
    for i in range ( 0 , n1 ) :
        if A1 [ i ] not in m . keys ( ) :
            m [ A1 [ i ] ] = 1
        else :
            m [ A1 [ i ] ] = m [ A1 [ i ] ] + 1
    for i in range ( 0 , n2 ) :
        temp = K - A2 [ i ]
        if temp in m . keys ( ) :
            res = res + 1
            m [ temp ] = m [ temp ] - 1
    return res
A1 = [ 1 , 1 , 3 , 4 , 5 , 6 , 6 ]
A2 = [ 1 , 4 , 4 , 5 , 7 ]
K = 10
n1 = len ( A1 )
n2 = len ( A2 )
print ( countPairs ( A1 , A2 , n1 , n2 , K ) )

Index/key variables: i, temp
Iterator variables: i
Python program:
from collections import defaultdict
def countOrderedPairs ( A , n ) :
    orderedPairs = 0
    m = defaultdict ( lambda : 0 )
    for i in range ( 0 , n ) :
        m [ A [ i ] ] += 1
    for X , Y in m . items ( ) :
        for j in range ( 1 , Y + 1 ) :
            if m [ j ] >= X :
                orderedPairs += 1
    return orderedPairs
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 1 , 2 , 2 , 3 ]
    n = len ( A )
    print ( countOrderedPairs ( A , n ) )

Index/key variables: i, j
Accumulator variables: orderedPairs
Iterator variables: X, Y, i, j
Python program:
def LongestSubarray ( arr , n , k ) :
    count = 1
    max_lenght = 1
    prev_mod = arr [ 0 ] % k
    for i in range ( 1 , n ) :
        curr_mod = arr [ i ] % k
        if curr_mod == prev_mod :
            count += 1
        else :
            max_lenght = max ( max_lenght , count )
            count = 1
            prev_mod = curr_mod
    return max ( max_lenght , count )
arr = [ 4 , 9 , 7 , 18 , 29 , 11 ]
n = len ( arr )
k = 11
print ( LongestSubarray ( arr , n , k ) )

Index/key variables: i
Accumulator variables: count
Iterator variables: i
Python program:
M = 3
N = 4
def binarySearch1D ( arr , K ) :
    low = 0
    high = N - 1
    while ( low <= high ) :
        mid = low + int ( ( high - low ) / 2 )
        if ( arr [ mid ] == K ) :
            return True
        if ( arr [ mid ] < K ) :
            low = mid + 1
        else :
            high = mid - 1
    return False
def searchMatrix ( matrix , K ) :
    low = 0
    high = M - 1
    while ( low <= high ) :
        mid = low + int ( ( high - low ) / 2 )
        if ( K >= matrix [ mid ] [ 0 ] and K <= matrix [ mid ] [ N - 1 ] ) :
            return binarySearch1D ( matrix [ mid ] , K )
        if ( K < matrix [ mid ] [ 0 ] ) :
            high = mid - 1
        else :
            low = mid + 1
    return False
if __name__ == ' _ _ main _ _ ' :
    matrix = [ [ 1 , 3 , 5 , 7 ] , [ 10 , 11 , 16 , 20 ] , [ 23 , 30 , 34 , 50 ] ]
    K = 3
    if ( searchMatrix ( matrix , K ) ) :
        print ( " Found " )
    else :
        print ( " Not ▁ found " )

Index/key variables: mid
Python program:
def replacePi ( input ) :
    output = " " ;
    size = len ( input ) ;
    for i in range ( size ) :
        if ( i + 1 < size and input [ i ] == ' p ' and input [ i + 1 ] == ' i ' ) :
            output += "3.14" ;
            i += 1 ;
        else :
            output += input [ i ] ;
    return output ;
input = "2 ▁ * ▁ pi ▁ + ▁ 3 ▁ * ▁ pi ▁ = ▁ 5 ▁ * ▁ pi " ;
print ( replacePi ( input ) ) ;

Index/key variables: i
Accumulator variables: output
Iterator variables: i
Python program:
def numberOfElements ( height , n ) :
    max_so_far = 0
    coun = 0
    for i in range ( n - 1 , - 1 , - 1 ) :
        if height [ i ] > max_so_far :
            max_so_far = height [ i ]
            coun = coun + 1
    return coun
if __name__ == ' _ _ main _ _ ' :
    n = 6
    height = [ 4 , 8 , 2 , 0 , 0 , 5 ]
    print ( numberOfElements ( height , n ) )

Index/key variables: i
Iterator variables: i