ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find Missing Number in Range

Go

Goal -- WPM

Ready
Exercise Algorithm Area
1package main
2
3import "fmt"
4
5// calculateExpectedSum computes the sum of numbers from 0 to n using the arithmetic series formula.
6func calculateExpectedSum(n int) int {
7 return n * (n + 1) / 2
8}
9
10// findMissingNumber finds the missing number in an array containing n distinct numbers from 0 to n.
11// The array has a length of n. This implementation uses the sum of numbers approach.
12// It handles edge cases such as an empty array or an array where 0 or n is missing.
13func findMissingNumber(nums []int) int {
14 n := len(nums)
15
16 // Edge case: empty array.
17 if n == 0 {
18 return 0 // If the array is empty, the range is 0..0, and 0 is missing.
19 }
20
21 // Calculate the expected sum of numbers from 0 to n.
22 expectedSum := calculateExpectedSum(n)
23
24 // Calculate the actual sum of numbers in the given array.
25 actualSum := 0
26 for _, num := range nums {
27 actualSum += num
28 }
29
30 // The missing number is the difference between the expected sum and the actual sum.
31 return expectedSum - actualSum
32}
33
34func main() {
35 nums1 := []int{3, 0, 1}
36 fmt.Printf("Missing number in %v: %d\n", nums1, findMissingNumber(nums1))
37
38 nums2 := []int{0, 1}
39 fmt.Printf("Missing number in %v: %d\n", nums2, findMissingNumber(nums2))
40
41 nums3 := []int{9, 6, 4, 2, 3, 5, 7, 0, 1}
42 fmt.Printf("Missing number in %v: %d\n", nums3, findMissingNumber(nums3))
43
44 nums4 := []int{0}
45 fmt.Printf("Missing number in %v: %d\n", nums4, findMissingNumber(nums4))
46
47 nums5 := []int{1}
48 fmt.Printf("Missing number in %v: %d\n", nums5, findMissingNumber(nums5))
49
50 nums6 := []int{}
51 fmt.Printf("Missing number in %v: %d\n", nums6, findMissingNumber(nums6))
52}
Algorithm description viewbox

Find Missing Number in Range

Algorithm description:

This Go program identifies the single missing number from an array that contains `n` distinct numbers taken from the range `0` to `n`. It leverages the property of arithmetic series to efficiently calculate the expected sum and then subtracts the actual sum of the array elements to find the missing value. This technique is highly efficient for problems involving sequences with a single missing element.

Algorithm explanation:

The `findMissingNumber` function determines the missing number in an array of `n` distinct numbers from the range `0` to `n`. It first calculates the expected sum of all numbers from `0` to `n` using the arithmetic series formula: `n * (n + 1) / 2`. Then, it iterates through the input array `nums` to compute the actual sum of its elements. The difference between the `expectedSum` and `actualSum` reveals the missing number. The time complexity is O(n) because the array is traversed once to calculate the actual sum. The space complexity is O(1) as it uses a constant amount of extra space. Edge cases such as an empty array (where `n=0`) are handled, correctly returning `0` as the missing number in the `0..0` range.

Pseudocode:

function findMissingNumber(nums):
  n = length(nums)
  if n == 0:
    return 0

  expectedSum = n * (n + 1) / 2
  actualSum = 0
  for each number num in nums:
    actualSum = actualSum + num

  return expectedSum - actualSum