Leaderboard
Showing:
Last duplicate element in a sorted array
Basic Accuracy: 57.88% Submissions: 470 Points: 1

Given a sorted array with duplicate elements and we have to find the index of the last duplicate element and return the index of it and also return the duplicate element.

 

Example 1:

​Input : arr[ ] = {1, 5, 5, 6, 6, 7}
Output : 4 6
Explanation:
Last duplicate element is 6 having index 4.​


Example 2:

Input : arr[ ] = {1, 2, 3, 4, 5}
Output: -1

 

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function dupLastIndex() that takes an array (arr), sizeOfArray (n), and return the two integers denoting the last index of the duplicate element and that duplicate element respectively, and if no duplicate element occurs return -1. The driver code takes care of the printing.

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

 

Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ 107

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Last duplicate element in a sorted array

Output Window