Python
def find_missing_number(nums):
n = len(nums) + 1
total_sum = (n * (n + 1)) // 2
actual_sum = sum(nums)
return total_sum - actual_sum
input_nums = [1, 2, 4, 6, 3, 7, 8]
missing_number = find_missing_number(input_nums)
print("The missing number is:", missing_number)
Java
public class MissingNumber {
public static int findMissingNumber(int[] nums) {
int n = nums.length + 1;
int totalSum = (n * (n + 1)) / 2;
int actualSum = 0;
for (int num : nums) {
actualSum += num;
}
return totalSum - actualSum;
}
public static void main(String[] args) {
int[] inputNums = {1, 2, 4, 6, 3, 7, 8};
int missingNumber = findMissingNumber(inputNums);
System.out.println("The missing number is: " + missingNumber);
}
}
Using C
#include <stdio.h>
int findMissingNumber(int nums[], int n) {
int totalSum = (n * (n + 1)) / 2;
int actualSum = 0;
for (int i = 0; i < n - 1; i++) {
actualSum += nums[i];
}
return totalSum - actualSum;
}
int main() {
int inputNums[] = {1, 2, 4, 6, 3, 7, 8};
int n = sizeof(inputNums) / sizeof(inputNums[0]);
int missingNumber = findMissingNumber(inputNums, n);
printf("The missing number is: %d\n", missingNumber);
return 0;
}