Find a Missing Number From a Sequence of Consecutive Numbers | XOR Method

Input:  Array, arrA[] with a miss­ing num­ber and Range

Out­put : miss­ing number

Example:

int A[] = { 1, 2, 7, 6, 3, 4 };
int range = 7;
Output: MIssing No is :5

In our earlier approach ” Click Here ” we have seen the method where we had calculated the Sum of numbers, but this approach might fail when number goes beyond the integer range.

XOR method will better solution in that case.

Approach: – Time Complexity -O(N), Space Complexity – O(1)

  • Do the XOR if 1 to n say its A
  • Do the XOR of given array say its B
  • Do the XOR of A and B will give the missing no

Code:

Output:

MIssing No is :5

__________________________________________________
Top Companies Interview Questions..-

GoogleMicrosoftAmazonFacebookmore..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: