giulio98/xlcost-single-prompt huggingface dataset:
# minimum number of single digit primes required whose sum is equal to n | function to check if i - th index is valid or not ; function to find the minimum number of single digit prime numbers required which when summed up equals to a given number n . ; not possible ; 
def check(i, val):
    if i - val < 0:
        return False
    return True


def MinimumPrimes(n):
    dp = [10 ** 9] * (n + 1)
    dp[0] = dp[2] = dp[3] = dp[5] = dp[7] = 1
    for i in range(1, n + 1):
        if check(i, 2):
            dp[i] = min(dp[i], 1 + dp[i - 2])
        if check(i, 3):
            dp[i] = min(dp[i], 1 + dp[i - 3])
        if check(i, 5):
            dp[i] = min(dp[i], 1 + dp[i - 5])
        if check(i, 7):
            dp[i] = min(dp[i], 1 + dp[i - 7])
    if dp[n] == 10 ** 9:
        return -1
    else:
        return dp[n]


if __name__ == "__main__":
    n = 12
    minimal = MinimumPrimes(n)
    if minimal != -1:
        print("Minimum number of single digit primes required : ", minimal)
    else:
        print("Not possible")

# Expected output:
Minimum number of single digit primes required :  2
# find all distinct subset ( or subsequence ) sums of an array | set | function to print all th distinct sum ; declare a boolean array of size equal to total sum of the array ; fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; iterate from maxsum to 1 and avoid lookup on any other row ; do not change the dp array for j less than arr [ i ] ; if dp [ j ] is true then print ; function to find the total sum and print the distinct sum ; find the sum of array elements ; function to print all the distinct sum ; 
def subsetSum(arr, n, maxSum):
    dp = [False for i in range(maxSum + 1)]
    dp[arr[0]] = True
    for i in range(1, n, 1):
        j = maxSum
        while (j >= 1):
            if (arr[i] <= j):
                if (arr[i] == j or dp[j] or dp[(j - arr[i])]):
                    dp[j] = True
                else:
                    dp[j] = False
            j -= 1
    print(0, end=" ")
    for j in range(maxSum + 1):
        if (dp[j]):
            print(j, end=" ")
    print("21")


def printDistinct(a, n):
    maxSum = 0
    for i in range(n):
        maxSum += a[i]
    subsetSum(a, n, maxSum)


if __name__ == '__main__':
    arr = [2, 3, 4, 5, 6]
    n = len(arr)
    printDistinct(arr, n)

# Expected output:
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21
# sudo placement [ 1.5 ] | wolfish | python program for sp - wolfish ; function to find the maxcost of path from ( n - 1 , n - 1 ) to ( 0 , 0 ) | recursive approach ; base condition ; reaches the point ; i + j ; check if it is a power of 2 , then only move diagonally ; if not a power of 2 then move side - wise ; function to return the maximum cost ; calling dp function to get the answer ; 

size = 1000


def maxCost(a: list, m: int, n: int) -> int:
    if n < 0 or m < 0:
        return int(-1e9)
    elif m == 0 and n == 0:
        return 0
    else:
        num = m + n
        if (num & (num - 1)) == 0:
            return a[m][n] + maxCost(a, m - 1, n - 1)
        else:
            return a[m][n] + max(maxCost(a, m - 1, n), maxCost(a, m, n - 1))


def answer(a: list, n: int) -> int:
    return maxCost(a, n - 1, n - 1)


if __name__ == "__main__":
    a = [[1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1], [1, 1, 1, 1]]
    n = 4
    print(answer(a, n))

# Expected output:
16
# longest common subsequence | dp using memoization | python3 program to memoize recursive implementation of lcs problem ; returns length of lcs for x [ 0. . m - 1 ] , y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; 

maximum = 1000


def lcs(X, Y, m, n, dp):
    if (m == 0 or n == 0):
        return 0
    if (dp[m - 1][n - 1] != -1):
        return dp[m - 1][n - 1]
    if (X[m - 1] == Y[n - 1]):
        dp[m - 1][n - 1] = 1 + lcs(X, Y, m - 1, n - 1, dp)
        return dp[m - 1][n - 1]
    else:
        dp[m - 1][n - 1] = max(lcs(X, Y, m, n - 1, dp),
                               lcs(X, Y, m - 1, n, dp))
        return dp[m - 1][n - 1]


X = "AGGTAB"
Y = "GXTXAYB"
m = len(X)
n = len(Y)
dp = [[-1 for i in range(maximum)]for i in range(m)]
print("Length of LCS:", lcs(X, Y, m, n, dp))

# Expected output:
Length of LCS: 4
# stepping numbers | prints all stepping numbers reachable from num and in range [ n , m ] ; if stepping number is in the range [ n , m ] then display ; if stepping number is 0 or greater than m , then return ; get the last digit of the currently visited stepping number ; there can be 2 cases either digit to be appended is lastdigit + 1 or lastdigit - 1 ; if lastdigit is 0 then only possible digit after 0 can be 1 for a stepping number ; if lastdigit is 9 then only possible digit after 9 can be 8 for a stepping number ; method displays all the stepping numbers in range [ n , m ] ; for every single digit number ' i ' find all the stepping numbers starting with i ; 
def dfs(n, m, stepNum):
    if (stepNum <= m and stepNum >= n):
        print(stepNum, end=" ")
    if (stepNum == 0 or stepNum > m):
        return
    lastDigit = stepNum % 10
    stepNumA = stepNum * 10 + (lastDigit - 1)
    stepNumB = stepNum * 10 + (lastDigit + 1)
    if (lastDigit == 0):
        dfs(n, m, stepNumB)
    elif (lastDigit == 9):
        dfs(n, m, stepNumA)
    else:
        dfs(n, m, stepNumA)
        dfs(n, m, stepNumB)


def displaySteppingNumbers(n, m):
    for i in range(10):
        dfs(n, m, i)


n, m = 0, 21
displaySteppingNumbers(n, m)

# Expected output:
0 1 10 12 2 21 3 4 5 6 7 8 9
# memoization ( 1d , 2d and 3d ) | returns length of lcs for x [ 0. . m - 1 ] , y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ;  ; 
def lcs(X, Y, m, n):
    global arr
    if (m == 0 or n == 0):
        return 0
    if (arr[m - 1][n - 1] != -1):
        return arr[m - 1][n - 1]
    if (X[m - 1] == Y[n - 1]):
        arr[m - 1][n - 1] = 1 + lcs(X, Y, m - 1, n - 1)
        return arr[m - 1][n - 1]
    else:
        arr[m - 1][n - 1] = max(lcs(X, Y, m, n - 1), lcs(X, Y, m - 1, n))
        return arr[m - 1][n - 1]


arr = [[0] * 1000] * 1000
for i in range(0, 1000):
    for j in range(0, 1000):
        arr[i][j] = -1
X = "AGGTAB"
Y = "GXTXAYB"
m = len(X)
n = len(Y)
print("Length of LCS is ", lcs(X, Y, m, n))

# Expected output:
Length of LCS is  4
# number of unique bst with a given key | dynamic programming | function to find number of unique bst ; dp to store the number of unique bst with key i ; base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; 
def numberOfBST(n):
    dp = [0] * (n + 1)
    dp[0], dp[1] = 1, 1
    for i in range(2, n + 1):
        for j in range(1, i + 1):
            dp[i] = dp[i] + (dp[i - j] * dp[j - 1])
    return dp[n]


if __name__ == "__main__":
    n = 3
    print("Number of structurally Unique BST with",
          n, "keys are :", numberOfBST(n))

# Expected output:
Number of structurally Unique BST with 3 keys are : 5
# minimum splits in a binary string such that every substring is a power of 4 or 6. | python 3 program for minimum splits in a string such that substring is a power of 4 or 6. ; function to find if given number is power of another number or not . ; divide given number repeatedly by base value . ; return false not a power ; function to find minimum number of partitions of given binary string so that each partition is power of 4 or 6. ; dp table to store results of partitioning done at differentindices . ; if the last digit is 1 , hence 4 ^ 0 = 1 and 6 ^ 0 = 1 ; fix starting position for partition ; binary representation with leading zeroes is not allowed . ; iterate for all different partitions starting from i ; find integer value of current binary partition . ; check if the value is a power of 4 or 6 or not apply recurrence relation ; if no partitions are possible , then make dp [ i ] = - 1 to represent this . ; 

import sys


def isPowerOf(val, base):
    while (val > 1):
        if (val % base != 0):
            return False
        val //= base

    return True


def numberOfPartitions(binaryNo):

    n = len(binaryNo)
    dp = [0] * n
    if ((ord(binaryNo[n - 1]) - ord('0')) == 0):
        dp[n - 1] = -1
    else:
        dp[n - 1] = 1
    for i in range(n - 2, -1, -1):
        val = 0
        if ((ord(binaryNo[i]) - ord('0')) == 0):
            dp[i] = -1
            continue

        dp[i] = sys.maxsize
        for j in range(i, n):
            val = (val * 2) + (ord(binaryNo[j]) - ord('0'))
            if (isPowerOf(val, 4) or isPowerOf(val, 6)):
                if (j == n - 1):
                    dp[i] = 1

                else:
                    if (dp[j + 1] != -1):
                        dp[i] = min(dp[i], dp[j + 1] + 1)
        if (dp[i] == sys.maxsize):
            dp[i] = -1

    return dp[0]


if __name__ == "__main__":
    binaryNo = "100110110"
    print(numberOfPartitions(binaryNo))

# Expected output:
3
# sum of product of r and rth binomial coefficient ( r * ncr ) | return summation of r * ncr ; 
def summation(n):
    return n << (n - 1)


n = 2
print(summation(n))

# Expected output:
4
# maximum average sum partition of an array | python3 program for maximum average sum partition ; bottom up approach to calculate score ; storing averages from starting to each i ; ; 

MAX = 1000
memo = [[0.0 for i in range(MAX)]for i in range(MAX)]


def score(n, A, k):
    if (memo[n][k] > 0):
        return memo[n][k]
    sum = 0
    i = n - 1
    while (i > 0):
        sum += A[i]
        memo[n][k] = max(memo[n][k], score(i, A, k - 1) + int(sum / (n - i)))
        i -= 1
    return memo[n][k]


def largestSumOfAverages(A, K):
    n = len(A)
    sum = 0
    for i in range(n):
        sum += A[i]
        memo[i + 1][1] = int(sum / (i + 1))
    return score(n, A, K)


if __name__ == '__main__':
    A = [9, 1, 2, 3, 9]
    K = 3
    print(largestSumOfAverages(A, K))

# Expected output:
20
# maximum and minimum values of an algebraic expression | python3 program to find the maximum and minimum values of an algebraic expression of given form ; finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by 50 to avoid negative indexing in array ; 
def minMaxValues(arr, n, m):
    sum = 0
    INF = 1000000000
    MAX = 50
    for i in range(0, (n + m)):
        sum += arr[i]
        arr[i] += 50
    dp = [[0 for x in range(MAX * MAX + 1)]for y in range(MAX + 1)]
    dp[0][0] = 1
    for i in range(0, (n + m)):
        for k in range(min(n, i + 1), 0, -1):
            for j in range(0, MAX * MAX + 1):
                if (dp[k - 1][j]):
                    dp[k][j + arr[i]] = 1
    max_value = -1 * INF
    min_value = INF
    for i in range(0, MAX * MAX + 1):
        if (dp[n][i]):
            temp = i - 50 * n
            max_value = max(max_value, temp * (sum - temp))
            min_value = min(min_value, temp * (sum - temp))
    print("MaximumValue:{}MinimumValue:{}".format(max_value, min_value))


