//Find missing Number in array 1-10;
public class MissingNumberInArray {
public static void main(String args[]){
int a[]={1,2,3,4,5,6,7,8,10};
int asum = 0,sum = 0;
for(int i=1;i<=10;i++)
sum=sum+i;
for(int i=0;i<a.length;i++)
asum=asum+a[i];
int missnum=sum-asum;
System.out.println("Missing Number"+missnum);
}
}
"You can find out the missing number in array by following algorithm:
ReplyDelete/* getMissingNumber takes array and size of array as arguments*/
int getMissingNumber (int arr[], int num)
{
int i;
/* For xor of all the elemets in arary */
int x1 = a[0];
/* For xor of all the elemets from 1 to n+1 */
int x2 = 1;
for (i = 1; i< n; i++)
x1 = x1^a[i];
for ( i = 2; i <= n+1; i++)
x2 = x2^i;
return (x1^x2);
}
I also found some more possible solutions. you can check out below link for more solutions:
Find out missing number in an array in java"