Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

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

Exam­ple:

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

In our ear­lier approach ” Click Here ” we have seen the method where we had cal­cu­lated the Sum of num­bers, but this approach might fail when num­ber goes beyond the inte­ger range.

XOR method will bet­ter solu­tion in that case.

Approach: — Time Com­plex­ity –O(N), Space Com­plex­ity — 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 miss­ing no

Code:


[/sourcecode]

Out­put:

MIssing No is :5

You may also like...