**Input:** Array, arrA[] with a missing number and Range

**Output : **missing 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:**

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

public class findMissingNo { | |

// 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 | |

public static int missingNo(int arrA[], int range) { | |

int A = 0; | |

int B = 0; | |

for (int i = 1; i <= range; i++) { | |

A = A ^ i; | |

} | |

for (int i = 0; i < arrA.length; i++) { | |

B = B ^ arrA[i]; | |

} | |

return A ^ B; | |

} | |

public static void main(String[] args) { | |

int A[] = { 1, 2, 7, 6, 3, 4 }; | |

int range = 7; | |

System.out.println("MIssing No is :" + missingNo(A, range)); | |

} | |

} |

**Output**:

MIssing No is :5