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

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

KQL: Find Missing Number in a Sequence

Kusto Query Language (KQL)

Goal -- WPM

Ready
Exercise Algorithm Area
1let findMissingNumber = (numbers: dynamic) {
2let n = array_length(numbers);
3if (n == 0) {
4print 0; // If array is empty, 0 is the missing number in sequence 0..0
5return;
6}
7
8// Calculate the expected sum of numbers from 0 to n
9let expectedSum = n * (n + 1) / 2;
10
11// Calculate the actual sum of numbers in the given array
12let actualSum = 0;
13for (let i = 0; i < n; i++) {
14actualSum = actualSum + numbers[i];
15}
16
17// The missing number is the difference between expected and actual sum
18print expectedSum - actualSum;
19};
20
21// Example Usage:
22findMissingNumber(dynamic([3, 0, 1])); // Expected: 2
23findMissingNumber(dynamic([0, 1])); // Expected: 2
24findMissingNumber(dynamic([9, 6, 4, 2, 3, 5, 7, 0, 1])); // Expected: 8
25findMissingNumber(dynamic([])); // Expected: 0
26findMissingNumber(dynamic([0])); // Expected: 1
Algorithm description viewbox

KQL: Find Missing Number in a Sequence

Algorithm description:

This KQL function finds a single missing number in a sequence of distinct numbers ranging from 0 to N. It leverages the mathematical property that the sum of numbers from 0 to N can be easily calculated. By comparing this expected sum with the actual sum of the provided numbers, the missing number can be identified. This is useful in scenarios like data integrity checks or when reconstructing incomplete datasets where a single value is known to be absent.

Algorithm explanation:

The `findMissingNumber` function takes a dynamic array `numbers` which is assumed to contain distinct numbers from 0 to N, with exactly one number missing. It first determines `n`, the expected upper bound of the sequence (which is the length of the input array). It then calculates the `expectedSum` of numbers from 0 to `n` using the formula `n * (n + 1) / 2`. Next, it iterates through the input array to calculate the `actualSum` of its elements. The missing number is simply the difference between `expectedSum` and `actualSum`. An edge case for an empty array is handled by returning 0, assuming the sequence was meant to be just `[0]`. The time complexity is O(N) because it iterates through the array once to calculate the sum. The space complexity is O(1) as it only uses a few variables for calculations.

Pseudocode:

function findMissingNumber(numbers):
  n = length of numbers
  if n is 0:
    return 0

  expectedSum = n * (n + 1) / 2
  actualSum = 0

  for each number in numbers:
    actualSum = actualSum + number

  return expectedSum - actualSum