πDay 2. Missing in Array π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
You are given an array arr[] of size n - 1 that contains distinct integers in the range from 1 to n (inclusive).
The array represents a permutation of numbers from 1 to n with one number missing. Your task is to find the missing number.
π Example Walkthrough:
Example 1:
Input:
arr[] = [1, 2, 3, 5]
Output:
4
Explanation:
All numbers from 1 to 5 should be present. The number 4 is missing.
Example 2:
Input:
arr[] = [8, 2, 4, 5, 3, 7, 1]
Output:
6
Explanation:
All numbers from 1 to 8 should be present. The number 6 is missing.
Example 3:
Input:
arr[] = [1]
Output:
2
Explanation:
Only 1 is present, hence the missing number is 2.
Constraints:
$(1 \leq \text{arr.size()} \leq 10^6)$
$(1 \leq \text{arr[i]} \leq \text{arr.size()} + 1)$
π― My Approach:
Optimal XOR Approach
This approach relies on properties of XOR:
a β a = 0 and a β 0 = a
If you XOR all numbers from
1 to nand all elements in the array, the duplicate numbers cancel each other out, leaving only the missing number.
Algorithm Steps:
Initialize
x = 0.XOR all elements of the array with
x.XOR all numbers from
1tonwithx.The result is the missing number.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
O(n), as we make two passes over the array (XOR and loop from 1 to n).Expected Auxiliary Space Complexity:
O(1), as we use only a constant amount of additional space.
π Solution Code
Code (C)
Code (C++)
β‘ Alternative Approaches
π 2οΈβ£ Sum Formula
Algorithm Steps:
Calculate the expected sum of 1 to
nusing the formulan*(n+1)/2.Subtract the sum of the array from it to get the missing number.
π Complexity Analysis:
Time Complexity:
O(n)Space Complexity:
O(1)
β Why This Approach?
Simple and intuitive. Effective when integer overflow is handled using long long.
π 3οΈβ£ Sorting
Algorithm Steps:
Sort the array.
Traverse from 1 to
nand compare values at each index.The first mismatch is the missing number.
π Complexity Analysis:
Time Complexity:
O(n log n)Space Complexity:
O(1)
β Why This Approach?
Easy to understand and implement, but not optimal due to the sorting step.
π 4οΈβ£ Hashing (Boolean Array)
Algorithm Steps:
Create a boolean array of size
n+1.Mark visited indices.
The index with
falseis the missing number.
π Complexity Analysis:
Time Complexity:
O(n)Space Complexity:
O(n)
β Why This Approach?
Straightforward and effective. Especially helpful when array contains invalid or repeated values. Useful in debugging.
π Comparison of Approaches
Approach
β±οΈ Time Complexity
ποΈ Space Complexity
β Pros
β οΈ Cons
XOR Method
π’ O(n)
π’ O(1)
Fastest + no extra memory
Less intuitive
Sum Formula
π’ O(n)
π’ O(1)
Clean math-based
Needs care with overflow
Sorting
π΄ O(n log n)
π’ O(1)
Simple logic
Slower due to sorting
Hashing/Boolean
π’ O(n)
π΄ O(n)
Easy to read/debug
Uses extra memory
β
Best Choice?
Scenario
Recommended Approach
β No extra space allowed & optimal runtime needed
π₯ XOR Method
β Readable and simple with math knowledge
π₯ Sum Formula
β Array can be modified and performance matters less
Sorting
β Clarity and debugging are priorities
Hashing (Boolean Array)
πΉ Overall Best for runtime and space: XOR Method πΉ Best for clarity: Hashing or Sum Formula
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated