Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Missing And Repeating

Easy Acceptance 24.83% Points 20.00

Given an unsorted array arr of positive integers. One number a from the set [1, 2,....,n] is missing and one number b occurs twice in the array. Find numbers a and b.

Note: The test cases are generated such that there always exists one missing and one repeating number within the range [1,n].

Examples
Example 1

Example 1

Input: arr[] = [2, 2]

Output: [2, 1]

Explanation: Repeating number is 2 and smallest positive missing number is 1.

Example 2

Example 2

Input: arr[] = [1, 3, 3]

Output: [3, 2]

Explanation: Repeating number is 3 and smallest positive missing number is 2.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 2 <= arr.size() <= 10^6
  • 1 <= arr[i] <= arr.size()
Companies
Samsung Walmart TCS Infosys
Topics
Array
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions