Find the Missing Number from 1 to n
Updated
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[]) → intExamples
Example 1
nums = [1,2,4,5]return = 3Here n = 5. The only value from 1 through 5 absent from the array is 3.
Example 2
nums = [2,3,1]return = 4The array length is 3, so n = 4; the missing value is 4.
Example 3
nums = []return = 1An empty array represents n = 1, so 1 is missing.
Constraints
1 <= n <= 100000.nums.length = n - 1.- Every element of
numsis a distinct integer in the inclusive range[1, n]. - Exactly one integer from
1throughnis absent.
More GEP interview questions
Similar questions at other companies
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Minimum Power-of-Two Removal OperationsIBM · OAIBM interview questions
- Minimum Power-of-Two Removal OperationsVisa · OAVisa interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
- Count Valid Square PairsAuxoAI · OA
- Picked Player Who LostSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions