
Kth Smallest Element in an Array
Hi everyone! While practicing array problems, I came across this question about finding the kth smallest element. It’s a good problem for understanding sorting and indexing. Problem Given an array and a number 'k', we need to find the kth smallest element. Example: Input: [7, 10, 4, 3, 20, 15], k = 3 Output: 7 My Approach At first, I thought about checking all elements manually, but that would be complicated. Then I realized: If we sort the array, the kth smallest element will be at index 'k-1'. So the solution becomes very simple. Logic Sort the array Return the element at position'k-1' Code (Python) class Solution: def kthSmallest(self, arr, k): arr.sort() return arr[k - 1] Time & Space Complexity Time: O(n log n) (due to sorting) Space: O(1) What I Learned Sorting can simplify many problems Indexing is important when working with ordered data Always think of a simple approach first before optimizing Thanks for reading! Feel free to share better approaches or optimizations.
Continue reading on Dev.to Python
Opens in a new tab

![[Learning notes and hw] getting started with R-cnn: Manually implementing Intersection over Union (IoU)](/_next/image?url=https%3A%2F%2Fmedia2.dev.to%2Fdynamic%2Fimage%2Fwidth%3D800%252Cheight%3D%252Cfit%3Dscale-down%252Cgravity%3Dauto%252Cformat%3Dauto%2Fhttps%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Farticles%252Favit2emoxc0g68e5ltqj.jpg&w=1200&q=75)

