Solution:read
#include <iostream>
#include <bitset>
using namespace std;
//Given an array of size n. It contains numbers in the range 1 to n.
//Each number is present at least once except for 2 numbers.
//Find the missing numbers.
static const int N = 10;
int main()
{
int numArray[N] = { 1, 7, 3, 4, 6, 2, 9, 9, 10, 10 };
bitset<N> bits;
bits.set();
for (int i = 0; i < N; i++)
{
// Clears bit that corresponds to the number; bit array is zero based
bits.set(numArray[i] - 1, false);
}
for (int i = 0; i < N; i++)
{
if (bits.test(i)) {
printf("Found unused value %d\n", i + 1);
}
}
return 0;
}
//This solution is kindly provided by Jeff Fore.
hide solution
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.