! Missing Number


Go Placed at Top Product Companies with Scaler

Industry Vetted Curriculum
Live Classes past Manufacture Experts
1:1 Mentorship with Experts
Career Support


  • Problem Statement
  • Uncomplicated Approach
    • C Implementation
    • Java Implementation
    • Python Implementation
  • XoR Approach
    • Dry Run of XoR Approach
    • C Implementation of XoR Approach
    • Java Implementation of XoR Approach
    • Python Implementation of XoR Arroyo
  • Do Questions
  • Frequently Asked Questions

Problem Statement

You are given an array of integers of size N – i ranging from 1 to N. There are no duplicates in the list. I of the integers is missing in the assortment. The task is to observe the missing number in the series.

Examples:

Input: list[] = {ane, 2, 4, 6, iii, vii, 8,10,5}
Output: 9
Explanation: The missing number from 1 to 10 is 9.

Input: list[] = {3, 2, 4, 5}
Output: 3
Caption: The missing number from 1 to 5 is ane.

Missing Number

Simple Arroyo

This method uses the formula of summation.

The size of the array is N – 1. So the sum of northward elements, that is the sum of numbers from 1 to N can be calculated by using the formula N * (Northward + 1) / 2. At present find the summation of all elements in the array and subtract it from the summation of first N natural numbers, the value obtained will be the value of the missing element.

Algorithm:

  • Calculate the summation of beginning N natural numbers as Total = Northward * (N + one) / 2
  • Create a variable sum to shop the summation of elements of the array.
  • Iterate the array from showtime to end.
  • Updating the value of sum every bit sum = sum + array[i]
  • Impress the missing number as the Full – sum

C Implementation

int MissingNo(int a[], int n) {   int i, full;   total = (north + 1) * (n + 2) / 2;   for (i = 0; i < due north; i++)     total -= a[i];   return total; }

Coffee Implementation

MissingNumbers(int[] arr) {   for (int i = 0; i < arr.length; i++) {     int index = Math.abs(arr[i]);     if (arr[index - ane] > 0) {       arr[index - 1] *= -1;     }   }   List < Integer > ans = new ArrayList < > ();   for (int i = 0; i < arr.length; i++) {     if (arr[i] > 0) {       ans.add together(i + 1);     }   }   return ans;  }

Python Implementation

def MissingNo(arr):     north = len(arr)     total = (northward + 1)*(n + two)/2     arr_sum = sum(arr)     return full - arr_sum

Time Complexity: O(Northward) where N is the size of the assortment.
Space Complexity: O(1) because no extra infinite is needed.

XoR Arroyo

This method uses the technique of XOR to solve the problem.

Approach: XOR has certain properties Assume
a1 ^ a2 ^ a3 ^ …^ an = A1 and a1 ^ a2 ^ a3 ^ …^ an-1 = A2, And then A1 ^ A2 = an

Algorithm:

  • Create two variables a1= 0 and a2 = 0
  • Iterate from 1 to north with i as the counter.
  • For every index i update a1 as a1 = a1 ^ i
  • At present iterate over the array from starting time to end.
  • For every index i update a2 equally a2 = a2 ^ arr[i]
  • Print the missing number as a1 ^ a2.

Dry Run of XoR Arroyo

Dry Run of XoR Approach
Dry Run of XoR Arroyo

C Implementation of XoR Approach

int MissingNo(int arr[], int northward) {   int x1 = arr[0];   int x2 = 1;    for (int i = i; i < n; i++)     x1 = x1 ^ arr[i];    for (int i = 2; i <= n + 1; i++)     x2 = x2 ^ i;    return (x1 ^ x2); }

Coffee Implementation of XoR Approach

static int MissingNo(int arr[], int northward) {   int x1 = arr[0];   int x2 = 1;    for (int i = ane; i < north; i++)     x1 = x1 ^ arr[i];    for (int i = 2; i <= n + i; i++)     x2 = x2 ^ i;    return (x1 ^ x2); }

Python Implementation of XoR Arroyo

def MissingNo(arr, due north):     x1 = arr[0]     x2 = 1           for i in range(1, due north):         x1 = x1 ^ arr[i]               for i in range(two, n + ii):         x2 = x2 ^ i           render x1 ^ x2

Time Complexity: O(N) where N is the size of the assortment because only 1 traversal is needed.
Space Complexity: O(1) because no extra infinite is needed.


Practice Questions

First Missing Integer
Repeat and Missing Number Array


Frequently Asked Questions

What are the constraints of this problem?
All the numbers in the array should be betwixt one to N, where N -1 is the size of the array.

Will these approaches piece of work for larger numbers?
No, it will merely work for numbers less than 10^6 because we tin non make an array of greater size.

bazewherfuld65.blogspot.com

Source: https://www.interviewbit.com/blog/find-the-missing-number/

0 Response to "! Missing Number"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel