#include <iostream>
int main() {
const int SIZE = 10;
int arr[SIZE] = {1, 2, 3, 4, 5, 6, 8, 9, 10}; // an example array
int sum = 0;
for (int i = 0; i < SIZE - 1; i++) {
sum += arr[i];
}
int expected_sum = (SIZE * (SIZE + 1)) / 2; // sum of all integers from 1 to SIZE
int missing_number = expected_sum - sum;
std::cout << "The missing number is " << missing_number << std::endl;
return 0;
}
The missing number is 7
SIZE
with one number missing. In this example, the missing number is 7.for
loop to iterate over each element of the array.SIZE
using the formula (SIZE * (SIZE + 1)) / 2
. This is the sum that would be obtained if no number were missing from the array.std::cout
.