0% completed
Problem Statement
You are given an array nums
of size n
, originally containing 1
to n
integers. Unfortunately, due to some error
, one number got duplicated
, replacing
another number that now goes missing
from the sequence.
Identify the duplicated number
and the missing number
and return them as a pair
.
Examples
-
Example 1:
- Input: nums =
[1,3,3,4]
- Expected Output:
[3,2]
- Justification: Here,
3
appears twice, and2
is missing from the sequence.
- Input: nums =
-
Example 2:
- Input: nums =
[2,2,3,5,4]
- Expected Output:
[2,1]
- Justification:
2
is duplicated, and1
is missing, making2
the repeated number and1
the missing one.
- Input: nums =
-
Example 3:
- Input: nums =
[1,5,3,2,7,7,6]
- Expected Output:
[7,4]
- Justification: In this sequence,
7
is the number that appears twice, and4
is absent, indicating7
as the duplicate and4
as the missing number.
- Input: nums =
Solution
To solve this problem, we'll employ an approach that meticulously scans through the given array to identify both the duplicate and the missing numbers. This method is efficient because it doesn't require sorting or additional data structures that significantly increase complexity. The core idea revolves around using the properties of numbers and their indices in the array, thus ensuring a solution that is both space and time-efficient.
Our strategy includes iterating over the array and marking visited numbers by flipping the sign of the number at the index corresponding to each value encountered. This technique helps us detect the duplicate number when we stumble upon a value pointing to an index with an already negative number. For finding the missing number, we'll go through the array once more to find the first positive number, indicating the index of the missing number. This approach stands out for its direct manipulation of the array, reducing the need for extra space and ensuring a quick solution.
Step-by-Step Algorithm
-
Initialize Variables: Start by initializing two variables,
duplicate
to -1 (to store the duplicated number) andmissing
to -1 to store missing number. -
Identify Duplicate: Loop through each number
n
in the input array.- Calculate the index
i
as the absolute value ofn
minus 1 (since arrays are 0-indexed). - If the value at index
i
in the array is negative, this meansn
is the duplicated number (as we have visited this index before). Setduplicate
ton
. - Otherwise, mark the value at index
i
as visited by multiplying it by -1.
- Calculate the index
-
Find Missing Number: Loop through the array again to find the missing number.
- For each index
i
, check if the value ati
is positive. - If found positive, it means this index (representing
i+1
number) was never visited, i.e.,i+1
is the missing number. Setmissing
toi+1
.
- For each index
-
Return Result: Return an array containing
duplicate
andmissing
as the first and second elements, respectively.
Algorithm Walkthrough
Let's consider the Input: [1,5,3,2,7,7,6]
-
Initialize Variables:
duplicate
= -1missing
= -1
-
Identify Duplicate:
-
Start with the original array:
[1, 5, 3, 2, 7, 7, 6]
-
Iteration 1: Process
n
= 1, marking index 0 (1-1=0). Updated array:[-1, 5, 3, 2, 7, 7, 6]
. -
Iteration 2: Process
n
= 5, marking index 4 (5-1=4). Updated array:[-1, 5, 3, 2, -7, 7, 6]
. -
Iteration 3: Process
n
= 3, marking index 2 (3-1=2). Updated array:[-1, 5, -3, 2, -7, 7, 6]
. -
Iteration 4: Process
n
= 2, marking index 1 (2-1=1). Updated array:[-1, -5, -3, 2, -7, 7, 6]
. -
Iteration 5: Process
n
= 7, marking index 6 (7-1=6). Updated array:[-1, -5, -3, 2, -7, 7, -6]
. -
Iteration 6: Process
n
= 7 again, find that index 6 is already negative, thus identifying7
as the duplicate. The array remains unchanged:[-1, -5, -3, 2, -7, 7, -6]
. -
Iteration 7: Process
n
= 6, marking index 5 (6-1=5). updated array:[-1, -5, -3, 2, -7, -7, -6]
.
-
-
Find Missing Number:
-
Revisiting the correctly updated array:
[-1, -5, -3, 2, -7, -7, -6]
, we look for the first positive number. -
The positive number is at index 3 (
2
), indicating that the number4
(index+1) is missing from the original array.
-
-
Return Result:
- The algorithm identifies
7
as the duplicate and4
as the missing number, so the result returned is[7, 4]
.
- The algorithm identifies
Code
Complexity Analysis
Time Complexity: O(n)
The algorithm iterates through the array of numbers twice. The first iteration is to identify the duplicate number by marking the indices visited (negative marking). The second iteration is to find the missing number by checking for a positive value in the modified array. Since each iteration goes through the array once, and there are two iterations, the time complexity is linear with respect to the size of the input array, n.
Space Complexity: O(1)
The solution modifies the input array in place to track visited numbers and does not use any additional data structures that grow with the input size. The space used for the output and a few variables does not depend on the input size, making the space complexity constant, O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible