Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Missing In Array

Easy Acceptance 29.59% Points 20.00

You are given an array arr[] of size n - 1, containing distinct integers ranging from 1 to n (inclusive), except one missing element. The task is to find and return the missing integer from the range.

Examples
Example 1

Example 1

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

Output: 4

Explanation: All the numbers from 1 to 5 are present except 4.

Example 2

Example 2

Input: arr[] = [8, 2, 4, 5, 3, 7, 1]

Output: 6

Explanation: All the numbers from 1 to 8 are present except 6.

Example 3

Example 3

Input: arr[] = [1]

Output: 2

Explanation: Only 1 is present so the missing element is 2.

Hints
Hint 1
Expected Time Complexity O(N)
Hint 2
Expected Space Complexity O(1)
Constraints
  • 1 <= arr.size() <= 10^6
  • 1 <= arr[i] <= arr.size() + 1
Companies
Amazon Microsoft Visa Adobe TCS + 6 more
Topics
Array Searching Algorithms
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