🧠
Optimal Approach (Cyclic Sort In-Place)
💡 This approach uses the cyclic sort pattern to place each number at its correct index, enabling detection of duplicate and missing numbers in O(n) time and O(1) space.
Intuition
Since numbers are from 1 to n, each number should be at index number-1. By swapping elements to their correct positions, the duplicate will cause a conflict, and the missing number will be the index where the correct number is not present.
Algorithm
- Initialize index i to 0.
- While i < n, if nums[i] is not at its correct position and not equal to the number at its target position, swap it with the number at its correct position.
- Else, increment i.
- After sorting, traverse the array to find the index where nums[i] != i + 1; that index + 1 is missing and nums[i] is duplicate.
💡 This approach rearranges the array in-place to reveal the corrupt pair without extra space or sorting overhead.
def find_corrupt_pair(nums):
i = 0
n = len(nums)
while i < n:
correct_index = nums[i] - 1
if nums[i] != nums[correct_index]:
nums[i], nums[correct_index] = nums[correct_index], nums[i]
else:
i += 1
for i in range(n):
if nums[i] != i + 1:
return [nums[i], i + 1]
return [-1, -1]
# Driver code
if __name__ == '__main__':
arr = [3, 1, 2, 5, 3]
print(find_corrupt_pair(arr)) # Output: [3, 4]
Line Notes
i = 0Start index for traversal
n = len(nums)Store length of array
correct_index = nums[i] - 1Calculate correct position for current number
if nums[i] != nums[correct_index]Check if current number is at correct position or needs swapping
nums[i], nums[correct_index] = nums[correct_index], nums[i]Swap to place number at correct index
else: i += 1Increment index only if no swap needed to avoid infinite loop
for i in range(n)Traverse to find mismatch after sorting
if nums[i] != i + 1Mismatch indicates duplicate and missing numbers
return [nums[i], i + 1]Return duplicate and missing pair
public class Solution {
public static int[] findCorruptPair(int[] nums) {
int i = 0, n = nums.length;
while (i < n) {
int correctIndex = nums[i] - 1;
if (nums[i] != nums[correctIndex]) {
int temp = nums[i];
nums[i] = nums[correctIndex];
nums[correctIndex] = temp;
} else {
i++;
}
}
for (i = 0; i < n; i++) {
if (nums[i] != i + 1) {
return new int[]{nums[i], i + 1};
}
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
int[] arr = {3, 1, 2, 5, 3};
int[] res = findCorruptPair(arr);
System.out.println(java.util.Arrays.toString(res)); // Output: [3, 4]
}
}
Line Notes
int i = 0, n = nums.length;Initialize index and array length
int correctIndex = nums[i] - 1;Calculate correct position for current element
if (nums[i] != nums[correctIndex])Check if swap needed to place element correctly
int temp = nums[i]; nums[i] = nums[correctIndex]; nums[correctIndex] = temp;Perform swap to correct position
else { i++; }Increment index only if no swap to avoid infinite loop
for (i = 0; i < n; i++)Scan array to find mismatch after sorting
if (nums[i] != i + 1)Mismatch indicates corrupt pair
return new int[]{nums[i], i + 1};Return duplicate and missing numbers
#include <bits/stdc++.h>
using namespace std;
vector<int> findCorruptPair(vector<int>& nums) {
int i = 0, n = (int)nums.size();
while (i < n) {
int correctIndex = nums[i] - 1;
if (nums[i] != nums[correctIndex]) {
swap(nums[i], nums[correctIndex]);
} else {
i++;
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) {
return {nums[i], i + 1};
}
}
return {-1, -1};
}
int main() {
vector<int> arr = {3, 1, 2, 5, 3};
vector<int> res = findCorruptPair(arr);
cout << "[" << res[0] << ", " << res[1] << "]\n"; // Output: [3, 4]
return 0;
}
Line Notes
int i = 0, n = (int)nums.size();Initialize index and array size
int correctIndex = nums[i] - 1;Calculate correct index for current element
if (nums[i] != nums[correctIndex])Check if current element is misplaced
swap(nums[i], nums[correctIndex]);Swap to place element correctly
else { i++; }Increment index only if no swap to avoid infinite loop
for (int i = 0; i < n; i++)Scan array to find mismatch after sorting
if (nums[i] != i + 1)Mismatch indicates duplicate and missing
return {nums[i], i + 1};Return the corrupt pair
function findCorruptPair(nums) {
let i = 0, n = nums.length;
while (i < n) {
let correctIndex = nums[i] - 1;
if (nums[i] !== nums[correctIndex]) {
[nums[i], nums[correctIndex]] = [nums[correctIndex], nums[i]];
} else {
i++;
}
}
for (let i = 0; i < n; i++) {
if (nums[i] !== i + 1) {
return [nums[i], i + 1];
}
}
return [-1, -1];
}
// Test
console.log(findCorruptPair([3, 1, 2, 5, 3])); // Output: [3, 4]
Line Notes
let i = 0, n = nums.length;Initialize index and array length
let correctIndex = nums[i] - 1;Calculate correct position for current number
if (nums[i] !== nums[correctIndex])Check if swap needed to place number correctly
[nums[i], nums[correctIndex]] = [nums[correctIndex], nums[i]];Swap elements in-place
else { i++; }Increment index only if no swap to avoid infinite loop
for (let i = 0; i < n; i++)Traverse array to find mismatch after sorting
if (nums[i] !== i + 1)Mismatch indicates duplicate and missing
return [nums[i], i + 1];Return the corrupt pair
Each element is swapped at most once to its correct position, so linear time. No extra space used except variables.
💡 For n=100,000, this approach is very efficient and uses minimal memory, ideal for interviews.
Interview Verdict: Accepted and optimal
This is the best approach to implement in interviews for this problem due to its efficiency and in-place operation.