X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Stable Sort and Position
Basic Accuracy: 59.0% Submissions: 835 Points: 1

Given an array arr[] of N integers which may contain duplicate elements, the index of an element of this array is given to us, the task is to find the final position of this element ( for which the index is given) in the array after the stable sort is applied on the array.

Example 1:

Input: N = 10, INDEX = 5
arr[]= {3, 4, 3, 5, 2, 3, 4, 3, 1, 5}
Output: 4
Explanation: Element initial index – 5
(third 3) After sorting array by stable
sorting algorithm,we get array as shown
below [1(8), 2(4), 3(0), 3(2), 3(5), 3(7),
4(1), 4(6), 5(3), 5(9)] with their
initial indices shown in parentheses next
to them,Element's index after sorting = 4.

This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function getIndexInSortedArray() that takes array arr, integer N, and integer index as parameters and returns the number of uneaten leaves.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.