How to find a missing number from an array?

A Quick Overview

Problem Statement

Given an array of integers of size N – 1 ranging from 1 to N without any duplicates, the task is to find the missing number in the array.

Input: list[] = {1, 2, 4, 6, 3, 7, 8, 10, 5}  Output: Explanation: The missing number from 1 to 10 is 9.

Example:

1. Calculate sum of the first N natural numbers as Total- N * (N + 1) / 2.  2. Calculate sum of elements of the array and store in a variable "Sum".

Simple Approach

3. Iterate array from start to end.  4. Update value of sum as sum = sum + array[i].  5. Print missing number as Total – sum.

Time Complexity: O(N), where N is size of array.  Space Complexity: O(1).

Check out other method with code implementation.