Find Missing Number in SequenceLoading saved progress…

Find Missing Number in Sequence

You're given a sorted array of consecutive integers that runs from nums[0] up to nums[nums.length - 1], with exactly one integer missing from the middle of the run. Return that missing integer. The endpoints are always present, so the array's first and last values mark the true bounds of the sequence.

Signature

// nums:    number[]   — sorted, consecutive integers with exactly ONE gap.
//                       The first and last elements are the true endpoints.
// returns: number     — the single integer missing from the run.
function arrayFindMissingNumberInSequence(nums): number;

Examples

// 3 is missing between 2 and 4.
arrayFindMissingNumberInSequence([1, 2, 4, 5]);
// → 3
// The run starts at 10, not 1; 12 is the gap.
arrayFindMissingNumberInSequence([10, 11, 13, 14]);
// → 12

Notes

  • Exactly one number is missing. The array is one element short of a complete run. You don't have to handle the no-gap or multiple-gaps cases.
  • The endpoints are assumed present. nums[0] is the true start and the last element is the true end, so the missing number always sits strictly between them — never before the first or after the last.
  • The run can start anywhere. It may begin at 0, at a positive number, or at a negative number like -3. Don't assume it starts at 1.
  • Stay O(1) on space. You only need to read each element once; you don't need an extra array or set.
  • Don't mutate the input. Read nums; return a single number.
Loading editor…