Banana-Leopard/TASTY huggingface dataset:
# Python program to search an element in row-wise
# and column-wise sorted matrix
 
# Searches the element x in mat[][]. If the
# element is found, then prints its position
# and returns true, otherwise prints "not found"
# and returns false
 
 
def search(mat, n, x):
    if(n == 0):
        return -1
 
    # Traverse through the matrix
    for i in range(n):
        for j in range(n):
 
            # If the element is found
            if(mat[i][j] == x):
                print("Element found at (", i, ",", j, ")")
                return 1
 
    print(" Element not found")
    return 0
 
 
# Driver code
if __name__ == "__main__":
    mat = [[10, 20, 30, 40], [15, 25, 35, 45],
           [27, 29, 37, 48], [32, 33, 39, 50]]
 
    # Function call
    search(mat, 4, 29)
 
# This code is contributed by rag2127

Time complexity: quadratic
Space complexity: constant
# Python3 program to search an element
# in row-wise and column-wise sorted matrix
 
# Searches the element x in mat[][]. If the
# element is found, then prints its position
# and returns true, otherwise prints "not found"
# and returns false
 
 
def search(mat, n, x):
 
    i = 0
 
    # set indexes for top right element
    j = n - 1
    while (i < n and j >= 0):
 
        if (mat[i][j] == x):
 
            print("Element found at ", i, ", ", j)
            return 1
 
        if (mat[i][j] > x):
            j -= 1
 
        # if mat[i][j] < x
        else:
            i += 1
 
    print("Element not found")
    return 0  # if (i == n || j == -1 )
 
 
# Driver Code
if __name__ == "__main__":
    mat = [[10, 20, 30, 40],
           [15, 25, 35, 45],
           [27, 29, 37, 48],
           [32, 33, 39, 50]]
 
    # Function call
    search(mat, 4, 29)
 
# This code is contributed by Anant Agarwal.

Time complexity: linear
Space complexity: constant
# Python3 program to Create a matrix with
# alternating rectangles of O and X
 
# Function to print alternating rectangles
# of 0 and X
def fill0X(m, n):
     
    # k - starting row index
    # m - ending row index
    # l - starting column index
    # n - ending column index
    # i - iterator
    i, k, l = 0, 0, 0
 
    # Store given number of rows and
    # columns for later use
    r = m
    c = n
 
    # A 2D array to store the output
    # to be printed
    a = [[None] * n for i in range(m)]
    x = 'X' # Initialize the character to
            # be stored in a[][]
 
    # Fill characters in a[][] in spiral form.
    # Every iteration fills one rectangle of
    # either Xs or Os
    while k < m and l < n:
         
        # Fill the first row from the
        # remaining rows
        for i in range(l, n):
            a[k][i] = x
        k += 1
 
        # Fill the last column from
        # the remaining columns
        for i in range(k, m):
            a[i][n - 1] = x
        n -= 1
 
        # Fill the last row from the
        # remaining rows
        if k < m:
            for i in range(n - 1, l - 1, -1):
                a[m - 1][i] = x
            m -= 1
 
        # Print the first column from
        # the remaining columns
        if l < n:
            for i in range(m - 1, k - 1, -1):
                a[i][l] = x
            l += 1
 
        # Flip character for next iteration
        x = 'X' if x == '0' else '0'
 
    # Print the filled matrix
    for i in range(r):
        for j in range(c):
            print(a[i][j], end = " ")
        print()
 
# Driver Code
if __name__ == '__main__':
     
    print("Output for m = 5, n = 6")
    fill0X(5, 6)
 
    print("Output for m = 4, n = 4")
    fill0X(4, 4)
 
    print("Output for m = 3, n = 4")
    fill0X(3, 4)
     
# This code is contributed by pranchalK

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to print all elements
# of given matrix in diagonal order
ROW = 5
COL = 4
 
# Main function that prints given
# matrix in diagonal order
 
 
def diagonalOrder(matrix):
 
    # There will be ROW+COL-1 lines in the output
    for line in range(1, (ROW + COL)):
        # Get column index of the first element
        # in this line of output. The index is 0
        # for first ROW lines and line - ROW for
        # remaining lines
        start_col = max(0, line - ROW)
 
        # Get count of elements in this line.
        # The count of elements is equal to
        # minimum of line number, COL-start_col and ROW
        count = min(line, (COL - start_col), ROW)
 
        # Print elements of this line
        for j in range(0, count):
            print(matrix[min(ROW, line) - j - 1]
                        [start_col + j], end="\t")
 
        print()
 
 
# Utility function to print a matrix
def printMatrix(matrix):
    for i in range(0, ROW):
        for j in range(0, COL):
            print(matrix[i][j], end="\t")
 
        print()
 
 
# Driver Code
M = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12],
     [13, 14, 15, 16],
     [17, 18, 19, 20]]
print("Given matrix is ")
printMatrix(M)
 
print("\nDiagonal printing of matrix is ")
diagonalOrder(M)
 
# This code is contributed by Nikita Tiwari.

Time complexity: quadratic
Space complexity: constant
# Python3 program to print all elements
# of given matrix in diagonal order
R = 5
C = 4
 
 
def isValid(i, j):
    if (i < 0 or i >= R or j >= C or j < 0):
        return False
    return True
 
 
def diagonalOrder(arr):
 
    # through this for loop we choose each element
    # of first column as starting point and print
    # diagonal starting at it.
    # arr[0][0], arr[1][0]....arr[R-1][0]
    # are all starting points
    for k in range(0, R):
        print(arr[k][0], end="  ")
 
        # set row index for next point in diagonal
        i = k - 1
 
        # set column index for next point in diagonal
        j = 1
 
        # Print Diagonally upward
        while (isValid(i, j)):
            print(arr[i][j], end="  ")
            i -= 1
            j += 1  # move in upright direction
 
        print()
 
    # Through this for loop we choose each
    # element of last row as starting point
    # (except the [0][c-1] it has already been
    # processed in previous for loop) and print
    # diagonal starting at it.
    # arr[R-1][0], arr[R-1][1]....arr[R-1][c-1]
    # are all starting points
 
    # Note : we start from k = 1 to C-1;
    for k in range(1, C):
        print(arr[R-1][k], end="  ")
 
        # set row index for next point in diagonal
        i = R - 2
 
        # set column index for next point in diagonal
        j = k + 1
 
        # Print Diagonally upward
        while (isValid(i, j)):
            print(arr[i][j], end="  ")
            i -= 1
            j += 1  # move in upright direction
 
        print()
 
 
# Driver Code
arr = [[1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20]]
 
# Function call
diagonalOrder(arr)
 
# This code is contributed by Nikita Tiwari.

Time complexity: quadratic
Space complexity: constant
R = 5
C = 5
def diagonalOrder(arr, n, m):
     
    # we will use a 2D vector to
    # store the diagonals of our array
    # the 2D vector will have (n+m-1)
    # rows that is equal to the number of
    # diagonals
    ans = [[] for i in range(n + m - 1)]
     
    for i in range(m):
        for j in range(n):
            ans[i + j].append(arr[j][i])
     
    for i in range(len(ans)):
        for j in range(len(ans[i])):
            print(ans[i][j], end = " ")
        print()
 
# Driver Code
# we have a matrix of n rows
# and m columns
n = 5
m = 4
 
# Function call
arr = [[1, 2, 3, 4],[ 5, 6, 7, 8],[9, 10, 11, 12 ],[13, 14, 15, 16 ],[ 17, 18, 19, 20]]
diagonalOrder(arr, n, m)
 
# This code is contributed by rag2127

Time complexity: quadratic
Space complexity: linear
# implementation to find the total
# energy required to rearrange the
# numbers
n = 4
 
# function to find the total energy
# required to rearrange the numbers
def calculateEnergy(mat,n):
 
    tot_energy = 0
 
    # nested loops to access the
    # elements of the given matrix
    for i in range(n):
        for j in range(n):
 
            #store quotient
            q = mat[i][j]//n
 
            # final destination location
            # (i_des, j_des) of the
            # element mat[i][j] is being
            # calculated
            i_des = q
            j_des = mat[i][j]- (n*q)
 
            # energy required for the
            # movement of the element
            # mat[i][j] is calculated
            # and then accumulated in
            # the 'tot_energy'
            tot_energy += (abs(i_des-i)
                         + abs(j_des-j))
 
    # required total energy
    return tot_energy
 
 
# Driver Program
mat = [[4, 7, 0, 3],
       [8, 5, 6, 1],
         [9, 11, 10, 2],
       [15, 13, 14, 12]]
print("Total energy required = ",
        calculateEnergy(mat,n), "units")
 
# This code is contributed by Shrikant13.

Time complexity: quadratic
Space complexity: constant
# Python3 program to count unique cells in
# a matrix
 
MAX = 100
 
# Returns true if mat[i][j] is unique
def isUnique(mat, i, j, n, m):
     
    # checking in row calculating sumrow
    # will be moving column wise
    sumrow = 0
    for k in range(m):
        sumrow += mat[i][k]
        if (sumrow > 1):
            return False
 
    # checking in column calculating sumcol
    # will be moving row wise
    sumcol = 0
    for k in range(n):
        sumcol += mat[k][j]
        if (sumcol > 1):
            return False
 
    return True
 
def countUnique(mat, n, m):
    uniquecount = 0
    for i in range(n):
        for j in range(m):
            if (mat[i][j] and isUnique(mat, i, j, n, m)):
                    uniquecount += 1
    return uniquecount
 
# Driver code
 
mat = [[0, 1, 0, 0],
        [0, 0, 1, 0],
        [1, 0, 0, 1]]
print(countUnique(mat, 3, 4))
 
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: constant
# Efficient Python3 program to count unique
# cells in a binary matrix
MAX = 100;
 
def countUnique(mat, n, m):
    rowsum = [0] * n;
    colsum = [0] * m;
 
    # Count number of 1s in each row
    # and in each column
    for i in range(n):
        for j in range(m):
            if (mat[i][j] != 0):
                rowsum[i] += 1;
                colsum[j] += 1;
 
    # Using above count arrays,
    # find cells
    uniquecount = 0;
    for i in range(n):
            for j in range(m):
                if (mat[i][j] != 0 and
                    rowsum[i] == 1 and
                    colsum[j] == 1):
                    uniquecount += 1;
    return uniquecount;
 
# Driver code
if __name__ == '__main__':
    mat = [ 0, 1, 0, 0 ],
          [ 0, 0, 1, 0 ],
          [ 1, 0, 0, 1 ];
    print(countUnique(mat, 3, 4));
 
# This code is contributed by 29AjayKumar

Time complexity: quadratic
Space complexity: linear
# Python 3 program for counting
# number of cell equals to given x
 
# function to count factors
# as number of cell
def count(n, x):
    cnt = 0
 
    # traverse and find the factors
    for i in range(1, n + 1):
 
        # // x%i == 0 means i is factor of x
        # x/i <= n means i and j are <= n (for i*j=x)
        if i <= x:
            if x // i <= n and x % i == 0:
                cnt += 1
    return cnt
 
# Driver code
n = 8
x = 24
print(count(n, x))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: constant
# Python 3 code to check
# if a matrix is
# sparse.
 
MAX = 100
  
