
import numpy as np
from PIL import Image






def 	a_		(	__lowercase       :     np.ndarray	, __lowercase       :     int	, __lowercase       :     int			)			->  np.ndarray:
			_snake_case          =      np.array(__lowercase			)
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('The input array is not a square matrix'			)
			_snake_case          =      0
			_snake_case          =      0
			_snake_case          =      0
			_snake_case          =      0

			# compute the shape of the output matrix
			_snake_case          =      (arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape maxpool_shape
			_snake_case          =      np.zeros((maxpool_shape, maxpool_shape)			)

			while i < arr.shape[0]:
						if i + size > arr.shape[0]:
									# if the end of the matrix is reached, break
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the maximum of the pooling matrix
									_snake_case          =      np.max(arr[i : i + size, j : j + size]			)
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1

						# reset the column index to 0
						_snake_case          =      0
						_snake_case          =      0

			return updated_arr






def 	a_		(	__lowercase       :     np.ndarray	, __lowercase       :     int	, __lowercase       :     int			)			->  np.ndarray:
			_snake_case          =      np.array(__lowercase			)
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('The input array is not a square matrix'			)
			_snake_case          =      0
			_snake_case          =      0
			_snake_case          =      0
			_snake_case          =      0

			# compute the shape of the output matrix
			_snake_case          =      (arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape avgpool_shape
			_snake_case          =      np.zeros((avgpool_shape, avgpool_shape)			)

			while i < arr.shape[0]:
						# if the end of the matrix is reached, break
						if i + size > arr.shape[0]:
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the average of the pooling matrix
									_snake_case          =      int(np.average(arr[i : i + size, j : j + size]			)			)
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1
						# reset the column index to 0
						_snake_case          =      0
						_snake_case          =      0

			return updated_arr


# Main Function
if __name__ == "__main__":
			from doctest import testmod

			testmod(name='''avgpooling''', verbose=True)

			# Loading the image
			_lowerCamelCase	:    Dict          =      Image.open('''path_to_image''')

			# Converting the image to numpy array and maxpooling, displaying the result
			# Ensure that the image is a square matrix

			Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

			# Converting the image to numpy array and averagepooling, displaying the result
			# Ensure that the image is a square matrix

			Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()

from __future__ import annotations

_lowerCamelCase	:    Optional[Any]          =      [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
_lowerCamelCase	:    Optional[int]          =      [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]






def 	a_		(	__lowercase       :     list[float]			)			->  list[float]:
			_snake_case          =      []
			_snake_case          =      len(__lowercase			)

			for i in range(__lowercase			):
						_snake_case          =      -1
						for j in range(i + 1	, __lowercase			):
									if arr[i] < arr[j]:
												_snake_case          =      arr[j]
												break
						result.append(__lowercase			)
			return result






def 	a_		(	__lowercase       :     list[float]			)			->  list[float]:
			_snake_case          =      []
			for i, outer in enumerate(__lowercase			):
						_snake_case          =      -1
						for inner in arr[i + 1 :]:
									if outer < inner:
												_snake_case          =      inner
												break
						result.append(__lowercase			)
			return result






def 	a_		(	__lowercase       :     list[float]			)			->  list[float]:
			_snake_case          =      len(__lowercase			)
			_snake_case          =      []
			_snake_case          =      [-1] * arr_size

			for index in reversed(range(__lowercase			)			):
						if stack:
									while stack[-1] <= arr[index]:
												stack.pop()
												if not stack:
															break
						if stack:
									_snake_case          =      stack[-1]
						stack.append(arr[index]			)
			return result


if __name__ == "__main__":
			from doctest import testmod
			from timeit import timeit

			testmod()
			print(next_greatest_element_slow(arr))
			print(next_greatest_element_fast(arr))
			print(next_greatest_element(arr))

			_lowerCamelCase	:    Union[str, Any]          =      (
			    '''from __main__ import arr, next_greatest_element_slow, '''
			    '''next_greatest_element_fast, next_greatest_element'''
			)
			print(
			    '''next_greatest_element_slow():''',
			    timeit('''next_greatest_element_slow(arr)''', setup=setup),
			)
			print(
			    '''next_greatest_element_fast():''',
			    timeit('''next_greatest_element_fast(arr)''', setup=setup),
			)
			print(
			    '''     next_greatest_element():''',
			    timeit('''next_greatest_element(arr)''', setup=setup),
			)