Skip to content

Instantly share code, notes, and snippets.

@thmain
Created February 25, 2016 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/a17a49cc94b20507e410 to your computer and use it in GitHub Desktop.
Save thmain/a17a49cc94b20507e410 to your computer and use it in GitHub Desktop.
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment