FastPrepFind the Missing Number from 1 to n
Problem · Array

Find the Missing Number from 1 to n

Learn this problem
EasyGEP logoGEPINTERNOA

Problem statement

You are given an integer array nums of length n - 1. The array contains distinct values chosen from every integer from 1 through n, inclusive, with exactly one value missing.

Return the missing value.

Function

findMissingNumber(nums: int[]) → int

Examples

Example 1

nums = [1,2,4,5]return = 3

Here n = 5. The only value from 1 through 5 absent from the array is 3.

Example 2

nums = [2,3,1]return = 4

The array length is 3, so n = 4; the missing value is 4.

Example 3

nums = []return = 1

An empty array represents n = 1, so 1 is missing.

Constraints

  • 1 <= n <= 100000.
  • nums.length = n - 1.
  • Every element of nums is a distinct integer in the inclusive range [1, n].
  • Exactly one integer from 1 through n is absent.

More GEP problems

drafts saved locally
public int findMissingNumber(int[] nums) {
    // Write your code here
}
nums[1,2,4,5]
expected3
checking account