xxxxxxxxxx
/* Kth smallest element of array
Approach - using max Heap
Time : O(N*logN) Space : O(K)
Step 1 : make a max heap from the 1st k elements of the array
Step 2 : now run a loop for the rest elements of the array (excluding 1st k elements)
step 2.1 : check if the element is smaller then the root of maxHeap
if yes then delete the root of maxheap, and insert this element 'x' into the maxheap
step 3 : when the loop is complete, then return the root of the maxheap (it is the kth smallest element of the array)
//Done
*/
xxxxxxxxxx
#find the smallest element in an array
#Approach:
#Sort the array in ascending order.
arr = [2,5,1,3,0]
# sorted method
my_arr = sorted(arr)
print(f"The smallest number is {my_arr[0]}")
#* USING FOR LOOP
smallest = arr[0]
for i in range(0,len(arr)):
if arr[i] < smallest :
smallest = arr[i]
print(f"The smallest number is {smallest}")