dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
k1 = 0
k2 = 0
flag = True
def traverseMatrixDiagonally ( m , i , j , row , col ) :
    global k1
    global k2
    global flag
    if ( i >= row or j >= col ) :
        if ( flag ) :
            a = k1
            k1 = k2
            k2 = a
            if ( flag ) :
                flag = False
            else :
                flag = True
            k1 += 1
        else :
            a = k1
            k1 = k2
            k2 = a
            if ( flag ) :
                flag = False
            else :
                flag = True
        print ( )
        return False
    print ( m [ i ] [ j ] , end = " ▁ " )
    if ( traverseMatrixDiagonally ( m , i + 1 , j + 1 , row , col ) ) :
        return True
    if ( traverseMatrixDiagonally ( m , k1 , k2 , row , col ) ) :
        return True
    return True
mtrx = [ [ 10 , 11 , 12 , 13 , 14 ] , [ 15 , 16 , 17 , 18 , 19 ] , [ 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 ] , [ 30 , 31 , 32 , 33 , 34 ] ]
traverseMatrixDiagonally ( mtrx , 0 , 0 , 5 , 5 )

Index/key variables: i, j
Boolean variables: flag
Python program:
n = 5
m = 7 ;
g = [ [ ] for i in range ( m ) ]
col = [ 0 for i in range ( n ) ] ;
cyc = True
res = [ 0 for i in range ( m ) ] ;
def dfs ( v ) :
    col [ v ] = 1 ;
    for p in g [ v ] :
        to = p [ 0 ]
        id = p [ 1 ] ;
        if ( col [ to ] == 0 ) :
            dfs ( to ) ;
            res [ id ] = 1 ;
        elif ( col [ to ] == 2 ) :
            res [ id ] = 1 ;
        else :
            res [ id ] = 2 ;
            cyc = True ;
    col [ v ] = 2 ;
if __name__ == ' _ _ main _ _ ' :
    g [ 0 ] . append ( [ 1 , 0 ] ) ;
    g [ 0 ] . append ( [ 2 , 1 ] ) ;
    g [ 1 ] . append ( [ 2 , 2 ] ) ;
    g [ 1 ] . append ( [ 3 , 3 ] ) ;
    g [ 2 ] . append ( [ 3 , 4 ] ) ;
    g [ 3 ] . append ( [ 4 , 5 ] ) ;
    g [ 4 ] . append ( [ 2 , 6 ] ) ;
    for i in range ( n ) :
        if ( col [ i ] == 0 ) :
            dfs ( i ) ;
    print ( 2 if cyc else 1 )
    for i in range ( m ) :
        print ( res [ i ] , end = ' ▁ ' )

Index/key variables: i, id, to, v
Iterator variables: i, p
Boolean variables: cyc
Python program:
def sumSubsets ( sets , n , target ) :
    x = [ 0 ] * len ( sets ) ;
    j = len ( sets ) - 1 ;
    while ( n > 0 ) :
        x [ j ] = n % 2 ;
        n = n // 2 ;
        j -= 1 ;
    sum = 0 ;
    for i in range ( len ( sets ) ) :
        if ( x [ i ] == 1 ) :
            sum += sets [ i ] ;
    if ( sum == target ) :
        print ( " { " , end = " " ) ;
        for i in range ( len ( sets ) ) :
            if ( x [ i ] == 1 ) :
                print ( sets [ i ] , end = " , ▁ " ) ;
        print ( " } , ▁ " , end = " " ) ;
def findSubsets ( arr , K ) :
    x = pow ( 2 , len ( arr ) ) ;
    for i in range ( 1 , x ) :
        sumSubsets ( arr , i , K ) ;
