Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
Finding missing numbers in a sorted array is a common programming problem with multiple efficient solutions. This article explores three different approaches to identify missing numbers without using built-in functions in C#.
Each method has its own advantages in terms of time and space complexity, making them suitable for different scenarios depending on your specific requirements.
Using Sum Formula (Mathematical Approach)
This method uses the mathematical formula n(n+1)/2 to calculate the expected sum of consecutive numbers, then subtracts the actual sum of array elements to find the missing number −
using System;
public class MissingNumberFinder {
public int FindMissingSumMethod(int[] arr) {
int actualSum = 0;
for (int i = 0; i < arr.Length; i++) {
actualSum += arr[i];
}
int n = arr.Length + 1; // Total numbers including missing one
int expectedSum = (n * (n - 1)) / 2; // Formula for sum 0 to n-1
return expectedSum - actualSum;
}
}
class Program {
static void Main(string[] args) {
MissingNumberFinder finder = new MissingNumberFinder();
int[] arr = { 0, 1, 3, 4, 5 };
Console.WriteLine("Missing number using sum method: " + finder.FindMissingSumMethod(arr));
}
}
The output of the above code is −
Missing number using sum method: 2
Using Boolean Array (Marking Approach)
This method creates a boolean array to mark which numbers are present, then finds the unmarked position to identify the missing number −
using System;
public class MissingNumberFinder {
public int FindMissingBooleanMethod(int[] arr) {
bool[] present = new bool[arr.Length + 2]; // Extra space for missing number
// Mark present numbers as true
for (int i = 0; i < arr.Length; i++) {
if (arr[i] < present.Length) {
present[arr[i]] = true;
}
}
// Find the first false position
for (int i = 0; i < present.Length; i++) {
if (!present[i]) {
return i;
}
}
return -1; // No missing number found
}
}
class Program {
static void Main(string[] args) {
MissingNumberFinder finder = new MissingNumberFinder();
int[] arr = { 0, 1, 3, 4, 5 };
Console.WriteLine("Missing number using boolean method: " + finder.FindMissingBooleanMethod(arr));
}
}
The output of the above code is −
Missing number using boolean method: 2
Using XOR Operation (Bit Manipulation)
This method leverages the XOR operation property where a ^ a = 0 and a ^ 0 = a. By XORing all expected numbers with all array elements, the missing number remains −
using System;
public class MissingNumberFinder {
public int FindMissingXORMethod(int[] arr) {
int result = 0;
int n = arr.Length + 1;
// XOR all numbers from 0 to n-1
for (int i = 0; i < n; i++) {
result ^= i;
}
// XOR all array elements
for (int i = 0; i < arr.Length; i++) {
result ^= arr[i];
}
return result;
}
}
class Program {
static void Main(string[] args) {
MissingNumberFinder finder = new MissingNumberFinder();
int[] arr = { 0, 1, 3, 4, 5 };
Console.WriteLine("Missing number using XOR method: " + finder.FindMissingXORMethod(arr));
}
}
The output of the above code is −
Missing number using XOR method: 2
Complete Example with All Methods
using System;
public class MissingNumberSolutions {
public int SumMethod(int[] arr) {
int actualSum = 0;
for (int i = 0; i < arr.Length; i++) {
actualSum += arr[i];
}
int n = arr.Length + 1;
int expectedSum = (n * (n - 1)) / 2;
return expectedSum - actualSum;
}
public int BooleanMethod(int[] arr) {
bool[] present = new bool[arr.Length + 2];
for (int i = 0; i < arr.Length; i++) {
if (arr[i] < present.Length) {
present[arr[i]] = true;
}
}
for (int i = 0; i < present.Length; i++) {
if (!present[i]) {
return i;
}
}
return -1;
}
public int XORMethod(int[] arr) {
int result = 0;
int n = arr.Length + 1;
for (int i = 0; i < n; i++) {
result ^= i;
}
for (int i = 0; i < arr.Length; i++) {
result ^= arr[i];
}
return result;
}
}
class Program {
static void Main(string[] args) {
MissingNumberSolutions solutions = new MissingNumberSolutions();
int[] arr = { 0, 1, 3, 4, 5 };
Console.WriteLine("Array: [0, 1, 3, 4, 5]");
Console.WriteLine("Sum Method: " + solutions.SumMethod(arr));
Console.WriteLine("Boolean Method: " + solutions.BooleanMethod(arr));
Console.WriteLine("XOR Method: " + solutions.XORMethod(arr));
}
}
The output of the above code is −
Array: [0, 1, 3, 4, 5] Sum Method: 2 Boolean Method: 2 XOR Method: 2
Method Comparison
| Method | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| Sum Formula | O(n) | O(1) | When memory is limited and overflow is not a concern |
| Boolean Array | O(n) | O(n) | When multiple missing numbers need to be found |
| XOR Operation | O(n) | O(1) | Most efficient for single missing number, avoids overflow |
Conclusion
The three methods for finding missing numbers each have distinct advantages: the sum formula is mathematically elegant, the boolean array approach is intuitive and extensible, while XOR operation provides the most robust solution by avoiding potential integer overflow issues. Choose the method that best fits your specific requirements for performance and memory usage.