def isSparse(array,m, n) :
     
    counter = 0
  
    # Count number of zeros
    # in the matrix
    for i in range(0,m) :
        for j in range(0,n) :
            if (array[i][j] == 0) :
                counter = counter + 1
  
    return (counter >
            ((m * n) // 2))
 
# Driver Function
array = [ [ 1, 0, 3 ],
          [ 0, 0, 4 ],
          [ 6, 0, 0 ] ]
m = 3
n = 3
 
if (isSparse(array, m, n)) :
    print("Yes")
else :
    print("No")
     
     
# this code is contributed by
# Nikita tiwari

Time complexity: quadratic
Space complexity: constant
# Python3 program to find common
# elements in two diagonals.
 
Max = 100
 
# Returns count of row wise same
# elements in two diagonals of
# mat[n][n]
def countCommon(mat, n):
    res = 0
     
    for i in range(n):
         
        if mat[i][i] == mat[i][n-i-1] :
            res = res + 1
    return res    
 
# Driver Code
mat = [[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]]
 
print(countCommon(mat, 3))
 
# This code is contributed by Anant Agarwal.

Time complexity: linear
Space complexity: constant
# Python3 program to check the if
# sum of a row is same as
# corresponding column
MAX = 100;
 
# Function to check the if sum
# of a row is same as
# corresponding column
def areSumSame(a, n, m):
    sum1 = 0
    sum2 = 0
    for i in range(0, n):
        sum1 = 0
        sum2 = 0
        for j in range(0, m):
            sum1 += a[i][j]
            sum2 += a[j][i]
         
        if (sum1 == sum2):
            return 1
     
    return 0
 
# Driver Code
n = 4; # number of rows
m = 4; # number of columns
M = [ [ 1, 2, 3, 4 ],
      [ 9, 5, 3, 1 ],
      [ 0, 3, 5, 6 ],
      [ 0, 4, 5, 6 ] ]
       
print(areSumSame(M, n, m))
 
# This code is contributed by Sam007.

Time complexity: linear
Space complexity: constant
# python program to find row with
# maximum 1 in row sorted binary
# matrix
 
N = 4
 
# function for finding row with
# maximum 1
def findMax (arr):
    row = 0
    j = N - 1
    for i in range(0, N):
        # find left most position
        # of 1 in a row find 1st
        # zero in a row
        while (arr[i][j] == 1
                     and j >= 0):
            row = i
            j -= 1
         
    print("Row number = " , row + 1,
         ", MaxCount = ", N - 1 - j)
 
# driver program
arr = [ [0, 0, 0, 1],
        [0, 0, 0, 1],
        [0, 0, 0, 0],
        [0, 1, 1, 1] ]
         
findMax(arr)
 
# This code is contributed by Sam007

Time complexity: linear
Space complexity: constant
# Simple Python code for check a matrix is
# symmetric or not.
  
# Fills transpose of mat[N][N] in tr[N][N]
def transpose(mat, tr, N):
    for i in range(N):
        for j in range(N):
            tr[i][j] = mat[j][i]
  
# Returns true if mat[N][N] is symmetric, else false
def isSymmetric(mat, N):
     
    tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ]
    transpose(mat, tr, N)
    for i in range(N):
        for j in range(N):
            if (mat[i][j] != tr[i][j]):
                return False
    return True
  
# Driver code
mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ]
if (isSymmetric(mat, 3)):
    print "Yes"
else:
    print "No"
 
# This code is contributed by Sachin Bisht

Time complexity: quadratic
Space complexity: quadratic
# Efficient Python code for check a matrix is
# symmetric or not.
 
# Returns true if mat[N][N] is symmetric, else false
def isSymmetric(mat, N):
    for i in range(N):
        for j in range(N):
            if (mat[i][j] != mat[j][i]):
                return False
    return True
  
# Driver code
mat = [ [ 1, 3, 5 ], [ 3, 2, 4 ], [ 5, 4, 1 ] ]
if (isSymmetric(mat, 3)):
    print "Yes"
else:
    print "No"
 
# This code is contributed by Sachin Bisht

Time complexity: quadratic
Space complexity: constant
# Python3 program for the above approach
 
# function which tells all cells are visited or not
def isAllCellTraversed(grid, n, m):
 
    visited = [[True for j in range(m)] for i in range(n)];
    total = n*m;
   
    # starting cell values
    startx = grid[0][0][0];
    starty = grid[0][0][1];
 
    for i in range(total-2):
 
        # if we get {0,0} before the end of loop
        # then returns False. Because it means we
        # didn't traverse all the cells
        if (grid[startx][starty][0] == -1 and
            grid[startx][starty][1] == -1):
            return False;
 
        # If found cycle then return False
        if (visited[startx][starty] == True):
            return False;
 
        visited[startx][starty] = True;
        x = grid[startx][starty][0];
        y = grid[startx][starty][1];
 
        # Update startx and starty values to next
        # cell values
        startx = x;
        starty = y;
 
    # finally if we reach our goal then returns True
    if (grid[startx][starty][0] == -1 and
        grid[startx][starty][1] == -1):
        return True;
 
    return False;
     
# Driver code
cell = [[[] for j in range(3)] for i in range(3)]
cell[0][0] = [0, 1];
cell[0][1] = [2, 0];
cell[1][0] = [-1,-1];
cell[1][1] = [1, 0];
cell[2][0] = [2, 1];
cell[2][1] = [1, 1];
 
if(not isAllCellTraversed(cell, 3, 2)):
  print("True");
else:
  print("False");
 
  # This code is contributed by rrrtnx.

Time complexity: linear
Space complexity: quadratic
# Python3 program to find number
# of possible moves of knight
n = 4;
m = 4;
 
# To calculate possible moves
def findPossibleMoves(mat, p, q):
    global n, m;
     
    # All possible moves of a knight
    X = [2, 1, -1, -2, -2, -1, 1, 2];
    Y = [1, 2, 2, 1, -1, -2, -2, -1];
 
    count = 0;
 
    # Check if each possible move
    # is valid or not
    for i in range(8):
         
        # Position of knight after move
        x = p + X[i];
        y = q + Y[i];
 
        # count valid moves
        if(x >= 0 and y >= 0 and x < n and
           y < m and mat[x][y] == 0):
            count += 1;
 
    # Return number of possible moves
    return count;
 
# Driver code
if __name__ == '__main__':
    mat = [[1, 0, 1, 0], [0, 1, 1, 1],
           [1, 1, 0, 1], [0, 1, 1, 1]];
 
    p, q = 2, 2;
 
    print(findPossibleMoves(mat, p, q));
 
# This code is contributed by 29AjayKumar

Time complexity: constant
Space complexity: constant
# A simple Python program to
# find sum of diagonals
MAX = 100
 
def printDiagonalSums(mat, n):
 
    principal = 0
    secondary = 0;
    for i in range(0, n):
        for j in range(0, n):
 
            # Condition for principal diagonal
            if (i == j):
                principal += mat[i][j]
 
            # Condition for secondary diagonal
            if ((i + j) == (n - 1)):
                secondary += mat[i][j]
         
    print("Principal Diagonal:", principal)
    print("Secondary Diagonal:", secondary)
 
# Driver code
a = [[ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ],
     [ 1, 2, 3, 4 ],
      [ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)
 
# This code is contributed
# by ihritik

Time complexity: quadratic
Space complexity: constant
# A simple Python3 program to find
# sum of diagonals
MAX = 100
 
def printDiagonalSums(mat, n):
 
    principal = 0
    secondary = 0
    for i in range(0, n):
        principal += mat[i][i]
        secondary += mat[i][n - i - 1]
         
    print("Principal Diagonal:", principal)
    print("Secondary Diagonal:", secondary)
 
# Driver code
a = [[ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ],
     [ 1, 2, 3, 4 ],
     [ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)
 
# This code is contributed
# by ihritik

Time complexity: linear
Space complexity: constant
# Python program to print boundary element
# of the matrix.
 
MAX = 100
 
 
def printBoundary(a, m, n):
    for i in range(m):
        for j in range(n):
            if (i == 0):
                print a[i][j],
            elif (i == m-1):
                print a[i][j],
            elif (j == 0):
                print a[i][j],
            elif (j == n-1):
                print a[i][j],
            else:
                print " ",
        print
 
 
# Driver code
if __name__ == "__main__":
    a = [[1, 2, 3, 4], [5, 6, 7, 8],
         [1, 2, 3, 4], [5, 6, 7, 8]]
 
    # Function call
    printBoundary(a, 4, 4)
 
# This code is contributed by Sachin Bisht

Time complexity: quadratic
Space complexity: constant
# Python program to print boundary element
# of the matrix.
 
MAX = 100
 
 
def printBoundary(a, m, n):
    sum = 0
    for i in range(m):
        for j in range(n):
            if (i == 0):
                sum += a[i][j]
            elif (i == m-1):
                sum += a[i][j]
            elif (j == 0):
                sum += a[i][j]
            elif (j == n-1):
                sum += a[i][j]
    return sum
 
 
# Driver code
if __name__ == "__main__":
    a = [[1, 2, 3, 4], [5, 6, 7, 8],
         [1, 2, 3, 4], [5, 6, 7, 8]]
 
    # Function call
    sum = printBoundary(a, 4, 4)
    print "Sum of boundary elements is", sum
 
# This code is contributed by Sachin Bisht

Time complexity: quadratic
Space complexity: constant
# Python3 program to print a matrix
# in spiral form.
MAX = 100
 
def printSpiral(mat, r, c):
 
    a = 0
    b = 2
 
    low_row = 0 if (0 > a) else a
    low_column = 0 if (0 > b) else b - 1
    high_row = r-1 if ((a + 1) >= r) else a + 1
    high_column = c-1 if ((b + 1) >= c) else b + 1
 
    while ((low_row > 0 - r and low_column > 0 - c)):
 
        i = low_column + 1
        while (i <= high_column and
               i < c and low_row >= 0):
            print( mat[low_row][i], end = " ")
            i += 1
        low_row -= 1
 
        i = low_row + 2
        while (i <= high_row and
               i < r and high_column < c):
            print(mat[i][high_column], end = " ")
            i += 1
        high_column += 1
 
        i = high_column - 2
        while (i >= low_column and
               i >= 0 and high_row < r):
            print(mat[high_row][i], end = " ")
            i -= 1
        high_row += 1
 
        i = high_row - 2
        while (i > low_row and
               i >= 0 and low_column >= 0):
            print(mat[i][low_column], end = " ")
            i -= 1
        low_column -= 1
     
    print()
 
# Driver code
if __name__ == "__main__":
     
    mat = [[ 1, 2, 3 ],
           [ 4, 5, 6 ],
           [ 7, 8, 9 ]]
    r = 3
    c = 3
    printSpiral(mat, r, c)
 
# This code is contributed by ita_c

Time complexity: quadratic
Space complexity: constant
# Python 3 program to print
# matrix in snake order
M = 4
N = 4
 
 
def printf(mat):
    global M, N
 
    # Traverse through all rows
    for i in range(M):
 
        # If current row is
        # even, print from
        # left to right
        if i % 2 == 0:
            for j in range(N):
                print(str(mat[i][j]),
                      end=" ")
 
        # If current row is
        # odd, print from
        # right to left
        else:
            for j in range(N - 1, -1, -1):
                print(str(mat[i][j]),
                      end=" ")
 
 
# Driver code
mat = [[10, 20, 30, 40],
       [15, 25, 35, 45],
       [27, 29, 37, 48],
       [32, 33, 39, 50]]
 
printf(mat)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: constant
# Python program to interchange
# the diagonals of matrix
N = 3;
 
# Function to interchange diagonals
def interchangeDiagonals(array):
 
    # swap elements of diagonal
    for i in range(N):
        if (i != N / 2):
            temp = array[i][i];
            array[i][i] = array[i][N - i - 1];
            array[i][N - i - 1] = temp;
         
    for i in range(N):
        for j in range(N):
            print(array[i][j], end = " ");
        print();
 
# Driver Code
if __name__ == '__main__':
    array = [ 4, 5, 6 ],[ 1, 2, 3 ],[ 7, 8, 9 ];
    interchangeDiagonals(array);
     
# This code is contributed by Rajput-Ji

Time complexity: quadratic
Space complexity: constant
# Python3 program to find the difference
# between the sum of diagonal.
def difference(arr, n):
 
    # Initialize sums of diagonals
    d1 = 0
    d2 = 0
 
    for i in range(0, n):
     
        for j in range(0, n):
         
            # finding sum of primary diagonal
            if (i == j):
                d1 += arr[i][j]
 
            # finding sum of secondary diagonal
            if (i == n - j - 1):
                d2 += arr[i][j]
         
    # Absolute difference of the sums
    # across the diagonals
    return abs(d1 - d2);
 
# Driver Code
n = 3
 
arr = [[11, 2, 4],
       [4 , 5, 6],
       [10, 8, -12]]
 
print(difference(arr, n))
     
# This code is contributed
# by ihritik

Time complexity: quadratic
Space complexity: constant