n = 2
m = 2
arr = [1, 2, 3, 4]
minMaxValues(arr, n, m)

# Expected output:
MaximumValue:25MinimumValue:21
# check if any valid sequence is divisible by m | python3 program to check if any valid sequence is divisible by m ; base case ; check if sum is divisible by m ; check if the current state is already computed ; 1. try placing '+ ; 2. try placing '- ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; 
def isPossible(n, index, Sum, M, arr, dp):
    global MAX
    if index == n:
        if (Sum % M) == 0:
            return True
        return False
    if dp[index][Sum] != -1:
        return dp[index][Sum]
    placeAdd = isPossible(n, index + 1,
                          Sum + arr[index], M, arr, dp)
    placeMinus = isPossible(n, index + 1,
                            Sum - arr[index], M, arr, dp)
    res = placeAdd or placeMinus
    dp[index][Sum] = res
    return res


MAX = 1000
arr = [1, 2, 3, 4, 6]
n = len(arr)
M = 4
dp = [[-1]*MAX for i in range(n+1)]
res = isPossible(n, 0, 0, M, arr, dp)

if res:
    print(True)
else:
    print(False)

# Expected output:
True
# dynamic programming on trees | set 2 | python3 code to find the maximum path length considering any node as root ; function to pre - calculate the array inn [ ] which stores the maximum height when travelled via branches ; initially every node has 0 height ; traverse in the subtree of u ; if child is same as parent ; dfs called ; recursively calculate the max height ; function to pre - calculate the array ouut [ ] which stores the maximum height when traveled via parent ; stores the longest and second longest branches ; traverse in the subtress of u ; compare and store the longest and second longest ; traverse in the subtree of u ; if longest branch has the node , then consider the second longest branch ; recursively calculate out [ i ] ; dfs function call ; function to prall the maximum heights from every node ; traversal to calculate inn [ ] array ; traversal to calculate out [ ] array ; prall maximum heights ; 

inn = [0] * 100
out = [0] * 100


def dfs1(v, u, parent):
    global inn, out
    inn[u] = 0
    for child in v[u]:
        if (child == parent):
            continue
        dfs1(v, child, u)
        inn[u] = max(inn[u], 1 + inn[child])


def dfs2(v, u, parent):
    global inn, out
    mx1, mx2 = -1, -1
    for child in v[u]:
        if (child == parent):
            continue
        if (inn[child] >= mx1):
            mx2 = mx1
            mx1 = inn[child]
        elif (inn[child] > mx2):
            mx2 = inn[child]
    for child in v[u]:
        if (child == parent):
            continue
        longest = mx1
        if (mx1 == inn[child]):
            longest = mx2
        out[child] = 1 + max(out[u], 1 + longest)
        dfs2(v, child, u)


def printHeights(v, n):
    global inn, out
    dfs1(v, 1, 0)
    dfs2(v, 1, 0)
    for i in range(1, n + 1):
        print("The maximum height when node", i,
              "is considered as root is", max(inn[i], out[i]))


if __name__ == '__main__':
    n = 11
    v = [[]for i in range(n + 1)]
    v[1].append(2)
    v[2].append(1)
    v[1].append(3)
    v[3].append(1)
    v[1].append(4)
    v[4].append(1)
    v[2].append(5)
    v[5].append(2)
    v[2].append(6)
    v[6].append(2)
    v[3].append(7)
    v[7].append(3)
    v[7].append(10)
    v[10].append(7)
    v[7].append(11)
    v[11].append(7)
    v[4].append(8)
    v[8].append(4)
    v[4].append(9)
    v[9].append(4)
    printHeights(v, n)

# Expected output:
The maximum height when node 1 is considered as root is 3
The maximum height when node 2 is considered as root is 4
The maximum height when node 3 is considered as root is 3
The maximum height when node 4 is considered as root is 4
The maximum height when node 5 is considered as root is 5
The maximum height when node 6 is considered as root is 5
The maximum height when node 7 is considered as root is 4
The maximum height when node 8 is considered as root is 5
The maximum height when node 9 is considered as root is 5
The maximum height when node 10 is considered as root is 5
The maximum height when node 11 is considered as root is 5
# golomb sequence | return the nth element of golomb sequence ; base case ; recursive step ; print the first n term of golomb sequence ; finding first n terms of golomb sequence . ; 
def findGolomb(n):
    if (n == 1):
        return 1
    return 1 + findGolomb(n - findGolomb(findGolomb(n - 1)))


def printGolomb(n):
    for i in range(1, n + 1):
        print(findGolomb(i), end=" ")


n = 9
printGolomb(n)

# Expected output:
1 2 2 3 3 4 4 4 5
# printing items in 0 / 1 knapsack | prints the items which are put in a knapsack of capacity w ; build table k [ ] [ ] in bottom up manner ; stores the result of knapsack ; either the result comes from the top ( k [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in knapsack table . if it comes from the latter one / it means the item is included . ; this item is included . ; since this weight is included its value is deducted ; 
def printknapSack(W, wt, val, n):
    K = [[0 for w in range(W + 1)]for i in range(n + 1)]
    for i in range(n + 1):
        for w in range(W + 1):
            if i == 0 or w == 0:
                K[i][w] = 0
            elif wt[i - 1] <= w:
                K[i][w] = max(val[i - 1] + K[i - 1]
                              [w - wt[i - 1]], K[i - 1][w])
            else:
                K[i][w] = K[i - 1][w]
    res = K[n][W]
    print(res)
    w = W
    for i in range(n, 0, -1):
        if res <= 0:
            break
        if res == K[i - 1][w]:
            continue
        else:
            print(wt[i - 1])
            res = res - val[i - 1]
            w = w - wt[i - 1]


val = [60, 100, 120]
wt = [10, 20, 30]
W = 50
n = len(val)
printknapSack(W, wt, val, n)

# Expected output:
220
30
20
# s | to sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; as dp [ 0 ] = 0 ( base case ) so min no of elements to be removed are n - 1 elements ; iterate from 1 to n - 1 ; 
def removals(arr, n, k):
    arr .sort()
    dp = [0 for i in range(n)]
    for i in range(n):
        dp[i] = -1
    ans = n - 1
    dp[0] = 0
    for i in range(1, n):
        dp[i] = i
        j = dp[i - 1]
        while (j != i and arr[i] - arr[j] > k):
            j += 1
            dp[i] = min(dp[i], j)
            ans = min(ans, (n - (i - j + 1)))
    return ans


a = [1, 3, 4, 9, 10, 11, 12, 17, 20]
n = len(a)
k = 4
print(removals(a, n, k))

# Expected output:
8
# maximum number of segments of lengths a , b and c | function to find the maximum number of segments ; stores the maximum number of segments each index can have ;  ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( i + a <= n ) : avoid buffer overflow ; if ( i + b <= n ) : avoid buffer overflow ; if ( i + c <= n ) : avoid buffer overflow ; 
def maximumSegments(n, a, b, c):
    dp = [-1] * (n + 10)

    dp[0] = 0
    for i in range(0, n):
        if (dp[i] != -1):
            dp[i + a] = max(dp[i] + 1, dp[i + a])
            dp[i + b] = max(dp[i] + 1, dp[i + b])
            dp[i + c] = max(dp[i] + 1, dp[i + c])
    return dp[n]


n = 7
a = 5
b = 2
c = 5
print((maximumSegments(n, a, b, c)))

# Expected output:
2
# maximize the sum of selected numbers from an array to make it empty | function to maximize the sum of selected numbers ; maximum in the sequence ; stores the occurrences of the numbers ; marks the occurrence of every number in the sequence ; ans to store the result ; using the above mentioned approach ; if occurence is greater than 0 ; add it to ans ; decrease i - 1 th element by 1 ; decrease ith element by 1 ; decrease i ; 
def maximizeSum(a, n):
    maximum = max(a)
    ans = dict .fromkeys(range(0, n + 1), 0)
    for i in range(n):
        ans[a[i]] += 1
    result = 0
    i = maximum
    while i > 0:
        if ans[i] > 0:
            result += i
            ans[i - 1] -= 1
            ans[i] -= 1
        else:
            i -= 1
    return result


