dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def checkStr1CanConStr2 ( str1 , str2 ) :
    N = len ( str1 )
    M = len ( str2 )
    st1 = set ( [ ] )
    st2 = set ( [ ] )
    hash1 = [ 0 ] * 256
    for i in range ( N ) :
        hash1 [ ord ( str1 [ i ] ) ] += 1
    for i in range ( N ) :
        st1 . add ( str1 [ i ] )
    for i in range ( M ) :
        st2 . add ( str2 [ i ] )
    if ( st1 != st2 ) :
        return False
    hash2 = [ 0 ] * 256
    for i in range ( M ) :
        hash2 [ ord ( str2 [ i ] ) ] += 1
    hash1 . sort ( )
    hash2 . sort ( )
    for i in range ( 256 ) :
        if ( hash1 [ i ] != hash2 [ i ] ) :
            return False
    return True
if __name__ == " _ _ main _ _ " :
    str1 = " xyyzzlll "
    str2 = " yllzzxxx "
    if ( checkStr1CanConStr2 ( str1 , str2 ) ) :
        print ( " True " )
    else :
        print ( " False " )

Index/key variables: i
Accumulator variables: st1, st2
Iterator variables: i
Python program:
def partSort ( arr , N , a , b ) :
    l = min ( a , b )
    r = max ( a , b )
    arr = ( arr [ 0 : l ] + sorted ( arr [ l : r + 1 ] ) + arr [ r : N ] )
    for i in range ( 0 , N , 1 ) :
        print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    arr = [ 7 , 8 , 4 , 5 , 2 ]
    a = 1
    b = 4
    N = len ( arr )
    partSort ( arr , N , a , b )

Index/key variables: i
Iterator variables: i
Python program:
INF = 2147483647
N = 4
def minCost ( cost ) :
    dist = [ 0 for i in range ( N ) ]
    for i in range ( N ) :
        dist [ i ] = INF
    dist [ 0 ] = 0
    for i in range ( N ) :
        for j in range ( i + 1 , N ) :
            if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) :
                dist [ j ] = dist [ i ] + cost [ i ] [ j ]
    return dist [ N - 1 ]