if __name__ == " _ _ main _ _ " :
    arr = [ 5 , 10 , 12 , 13 , 15 , 18 ] ;
    K = 30 ;
    findSubsets ( arr , K ) ;

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i
Python program:
maxN = 10
fact = [ 0 ] * maxN ;
def ncr ( n , r ) :
    return ( fact [ n ] // fact [ r ] ) // fact [ n - r ] ;
def findSum ( arr , n ) :
    fact [ 0 ] = 1 ;
    for i in range ( 1 , n ) :
        fact [ i ] = i * fact [ i - 1 ] ;
    mul = 0 ;
    for i in range ( n ) :
        mul += ( 2 ** i ) * ncr ( n - 1 , i ) ;
    ans = 0 ;
    for i in range ( n ) :
        ans += mul * arr [ i ] ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 1 ] ;
    n = len ( arr ) ;
    print ( findSum ( arr , n ) ) ;

Index/key variables: i, n, r
Accumulator variables: ans, mul
Iterator variables: i
Python program:
R = 4
C = 4
def countPaths ( maze ) :
    if ( maze [ 0 ] [ 0 ] == - 1 ) :
        return 0
    for i in range ( R ) :
        if ( maze [ i ] [ 0 ] == 0 ) :
            maze [ i ] [ 0 ] = 1
        else :
            break
    for i in range ( 1 , C ) :
        if ( maze [ 0 ] [ i ] == 0 ) :
            maze [ 0 ] [ i ] = 1
        else :
            break
    for i in range ( 1 , R ) :
        for j in range ( 1 , C ) :
            if ( maze [ i ] [ j ] == - 1 ) :
                continue
            if ( maze [ i - 1 ] [ j ] > 0 ) :
                maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] )
            if ( maze [ i ] [ j - 1 ] > 0 ) :
                maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] )
    if ( maze [ R - 1 ] [ C - 1 ] > 0 ) :
        return maze [ R - 1 ] [ C - 1 ]
    else :
        return 0
def numberOfPaths ( m , n ) :
    path = 1
    for i in range ( n , m + n - 1 ) :
        path *= i
        path //= ( i - n + 1 )
    return path
def solve ( maze ) :
    ans = ( numberOfPaths ( R , C ) - countPaths ( maze ) )
    return ans
maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
print ( solve ( maze ) )

Index/key variables: i, j
Accumulator variables: path
Iterator variables: i, j
Python program:
N = 4
def printSolution ( sol ) :
    for i in range ( N ) :
        for j in range ( N ) :
            print ( sol [ i ] [ j ] , end = " ▁ " )
        print ( )
def isSafe ( maze , x , y ) :
    if ( x >= 0 and x < N and y >= 0 and y < N and maze [ x ] [ y ] != 0 ) :
        return True
    return False
def solveMaze ( maze ) :
    sol = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
    if ( solveMazeUtil ( maze , 0 , 0 , sol ) == False ) :
        print ( " Solution ▁ doesn ' t ▁ exist " )
        return False
    printSolution ( sol )
    return True
def solveMazeUtil ( maze , x , y , sol ) :
    if ( x == N - 1 and y == N - 1 ) :
        sol [ x ] [ y ] = 1
        return True
    if ( isSafe ( maze , x , y ) == True ) :
        sol [ x ] [ y ] = 1
        for i in range ( 1 , N ) :
            if ( i <= maze [ x ] [ y ] ) :
                if ( solveMazeUtil ( maze , x + i , y , sol ) == True ) :
                    return True
                if ( solveMazeUtil ( maze , x , y + i , sol ) == True ) :
                    return True
        sol [ x ] [ y ] = 0
        return False
    return False
maze = [ [ 2 , 1 , 0 , 0 ] , [ 3 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 ] ]
solveMaze ( maze )

Index/key variables: i, j, x, y
Iterator variables: i, j
Python program:
import math
set = [ ]
prime = [ ]
def isPrime ( x ) :
    sqroot = int ( math . sqrt ( x ) )
    flag = True
    if ( x == 1 ) :
        return False
    for i in range ( 2 , sqroot + 1 ) :
        if ( x % i == 0 ) :
            return False
    return True
