kth missing element

Given an increasing sequence 'VEC', find the 'Kth' missing contiguous element in the given sequence starting from the leftmost element of the array.

Example :
Given 'VEC' : {1,4,5,7}
'K' : 3

alt text

As shown in the above figure, numbers 2, 3, and 6 are missing. Since 6 is the third missing element, it is the required answer.

Input Format :

The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains an integer ‘N’ denoting the number of elements in the array/list.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array/list.

The third line of each test case contains an integer ‘K’ denoting the 'Kth' missing element.

Output Format :

For each test case, print the 'Kth' missing contiguous element in the given sequence.

Note :

You don't need to print anything, it has already been taken care of. Just implement the given function.

Follow Up :

Try to solve it in O(log(N)).

Constraints :

1 <= T <= 10^2
1 <= N <= 10^4
1 <= K <= 10^9
-10^9 <= VEC[i] <= 10^9

Time Limit : 1 sec   
CodingNinjas
author
2y

I told the solving approach by using max heap in O(NlogK) time complexity.

CodingNinjas
author
2y
Single Array Traversal

For each element check whether the current and next element is consecutive or not. If not, take the difference between the two and check till the difference is greater or equal ...read more

CodingNinjas
author
2y
Binary Search

At any index, we can check how many elements are missing till the element at that index. Now, using binary search we’ll find the closest index to the required answer.

Here is the algorit...read more

Add answer anonymously...
Athenahealth Technology Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter