ℹ️ 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

Objective-C

Goal -- WPM

Ready
Exercise Algorithm Area
1#import <Foundation/Foundation.h>
2
3// Helper function to calculate the sum of numbers from 0 to n
4long long sumOfRange(NSInteger n) {
5// Using the formula n*(n+1)/2
6return (long long)n * (n + 1) / 2;
7}
8
9// Function to find the missing number in an array
10NSInteger findMissingNumber(NSArray<NSNumber *> *nums) {
11NSInteger n = nums.count;
12
13// Calculate the expected sum of numbers from 0 to n
14long long expectedSum = sumOfRange(n);
15
16// Calculate the actual sum of numbers in the given array
17long long actualSum = 0;
18for (NSNumber *num in nums) {
19actualSum += num.integerValue;
20}
21
22// The missing number is the difference between the expected sum and the actual sum
23return (NSInteger)(expectedSum - actualSum);
24}
25
26// Alternative implementation using XOR
27NSInteger findMissingNumberXOR(NSArray<NSNumber *> *nums) {
28NSInteger n = nums.count;
29NSInteger missing = n;
30
31for (NSInteger i = 0; i < n; i++) {
32missing ^= i ^ nums[i].integerValue;
33}
34
35return missing;
36}
37
38int main(int argc, const char * argv[]) {
39@autoreleasepool {
40NSArray<NSNumber *> *testArray1 = @[@3, @0, @1];
41NSLog(@"Array: %@, Missing: %ld", testArray1, findMissingNumber(testArray1)); // Expected: 2
42NSLog(@"Array: %@, Missing (XOR): %ld", testArray1, findMissingNumberXOR(testArray1)); // Expected: 2
43
44NSArray<NSNumber *> *testArray2 = @[@0, @1];
45NSLog(@"Array: %@, Missing: %ld", testArray2, findMissingNumber(testArray2)); // Expected: 2
46NSLog(@"Array: %@, Missing (XOR): %ld", testArray2, findMissingNumberXOR(testArray2)); // Expected: 2
47
48NSArray<NSNumber *> *testArray3 = @[@9, @6, @4, @2, @3, @5, @7, @0, @1];
49NSLog(@"Array: %@, Missing: %ld", testArray3, findMissingNumber(testArray3)); // Expected: 8
50NSLog(@"Array: %@, Missing (XOR): %ld", testArray3, findMissingNumberXOR(testArray3)); // Expected: 8
51
52NSArray<NSNumber *> *testArray4 = @[@0];
53NSLog(@"Array: %@, Missing: %ld", testArray4, findMissingNumber(testArray4)); // Expected: 1
54NSLog(@"Array: %@, Missing (XOR): %ld", testArray4, findMissingNumberXOR(testArray4)); // Expected: 1
55
56NSArray<NSNumber *> *testArray5 = @[@1];
57NSLog(@"Array: %@, Missing: %ld", testArray5, findMissingNumber(testArray5)); // Expected: 0
58NSLog(@"Array: %@, Missing (XOR): %ld", testArray5, findMissingNumberXOR(testArray5)); // Expected: 0
59}
60return 0;
61}
Algorithm description viewbox

Find Missing Number in Range

Algorithm description:

This Objective-C code finds a single missing number in an array that contains n distinct numbers taken from the range 0 to n. Two methods are provided: one using the sum of numbers and another using the XOR operation. Both methods are efficient and avoid sorting the array. This is a common problem in coding interviews and is useful for understanding arithmetic series properties and bitwise operations.

Algorithm explanation:

The `findMissingNumber` function leverages the property of arithmetic series. It first calculates the expected sum of all numbers from 0 to n (where n is the count of numbers in the input array) using the formula `n * (n + 1) / 2`. Then, it iterates through the given array and calculates the actual sum of its elements. The difference between the `expectedSum` and `actualSum` will be the missing number. This method has a time complexity of O(n) due to the single pass through the array and a space complexity of O(1) as it only uses a few variables. The `findMissingNumberXOR` function uses the XOR bitwise operation. The XOR of all numbers from 0 to n, XORed with the XOR of all numbers in the array, will result in the missing number. This is because `a ^ a = 0` and `a ^ 0 = a`. Each number present in both the range and the array cancels itself out, leaving only the missing number. This method also has O(n) time complexity and O(1) space complexity. Both methods correctly handle edge cases such as an empty array (n=0, missing=0) or an array with only one element.

Pseudocode:

function findMissingNumber(array nums):
  n = length of nums
  expectedSum = sum of numbers from 0 to n
  actualSum = 0
  for each number in nums:
    actualSum = actualSum + number
  return expectedSum - actualSum

function findMissingNumberXOR(array nums):
  n = length of nums
  missing = n
  for i from 0 to n-1:
    missing = missing XOR i XOR nums[i]
  return missing