Find the Missing Number
Easy Array Math Algorithm
🏢Google, Facebook, Amazon
0 views0 likesProblem Description
Find the Missing Number
Given an array of size 'n' containing n-1 distinct numbers in the range of 1 to n. Find the missing number.
Write a function that takes an array of integers as an input and returns the missing number.
Example 1:
Input: [1, 2, 4, 5, 6]
Output: 3Example 2:
Input: [2, 3, 1, 5]
Output: 4Constraints:
- The array contains distinct integers in the range 1 to n.
- The size of the array is n-1.
Hints
Think about the formula for the sum of the first n natural numbers.
Consider how you can use this formula to find the missing number.