if __name__ == "__main__":
    a = [1, 2, 3]
    n = len(a)
    print(maximizeSum(a, n))

# Expected output:
4
# print n terms of newman | function to find the n - th element ; declare array to store sequence ; 
def sequence(n):
    f = [0, 1, 1]
    print(f[1], end=" "),
    print(f[2], end=" "),
    for i in range(3, n + 1):
        f .append(f[f[i - 1]] + f[i - f[i - 1]])
        print(f[i], end=" "),


n = 13
sequence(n)

# Expected output:
1 1 2 2 3 4 4 4 5 6 7 7 8
# lcs formed by consecutive segments of at least length k | returns the length of the longest common subsequence with a minimum of length of k consecutive segments ; length of strings ; declare the lcs and cnt array ; iterate from i = 1 to n and j = 1 to j = m ; stores the maximum of lcs [ i - 1 ] [ j ] and lcs [ i ] [ j - 1 ] ; when both the characters are equal of s1 and s2 ; when length of common segment is more than k , then update lcs answer by adding that segment to the answer ; formulate for all length of segments to get the longest subsequence with consecutive common segment of length of min k length ; update lcs value by adding segment length ; 
def longestSubsequenceCommonSegment(k, s1, s2):
    n = len(s1)
    m = len(s2)
    lcs = [[0 for x in range(m + 1)]for y in range(n + 1)]
    cnt = [[0 for x in range(m + 1)]for y in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1])
            if (s1[i - 1] == s2[j - 1]):
                cnt[i][j] = cnt[i - 1][j - 1] + 1
            if (cnt[i][j] >= k):
                for a in range(k, cnt[i][j] + 1):
                    lcs[i][j] = max(lcs[i][j], lcs[i - a][j - a] + a)
    return lcs[n][m]


k = 4
s1 = "aggasdfa"
s2 = "aggajasdfa"
print(longestSubsequenceCommonSegment(k, s1, s2))

# Expected output:
8
# check for possible path in 2d matrix | python3 program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; directions ; queue ; insert the top right corner . ; until queue is empty ; mark as visited ; destination is reached . ; check all four directions ; using the direction array ; not blocked and valid ; 

row = 5
col = 5


def isPath(arr):
    Dir = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    q = []
    q .append((0, 0))
    while (len(q) > 0):
        p = q[0]
        q .pop(0)
        arr[p[0]][p[1]] = -1
        if (p == (row - 1, col - 1)):
            return True
        for i in range(4):
            a = p[0] + Dir[i][0]
            b = p[1] + Dir[i][1]
            if (a >= 0 and b >= 0 and a < row and b < col and arr[a][b] != -1):
                q .append((a, b))
    return False


arr = [[0, 0, 0, -1, 0], [-1, 0, 0, -1, -1],
       [0, 0, 0, -1, 0], [-1, 0, -1, 0, -1], [0, 0, -1, 0, 0]]
if (isPath(arr)):
    print("Yes")
else:
    print("No")

# Expected output:
No
# Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ;
def printNSE(arr, n):
    s = []
    mp = {}
    s .append(arr[0])
    for i in range(1, n):
        if (len(s) == 0):
            s .append(arr[i])
            continue
        while (len(s) != 0 and s[-1] > arr[i]):
            mp[s[-1]] = arr[i]
            s .pop()
        s .append(arr[i])
    while (len(s) != 0):
        mp[s[-1]] = -1
        s .pop()
    for i in range(n):
        print(arr[i], "--->", mp[arr[i]])


arr = [11, 13, 21, 3]
n = len(arr)
printNSE(arr, n)

# Expected output:
11 ---> 3
13 ---> 3
21 ---> 3
3 ---> -1