Skip to content

Instantly share code, notes, and snippets.

@anil477
Created June 13, 2017 15:57
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 anil477/dc0e849791a1724985cb8356a909e06a to your computer and use it in GitHub Desktop.
Save anil477/dc0e849791a1724985cb8356a909e06a to your computer and use it in GitHub Desktop.
Java program to find missing Number using xor
class Main
{
// Function to find missing number
static int getMissingNo (int a[], int n)
{
int i;
int x1 = a[0];
int x2 = 1;
/* For xor of all the elements in array */
for (i = 1; i< n; i++)
x1 = x1^a[i];
/* For xor of all the elements from 1 to n+1 */
for ( i = 2; i <= n+1; i++)
x2 = x2^i;
return (x1^x2);
}
/* program to test above function */
public static void main(String args[])
{
int a[] = {1,2,4,5,6};
int miss = getMissingNo(a,5);
System.out.println(miss);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment