Python Interview coding 3 With Funny Example

```html

🔢 How to Find the Missing Number in an Array (Python Solution)

Given an array of n distinct numbers taken from 0 to n, one number is missing. Our goal is to find that missing number efficiently. 🚀

Let’s explore two different ways to solve this problem in Python!

🛠️ Method 1: Using the Sum Formula

We can use the mathematical formula for the sum of the first n natural numbers:

Sum = (n * (n + 1)) / 2

If we subtract the sum of the given array from this expected sum, we get the missing number.


def find_missing_number(nums):
    n = len(nums)  # Total count should be n + 1
    expected_sum = (n * (n + 1)) // 2  # Formula for sum of first n numbers
    actual_sum = sum(nums)  # Sum of elements in the given array
    return expected_sum - actual_sum  # The difference gives the missing number

# Example Usage
nums = [3, 0, 1]
print(f"Missing number: {find_missing_number(nums)}")  # Output: 2

    

🔍 Explanation (Line-by-Line)

  • n = len(nums) → Since numbers are from 0 to n, there should be n+1 elements.
  • expected_sum = (n * (n + 1)) // 2 → Compute the sum of numbers from 0 to n.
  • actual_sum = sum(nums) → Get the sum of the given array.
  • return expected_sum - actual_sum → The difference gives the missing number.

📊 Time and Space Complexity

  • Time Complexity: O(n) → We compute the sum of elements once.
  • Space Complexity: O(1) → Only a few extra variables are used.

✅ Method 2: Using XOR

The XOR trick works because a ⊕ a = 0 for any number a, and 0 ⊕ b = b. If we XOR all numbers from 0 to n with the given numbers, only the missing number remains.


def find_missing_number_xor(nums):
    n = len(nums)
    xor_all = 0
    xor_arr = 0

    # XOR all numbers from 0 to n
    for i in range(n + 1):
        xor_all ^= i  

    # XOR all elements in the array
    for num in nums:
        xor_arr ^= num  

    return xor_all ^ xor_arr  # Missing number remains

# Example Usage
nums = [3, 0, 1]
print(f"Missing number: {find_missing_number_xor(nums)}")  # Output: 2

    

🔍 Explanation (Line-by-Line)

  • xor_all = 0 → Variable to store XOR of all numbers from 0 to n.
  • xor_arr = 0 → Variable to store XOR of elements in the array.
  • for i in range(n + 1): xor_all ^= i → XOR all numbers from 0 to n.
  • for num in nums: xor_arr ^= num → XOR all elements in the array.
  • return xor_all ^ xor_arr → The missing number remains.

📊 Time and Space Complexity

  • Time Complexity: O(n) → We iterate through the numbers twice.
  • Space Complexity: O(1) → No extra storage used.

🏆 Conclusion: Which Method is Best?

Method Pros Cons Time Complexity Space Complexity
Sum Formula Simple and easy to understand May cause overflow for large numbers O(n) O(1)
XOR Method Efficient and avoids overflow Harder to understand O(n) O(1)

Both methods work well! The XOR method is better for large numbers, while the sum formula is easier to understand. Try both and see which one you like! 🚀

Have you ever lost something and had to find it? This problem is just like that! Let me know in the comments what you think. 😆

python-interview-coding-4-with-funny-example ```

Comments