cost = [ [ 0 , 15 , 80 , 90 ] , [ INF , 0 , 40 , 50 ] , [ INF , INF , 0 , 70 ] , [ INF , INF , INF , 0 ] ]
print ( " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " , N , " ▁ is ▁ " , minCost ( cost ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
mn = 1000
def parity ( even , odd , v , i ) :
    global mn
    if ( i == len ( v ) or len ( even ) == 0 or len ( odd ) == 0 ) :
        count = 0
        for j in range ( len ( v ) - 1 ) :
            if ( v [ j ] % 2 != v [ j + 1 ] % 2 ) :
                count += 1
        if ( count < mn ) :
            mn = count
        return
    if ( v [ i ] != - 1 ) :
        parity ( even , odd , v , i + 1 )
    else :
        if ( len ( even ) != 0 ) :
            x = even [ len ( even ) - 1 ]
            even . remove ( even [ len ( even ) - 1 ] )
            v [ i ] = x
            parity ( even , odd , v , i + 1 )
            even . append ( x )
        if ( len ( odd ) != 0 ) :
            x = odd [ len ( odd ) - 1 ]
            odd . remove ( odd [ len ( odd ) - 1 ] )
            v [ i ] = x
            parity ( even , odd , v , i + 1 )
            odd . append ( x )
def mnDiffParity ( v , n ) :
    global mn
    even = [ ]
    odd = [ ]
    m = { i : 0 for i in range ( 100 ) }
    for i in range ( 1 , n + 1 ) :
        m [ i ] = 1
    for i in range ( len ( v ) ) :
        if ( v [ i ] != - 1 ) :
            m . pop ( v [ i ] )
    for key in m . keys ( ) :
        if ( key % 2 == 0 ) :
            even . append ( key )
        else :
            odd . append ( key )
    parity ( even , odd , v , 0 )
    print ( mn + 4 )
if __name__ == ' _ _ main _ _ ' :
    n = 8
    v = [ 2 , 1 , 4 , - 1 , - 1 , 6 , - 1 , 8 ]
    mnDiffParity ( v , n )

Index/key variables: i, j
Accumulator variables: count, even, odd
Iterator variables: i, j, key
Python program:
MAX = 100005
adjacent = [ [ ] for i in range ( MAX ) ]
visited = [ False ] * MAX
startnode = endnode = thirdnode = None
maxi , N = - 1 , None
parent = [ None ] * MAX
vis = [ False ] * MAX
def dfs ( u , count ) :
    visited [ u ] = True
    temp = 0
    global startnode , maxi
    for i in range ( 0 , len ( adjacent [ u ] ) ) :
        if not visited [ adjacent [ u ] [ i ] ] :
            temp += 1
            dfs ( adjacent [ u ] [ i ] , count + 1 )
    if temp == 0 :
        if maxi < count :
            maxi = count
            startnode = u
def dfs1 ( u , count ) :
    visited [ u ] = True
    temp = 0
    global endnode , maxi
    for i in range ( 0 , len ( adjacent [ u ] ) ) :
        if not visited [ adjacent [ u ] [ i ] ] :
            temp += 1
            parent [ adjacent [ u ] [ i ] ] = u
            dfs1 ( adjacent [ u ] [ i ] , count + 1 )
    if temp == 0 :
        if maxi < count :
            maxi = count
            endnode = u
def dfs2 ( u , count ) :
    visited [ u ] = True
    temp = 0
    global thirdnode , maxi
    for i in range ( 0 , len ( adjacent [ u ] ) ) :
        if ( not visited [ adjacent [ u ] [ i ] ] and not vis [ adjacent [ u ] [ i ] ] ) :
            temp += 1
            dfs2 ( adjacent [ u ] [ i ] , count + 1 )
    if temp == 0 :
        if maxi < count :
            maxi = count
            thirdnode = u
def findNodes ( ) :
    dfs ( 1 , 0 )
    global maxi
    for i in range ( 0 , N + 1 ) :
        visited [ i ] = False
    maxi = - 1
    dfs1 ( startnode , 0 )
    for i in range ( 0 , N + 1 ) :
        visited [ i ] = False
    x = endnode
    vis [ startnode ] = True
    while x != startnode :
        vis [ x ] = True
        x = parent [ x ]
    maxi = - 1
    for i in range ( 1 , N + 1 ) :
        if vis [ i ] :
            dfs2 ( i , 0 )
if __name__ == " _ _ main _ _ " :
    N = 4
    adjacent [ 1 ] . append ( 2 )
    adjacent [ 2 ] . append ( 1 )
    adjacent [ 1 ] . append ( 3 )
    adjacent [ 3 ] . append ( 1 )
    adjacent [ 1 ] . append ( 4 )
    adjacent [ 4 ] . append ( 1 )
    findNodes ( )
    print ( " ( { } , ▁ { } , ▁ { } ) " . format ( startnode , endnode , thirdnode ) )

Index/key variables: i, startnode, u, x
Accumulator variables: temp
Iterator variables: i
Python program:
def maxvolume ( s ) :
    maxvalue = 0
    i = 1
    for i in range ( s - 1 ) :
        j = 1
        for j in range ( s ) :
            k = s - i - j
            maxvalue = max ( maxvalue , i * j * k )
    return maxvalue
s = 8
print ( maxvolume ( s ) )

Iterator variables: i, j
Python program:
def performQuery ( arr , Q ) :
    for i in range ( 0 , len ( Q ) ) :
        orr = 0
        x = Q [ i ] [ 0 ]
        arr [ x - 1 ] = Q [ i ] [ 1 ]
        for j in range ( 0 , len ( arr ) ) :
            orr = orr | arr [ j ]
        print ( orr , end = " ▁ " )
arr = [ 1 , 2 , 3 ]
Q = [ [ 1 , 4 ] , [ 3 , 0 ] ]
performQuery ( arr , Q )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def smallest ( k , d ) :
    cnt = 1
    m = d % k
    v = [ 0 for i in range ( k ) ] ;
    v [ m ] = 1
    while ( 1 ) :
        if ( m == 0 ) :
            return cnt
        m = ( ( ( m * ( 10 % k ) ) % k ) + ( d % k ) ) % k
        if ( v [ m ] == 1 ) :
            return - 1
        v [ m ] = 1
        cnt += 1
    return - 1
d = 1
k = 41
print ( smallest ( k , d ) )

Index/key variables: m
Accumulator variables: cnt
Python program:
import math
def checkCommonDivisor ( arr , N , X ) :
    G = 0
    for i in range ( N ) :
        G = math . gcd ( G , arr [ i ] )
    copy_G = G
    for divisor in range ( 2 , X + 1 ) :
        while ( G % divisor == 0 ) :
            G = G // divisor
    if ( G <= X ) :
        print ( " Yes " )
        for i in range ( N ) :
            print ( arr [ i ] // copy_G , end = " ▁ " )
        print ( )
    else :
        print ( " No " )
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , 15 , 6 ]
    X = 6
    N = len ( arr )
    checkCommonDivisor ( arr , N , X )

Index/key variables: i
Iterator variables: divisor, i
Python program:
class Node :
    def __init__ ( self , next = None , prev = None , data = None ) :
        self . next = next
        self . prev = prev
        self . data = data
def reverse ( head_ref ) :
    temp = None
    current = head_ref
    while ( current != None ) :
        temp = current . prev
        current . prev = current . next
        current . next = temp
        current = current . prev
    if ( temp != None ) :
        head_ref = temp . prev
        return head_ref
def merge ( first , second ) :
    if ( first == None ) :
        return second
    if ( second == None ) :
        return first
    if ( first . data < second . data ) :
        first . next = merge ( first . next , second )
        first . next . prev = first
        first . prev = None
        return first
    else :
        second . next = merge ( first , second . next )
        second . next . prev = second
        second . prev = None
        return second
def sort ( head ) :
    if ( head == None or head . next == None ) :
        return head
    current = head . next
    while ( current != None ) :
        if ( current . data < current . prev . data ) :
            break
        current = current . next
    if ( current == None ) :
        return head
    current . prev . next = None
    current . prev = None
    current = reverse ( current )
    return merge ( head , current )
def push ( head_ref , new_data ) :
    new_node = Node ( )
    new_node . data = new_data
    new_node . prev = None
    new_node . next = ( head_ref )
    if ( ( head_ref ) != None ) :
        ( head_ref ) . prev = new_node
    ( head_ref ) = new_node
    return head_ref
def printList ( head ) :
    if ( head == None ) :
        print ( " Doubly ▁ Linked ▁ list ▁ empty " )
    while ( head != None ) :
        print ( head . data , end = " ▁ " )
        head = head . next
head = None
head = push ( head , 1 )
head = push ( head , 4 )
head = push ( head , 6 )
head = push ( head , 10 )
head = push ( head , 12 )
head = push ( head , 7 )
head = push ( head , 5 )
head = push ( head , 2 )
print ( " Original ▁ Doubly ▁ linked ▁ list : n " )
printList ( head )
head = sort ( head )
print ( " Doubly linked list after sorting : " )
printList ( head )

Class/struct names: Node
Python program:
class Node :
    def __init__ ( self , x ) :
        self . data = x
        self . next = None
def printlist ( head ) :
    if ( not head ) :
        print ( " Empty ▁ List " )
        return
    while ( head != None ) :
        print ( head . data , end = " ▁ " )
        if ( head . next ) :
            print ( end = " - > ▁ " )
        head = head . next
    print ( )
def isVowel ( x ) :
    return ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' or x == ' A ' or x == ' E ' or x == ' I ' or x == ' O ' or x == ' U ' )
def arrange ( head ) :
    newHead = head
    latestVowel = None
    curr = head
    if ( head == None ) :
        return None
    if ( isVowel ( head . data ) ) :
        latestVowel = head
    else :
        while ( curr . next != None and not isVowel ( curr . next . data ) ) :
            curr = curr . next
        if ( curr . next == None ) :
            return head
        latestVowel = newHead = curr . next
        curr . next = curr . next . next
        latestVowel . next = head
    while ( curr != None and curr . next != None ) :
        if ( isVowel ( curr . next . data ) ) :
            if ( curr == latestVowel ) :
                latestVowel = curr = curr . next
            else :
                temp = latestVowel . next
                latestVowel . next = curr . next
                latestVowel = latestVowel . next
                curr . next = curr . next . next
                latestVowel . next = temp
        else :
            curr = curr . next
    return newHead
if __name__ == ' _ _ main _ _ ' :
    head = Node ( ' a ' )
    head . next = Node ( ' b ' )
    head . next . next = Node ( ' c ' )
    head . next . next . next = Node ( ' e ' )
    head . next . next . next . next = Node ( ' d ' )
    head . next . next . next . next . next = Node ( ' o ' )
    head . next . next . next . next . next . next = Node ( ' x ' )
    head . next . next . next . next . next . next . next = Node ( ' i ' )
    print ( " Linked ▁ list ▁ before ▁ : " )
    printlist ( head )
    head = arrange ( head )
    print ( " Linked ▁ list ▁ after ▁ : " )
    printlist ( head )

Class/struct names: Node
Python program:
class newNode :
    def __init__ ( self , data ) :
        self . data = data
        self . right = self . left = None
def KthLargestUsingMorrisTraversal ( root , k ) :
    curr = root
    Klargest = None
    count = 0
    while ( curr != None ) :
        if ( curr . right == None ) :
            count += 1
            if ( count == k ) :
                Klargest = curr
            curr = curr . left
        else :
            succ = curr . right
            while ( succ . left != None and succ . left != curr ) :
                succ = succ . left
            if ( succ . left == None ) :
                succ . left = curr
                curr = curr . right
            else :
                succ . left = None
                count += 1
                if ( count == k ) :
                    Klargest = curr
                curr = curr . left
    return Klargest
if __name__ == ' _ _ main _ _ ' :
    root = newNode ( 4 )
    root . left = newNode ( 2 )
    root . right = newNode ( 7 )
    root . left . left = newNode ( 1 )
    root . left . right = newNode ( 3 )
    root . right . left = newNode ( 6 )
    root . right . right = newNode ( 10 )
    print ( " Finding ▁ K - th ▁ largest ▁ Node ▁ in ▁ BST ▁ : ▁ " , KthLargestUsingMorrisTraversal ( root , 2 ) . data )

Accumulator variables: count
Class/struct names: newNode
Python program:
MAX_SIZE = 10
def sortByRow ( mat , n , ascending ) :
    for i in range ( n ) :
        if ( ascending ) :
            mat [ i ] . sort ( )
        else :
            mat [ i ] . sort ( reverse = True )
def transpose ( mat , n ) :
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            temp = mat [ i ] [ j ]
            mat [ i ] [ j ] = mat [ j ] [ i ]
            mat [ j ] [ i ] = temp
def sortMatRowAndColWise ( mat , n ) :
    sortByRow ( mat , n , True )
    transpose ( mat , n )
    sortByRow ( mat , n , False )
    transpose ( mat , n )
def printMat ( mat , n ) :
    for i in range ( n ) :
        for j in range ( n ) :
            print ( mat [ i ] [ j ] , " ▁ " , end = " " )
        print ( )
n = 3
mat = [ [ 3 , 2 , 1 ] , [ 9 , 8 , 7 ] , [ 6 , 5 , 4 ] ]
print ( " Original ▁ Matrix : " )
printMat ( mat , n )
sortMatRowAndColWise ( mat , n )
print ( " Matrix ▁ After ▁ Sorting : " )
printMat ( mat , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
MAX_SIZE = 10
def sortByRow ( mat , n ) :
    for i in range ( n ) :
        for j in range ( n - 1 ) :
            if mat [ i ] [ j ] > mat [ i ] [ j + 1 ] :
                temp = mat [ i ] [ j ]
                mat [ i ] [ j ] = mat [ i ] [ j + 1 ]
                mat [ i ] [ j + 1 ] = temp
def transpose ( mat , n ) :
    for i in range ( n ) :
        for j in range ( i + 1 , n ) :
            t = mat [ i ] [ j ]
            mat [ i ] [ j ] = mat [ j ] [ i ]
            mat [ j ] [ i ] = t
def sortMatRowAndColWise ( mat , n ) :
    sortByRow ( mat , n )
    transpose ( mat , n )
    sortByRow ( mat , n )
    transpose ( mat , n )
def printMat ( mat , n ) :
    for i in range ( n ) :
        for j in range ( n ) :
            print ( str ( mat [ i ] [ j ] ) , end = " ▁ " )
        print ( ) ;
mat = [ [ 4 , 1 , 3 ] , [ 9 , 6 , 8 ] , [ 5 , 2 , 7 ] ]
n = 3
print ( " Original ▁ Matrix : " )
printMat ( mat , n )
sortMatRowAndColWise ( mat , n )
print ( " Matrix After Sorting : " )
printMat ( mat , n )

Index/key variables: i, j
Iterator variables: i, j
Python program:
cola = 2
rowa = 3
colb = 3
rowb = 2
def Kroneckerproduct ( A , B ) :
    C = [ [ 0 for j in range ( cola * colb ) ] for i in range ( rowa * rowb ) ]
    for i in range ( 0 , rowa ) :
        for k in range ( 0 , rowb ) :
            for j in range ( 0 , cola ) :
                for l in range ( 0 , colb ) :
                    C [ i + l + 1 ] [ j + k + 1 ] = A [ i ] [ j ] * B [ k ] [ l ]
                    print ( C [ i + l + 1 ] [ j + k + 1 ] , end = ' ▁ ' )
            print ( " " )
A = [ [ 0 for j in range ( 2 ) ] for i in range ( 3 ) ]
B = [ [ 0 for j in range ( 3 ) ] for i in range ( 2 ) ]
A [ 0 ] [ 0 ] = 1
A [ 0 ] [ 1 ] = 2
A [ 1 ] [ 0 ] = 3
A [ 1 ] [ 1 ] = 4
A [ 2 ] [ 0 ] = 1
A [ 2 ] [ 1 ] = 0
B [ 0 ] [ 0 ] = 0
B [ 0 ] [ 1 ] = 5
B [ 0 ] [ 2 ] = 2
B [ 1 ] [ 0 ] = 6
B [ 1 ] [ 1 ] = 7
B [ 1 ] [ 2 ] = 3
Kroneckerproduct ( A , B )

Index/key variables: i, j, k, l
Iterator variables: i, j, k, l
Python program:
def islowertriangular ( M ) :
    for i in range ( 0 , len ( M ) ) :
        for j in range ( i + 1 , len ( M ) ) :
            if ( M [ i ] [ j ] != 0 ) :
                return False
    return True
M = [ [ 1 , 0 , 0 , 0 ] , [ 1 , 4 , 0 , 0 ] , [ 4 , 6 , 2 , 0 ] , [ 0 , 4 , 7 , 6 ] ]
if islowertriangular ( M ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j
Iterator variables: i, j
Python program:
def isuppertriangular ( M ) :
    for i in range ( 1 , len ( M ) ) :
        for j in range ( 0 , i ) :
            if ( M [ i ] [ j ] != 0 ) :
                return False
    return True
M = [ [ 1 , 3 , 5 , 3 ] , [ 0 , 4 , 6 , 2 ] , [ 0 , 0 , 2 , 5 ] , [ 0 , 0 , 0 , 6 ] ]
if isuppertriangular ( M ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j
Iterator variables: i, j
Python program:
m = 3
n = 2
def countSets ( a ) :
    res = 0
    for i in range ( n ) :
        u = 0
        v = 0
        for j in range ( m ) :
            if a [ i ] [ j ] :
                u += 1
            else :
                v += 1
        res += pow ( 2 , u ) - 1 + pow ( 2 , v ) - 1
    for i in range ( m ) :
        u = 0
        v = 0
        for j in range ( n ) :
            if a [ j ] [ i ] :
                u += 1
            else :
                v += 1
        res += pow ( 2 , u ) - 1 + pow ( 2 , v ) - 1
    return res - ( n * m )
a = [ [ 1 , 0 , 1 ] , [ 0 , 1 , 0 ] ]
print ( countSets ( a ) )

Index/key variables: i, j
Accumulator variables: res, u, v
Iterator variables: i, j
Python program:
import math
MAX = 100 ;
def findNormal ( mat , n ) :
    sum = 0 ;
    for i in range ( n ) :
        for j in range ( n ) :
            sum += mat [ i ] [ j ] * mat [ i ] [ j ] ;
    return math . floor ( math . sqrt ( sum ) ) ;
def findTrace ( mat , n ) :
    sum = 0 ;
    for i in range ( n ) :
        sum += mat [ i ] [ i ] ;
    return sum ;
mat = [ [ 1 , 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 , 4 ] , [ 5 , 5 , 5 , 5 , 5 ] ] ;
print ( " Trace ▁ of ▁ Matrix ▁ = " , findTrace ( mat , 5 ) ) ;
print ( " Normal ▁ of ▁ Matrix ▁ = " , findNormal ( mat , 5 ) ) ;

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
def maxDet ( n ) :
    return 2 * n * n * n
def resMatrix ( n ) :
    for i in range ( 3 ) :
        for j in range ( 3 ) :
            if i == 0 and j == 2 :
                print ( "0" , end = " ▁ " )
            elif i == 1 and j == 0 :
                print ( "0" , end = " ▁ " )
            elif i == 2 and j == 1 :
                print ( "0" , end = " ▁ " )
            else :
                print ( n , end = " ▁ " )
        print ( " " )
n = 15
print ( " Maximum ▁ Detrminat = " , maxDet ( n ) )
print ( " Resultant ▁ Matrix : " )
resMatrix ( n )

Iterator variables: i, j
Python program:
def countNegative ( M , n , m ) :
    count = 0
    for i in range ( n ) :
        for j in range ( m ) :
            if M [ i ] [ j ] < 0 :
                count += 1
            else :
                break
    return count
M = [ [ - 3 , - 2 , - 1 , 1 ] , [ - 2 , 2 , 3 , 4 ] , [ 4 , 5 , 7 , 8 ] ]
print ( countNegative ( M , 3 , 4 ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i, j
Python program:
def countNegative ( M , n , m ) :
    count = 0
    i = 0
    j = m - 1
    while j >= 0 and i < n :
        if M [ i ] [ j ] < 0 :
            count += ( j + 1 )
            i += 1
        else :
            j -= 1
    return count
M = [ [ - 3 , - 2 , - 1 , 1 ] , [ - 2 , 2 , 3 , 4 ] , [ 4 , 5 , 7 , 8 ] ]
print ( countNegative ( M , 3 , 4 ) )

Index/key variables: i, j
Accumulator variables: count
Python program:
def getLastNegativeIndex ( array , start , end , n ) :
    if ( start == end ) :
        return start
    mid = start + ( end - start ) // 2
    if ( array [ mid ] < 0 ) :
        if ( mid + 1 < n and array [ mid + 1 ] >= 0 ) :
            return mid
        return getLastNegativeIndex ( array , mid + 1 , end , n )
    else :
        return getLastNegativeIndex ( array , start , mid - 1 , n )
def countNegative ( M , n , m ) :
    count = 0
    nextEnd = m - 1
    for i in range ( n ) :
        if ( M [ i ] [ 0 ] >= 0 ) :
            break
        nextEnd = getLastNegativeIndex ( M [ i ] , 0 , nextEnd , 4 )
        count += nextEnd + 1
    return count
M = [ [ - 3 , - 2 , - 1 , 1 ] , [ - 2 , 2 , 3 , 4 ] , [ 4 , 5 , 7 , 8 ] ]
r = 3
c = 4
print ( countNegative ( M , r , c ) )

Index/key variables: i, mid
Accumulator variables: count
Iterator variables: i
Python program:
N = 5
def findMaxValue ( mat ) :
    maxValue = 0
    for a in range ( N - 1 ) :
        for b in range ( N - 1 ) :
            for d in range ( a + 1 , N ) :
                for e in range ( b + 1 , N ) :
                    if maxValue < int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) :
                        maxValue = int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ;
    return maxValue ;
mat = [ [ 1 , 2 , - 1 , - 4 , - 20 ] , [ - 8 , - 3 , 4 , 2 , 1 ] , [ 3 , 8 , 6 , 1 , 3 ] , [ - 4 , - 1 , 1 , 7 , - 6 ] , [ 0 , - 4 , 10 , - 5 , 1 ] ] ;
print ( " Maximum ▁ Value ▁ is ▁ " + str ( findMaxValue ( mat ) ) )

Index/key variables: a, b, d, e
Iterator variables: a, b, d, e
Python program:
import sys
N = 5
def findMaxValue ( mat ) :
    maxValue = - sys . maxsize - 1
    maxArr = [ [ 0 for x in range ( N ) ] for y in range ( N ) ]
    maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ]
    maxv = mat [ N - 1 ] [ N - 1 ] ;
    for j in range ( N - 2 , - 1 , - 1 ) :
        if ( mat [ N - 1 ] [ j ] > maxv ) :
            maxv = mat [ N - 1 ] [ j ]
        maxArr [ N - 1 ] [ j ] = maxv
    maxv = mat [ N - 1 ] [ N - 1 ] ;
    for i in range ( N - 2 , - 1 , - 1 ) :
        if ( mat [ i ] [ N - 1 ] > maxv ) :
            maxv = mat [ i ] [ N - 1 ]
        maxArr [ i ] [ N - 1 ] = maxv
    for i in range ( N - 2 , - 1 , - 1 ) :
        for j in range ( N - 2 , - 1 , - 1 ) :
            if ( maxArr [ i + 1 ] [ j + 1 ] - mat [ i ] [ j ] > maxValue ) :
                maxValue = ( maxArr [ i + 1 ] [ j + 1 ] - mat [ i ] [ j ] )
            maxArr [ i ] [ j ] = max ( mat [ i ] [ j ] , max ( maxArr [ i ] [ j + 1 ] , maxArr [ i + 1 ] [ j ] ) )
    return maxValue
mat = [ [ 1 , 2 , - 1 , - 4 , - 20 ] , [ - 8 , - 3 , 4 , 2 , 1 ] , [ 3 , 8 , 6 , 1 , 3 ] , [ - 4 , - 1 , 1 , 7 , - 6 ] , [ 0 , - 4 , 10 , - 5 , 1 ] ]
print ( " Maximum ▁ Value ▁ is " , findMaxValue ( mat ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
import sys
INF = sys . maxsize
N = 4
def youngify ( mat , i , j ) :
    downVal = mat [ i + 1 ] [ j ] if ( i + 1 < N ) else INF
    rightVal = mat [ i ] [ j + 1 ] if ( j + 1 < N ) else INF
    if ( downVal == INF and rightVal == INF ) :
        return
    if ( downVal < rightVal ) :
        mat [ i ] [ j ] = downVal
        mat [ i + 1 ] [ j ] = INF
        youngify ( mat , i + 1 , j )
    else :
        mat [ i ] [ j ] = rightVal
        mat [ i ] [ j + 1 ] = INF
        youngify ( mat , i , j + 1 )
def extractMin ( mat ) :
    ret = mat [ 0 ] [ 0 ]
    mat [ 0 ] [ 0 ] = INF
    youngify ( mat , 0 , 0 )
    return ret
def printSorted ( mat ) :
    print ( " Elements ▁ of ▁ matrix ▁ in ▁ sorted ▁ order ▁ n " )
    i = 0
    while i < N * N :
        print ( extractMin ( mat ) , end = " ▁ " )
        i += 1
if __name__ == " _ _ main _ _ " :
    mat = [ [ 10 , 20 , 30 , 40 ] , [ 15 , 25 , 35 , 45 ] , [ 27 , 29 , 37 , 48 ] , [ 32 , 33 , 39 , 50 ] ]
    printSorted ( mat )

Index/key variables: i, j