Find Missing Number in Array 1 to N with O(n) Solution

Got asked this in my interview — "Find the missing number in an array of 1 to N" Simple question, but interviewers use it to test your math intuition and whether you know an O(n) solution. Here's what you should know: 👉 The idea : 🔹 Sum of 1 to N = N*(N+1)/2 — subtract the actual array sum. The difference is the missing number. #Python #DSA #CodingInterview #InterviewPrep #PlacementPrep

  • No alternative text description for this image

And yet that executes in worst case time and doesn't generalize to different expected properties. What is the argument against "expected = current + 1" and break on "current <> expected"?

Like
Reply

To view or add a comment, sign in

Explore content categories