Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Missing In Array

Easy Acceptance 29.59% Points 20

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.

Constraints
  • 1 <= arr.size() <= 10^6
  • 1 <= arr[i] <= arr.size() + 1
Hints
  • Expected Time Complexity O(N)
  • Expected Space Complexity O(1)
Topics
Array Searching Algorithms
Companies
Amazon Microsoft Visa Adobe TCS Intuit Cisco DE Shaw Flipkart Morgan Stanly Qualcomm
Added 30 Nov 2024 29.59% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3 5
4