arrays
algorithms
linear-time
constant-space
duplicate-elements

Find the missing and duplicate elements in an array in linear time and constant space

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In computer science, arrays are fundamental data structures used for storing elements of the same type. Often, problems revolve around effectively managing and manipulating these elements. One such challenge is identifying missing and duplicate elements in an array. Typically, solutions have a complexity of O(n log n) due to sorting or O(n) space complexity due to auxiliary data structures. However, it is possible to solve this problem in linear time (O(n)) with constant space (O(1)) via algorithmic techniques. This article will explore this solution and its underlying concepts.

Problem Statement

Given an array containing `n` numbers taken from the range `1` to `n`, where one of these numbers is missing and another is a duplicate, the objective is to determine both the missing and duplicate numbers.

Solution Approach

This problem can be solved using the properties of numbers and arithmetic operations. By leveraging the relationship between the expected sum of numbers, the sum of squares, and their actuals, we can identify the missing and duplicate elements.

Expected Sum and Sum of Squares

  1. Sum of numbers (S):
    The expected sum of numbers from `1` to `n` is:

S_expected=n×(n+1)2S\_{\text{expected}} = \frac{n \times (n + 1)}{2}

  1. Sum of squares (S2):
    The expected sum of squares of numbers from `1` to `n` is:

S2_expected=n×(n+1)×(2n+1)6S2\_{\text{expected}} = \frac{n \times (n + 1) \times (2n + 1)}{6}

  1. Actual Sums:
    Compute the actual sum of numbers and the sum of squares in the given array, denoted as `S_actual` and `S2_actual`.

Identifying Missing and Duplicate Elements

By establishing the discrepancies between expected and actual sums, we can deduce the missing and duplicate numbers.

  1. Difference of Sums (D1):

D1=S_actualS_expectedD1 = S\_{\text{actual}} - S\_{\text{expected}}

Here, `D1` gives the value of the duplicate number minus the missing number.

  1. Difference of Squares (D2):

D2=S2_actualS2_expectedD2 = S2\_{\text{actual}} - S2\_{\text{expected}}

  1. Solving for Missing and Duplicate:
    Using the values obtained from `D1` and `D2`, we can set up the following equations:
    1. xy=D1x - y = D1
      (duplicate - missing)
    2. x2y2=D2x^2 - y^2 = D2
      can be rewritten as (xy)(x+y)=D2(x - y)(x + y) = D2 Solving these equations:
    • From equation 1: xy=D1x - y = D1, we get x=y+D1x = y + D1.
    • Substitute `x` in the transformed equation 2:

(y+D1+y)D1=D2(y + D1 + y) \cdot D1 = D2

2y+D1=D2D12y + D1 = \frac{D2}{D1}

• From the above, we can solve for yy (missing number):

y=D2D1D12y = \frac{\frac{D2}{D1} - D1}{2}

• Substitute yy back to find xx (duplicate number):

x=y+D1x = y + D1

Example

Consider an array `[4, 3, 6, 2, 1, 1]` for `n = 6`.

• Expected sum, Sexpected=21S_{\text{expected}} = 21. • Expected sum of squares, S2expected=91S2_{\text{expected}} = 91.

Calculate:

• Actual sum, Sactual=17S_{\text{actual}} = 17. • Actual sum of squares, S2actual=55S2_{\text{actual}} = 55.

Compute differences:

D1=SactualSexpected=4D1 = S_{\text{actual}} - S_{\text{expected}} = -4D2=S2actualS2expected=36D2 = S2_{\text{actual}} - S2_{\text{expected}} = -36

Solve equations:

xy=4x - y = -4D2D1=9\frac{D2}{D1} = 9, thus 2y=13y=52y = 13 \Rightarrow y = 5 • Substitute yy in xy=4x - y = -4: x=1x = 1

Thus, the missing number is 55, and the duplicate is 11.

Summary Table

Here is a quick reference of the key components and formulas:

ComponentFormula/Value
Expected SumSexpected=n×(n+1)2S_{\text{expected}} = \frac{n \times (n + 1)}{2}
Expected Sum of SquaresS2expected=n×(n+1)×(2n+1)6S2_{\text{expected}} = \frac{n \times (n + 1) \times (2n + 1)}{6}
Difference of SumsD1=SactualSexpectedD1 = S_{\text{actual}} - S_{\text{expected}}
Difference of SquaresD2=S2actualS2expectedD2 = S2_{\text{actual}} - S2_{\text{expected}}
Formula for Missingy=D2D1D12y = \frac{\frac{D2}{D1} - D1}{2}
Formula for Duplicatex=y+D1x = y + D1

Conclusion

Finding the missing and duplicate numbers in an array can be efficiently solved using basic arithmetic properties and algebraic manipulation. This approach guarantees a solution in linear time with constant extra space, optimizing both computational and space resources. These techniques showcase the power of mathematical reasoning in algorithm design and enhance our capability to tackle complex data structure problems effectively.


Course illustration
Course illustration

All Rights Reserved.