Karteikarten
Find the Missing Number
Karten in diesem Stapel (1)
- 1You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.class Main
{
// Function to find missing number
static int getMissingNo (int a[], int n)
{
int x1 = a[0];
int x2 = 1;
/* For xor of all the elements
in array */
for (int i = 1; i < n; i++)
x1 = x1 ^ a[i];
/* For xor of all the elements
from 1 to n+1 */
for (int 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);
}
}
Informationen
- Beschreibung
- Karteikarten am Find the Missing Number, erstellt von Suhas S am 21/03/2018.
- Erstellt von
- Suhas S
- Aufrufe
- 0
- Bewertung