0

I encountered an interview question.

Given two sorted arrays.
Initially both have set set of elements but one element is removed from one array.
Find the element removed.
Constraint is we have to done it inplace in O(logn) For ex:

arr1[]={1,2,3,8,12,16};
arr2[]={1,2,8,12,16};

element removed is 3

3
  • Please read How to Ask to ask good questions, so that you get good answers. In particular, you should at least have tried something yourself and show the code / pseudo code of what you have tried. Commented Apr 13, 2016 at 18:35
  • @theblindprophet this won't work here because the numbers in the arrays don't follow... answer you provide is for continuous range... Commented Apr 13, 2016 at 18:42
  • ahhh, i see thanks Commented Apr 13, 2016 at 18:43

1 Answer 1

5

I am typing from mobile, so it is a pseudo code, but you will get it:

take arr1.len / 2. It is 3. Check arr1[3] and arr2[3]. If they equal then missing vsalue is in index greater than 3 else less than 3. Here we get 8 and 12. So missing is before. We take index 3/2=1. Compare arr1[1] and arr2[1]. They are equal, so missing is after index 1 and before 3. So it is arr1[2] = 3.

This is the idea. You are doing a binary search, deviding searvh area by half everytime. You take left or right part of the array depending on comparing. You just need to implement this and do some checks, but the idea is clear I think.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.