dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
from collections import deque
ROW = 9
COL = 10
class Point :
    def __init__ ( self , x : int , y : int ) :
        self . x = x
        self . y = y
class queueNode :
    def __init__ ( self , pt : Point , dist : int ) :
        self . pt = pt
        self . dist = dist
def isValid ( row : int , col : int ) :
    return ( row >= 0 ) and ( row < ROW ) and ( col >= 0 ) and ( col < COL )
rowNum = [ - 1 , 0 , 0 , 1 ]
colNum = [ 0 , - 1 , 1 , 0 ]
def BFS ( mat , src : Point , dest : Point ) :
    if mat [ src . x ] [ src . y ] != 1 or mat [ dest . x ] [ dest . y ] != 1 :
        return - 1
    visited = [ [ False for i in range ( COL ) ] for j in range ( ROW ) ]
    visited [ src . x ] [ src . y ] = True
    q = deque ( )
    s = queueNode ( src , 0 )
    q . append ( s )
    while q :
        curr = q . popleft ( )
        pt = curr . pt
        if pt . x == dest . x and pt . y == dest . y :
            return curr . dist
        for i in range ( 4 ) :
            row = pt . x + rowNum [ i ]
            col = pt . y + colNum [ i ]
            if ( isValid ( row , col ) and mat [ row ] [ col ] == 1 and not visited [ row ] [ col ] ) :
                visited [ row ] [ col ] = True
                Adjcell = queueNode ( Point ( row , col ) , curr . dist + 1 )
                q . append ( Adjcell )
    return - 1
def main ( ) :
    mat = [ [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] , [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ] ]
    source = Point ( 0 , 0 )
    dest = Point ( 3 , 4 )
    dist = BFS ( mat , source , dest )
    if dist != - 1 :
        print ( " Shortest ▁ Path ▁ is " , dist )
    else :
        print ( " Shortest ▁ Path ▁ doesn ' t ▁ exist " )
main ( )

Index/key variables: col, i, row
Accumulator variables: q
Iterator variables: i
Class/struct names: Point, queueNode
Python program:
class Node ( object ) :
    def __init__ ( self , item ) :
        self . data = item
        self . left = None
        self . right = None
def BTToDLLUtil ( root ) :
    if root is None :
        return root
    if root . left :
        left = BTToDLLUtil ( root . left )
        while left . right :
            left = left . right
        left . right = root
        root . left = left
    if root . right :
        right = BTToDLLUtil ( root . right )
        while right . left :
            right = right . left
        right . left = root
        root . right = right
    return root
def BTToDLL ( root ) :
    if root is None :
        return root
    root = BTToDLLUtil ( root )
    while root . left :
        root = root . left
    return root
def print_list ( head ) :
    if head is None :
        return
    while head :
        print ( head . data , end = " ▁ " )
        head = head . right
if __name__ == ' _ _ main _ _ ' :
    root = Node ( 10 )
    root . left = Node ( 12 )
    root . right = Node ( 15 )
    root . left . left = Node ( 25 )
    root . left . right = Node ( 30 )
    root . right . left = Node ( 36 )
    head = BTToDLL ( root )
    print_list ( head )

Class/struct names: Node
Python program:
R = 3
C = 4
def modifyMatrix ( mat ) :
    row = [ 0 ] * R
    col = [ 0 ] * C
    for i in range ( 0 , R ) :
        row [ i ] = 0
    for i in range ( 0 , C ) :
        col [ i ] = 0
    for i in range ( 0 , R ) :
        for j in range ( 0 , C ) :
            if ( mat [ i ] [ j ] == 1 ) :
                row [ i ] = 1
                col [ j ] = 1
    for i in range ( 0 , R ) :
        for j in range ( 0 , C ) :
            if ( row [ i ] == 1 or col [ j ] == 1 ) :
                mat [ i ] [ j ] = 1
def printMatrix ( mat ) :
    for i in range ( 0 , R ) :
        for j in range ( 0 , C ) :
            print ( mat [ i ] [ j ] , end = " ▁ " )
        print ( )
mat = [ [ 1 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
print ( " Input ▁ Matrix ▁ n " )
printMatrix ( mat )
modifyMatrix ( mat )
print ( " Matrix ▁ after ▁ modification ▁ n " )
printMatrix ( mat )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def modifyMatrix ( mat ) :
    row_flag = False
    col_flag = False
    for i in range ( 0 , len ( mat ) ) :
        for j in range ( 0 , len ( mat ) ) :
            if ( i == 0 and mat [ i ] [ j ] == 1 ) :
                row_flag = True
            if ( j == 0 and mat [ i ] [ j ] == 1 ) :
                col_flag = True
            if ( mat [ i ] [ j ] == 1 ) :
                mat [ 0 ] [ j ] = 1
                mat [ i ] [ 0 ] = 1
    for i in range ( 1 , len ( mat ) ) :
        for j in range ( 1 , len ( mat ) + 1 ) :
            if ( mat [ 0 ] [ j ] == 1 or mat [ i ] [ 0 ] == 1 ) :
                mat [ i ] [ j ] = 1
    if ( row_flag == True ) :
        for i in range ( 0 , len ( mat ) ) :
            mat [ 0 ] [ i ] = 1
    if ( col_flag == True ) :
        for i in range ( 0 , len ( mat ) ) :
            mat [ i ] [ 0 ] = 1
def printMatrix ( mat ) :
    for i in range ( 0 , len ( mat ) ) :
        for j in range ( 0 , len ( mat ) + 1 ) :
            print ( mat [ i ] [ j ] , end = " " )
        print ( )
mat = [ [ 1 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] ]
print ( " Input ▁ Matrix ▁ : " )
printMatrix ( mat )
modifyMatrix ( mat )
print ( " Matrix ▁ After ▁ Modification ▁ : " )
printMatrix ( mat )

Index/key variables: i, j
Iterator variables: i, j
Boolean variables: col_flag, row_flag
Python program:
ROW = 4
COL = 5
def findUniqueRows ( M ) :
    for i in range ( ROW ) :
        flag = 0
        for j in range ( i ) :
            flag = 1
            for k in range ( COL ) :
                if ( M [ i ] [ k ] != M [ j ] [ k ] ) :
                    flag = 0
            if ( flag == 1 ) :
                break
        if ( flag == 0 ) :
            for j in range ( COL ) :
                print ( M [ i ] [ j ] , end = " ▁ " )
            print ( )
if __name__ == ' _ _ main _ _ ' :
    M = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 0 , 0 ] ]
    findUniqueRows ( M )

Index/key variables: i, j, k
Iterator variables: i, j, k
Python program:
def printArray ( matrix ) :
    rowCount = len ( matrix )
    if rowCount == 0 :
        return
    columnCount = len ( matrix [ 0 ] )
    if columnCount == 0 :
        return
    row_output_format = " ▁ " . join ( [ " % s " ] * columnCount )
    printed = { }
    for row in matrix :
        routput = row_output_format % tuple ( row )
        if routput not in printed :
            printed [ routput ] = True
            print ( routput )
mat = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] ]
printArray ( mat )

Index/key variables: routput
Iterator variables: row
Python program:
R = 3
C = 5
def maxArea ( mat ) :
    hist = [ [ 0 for i in range ( C + 1 ) ] for i in range ( R + 1 ) ]
    for i in range ( 0 , C , 1 ) :
        hist [ 0 ] [ i ] = mat [ 0 ] [ i ]
        for j in range ( 1 , R , 1 ) :
            if ( ( mat [ j ] [ i ] == 0 ) ) :
                hist [ j ] [ i ] = 0
            else :
                hist [ j ] [ i ] = hist [ j - 1 ] [ i ] + 1
    for i in range ( 0 , R , 1 ) :
        count = [ 0 for i in range ( R + 1 ) ]
        for j in range ( 0 , C , 1 ) :
            count [ hist [ i ] [ j ] ] += 1
        col_no = 0
        j = R
        while ( j >= 0 ) :
            if ( count [ j ] > 0 ) :
                for k in range ( 0 , count [ j ] , 1 ) :
                    hist [ i ] [ col_no ] = j
                    col_no += 1
            j -= 1
    max_area = 0
    for i in range ( 0 , R , 1 ) :
        for j in range ( 0 , C , 1 ) :
            curr_area = ( j + 1 ) * hist [ i ] [ j ]
            if ( curr_area > max_area ) :
                max_area = curr_area
    return max_area
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 0 , 1 , 1 ] , [ 1 , 1 , 0 , 1 , 0 ] ]
    print ( " Area ▁ of ▁ the ▁ largest ▁ rectangle ▁ is " , maxArea ( mat ) )

Index/key variables: col_no, i, j
Iterator variables: i, j, k
Python program:
M = 4
N = 5
def preProcess ( mat , aux ) :
    for i in range ( 0 , N , 1 ) :
        aux [ 0 ] [ i ] = mat [ 0 ] [ i ]
    for i in range ( 1 , M , 1 ) :
        for j in range ( 0 , N , 1 ) :
            aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ]
    for i in range ( 0 , M , 1 ) :
        for j in range ( 1 , N , 1 ) :
            aux [ i ] [ j ] += aux [ i ] [ j - 1 ]
def sumQuery ( aux , tli , tlj , rbi , rbj ) :
    res = aux [ rbi ] [ rbj ]
    if ( tli > 0 ) :
        res = res - aux [ tli - 1 ] [ rbj ]
    if ( tlj > 0 ) :
        res = res - aux [ rbi ] [ tlj - 1 ]
    if ( tli > 0 and tlj > 0 ) :
        res = res + aux [ tli - 1 ] [ tlj - 1 ]
    return res
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 5 , 3 , 8 , 1 , 2 ] , [ 4 , 6 , 7 , 5 , 5 ] , [ 2 , 4 , 8 , 9 , 4 ] ]
aux = [ [ 0 for i in range ( N ) ] for j in range ( M ) ]
preProcess ( mat , aux )
tli = 2
tlj = 2
rbi = 3
rbj = 4
print ( " Query1 : " , sumQuery ( aux , tli , tlj , rbi , rbj ) )
tli = 0
tlj = 0
rbi = 1
rbj = 1
print ( " Query2 : " , sumQuery ( aux , tli , tlj , rbi , rbj ) )
tli = 1
tlj = 2
rbi = 3
rbj = 3
print ( " Query3 : " , sumQuery ( aux , tli , tlj , rbi , rbj ) )

Index/key variables: i, j, rbi, rbj
Iterator variables: i, j
Python program:
class rankMatrix ( object ) :
    def __init__ ( self , Matrix ) :
        self . R = len ( Matrix )
        self . C = len ( Matrix [ 0 ] )
    def swap ( self , Matrix , row1 , row2 , col ) :
        for i in range ( col ) :
            temp = Matrix [ row1 ] [ i ]
            Matrix [ row1 ] [ i ] = Matrix [ row2 ] [ i ]
            Matrix [ row2 ] [ i ] = temp
    def rankOfMatrix ( self , Matrix ) :
        rank = self . C
        for row in range ( 0 , rank , 1 ) :
            if Matrix [ row ] [ row ] != 0 :
                for col in range ( 0 , self . R , 1 ) :
                    if col != row :
                        multiplier = ( Matrix [ col ] [ row ] / Matrix [ row ] [ row ] )
                        for i in range ( rank ) :
                            Matrix [ col ] [ i ] -= ( multiplier * Matrix [ row ] [ i ] )
            else :
                reduce = True
                for i in range ( row + 1 , self . R , 1 ) :
                    if Matrix [ i ] [ row ] != 0 :
                        self . swap ( Matrix , row , i , rank )
                        reduce = False
                        break
                if reduce :
                    rank -= 1
                    for i in range ( 0 , self . R , 1 ) :
                        Matrix [ i ] [ row ] = Matrix [ i ] [ rank ]
                row -= 1
        return ( rank )
    def Display ( self , Matrix , row , col ) :
        for i in range ( row ) :
            for j in range ( col ) :
                print ( " ▁ " + str ( Matrix [ i ] [ j ] ) )
            print ( ' ' )
if __name__ == ' _ _ main _ _ ' :
    Matrix = [ [ 10 , 20 , 10 ] , [ - 20 , - 30 , 10 ] , [ 30 , 50 , 0 ] ]
    RankMatrix = rankMatrix ( Matrix )
    print ( " Rank ▁ of ▁ the ▁ Matrix ▁ is : " , ( RankMatrix . rankOfMatrix ( Matrix ) ) )

Index/key variables: col, i, j, rank, row, row1, row2
Iterator variables: col, i, j, row
Boolean variables: reduce
Class/struct names: rankMatrix
Python program:
class Solution ( ) :
    def maxHist ( self , row ) :
        result = [ ]
        top_val = 0
        max_area = 0
        area = 0
        i = 0
        while ( i < len ( row ) ) :
            if ( len ( result ) == 0 ) or ( row [ result [ - 1 ] ] <= row [ i ] ) :
                result . append ( i )
                i += 1
            else :
                top_val = row [ result . pop ( ) ]
                area = top_val * i
                if ( len ( result ) ) :
                    area = top_val * ( i - result [ - 1 ] - 1 )
                max_area = max ( area , max_area )
        while ( len ( result ) ) :
            top_val = row [ result . pop ( ) ]
            area = top_val * i
            if ( len ( result ) ) :
                area = top_val * ( i - result [ - 1 ] - 1 )
            max_area = max ( area , max_area )
        return max_area
    def maxRectangle ( self , A ) :
        result = self . maxHist ( A [ 0 ] )
        for i in range ( 1 , len ( A ) ) :
            for j in range ( len ( A [ i ] ) ) :
                if ( A [ i ] [ j ] ) :
                    A [ i ] [ j ] += A [ i - 1 ] [ j ]
            result = max ( result , self . maxHist ( A [ i ] ) )
        return result
if __name__ == ' _ _ main _ _ ' :
    A = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 ] ]
    ans = Solution ( )
    print ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is " , ans . maxRectangle ( A ) )

Index/key variables: i, j
Accumulator variables: result
Iterator variables: i, j
Class/struct names: Solution
Python program:
class Cell :
    def __init__ ( self , r , c ) :
        self . r = r
        self . c = c
def printSums ( mat , arr , n ) :
    for i in range ( 0 , n ) :
        Sum = 0 ; r = arr [ i ] . r ; c = arr [ i ] . c
        for j in range ( 0 , R ) :
            for k in range ( 0 , C ) :
                if j != r and k != c :
                    Sum += mat [ j ] [ k ]
        print ( Sum )
if __name__ == " _ _ main _ _ " :
    mat = [ [ 1 , 1 , 2 ] , [ 3 , 4 , 6 ] , [ 5 , 3 , 2 ] ]
    R = C = 3
    arr = [ Cell ( 0 , 0 ) , Cell ( 1 , 1 ) , Cell ( 0 , 1 ) ]
    n = len ( arr )
    printSums ( mat , arr , n )

Index/key variables: i, j, k
Accumulator variables: Sum
Iterator variables: i, j, k
Class/struct names: Cell
Python program:
class Cell :
    def __init__ ( self , r , c ) :
        self . r = r
        self . c = c
def printSums ( mat , arr , n ) :
    Sum = 0
    row , col = [ 0 ] * R , [ 0 ] * C
    for i in range ( 0 , R ) :
        for j in range ( 0 , C ) :
            Sum += mat [ i ] [ j ]
            row [ i ] += mat [ i ] [ j ]
            col [ j ] += mat [ i ] [ j ]
    for i in range ( 0 , n ) :
        r0 , c0 = arr [ i ] . r , arr [ i ] . c
        print ( Sum - row [ r0 ] - col [ c0 ] + mat [ r0 ] [ c0 ] )
if __name__ == " _ _ main _ _ " :
    mat = [ [ 1 , 1 , 2 ] , [ 3 , 4 , 6 ] , [ 5 , 3 , 2 ] ]
    R = C = 3
    arr = [ Cell ( 0 , 0 ) , Cell ( 1 , 1 ) , Cell ( 0 , 1 ) ]
    n = len ( arr )
    printSums ( mat , arr , n )

Index/key variables: c0, i, j, r0
Accumulator variables: Sum
Iterator variables: i, j
Class/struct names: Cell
Python program:
def countIslands ( mat ) :
    count = 0 ;
    for i in range ( 0 , M ) :
        for j in range ( 0 , N ) :
            if ( mat [ i ] [ j ] == ' X ' ) :
                if ( ( i == 0 or mat [ i - 1 ] [ j ] == ' O ' ) and ( j == 0 or mat [ i ] [ j - 1 ] == ' O ' ) ) :
                    count = count + 1
    return count
M = 6
N = 3
mat = [ [ ' O ' , ' O ' , ' O ' ] , [ ' X ' , ' X ' , ' O ' ] , [ ' X ' , ' X ' , ' O ' ] , [ ' O ' , ' O ' , ' X ' ] , [ ' O ' , ' O ' , ' X ' ] , [ ' X ' , ' X ' , ' O ' ] ]
print ( " Number ▁ of ▁ rectangular ▁ islands ▁ is " , countIslands ( mat ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
M = 4
N = 5
def findCommon ( mat ) :
    column = [ N - 1 ] * M
    min_row = 0
    while ( column [ min_row ] >= 0 ) :
        for i in range ( M ) :
            if ( mat [ i ] [ column [ i ] ] < mat [ min_row ] [ column [ min_row ] ] ) :
                min_row = i
        eq_count = 0
        for i in range ( M ) :
            if ( mat [ i ] [ column [ i ] ] > mat [ min_row ] [ column [ min_row ] ] ) :
                if ( column [ i ] == 0 ) :
                    return - 1
                column [ i ] -= 1
            else :
                eq_count += 1
        if ( eq_count == M ) :
            return mat [ min_row ] [ column [ min_row ] ]
    return - 1
if __name__ == " _ _ main _ _ " :
    mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 2 , 4 , 5 , 8 , 10 ] , [ 3 , 5 , 7 , 9 , 11 ] , [ 1 , 3 , 5 , 7 , 9 ] ]
    result = findCommon ( mat )
    if ( result == - 1 ) :
        print ( " No ▁ common ▁ element " )
    else :
        print ( " Common ▁ element ▁ is " , result )

Index/key variables: i, min_row
Accumulator variables: eq_count
Iterator variables: i
Python program:
from collections import defaultdict
M = 4
N = 5
def findCommon ( mat ) :
    global M
    global N
    cnt = dict ( )
    cnt = defaultdict ( lambda : 0 , cnt )
    i = 0
    j = 0
    while ( i < M ) :
        cnt [ mat [ i ] [ 0 ] ] = cnt [ mat [ i ] [ 0 ] ] + 1
        j = 1
        while ( j < N ) :
            if ( mat [ i ] [ j ] != mat [ i ] [ j - 1 ] ) :
                cnt [ mat [ i ] [ j ] ] = cnt [ mat [ i ] [ j ] ] + 1
            j = j + 1
        i = i + 1
    for ele in cnt :
        if ( cnt [ ele ] == M ) :
            return ele
    return - 1
mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 2 , 4 , 5 , 8 , 10 ] , [ 3 , 5 , 7 , 9 , 11 ] , [ 1 , 3 , 5 , 7 , 9 ] , ]
result = findCommon ( mat )
if ( result == - 1 ) :
    print ( " No ▁ common ▁ element " )
else :
    print ( " Common ▁ element ▁ is ▁ " , result )

Index/key variables: ele, i, j
Iterator variables: ele
Python program:
M = 6
N = 6
def floodFillUtil ( mat , x , y , prevV , newV ) :
    if ( x < 0 or x >= M or y < 0 or y >= N ) :
        return
    if ( mat [ x ] [ y ] != prevV ) :
        return
    mat [ x ] [ y ] = newV
    floodFillUtil ( mat , x + 1 , y , prevV , newV )
    floodFillUtil ( mat , x - 1 , y , prevV , newV )
    floodFillUtil ( mat , x , y + 1 , prevV , newV )
    floodFillUtil ( mat , x , y - 1 , prevV , newV )
def replaceSurrounded ( mat ) :
    for i in range ( M ) :
        for j in range ( N ) :
            if ( mat [ i ] [ j ] == ' O ' ) :
                mat [ i ] [ j ] = ' - '
    for i in range ( M ) :
        if ( mat [ i ] [ 0 ] == ' - ' ) :
            floodFillUtil ( mat , i , 0 , ' - ' , ' O ' )
    for i in range ( M ) :
        if ( mat [ i ] [ N - 1 ] == ' - ' ) :
            floodFillUtil ( mat , i , N - 1 , ' - ' , ' O ' )
    for i in range ( N ) :
        if ( mat [ 0 ] [ i ] == ' - ' ) :
            floodFillUtil ( mat , 0 , i , ' - ' , ' O ' )
    for i in range ( N ) :
        if ( mat [ M - 1 ] [ i ] == ' - ' ) :
            floodFillUtil ( mat , M - 1 , i , ' - ' , ' O ' )
    for i in range ( M ) :
        for j in range ( N ) :
            if ( mat [ i ] [ j ] == ' - ' ) :
                mat [ i ] [ j ] = ' X '
if __name__ == ' _ _ main _ _ ' :
    mat = [ [ ' X ' , ' O ' , ' X ' , ' O ' , ' X ' , ' X ' ] , [ ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' X ' ] , [ ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' ] , [ ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' ] ] ;
    replaceSurrounded ( mat )
    for i in range ( M ) :
        print ( * mat [ i ] )