def display ( ) :
    global set , prime
    length = len ( set )
    for i in range ( 0 , length ) :
        print ( set [ i ] , end = " ▁ " )
    print ( )
def primeSum ( total , N , S , index ) :
    global set , prime
    if ( total == S and len ( set ) == N ) :
        display ( )
        return
    if ( total > S or index == len ( prime ) ) :
        return
    set . append ( prime [ index ] )
    primeSum ( total + prime [ index ] , N , S , index + 1 )
    set . pop ( )
    primeSum ( total , N , S , index + 1 )
def allPrime ( N , S , P ) :
    global set , prime
    for i in range ( P + 1 , S + 1 ) :
        if ( isPrime ( i ) ) :
            prime . append ( i )
    if ( len ( prime ) < N ) :
        return
    primeSum ( 0 , N , S , 0 )
S = 54
N = 2
P = 3
allPrime ( N , S , P )

Index/key variables: i, index
Accumulator variables: prime
Iterator variables: i
Boolean variables: flag
Python program:
def grayCodeUtil ( res , n , num ) :
    if ( n == 0 ) :
        res . append ( num [ 0 ] )
        return
    grayCodeUtil ( res , n - 1 , num )
    num [ 0 ] = num [ 0 ] ^ ( 1 << ( n - 1 ) )
    grayCodeUtil ( res , n - 1 , num )
def grayCodes ( n ) :
    res = [ ]
    num = [ 0 ]
    grayCodeUtil ( res , n , num )
    return res
n = 3
code = grayCodes ( n )
for i in range ( len ( code ) ) :
    print ( code [ i ] )

Index/key variables: i
Iterator variables: i
Python program:
def isParenthesis ( c ) :
    return ( ( c == ' ( ' ) or ( c == ' ) ' ) )
def isValidString ( str ) :
    cnt = 0
    for i in range ( len ( str ) ) :
        if ( str [ i ] == ' ( ' ) :
            cnt += 1
        elif ( str [ i ] == ' ) ' ) :
            cnt -= 1
        if ( cnt < 0 ) :
            return False
    return ( cnt == 0 )
def removeInvalidParenthesis ( str ) :
    if ( len ( str ) == 0 ) :
        return
    visit = set ( )
    q = [ ]
    temp = 0
    level = 0
    q . append ( str )
    visit . add ( str )
    while ( len ( q ) ) :
        str = q [ 0 ]
        q . pop ( )
        if ( isValidString ( str ) ) :
            print ( str )
            level = True
        if ( level ) :
            continue
        for i in range ( len ( str ) ) :
            if ( not isParenthesis ( str [ i ] ) ) :
                continue
            temp = str [ 0 : i ] + str [ i + 1 : ]
            if temp not in visit :
                q . append ( temp )
                visit . add ( temp )
expression = " ( ) ( ) ) ( ) "
removeInvalidParenthesis ( expression )
expression = " ( ) v ) "
removeInvalidParenthesis ( expression )

Index/key variables: i
Accumulator variables: cnt, q, visit
Iterator variables: i
Boolean variables: level
Python program:
N = 4
ld = [ 0 ] * 30
rd = [ 0 ] * 30
cl = [ 0 ] * 30
def printSolution ( board ) :
    for i in range ( N ) :
        for j in range ( N ) :
            print ( board [ i ] [ j ] , end = " ▁ " )
        print ( )
def solveNQUtil ( board , col ) :
    if ( col >= N ) :
        return True
    for i in range ( N ) :
        if ( ( ld [ i - col + N - 1 ] != 1 and rd [ i + col ] != 1 ) and cl [ i ] != 1 ) :
            board [ i ] [ col ] = 1
            ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 1
            if ( solveNQUtil ( board , col + 1 ) ) :
                return True
            ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 0
    return False
def solveNQ ( ) :
    board = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
    if ( solveNQUtil ( board , 0 ) == False ) :
        printf ( " Solution ▁ does ▁ not ▁ exist " )
        return False
    printSolution ( board )
    return True
solveNQ ( )

Index/key variables: col, i, j
Iterator variables: i, j
Python program:
def printPairs ( a , n , x ) :
    rem = [ ]
    for i in range ( x ) :
        rem . append ( 0 )
    for i in range ( n ) :
        if ( a [ i ] < x ) :
            rem [ a [ i ] % x ] += 1
    for i in range ( 1 , x // 2 ) :
        if ( rem [ i ] > 0 and rem [ x - i ] > 0 ) :
            print ( " Yes " )
            break
    if ( i >= x // 2 ) :
        if ( x % 2 == 0 ) :
            if ( rem [ x // 2 ] > 1 ) :
                print ( " Yes " )
            else :
                print ( " No " )
        else :
            if ( rem [ x // 2 ] > 0 and rem [ x - x // 2 ] > 0 ) :
                print ( " Yes " )
            else :
                print ( " No " )
A = [ 1 , 4 , 45 , 6 , 10 , 8 ]
n = 16
arr_size = len ( A )
printPairs ( A , arr_size , n )

Index/key variables: i
Accumulator variables: rem
Iterator variables: i
Python program:
def fact ( n ) :
    ans = 1
    for i in range ( 2 , n + 1 , 1 ) :
        ans *= i
    return ans
def remainder ( n , a , p ) :
    len1 = fact ( n )
    ans = 1
    for i in range ( 1 , len1 + 1 , 1 ) :
        ans = ( ans * a ) % p
    return ans
if __name__ == ' _ _ main _ _ ' :
    A = 2
    N = 1
    P = 2
    print ( remainder ( N , A , P ) )

Accumulator variables: ans
Iterator variables: i
Python program:
def check ( n , w ) :
    a = [ 0 for i in range ( 105 ) ] ;
    p = 0
    while ( n > 0 ) :
        a [ p ] = n % w
        p += 1
        n //= w
    flag = True
    for i in range ( 101 ) :
        if ( a [ i ] == 0 or a [ i ] == 1 ) :
            continue
        elif ( a [ i ] == w or a [ i ] == w - 1 ) :
            a [ i + 1 ] += 1
        else :
            flag = False
    return flag
if __name__ == " _ _ main _ _ " :
    B = 3
    N = 7
    if ( check ( N , B ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i, p
Accumulator variables: n
Iterator variables: i
Boolean variables: flag
Python program:
from math import sqrt , floor , ceil , log2
from typing import List
MAX = 1000
def isPerfectSquare ( x : int ) -> bool :
    sr = sqrt ( x )
    return True if ( ( sr - floor ( sr ) ) == 0 ) else False
def getMid ( s : int , e : int ) -> int :
    return s + ( e - s ) // 2
def queryUtil ( st : List [ int ] , ss : int , se : int , qs : int , qe : int , index : int ) -> int :
    if ( qs <= ss and qe >= se ) :
        return st [ index ]
    if ( se < qs or ss > qe ) :
        return 0
    mid = getMid ( ss , se )
    return queryUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 )
def updateValueUtil ( st : List [ int ] , ss : int , se : int , i : int , diff : int , si : int ) -> None :
    if ( i < ss or i > se ) :
        return
    st [ si ] = st [ si ] + diff
    if ( se != ss ) :
        mid = getMid ( ss , se )
        updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 )
        updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 )
def updateValue ( arr : List [ int ] , st : List [ int ] , n : int , i : int , new_val : int ) -> None :
    if ( i < 0 or i > n - 1 ) :
        print ( " Invalid ▁ Input " )
        return
    diff = 0
    oldValue = 0
    oldValue = arr [ i ]
    arr [ i ] = new_val
    if ( isPerfectSquare ( oldValue ) and isPerfectSquare ( new_val ) ) :
        return
    if ( not isPerfectSquare ( oldValue ) and not isPerfectSquare ( new_val ) ) :
        return
    if ( isPerfectSquare ( oldValue ) and not isPerfectSquare ( new_val ) ) :
        diff = - 1
    if ( not isPerfectSquare ( oldValue ) and not isPerfectSquare ( new_val ) ) :
        diff = 1
    updateValueUtil ( st , 0 , n - 1 , i , diff , 0 )
def query ( st : List [ int ] , n : int , qs : int , qe : int ) -> None :
    perfectSquareInRange = queryUtil ( st , 0 , n - 1 , qs , qe , 0 )
    print ( perfectSquareInRange )
def constructSTUtil ( arr : List [ int ] , ss : int , se : int , st : List [ int ] , si : int ) -> int :
    if ( ss == se ) :
        if ( isPerfectSquare ( arr [ ss ] ) ) :
            st [ si ] = 1
        else :
            st [ si ] = 0
        return st [ si ]
    mid = getMid ( ss , se )
    st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 )
    return st [ si ]
def constructST ( arr : List [ int ] , n : int ) -> List [ int ] :
    x = ( ceil ( log2 ( n ) ) )
    max_size = 2 * pow ( 2 , x ) - 1
    st = [ 0 for _ in range ( max_size ) ]
    constructSTUtil ( arr , 0 , n - 1 , st , 0 )
    return st
if __name__ == " _ _ main _ _ " :
    arr = [ 16 , 15 , 8 , 9 , 14 , 25 ]
    n = len ( arr )
    st = constructST ( arr , n )
    start = 0
    end = 4
    query ( st , n , start , end )
    i = 3
    x = 11
    updateValue ( arr , st , n , i , x )
    start = 0
    end = 4
    query ( st , n , start , end )

Index/key variables: i, index, int, si, ss
Python program:
from typing import List
class Node :
    def __init__ ( self ) -> None :
        self . value = 0
        self . max_set_bits = 0
tree = [ Node ( ) for _ in range ( 4 * 10000 ) ]
def setBits ( x : int ) -> int :
    parity = 0
    while ( x != 0 ) :
        if ( x & 1 ) :
            parity += 1
        x = x >> 1
    return parity
def buildSegmentTree ( a : List [ int ] , index : int , beg : int , end : int ) -> None :
    if ( beg == end ) :
        tree [ index ] . value = a [ beg ]
        tree [ index ] . max_set_bits = setBits ( a [ beg ] )
    else :
        mid = ( beg + end ) // 2
        buildSegmentTree ( a , 2 * index + 1 , beg , mid )
        buildSegmentTree ( a , 2 * index + 2 , mid + 1 , end )
        if ( tree [ 2 * index + 1 ] . max_set_bits > tree [ 2 * index + 2 ] . max_set_bits ) :
            tree [ index ] . max_set_bits = tree [ 2 * index + 1 ] . max_set_bits
            tree [ index ] . value = tree [ 2 * index + 1 ] . value
        elif ( tree [ 2 * index + 2 ] . max_set_bits > tree [ 2 * index + 1 ] . max_set_bits ) :
            tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits
            tree [ index ] . value = tree [ 2 * index + 2 ] . value
        else :
            tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits
            tree [ index ] . value = max ( tree [ 2 * index + 2 ] . value , tree [ 2 * index + 1 ] . value )
def query ( index : int , beg : int , end : int , l : int , r : int ) -> Node :
    result = Node ( )
    result . value = result . max_set_bits = - 1
    if ( beg > r or end < l ) :
        return result
    if ( beg >= l and end <= r ) :
        return tree [ index ]
    mid = ( beg + end ) // 2
    if ( l > mid ) :
        return query ( 2 * index + 2 , mid + 1 , end , l , r )
    if ( r <= mid ) :
        return query ( 2 * index + 1 , beg , mid , l , r )
    left = query ( 2 * index + 1 , beg , mid , l , r )
    right = query ( 2 * index + 2 , mid + 1 , end , l , r )
    if ( left . max_set_bits > right . max_set_bits ) :
        result . max_set_bits = left . max_set_bits
        result . value = left . value
    elif ( right . max_set_bits > left . max_set_bits ) :
        result . max_set_bits = right . max_set_bits
        result . value = right . value
    else :
        result . max_set_bits = left . max_set_bits
        result . value = max ( right . value , left . value )
    return result
if __name__ == " _ _ main _ _ " :
    a = [ 18 , 9 , 8 , 15 , 14 , 5 ]
    N = len ( a )
    buildSegmentTree ( a , 0 , 0 , N - 1 )
    print ( query ( 0 , 0 , N - 1 , 1 , 4 ) . value )
    print ( query ( 0 , 0 , N - 1 , 0 , 2 ) . value )

Index/key variables: beg, index, int
Accumulator variables: parity
Class/struct names: Node
Python program:
import numpy as np
maxSize = 50
def matrix_product ( a , b ) :
    c = np . zeros ( ( 7 , 7 ) ) ;
    for i in range ( 7 ) :
        for j in range ( 7 ) :
            for k in range ( 7 ) :
                c [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ;
    return c
def mul_expo ( mul , p ) :
    s = [ [ 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ] ;
    while ( p != 1 ) :
        if ( p % 2 == 1 ) :
            s = matrix_product ( s , mul ) ;
        mul = matrix_product ( mul , mul ) ;
        p //= 2 ;
    return matrix_product ( mul , s ) ;
def expectedSteps ( x ) :
    if ( x == 0 ) :
        return 0 ;
    if ( x <= 6 ) :
        return 6 ;
    mul = [ [ 7 / 6 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ - 1 / 6 , 0 , 0 , 0 , 0 , 0 , 0 ] ] ;
    mul = mul_expo ( mul , x - 6 ) ;
    return ( mul [ 0 ] [ 0 ] + mul [ 1 ] [ 0 ] + mul [ 2 ] [ 0 ] + mul [ 3 ] [ 0 ] + mul [ 4 ] [ 0 ] + mul [ 5 ] [ 0 ] ) * 6 ;
if __name__ == " _ _ main _ _ " :
    n = 10 ;
    print ( expectedSteps ( n - 1 ) ) ;

Index/key variables: i, j, k
Accumulator variables: p
Iterator variables: i, j, k
Python program:
def canPlace ( a , n , p , sep ) :
    prisoners_placed = 1
    last_prisoner_placed = a [ 0 ]
    for i in range ( 1 , n ) :
        current_cell = a [ i ]
        if ( current_cell - last_prisoner_placed >= sep ) :
            prisoners_placed += 1
            last_prisoner_placed = current_cell
            if ( prisoners_placed == p ) :
                return True
    return False
def maxDistance ( cell , n , p ) :
    cell = sorted ( cell )
    start = 0
    end = cell [ n - 1 ] - cell [ 0 ]
    ans = 0
    while ( start <= end ) :
        mid = start + ( ( end - start ) // 2 )
        if ( canPlace ( cell , n , p , mid ) ) :
            ans = mid
            start = mid + 1
        else :
            end = mid - 1
    return ans
cell = [ 1 , 2 , 8 , 4 , 9 ]
n = len ( cell )
p = 3
print ( maxDistance ( cell , n , p ) )

Index/key variables: i
Accumulator variables: prisoners_placed
Iterator variables: i
Python program:
from math import gcd as __gcd
maxLen = 30
seg = [ 0 for i in range ( 3 * maxLen ) ]
def build ( l , r , inn , arr ) :
    if ( l == r ) :
        seg [ inn ] = arr [ l ]
        return seg [ inn ]
    mid = ( l + r ) // 2
    seg [ inn ] = __gcd ( build ( l , mid , 2 * inn + 1 , arr ) , build ( mid + 1 , r , 2 * inn + 2 , arr ) )
    return seg [ inn ]
def query ( l , r , l1 , r1 , inn ) :
    if ( l1 <= l and r <= r1 ) :
        return seg [ inn ]
    if ( l > r1 or r < l1 ) :
        return 0
    mid = ( l + r ) // 2
    x = __gcd ( query ( l , mid , l1 , r1 , 2 * inn + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * inn + 2 ) )
    return x
def findLen ( arr , n ) :
    build ( 0 , n - 1 , 0 , arr )
    i = 0
    j = 0
    ans = 10 ** 9
    while ( i < n ) :
        while ( j < n and query ( 0 , n - 1 , i , j , 0 ) != 1 ) :
            j += 1
        if ( j == n ) :
            break ;
        ans = minn ( ( j - i + 1 ) , ans )
        i += 1
        j = max ( j , i )
    if ( ans == 10 ** 9 ) :
        return - 1
    else :
        return ans
arr = [ 2 , 2 , 2 ]
n = len ( arr )
print ( findLen ( arr , n ) )

Index/key variables: inn, l
Accumulator variables: i, j
Python program:
def findGrid ( n ) :
    arr = [ [ 0 for k in range ( n ) ] for l in range ( n ) ]
    x = 0
    for i in range ( n // 4 ) :
        for j in range ( n // 4 ) :
            for k in range ( 4 ) :
                for l in range ( 4 ) :
                    arr [ i * 4 + k ] [ j * 4 + l ] = x
                    x += 1
    for i in range ( n ) :
        for j in range ( n ) :
            print ( arr [ i ] [ j ] , end = " ▁ " )
        print ( )
n = 4
findGrid ( n )

Index/key variables: i, j
Accumulator variables: x
Iterator variables: i, j, k, l
Python program:
class Node :
    def __init__ ( self , data = None , left = None , right = None ) :
        self . data = data
        self . right = right
        self . left = left
maxLen = 30
segtree = [ 0 ] * ( maxLen * 4 )
def buildTree ( l , r , i , arr ) :
    global segtree
    global maxLen
    if ( l == r ) :
        segtree [ i ] = l
        return l
    l1 = buildTree ( l , int ( ( l + r ) / 2 ) , 2 * i + 1 , arr )
    r1 = buildTree ( int ( ( l + r ) / 2 ) + 1 , r , 2 * i + 2 , arr )
    if ( arr [ l1 ] > arr [ r1 ] ) :
        segtree [ i ] = l1
    else :
        segtree [ i ] = r1
    return segtree [ i ]
def rangeMax ( l , r , rl , rr , i , arr ) :
    global segtree
    global maxLen
    if ( r < rl or l > rr ) :
        return - 1
    if ( l >= rl and r <= rr ) :
        return segtree [ i ]
    l1 = rangeMax ( l , int ( ( l + r ) / 2 ) , rl , rr , 2 * i + 1 , arr )
    r1 = rangeMax ( int ( ( l + r ) / 2 ) + 1 , r , rl , rr , 2 * i + 2 , arr )
    if ( l1 == - 1 ) :
        return r1
    if ( r1 == - 1 ) :
        return l1
    if ( arr [ l1 ] > arr [ r1 ] ) :
        return l1
    else :
        return r1
def inorder ( curr ) :
    if ( curr == None ) :
        return
    inorder ( curr . left )
    print ( curr . data , end = " ▁ " )
    inorder ( curr . right )
def createCartesianTree ( l , r , arr , n ) :
    if ( r < l ) :
        return None
    m = rangeMax ( 0 , n - 1 , l , r , 0 , arr )
    curr = Node ( arr [ m ] )
    curr . left = createCartesianTree ( l , m - 1 , arr , n )
    curr . right = createCartesianTree ( m + 1 , r , arr , n )
    return curr
arr = [ 8 , 11 , 21 , 100 , 5 , 70 , 55 ]
n = len ( arr )
buildTree ( 0 , n - 1 , 0 , arr )
inorder ( createCartesianTree ( 0 , n - 1 , arr , n ) )

Index/key variables: i, l1, m, r1
Class/struct names: Node