Prepin
Log in

Identify the element at the specified position in a sorted array, given an unsorted input array. Example - Input: {5, 1, 7, 19, 0, 16}, k = 3 Output: 5 Explanation: Once the array is sorted, it appears as follows: {0, 1, 5, 7, 16, 19} Thus, the element at the 3rd position is 5.

Approach / Explanation

This question presents you with an unsorted array and a position 'k'. Your objective is to identify the element at the 'k'th position in the sorted version of the array. Therefore, you'll need to sort the array first before locating the element at the 'k'th position. To tackle this question, consider the following important points: Familiarize yourself with sorting methods. Any sorting algorithm can be employed, whether it's a simple one like Bubble sort or Insertion sort, or more advanced ones like Quick sort or Merge sort. Keep in mind that array indexing begins at 0. For instance, if 'k' is specified as 3, you are required to find the 3rd element, which actually corresponds to the 2nd index (0, 1, 2). If the 'k' value exceeds the size of the array or is less than 1, you should return an error or manage this situation appropriately in your algorithm.

Suggested Answer

To address this, we can start by sorting the array using a sorting algorithm. For demonstration purposes, let's use Bubble Sort. Once the array is sorted, we can return the element located at the 'k'-1 index, since indexing begins at 0. Below is an example implementation in Python for the task: def find_kth_element ( arr , k ) : n = len ( arr ) # Bubble Sort for i in range ( n ) : for j in range ( 0 , n - i - 1 ) : if arr [ j ] > arr [ j + 1 ] : arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ] return arr [ k - 1 ] Therefore, if we execute find_kth_element([5, 1, 7, 19, 0, 16], 3) , the output will be 5.

Alternative Answer

An alternative effective method to tackle this issue is by utilizing the QuickSelect algorithm, which is an in-place version of QuickSort. In this algorithm, we choose a 'pivot' and divide the remaining elements into two categories based on whether they are less than or greater than the pivot. Below is a Python implementation of the problem using QuickSelect: def partition ( nums , low , high ) : pivot = nums [ high ] i = low - 1 for j in range ( low , high ) : if nums [ j ] <= pivot : i += 1 nums [ i ] , nums [ j ] = nums [ j ] , nums [ i ] nums [ i + 1 ] , nums [ high ] = nums [ high ] , nums [ i + 1 ] return i + 1 def quickselect ( low , high , pos , nums ) : if low < high : pi = partition ( nums , low , high ) if pos < pi : return quickselect ( low , pi - 1 , pos , nums ) elif pos > pi : return quickselect ( pi + 1 , high , pos , nums ) return nums [ pi ] else : return nums [ low ] def findKthLargest ( nums , k ) : return quickselect ( 0 , len ( nums ) - 1 , len ( nums ) - k , nums ) Therefore, calling findKthLargest([5, 1, 7, 19, 0, 16], 3) will yield an output of 5. This method proves to be more efficient when working with larger datasets.

Question Details

Difficulty & Category

Medium
technical

Companies

Philips

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.