Index/key variables: i, j, x, y
Iterator variables: i, j
Python program:
N = 6
def maximumSubSquare ( arr ) :
    dp = [ [ [ - 1 , - 1 ] for i in range ( 51 ) ] for j in range ( 51 ) ]
    maxside = [ [ 0 for i in range ( 51 ) ] for j in range ( 51 ) ]
    x = 0
    y = 0
    for i in range ( N ) :
        x = 0
        for j in range ( N ) :
            if ( arr [ i ] [ j ] == ' X ' ) :
                x += 1
            else :
                x = 0
            dp [ i ] [ j ] [ 0 ] = x
    for i in range ( N ) :
        for j in range ( N ) :
            if ( arr [ j ] [ i ] == ' X ' ) :
                y += 1
            else :
                y = 0
            dp [ j ] [ i ] [ 1 ] = y
    maxval = 0
    val = 0
    for i in range ( N ) :
        for j in range ( N ) :
            val = min ( dp [ i ] [ j ] [ 0 ] , dp [ i ] [ j ] [ 1 ] )
            if ( dp [ i ] [ j - val + 1 ] [ 1 ] >= val and dp [ i - val + 1 ] [ j ] [ 0 ] >= val ) :
                maxside [ i ] [ j ] = val
            else :
                maxside [ i ] [ j ] = 0
            maxval = max ( maxval , maxside [ i ] [ j ] )
    return maxval
mat = [ [ ' X ' , ' O ' , ' X ' , ' X ' , ' X ' , ' X ' ] , [ ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' O ' , ' X ' ] , [ ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' ] , [ ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' ] ]
print ( maximumSubSquare ( mat ) )

Index/key variables: i, j
Accumulator variables: x, y
Iterator variables: i, j
Python program:
R = 5
C = 4
intmin = - 10000000
intmax = 10000000
def isValid ( x , y1 , y2 ) :
    return ( ( x >= 0 and x < R and y1 >= 0 and y1 < C and y2 >= 0 and y2 < C ) )
def getMaxUtil ( arr , mem , x , y1 , y2 ) :
    if isValid ( x , y1 , y2 ) == False :
        return intmin
    if x == R - 1 and y1 == 0 and y2 == C - 1 :
        if y1 == y2 :
            return arr [ x ] [ y1 ]
        else :
            return arr [ x ] [ y1 ] + arr [ x ] [ y2 ]
    if x == R - 1 :
        return intmin
    if mem [ x ] [ y1 ] [ y2 ] != - 1 :
        return mem [ x ] [ y1 ] [ y2 ]
    ans = intmin
    temp = 0
    if y1 == y2 :
        temp = arr [ x ] [ y1 ]
    else :
        temp = arr [ x ] [ y1 ] + arr [ x ] [ y2 ]
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) )
    ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) )
    mem [ x ] [ y1 ] [ y2 ] = ans
    return ans
def geMaxCollection ( arr ) :
    mem = [ [ [ - 1 for i in range ( C ) ] for i in range ( C ) ] for i in range ( R ) ]
    return getMaxUtil ( arr , mem , 0 , 0 , C - 1 )
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 3 , 6 , 8 , 2 ] , [ 5 , 2 , 4 , 3 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , ]
    print ( ' Maximum ▁ collection ▁ is ▁ ' , geMaxCollection ( arr ) )

Index/key variables: x, y1, y2
Python program:
R = 5
C = 5
def isValid ( i , j ) :
    return ( i >= 0 and i < R and j >= 0 and j < C )
def maxCoinsRec ( arr , i , j , dir ) :
    if ( isValid ( i , j ) == False or arr [ i ] [ j ] == ' # ' ) :
        return 0
    if ( arr [ i ] [ j ] == ' C ' ) :
        result = 1
    else :
        result = 0
    if ( dir == 1 ) :
        return ( result + max ( maxCoinsRec ( arr , i + 1 , j , 0 ) , maxCoinsRec ( arr , i , j + 1 , 1 ) ) )
    return ( result + max ( maxCoinsRec ( arr , i + 1 , j , 1 ) , maxCoinsRec ( arr , i , j - 1 , 0 ) ) )
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ ' E ' , ' C ' , ' C ' , ' C ' , ' C ' ] , [ ' C ' , ' # ' , ' C ' , ' # ' , ' E ' ] , [ ' # ' , ' C ' , ' C ' , ' # ' , ' C ' ] , [ ' C ' , ' E ' , ' E ' , ' C ' , ' E ' ] , [ ' C ' , ' E ' , ' # ' , ' C ' , ' E ' ] ]
    print ( " Maximum ▁ number ▁ of ▁ collected ▁ coins ▁ is ▁ " , maxCoinsRec ( arr , 0 , 0 , 1 ) )

Index/key variables: i, j