Created
June 13, 2017 15:57
-
-
Save anil477/dc0e849791a1724985cb8356a909e06a to your computer and use it in GitHub Desktop.
Java program to find missing Number using xor
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
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