2965. Find Missing and Repeated Values - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement:

You are given an array of ( n ) integers, where the integers are in the range ([1, n]). In this array, exactly one number appears twice (the repeated value) and exactly one number is missing (the missing value). Your task is to find the repeated number and the missing number and return them (typically as a pair or an array).

For example, if the array represents a set (\{1, 2, \ldots, n\}) with one error (one number occurs twice and one number is missing), determine which number is repeated and which number is missing.

Example Inputs and Outputs:

  1. Example 1:

    • Input: nums = [1, 2, 2, 4]
    • Output: [2, 3]
    • Explanation:
      The number 2 appears twice, and the number 3 is missing.
  2. Example 2:

    • Input: nums = [3, 1, 2, 5, 3]
    • Output: [3, 4]
    • Explanation:
      The number 3 is repeated, and the number 4 is missing.
  3. Example 3:

    • Input: nums = [1, 1]
    • Output: [1, 2]
    • Explanation:
      In an array of size 2, the numbers should be ([1, 2]). Here, 1 is repeated and 2 is missing.

Constraints:

  • (1 \leq n \leq 10^5) (the size of the array)
  • The array elements are in the range ([1, n]).
  • Exactly one value is repeated and exactly one value is missing.

Hints for Solving the Problem:

  1. Sorting Insight:

    • How can sorting the array help you quickly identify when a duplicate occurs and when a gap (missing number) is present?
  2. Counting with a Hash Table:

    • By using a hash map (or dictionary), you can count the frequency of each number. Which number appears twice, and which number doesn’t appear at all?
  3. Mathematical Relationships:

    • The expected sum of numbers from (1) to (n) is (S = frac{n(n+1)}{2}) and the expected sum of squares is (S_2 = \frac{n(n+1)(2n+1)}{6}). How can you use the differences between the actual and expected sums (and sum of squares) to derive equations that yield the repeated and missing numbers?

Approach 1: Sorting-Based Method

Step-by-Step Explanation:

  1. Sort the Array:

    • Sort the array in non-decreasing order.
  2. Scan Through the Sorted Array:

    • As you iterate over the sorted array, look for the duplicate (i.e. when ( \text{nums}[i] == \text{nums}[i+1] )).

    • Also check the gap between consecutive elements. If the difference is more than 1, the missing number lies in the gap.

    • Additionally, check the boundaries: if the first element is not 1 or the last element is not ( n ).

  3. Return the Result:

    • Once you identify both the repeated and the missing numbers, return them.

Python Code (Sorting Approach):

Python3
Python3

. . . .

Java Code (Sorting Approach):

Java
Java

. . . .

Approach 2: Hash Table (Counting) Method

Step-by-Step Explanation:

  1. Count Occurrences:

    • Traverse the array and count the frequency of each number using a hash map (or dictionary).
  2. Identify the Error:

    • The number that appears twice (frequency 2) is the duplicate.
    • The number that does not appear (frequency 0) in the range ([1, n]) is the missing number.

Python Code (Hash Table Approach):

Python3
Python3

. . . .

Java Code (Hash Table Approach):

Java
Java

. . . .

Approach 3: Mathematical Method

Step-by-Step Explanation:

  1. Compute Expected Sums:

    • The expected sum of numbers from (1) to (n) is:
      [ S = \frac{n(n+1)}{2} ]
    • The expected sum of squares is:
      [ S_2 = \frac{n(n+1)(2n+1)}{6} ]
  2. Compute Actual Sums:

    • Let (S_{actual}) be the sum of the given array and (S_{2,actual}) be the sum of squares of the given array.
  3. Set Up Equations:

    • Let (A) be the repeated number and (B) be the missing number. Then:
      [ S_{actual} - S = A - B ] and
      [ S_{2,actual} - S_2 = A^2 - B^2 = (A - B)(A + B) ]
    • With these two equations, solve for (A) and (B).

Python Code (Mathematical Approach):

Python3
Python3

. . . .

Java Code (Mathematical Approach):

Java
Java

. . . .

Complexity Analysis:

  • Sorting Approach:

    • Time Complexity: (O(n \log n)) due to sorting.
    • Space Complexity: (O(1)) if sorting is done in-place.
  • Hash Table Approach:

    • Time Complexity: (O(n))
    • Space Complexity: (O(n))
  • Mathematical Approach:

    • Time Complexity: (O(n))
    • Space Complexity: (O(1))

Edge Cases:

  1. Single Element Array:

    • For an array like ([x]) (with (n = 1)), the only valid output would be ([x, 2]) if (x \neq 1); however, by problem definition, (n \geq 2).
  2. All Zeros or Invalid Input:

    • Input always contains numbers in the range ([1, n]) with exactly one duplicate and one missing number. Deviations are not expected per constraints.

Common Mistakes:

  1. Off-by-One Errors:

    • When iterating over the range (1) to (n), ensure the boundaries are correctly handled.
  2. Incorrect Frequency Counting:

    • In the hash table approach, miscounting frequencies may lead to wrong identification of the duplicate or missing number.
  3. Integer Overflow:

    • In the mathematical approach, using a proper data type (like long in Java) is crucial when summing large numbers or squares.

Alternative Variations:

  1. Multiple Duplicates/Missing Values:
    • Variants of the problem may involve more than one duplicate or more than one missing value, requiring modified logic.
  2. Finding Only the Duplicate or Only the Missing Number:
    • Some problems focus solely on finding one of the two values, which may simplify the approach.

Related Problems for Further Practice:

  1. Set Mismatch (LeetCode 645):
    • A similar problem asking to find the duplicate and missing number.
  2. Find All Numbers Disappeared in an Array (LeetCode 448):
    • Identify all numbers missing from an array.
  3. Missing Number (LeetCode 268):
    • Find the single missing number when no duplicate exists.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Are cheap coding bootcamps worth it?
Prioritizing core algorithms for maximum interview readiness
Is Okta a good company to join?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;