Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Problem Statement
Given:
An array of size n - 1 containing distinct integers from 1 to n.
Task:
Find the missing number in the sequence.
🧾 Sample Input/Output
Input:
int[] arr = {1, 2, 4, 6, 3, 7, 8};
Output:
5
Explanation:
Numbers from 1 to 8 are expected. 5 is missing.
🔍 Naive Approach: Linear
Search for Every Element
Check for each number from 1 to n, whether it’s present.
public
static int findMissingNaive(int[] arr, int n) {
for (int i = 1; i <= n; i++) {
boolean found = false;
for (int j : arr) {
if (j == i) {
found = true;
break;
}
}
if (!found) return i;
}
return -1;
}
⛔ Time: O(n²)
⛔
Too slow for large arrays.
✅ Optimized Approach: Sum Formula
Sum of 1 to n = n * (n + 1) / 2
Subtract the actual sum to find the missing number.
public
static int findMissingSum(int[] arr, int n) {
int total = n * (n + 1) / 2;
int sum = 0;
for (int num : arr) {
sum += num;
}
return total - sum;
}
✅ Time: O(n)
✅
Space: O(1)
✅ XOR Approach (No Overflow Risk)
public
static int findMissingXOR(int[] arr, int n) {
int xor1 = 0, xor2 = 0;
for (int i = 1; i <= n; i++) xor1 ^= i;
for (int num : arr) xor2 ^= num;
return xor1 ^ xor2;
}
✅ Works even if values are large
(no overflow)
✅
Based on property: a ^ a = 0, and a ^ 0 = a
🧠 Edge Cases
Input |
Output |
Notes |
[2], n = 2 |
1 |
One missing at start |
[1, 2, 3, 5], n = 5 |
4 |
Middle missing |
[1, 2, 3, 4], n = 5 |
5 |
End missing |
[1, 2, 3, 4, 5], n = 5 |
❌ |
Nothing missing — validate input |
🚀 Real Interview
Challenge #1: Validate Input Before Searching
if
(arr.length != n - 1) {
throw new
IllegalArgumentException("Array size must be n - 1");
}
✅ Always sanitize input first —
interviewers love to test this.
🚀 Real Interview
Challenge #2: Find Missing and Duplicate (when one is repeated)
// e.g., [1, 2, 2, 4, 5] → missing = 3, repeated = 2
Use a frequency map or modify input using index marking
(advanced version)
🚀 Real Interview
Challenge #3: Return Missing Number Without Using Extra Space
You’ve already done it! ✅ XOR and SUM methods use O(1)
space
Use these to show optimization under memory constraints.
✅ Summary
Method |
Time |
Space |
Suitable For |
Brute Force |
O(n²) |
O(1) |
Not practical |
Sum Formula |
O(n) |
O(1) |
Simple, elegant, can overflow |
XOR Trick |
O(n) |
O(1) |
Fast, overflow-safe |
Because
they test core programming logic, data handling, loops, and algorithm
efficiency.
int[] is a primitive array; Integer[] is an array of objects (wrappers). The latter allows null values and works with collections.
For fixed-size problems, use arrays. For dynamic data, ArrayList is better — but stick to arrays unless otherwise asked
Index out of bounds, mutating arrays while iterating, and forgetting that Java arrays have fixed size.
Use a Set for uniqueness or sort the array first and remove duplicates in-place using two pointers.
It’s great for sorted arrays, especially for problems involving pair sums, removing duplicates, and reversing data in-place.
Use reversal techniques or cyclic replacements to do it in O(1) space.
Linear search = O(n); binary search = O(log n), but only on sorted arrays.
Always consider edge cases: empty arrays, one element, all duplicates, etc. Optimize with hashmaps or prefix sums where needed.
Use Arrays.sort(), System.arraycopy(), Arrays.toString(), and Collections when applicable — but show the manual solution first in interviews.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)