Given an array **arr[]** of size **N-1 **with integers in the range of **[1, N]**, the task is to find the missing number from the first **N** integers.

**Note:** There are no duplicates in the list.

**Examples:**

Input:arr[] = {1, 2, 4, 6, 3, 7, 8}, N = 8Output:5Explanation:The missing number between 1 to 8 is 5

Input:arr[] = {1, 2, 3, 5}, N = 5Output:4Explanation:The missing number between 1 to 5 is 4

Recommended Practice

Missing number in array

Try It!

** Approach 1 (Using **Hashing

**The idea behind the following approach is**

__):__The numbers will be in the range

(1, N), an array of sizeNcan be maintained to keep record of the elements present in the given array

- Create a temp array
**temp[]**of size**n + 1**with all initial values as 0. - Traverse the input array
**arr[],**and do following for each**arr[i]**- if(temp[arr[i]] == 0) temp[arr[i]] = 1

- Traverse
**temp[]**and output the array element having value as 0 (This is the missing element).

Below is the implementation of the above approach:

## C++

`// C++ program to Find the missing element`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`void`

`findMissing(`

`int`

`arr[], `

`int`

`N)`

`{`

`int`

`i;`

`int`

`temp[N + 1];`

`for`

`(`

`int`

`i = 0; i <= N; i++){`

`temp[i] = 0;`

`}`

`for`

`(i = 0; i < N; i++){`

`temp[arr[i] - 1] = 1;`

`}`

`int`

`ans;`

`for`

`(i = 0; i <= N ; i++) {`

`if`

`(temp[i] == 0)`

`ans = i + 1;`

`}`

`cout << ans;`

`}`

`/* Driver code */`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 3, 7, 5, 6, 2 };`

`int`

`n = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`findMissing(arr, n);`

`}`

## C

`#include <stdio.h>`

`void`

`findMissing(`

`int`

`arr[], `

`int`

`N)`

`{`

`int`

`temp[N + 1];`

`for`

`(`

`int`

`i = 0; i <= N; i++) {`

`temp[i] = 0;`

`}`

`for`

`(`

`int`

`i = 0; i < N; i++) {`

`temp[arr[i] - 1] = 1;`

`}`

`int`

`ans;`

`for`

`(`

`int`

`i = 0; i <= N; i++) {`

`if`

`(temp[i] == 0)`

`ans = i + 1;`

`}`

`printf`

`(`

`"%d"`

`, ans);`

`}`

`/* Driver code */`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 3, 7, 5, 6, 2 };`

`int`

`n = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`findMissing(arr, n);`

`}`

`// This code is contributed by nikhilm2302`

## Java

`// Java code to implement the approach`

`import`

`java.io.*;`

`import`

`java.util.*;`

`class`

`GFG {`

`// Function to find the missing number`

`public`

`static`

`void`

`findMissing(`

`int`

`arr[], `

`int`

`N)`

`{`

`int`

`i;`

`int`

`temp[] = `

`new`

`int`

`[N + `

`1`

`];`

`for`

`(i = `

`0`

`; i <= N; i++) {`

`temp[i] = `

`0`

`;`

`}`

`for`

`(i = `

`0`

`; i < N; i++) {`

`temp[arr[i] - `

`1`

`] = `

`1`

`;`

`}`

`int`

`ans = `

`0`

`;`

`for`

`(i = `

`0`

`; i <= N; i++) {`

`if`

`(temp[i] == `

`0`

`)`

`ans = i + `

`1`

`;`

`}`

`System.out.println(ans);`

`}`

`// Driver Code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`arr[] = { `

`1`

`, `

`3`

`, `

`7`

`, `

`5`

`, `

`6`

`, `

`2`

`};`

`int`

`n = arr.length;`

`// Function call`

`findMissing(arr, n);`

`}`

`}`

## Python3

`# Find Missing Element`

`def`

`findMissing(arr, N):`

`# create a list of zeroes`

`temp `

`=`

`[`

`0`

`] `

`*`

`(N`

`+`

`1`

`)`

`for`

`i `

`in`

`range`

`(`

`0`

`, N):`

`temp[arr[i] `

`-`

`1`

`] `

`=`

`1`

`for`

`i `

`in`

`range`

`(`

`0`

`, N`

`+`

`1`

`):`

`if`

`(temp[i] `

`=`

`=`

`0`

`):`

`ans `

`=`

`i `

`+`

`1`

`print`

`(ans)`

`# Driver code`

`if`

`__name__ `

`=`

`=`

`'__main__'`

`:`

`arr `

`=`

`[`

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`]`

`N `

`=`

`len`

`(arr)`

`# Function call`

`findMissing(arr, N)`

`# This code is contributed by nikhilm2302`

## C#

`using`

`System;`

`public`

`class`

`GFG {`

`public`

`static`

`void`

`findMissing(`

`int`

`[] arr, `

`int`

`N)`

`{`

`// this will create a new array containing 0`

`int`

`[] temp = `

`new`

`int`

`[N + 1];`

`for`

`(`

`int`

`i = 0; i < N; i++) {`

`temp[arr[i] - 1] = 1;`

`}`

`int`

`ans = 0;`

`for`

`(`

`int`

`i = 0; i <= N; i++) {`

`if`

`(temp[i] == 0)`

`ans = i + 1;`

`}`

`Console.WriteLine(ans);`

`}`

`static`

`public`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 3, 7, 5, 6, 2 };`

`int`

`n = arr.Length;`

`findMissing(arr, n);`

`}`

`}`

`// This code is contributed by nikhilm2302.`

## Javascript

`<script>`

`// Javascript code to implement the approach`

`// Function to find the missing number`

`function`

`findMissing(arr,N){`

`let i;`

`let temp = [];`

`for`

`(i = 0; i <= N; i++) {`

`temp[i] = 0;`

`}`

`for`

`(i = 0; i < N; i++) {`

`temp[arr[i] - 1] = 1;`

`}`

`let ans = 0;`

`for`

`(i = 0; i <= N; i++) {`

`if`

`(temp[i] == 0)`

`ans = i + 1;`

`}`

`console.log(ans);`

`}`

`// Driver code`

`let arr = [ 1, 3, 7, 5, 6, 2 ];`

`let n = arr.length;`

`// Function call`

`findMissing(arr,n);`

`</script>`

**Output**

4

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

** Approach 2 (Using summation of first N natural numbers):** The idea behind the approach is to use the summation of the first N numbers.

Find the sum of the numbers in the range

[1, N]using the formulaN * (N+1)/2. Now find the sum of all the elements in the array and subtract it from the sum of the first N natural numbers. This will give the value of the missing element.

Follow the steps mentioned below to implement the idea:

- Calculate the sum of the first N natural numbers as
**sumtotal= N*(N+1)/2**. - Traverse the array from start to end.
- Find the sum of all the array elements.

- Print the missing number as
**SumTotal – sum of array**

Below is the implementation of the above approach:

## C++14

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to get the missing number`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`// Given the range of elements`

`// are 1 more than the size of array`

`int`

`N = n + 1;`

`int`

`total = (N) * (N + 1) / 2;`

`for`

`(`

`int`

`i = 0; i < n; i++)`

`total -= a[i];`

`return`

`total;`

`}`

`// Driver Code`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`cout << miss;`

`return`

`0;`

`}`

## C

`#include <stdio.h>`

`// Function to find the missing number`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`i, total;`

`total = (n + 1) * (n + 2) / 2;`

`for`

`(i = 0; i < n; i++)`

`total -= a[i];`

`return`

`total;`

`}`

`// Driver code`

`void`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`printf`

`(`

`"%d"`

`, miss);`

`}`

## Java

`// Java program to find missing Number`

`import`

`java.util.*;`

`import`

`java.util.Arrays;`

`class`

`GFG {`

`// Function to find the missing number`

`public`

`static`

`int`

`getMissingNo(`

`int`

`[] nums, `

`int`

`n)`

`{`

`int`

`sum = ((n + `

`1`

`) * (n + `

`2`

`)) / `

`2`

`;`

`for`

`(`

`int`

`i = `

`0`

`; i < n; i++)`

`sum -= nums[i];`

`return`

`sum;`

`}`

`// Driver code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`[] arr = { `

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`};`

`int`

`N = arr.length;`

`System.out.println(getMissingNo(arr, N));`

`}`

`}`

## Python

`# Function to find the missing element`

`def`

`getMissingNo(arr, n):`

`total `

`=`

`(n `

`+`

`1`

`)`

`*`

`(n `

`+`

`2`

`)`

`/`

`2`

`sum_of_A `

`=`

`sum`

`(arr)`

`return`

`total `

`-`

`sum_of_A`

`# Driver code`

`if`

`__name__ `

`=`

`=`

`'__main__'`

`:`

`arr `

`=`

`[`

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`]`

`N `

`=`

`len`

`(arr)`

`# Function call`

`miss `

`=`

`getMissingNo(arr, N)`

`print`

`(miss)`

`# This code is contributed by Pratik Chhajer`

## C#

`// C# program to find missing Number`

`using`

`System;`

`class`

`GFG {`

`// Function to find missing number`

`static`

`int`

`getMissingNo(`

`int`

`[] a, `

`int`

`n)`

`{`

`int`

`total = (n + 1) * (n + 2) / 2;`

`for`

`(`

`int`

`i = 0; i < n; i++)`

`total -= a[i];`

`return`

`total;`

`}`

`/* program to test above function */`

`public`

`static`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 2, 3, 5 };`

`int`

`N = 4;`

`int`

`miss = getMissingNo(arr, N);`

`Console.Write(miss);`

`}`

`}`

`// This code is contributed by Sam007_`

## PHP

`<?php`

`// PHP program to find`

`// the Missing Number`

`// getMissingNo takes array and`

`// size of array as arguments`

`function`

`getMissingNo (`

`$a`

`, `

`$n`

`)`

`{`

`$total`

`= (`

`$n`

`+ 1) * (`

`$n`

`+ 2) / 2;`

`for`

`( `

`$i`

`= 0; `

`$i`

`< `

`$n`

`; `

`$i`

`++)`

`$total`

`-= `

`$a`

`[`

`$i`

`];`

`return`

`$total`

`;`

`}`

`// Driver Code`

`$arr`

`= `

`array`

`(1, 2, 3, 5);`

`$N`

`= 4;`

`$miss`

`= getMissingNo(`

`$arr`

`, `

`$N`

`);`

`echo`

`(`

`$miss`

`);`

`// This code is contributed by Ajit.`

`?>`

## Javascript

`<script>`

`// Function to get the missing number`

`function`

`getMissingNo(a, n) {`

`let total = Math.floor((n + 1) * (n + 2) / 2);`

`for`

`(let i = 0; i < n; i++)`

`total -= a[i];`

`return`

`total;`

`}`

`// Driver Code`

`let arr = [ 1, 2, 3, 5 ];`

`let N = arr.length;`

`let miss = getMissingNo(arr, N);`

`document.write(miss);`

`// This code is contributed by Surbhi Tyagi`

`</script>`

**Output**

4

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

** Modification for Overflow:** The approach remains the same but there can be an overflow if N is large.

In order to avoid integer overflow, pick one number from the range [1, N] and subtract a number from the given array (don’t subtract the same number twice). This way there won’t be any integer overflow.

**Algorithm:**

- Create a variable
**sum = 1**which will store the missing number and a counter variable**c = 2**. - Traverse the array from start to end.
- Update the value of sum as
**sum = sum – array[i] + c**and increment**c**by**1**. This performs the task mentioned in the above idea]

- Update the value of sum as
- Print the missing number as a
**sum**.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to get the missing element`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`i, total = 1;`

`for`

`(i = 2; i <= (n + 1); i++) {`

`total += i;`

`total -= a[i - 2];`

`}`

`return`

`total;`

`}`

`// Driver Program`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`cout << getMissingNo(arr, N);`

`return`

`0;`

`}`

`// This code is contributed by Aditya Kumar (adityakumar129)`

## C

`#include <stdio.h>`

`// Function to get the missing element`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`i, total = 1;`

`for`

`(i = 2; i <= (n + 1); i++) {`

`total += i;`

`total -= a[i - 2];`

`}`

`return`

`total;`

`}`

`// Driver code`

`void`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`printf`

`(`

`"%d"`

`, getMissingNo(arr, N));`

`}`

`// This code is contributed by Aditya Kumar (adityakumar129)`

## Java

`// Java implementation`

`class`

`GFG {`

`// Function to get the missing number`

`static`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`total = `

`1`

`;`

`for`

`(`

`int`

`i = `

`2`

`; i <= (n + `

`1`

`); i++) {`

`total += i;`

`total -= a[i - `

`2`

`];`

`}`

`return`

`total;`

`}`

`// Driver Code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`[] arr = { `

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`};`

`int`

`N = arr.length;`

`// Function call`

`System.out.println(getMissingNo(arr, N));`

`}`

`}`

`// This code is contributed by Aditya Kumar (adityakumar129)`

## Python3

`# Function to get the missing number`

`def`

`getMissingNo(a, n):`

`i, total `

`=`

`0`

`, `

`1`

`for`

`i `

`in`

`range`

`(`

`2`

`, n `

`+`

`2`

`):`

`total `

`+`

`=`

`i`

`total `

`-`

`=`

`a[i `

`-`

`2`

`]`

`return`

`total`

`# Driver Code`

`if`

`__name__ `

`=`

`=`

`'__main__'`

`:`

`arr `

`=`

`[`

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`]`

`N `

`=`

`len`

`(arr)`

`# Function call`

`print`

`(getMissingNo(arr, N))`

`# This code is contributed by Mohit kumar`

## C#

`using`

`System;`

`class`

`GFG {`

`// Function to find the missing number`

`static`

`int`

`getMissingNo(`

`int`

`[] a, `

`int`

`n)`

`{`

`int`

`i, total = 1;`

`for`

`(i = 2; i <= (n + 1); i++) {`

`total += i;`

`total -= a[i - 2];`

`}`

`return`

`total;`

`}`

`// Driver Code`

`public`

`static`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 2, 3, 5 };`

`int`

`N = (arr.Length);`

`// Function call`

`Console.Write(getMissingNo(arr, N));`

`}`

`}`

`// This code is contributed by SoumikMondal`

## Javascript

`<script>`

`// a represents the array`

`// n : Number of elements in array a`

`function`

`getMissingNo(a, n)`

`{`

`let i, total=1;`

`for`

`(i = 2; i<= (n+1); i++)`

`{`

`total += i;`

`total -= a[i-2];`

`}`

`return`

`total;`

`}`

`//Driver Program`

`let arr = [1, 2, 3, 5];`

`let N = arr.length;`

`// Function call`

`document.write(getMissingNo(arr, N));`

`//This code is contributed by Mayank Tyagi`

`</script>`

**Output**

4

**Time Complexity:** O(N). Only one traversal of the array is needed.**Auxiliary Space: **O(1).No extra space is needed

** Approach 3 (Using binary operations):** This method uses the technique of XOR to solve the problem.

XOR has certain properties

- Assume a
_{1}⊕ a_{2}⊕ a_{3}⊕ . . . ⊕ a_{n}= a and a_{1}⊕ a_{2}⊕ a_{3}⊕ . . . ⊕ a_{n-1}= b- Then a ⊕ b = a
_{n}

Follow the steps mentioned below to implement the idea:

- Create two variables
**a = 0**and**b = 0** - Run a loop from
**i = 1 to N**:- For every index, update
**a**as**a = a ^ i**

- For every index, update
- Now traverse the array from
**i**= start to end.- For every index, update
**b**as**b = b ^ arr[i]***.*

- For every index, update
- The missing number is
**a ^ b**.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to get the missing number`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`// For xor of all the elements in array`

`int`

`x1 = a[0];`

`// For xor of all the elements from 1 to n+1`

`int`

`x2 = 1;`

`for`

`(`

`int`

`i = 1; i < n; i++)`

`x1 = x1 ^ a[i];`

`for`

`(`

`int`

`i = 2; i <= n + 1; i++)`

`x2 = x2 ^ i;`

`return`

`(x1 ^ x2);`

`}`

`// Driver Code`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`cout << miss;`

`return`

`0;`

`}`

## C

`#include <stdio.h>`

`// Function to find the missing number`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`i;`

`// For xor of all the elements in array`

`int`

`x1 = a[0];`

`// For xor of all the elements from 1 to n+1`

`int`

`x2 = 1;`

`for`

`(i = 1; i < n; i++)`

`x1 = x1 ^ a[i];`

`for`

`(i = 2; i <= n + 1; i++)`

`x2 = x2 ^ i;`

`return`

`(x1 ^ x2);`

`}`

`// Driver code`

`void`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`printf`

`(`

`"%d"`

`, miss);`

`}`

## Java

`// Java program to find missing Number`

`// using xor`

`class`

`Main {`

`// Function to find missing number`

`static`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`x1 = a[`

`0`

`];`

`int`

`x2 = `

`1`

`;`

`// For xor of all the elements in array`

`for`

`(`

`int`

`i = `

`1`

`; i < n; i++)`

`x1 = x1 ^ a[i];`

`// For xor of all the elements from 1 to n+1`

`for`

`(`

`int`

`i = `

`2`

`; i <= n + `

`1`

`; i++)`

`x2 = x2 ^ i;`

`return`

`(x1 ^ x2);`

`}`

`// Driver code`

`public`

`static`

`void`

`main(String args[])`

`{`

`int`

`arr[] = { `

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`};`

`int`

`N = arr.length;`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`System.out.println(miss);`

`}`

`}`

## Python3

`# Python3 program to find`

`# the missing Number`

`# getMissingNo takes list as argument`

`def`

`getMissingNo(a, n):`

`x1 `

`=`

`a[`

`0`

`]`

`x2 `

`=`

`1`

`for`

`i `

`in`

`range`

`(`

`1`

`, n):`

`x1 `

`=`

`x1 ^ a[i]`

`for`

`i `

`in`

`range`

`(`

`2`

`, n `

`+`

`2`

`):`

`x2 `

`=`

`x2 ^ i`

`return`

`x1 ^ x2`

`# Driver program to test above function`

`if`

`__name__ `

`=`

`=`

`'__main__'`

`:`

`arr `

`=`

`[`

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`]`

`N `

`=`

`len`

`(arr)`

`# Driver code`

`miss `

`=`

`getMissingNo(arr, N)`

`print`

`(miss)`

`# This code is contributed by Yatin Gupta`

## C#

`// C# program to find missing Number`

`// using xor`

`using`

`System;`

`class`

`GFG {`

`// Function to find missing number`

`static`

`int`

`getMissingNo(`

`int`

`[] a, `

`int`

`n)`

`{`

`int`

`x1 = a[0];`

`int`

`x2 = 1;`

`// For xor of all the elements in array`

`for`

`(`

`int`

`i = 1; i < n; i++)`

`x1 = x1 ^ a[i];`

`// For xor of all the elements from 1 to n+1`

`for`

`(`

`int`

`i = 2; i <= n + 1; i++)`

`x2 = x2 ^ i;`

`return`

`(x1 ^ x2);`

`}`

`// Driver code`

`public`

`static`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 2, 3, 5 };`

`int`

`N = 4;`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`Console.Write(miss);`

`}`

`}`

`// This code is contributed by Sam007_`

## PHP

`<?php`

`// PHP program to find`

`// the Missing Number`

`// getMissingNo takes array and`

`// size of array as arguments`

`function`

`getMissingNo(`

`$a`

`, `

`$n`

`)`

`{`

`// For xor of all the`

`// elements in array`

`$x1`

`= `

`$a`

`[0];`

`// For xor of all the`

`// elements from 1 to n + 1`

`$x2`

`= 1;`

`for`

`(`

`$i`

`= 1; `

`$i`

`< `

`$n`

`; `

`$i`

`++)`

`$x1`

`= `

`$x1`

`^ `

`$a`

`[`

`$i`

`];`

`for`

`(`

`$i`

`= 2; `

`$i`

`<= `

`$n`

`+ 1; `

`$i`

`++)`

`$x2`

`= `

`$x2`

`^ `

`$i`

`;`

`return`

`(`

`$x1`

`^ `

`$x2`

`);`

`}`

`// Driver Code`

`$arr`

`= `

`array`

`(1, 2, 3, 5);`

`$N`

`= 4;`

`$miss`

`= getMissingNo(`

`$arr`

`, `

`$N`

`);`

`echo`

`(`

`$miss`

`);`

`// This code is contributed by Ajit.`

`?>`

## Javascript

`<script>`

`// Function to get the missing number`

`function`

`getMissingNo(a, n)`

`{`

`// For xor of all the elements in array`

`var`

`x1 = a[0];`

`// For xor of all the elements from 1 to n+1`

`var`

`x2 = 1;`

`for`

`(`

`var`

`i = 1; i < n; i++) x1 = x1 ^ a[i];`

`for`

`(`

`var`

`i = 2; i <= n + 1; i++) x2 = x2 ^ i;`

`return`

`x1 ^ x2;`

`}`

`// Driver Code`

`var`

`arr = [1, 2, 3, 5];`

`var`

`N = arr.length;`

`var`

`miss = getMissingNo(arr, N);`

`document.write(miss);`

`// This code is contributed by rdtank.`

`</script>`

**Output**

4

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

__Approach 4 (Using____ __Cyclic Sort** ):** The idea behind it is as follows:

All the given array numbers are sorted and in the range of 1 to n-1. If the range is 1 to N then the index of every array element will be the same as (value – 1).

Follow the below steps to implement the idea:

- Use
**cyclic sort**to sort the elements in linear time. - Now traverse from
**i**= 0 to the end of the array:- If
**arr[i]**is not the same as**i+1**then the missing element is (**i+1**).

- If
- If all elements are present then
**N**is the missing element in the range**[1, N]**.

Below is the implementation of the above approach.

## C++

`// C++ program to find the missing Number\`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to find the missing number`

`int`

`getMissingNo(`

`int`

`a[], `

`int`

`n)`

`{`

`int`

`i = 0;`

`while`

`(i < n) {`

`int`

`correct = a[i] - 1;`

`if`

`(a[i] < n && a[i] != a[correct]) {`

`swap(a[i], a[correct]);`

`}`

`else`

`{`

`i++;`

`}`

`}`

`for`

`(`

`int`

`i = 0; i < n; i++) {`

`if`

`(i != a[i] - 1) {`

`return`

`i + 1;`

`}`

`}`

`return`

`n;`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 2, 3, 5 };`

`int`

`N = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`// Function call`

`int`

`miss = getMissingNo(arr, N);`

`cout << (miss);`

`return`

`0;`

`}`

## Java

`// java program to check missingNo`

`import`

`java.util.*;`

`public`

`class`

`MissingNumber {`

`// Driver code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`[] arr = { `

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`};`

`int`

`N = arr.length;`

`// Function call`

`int`

`ans = getMissingNo(arr, N);`

`System.out.println(ans);`

`}`

`// Function to find the missing number`

`static`

`int`

`getMissingNo(`

`int`

`[] arr, `

`int`

`n)`

`{`

`int`

`i = `

`0`

`;`

`while`

`(i < n) {`

`// as array is of 1 based indexing so the`

`// correct position or index number of each`

`// element is element-1 i.e. 1 will be at 0th`

`// index similarly 2 correct index will 1 so`

`// on...`

`int`

`correctpos = arr[i] - `

`1`

`;`

`if`

`(arr[i] < n && arr[i] != arr[correctpos]) {`

`// if array element should be lesser than`

`// size and array element should not be at`

`// its correct position then only swap with`

`// its correct position or index value`

`swap(arr, i, correctpos);`

`}`

`else`

`{`

`// if element is at its correct position`

`// just increment i and check for remaining`

`// array elements`

`i++;`

`}`

`}`

`// check for missing element by comparing elements`

`// with their index values`

`for`

`(`

`int`

`index = `

`0`

`; index < arr.length; index++) {`

`if`

`(arr[index] != index + `

`1`

`) {`

`return`

`index + `

`1`

`;`

`}`

`}`

`return`

`arr.length;`

`}`

`static`

`void`

`swap(`

`int`

`[] arr, `

`int`

`i, `

`int`

`correctpos)`

`{`

`// swap elements with their correct indexes`

`int`

`temp = arr[i];`

`arr[i] = arr[correctpos];`

`arr[correctpos] = temp;`

`}`

`}`

`// this code is contributed by devendra solunke`

## Python3

`# Python3 program to check missingNo`

`# Function to find the missing number`

`def`

`getMissingNo(arr, n) :`

`i `

`=`

`0`

`;`

`while`

`(i < n) :`

`# as array is of 1 based indexing so the`

`# correct position or index number of each`

`# element is element-1 i.e. 1 will be at 0th`

`# index similarly 2 correct index will 1 so`

`# on...`

`correctpos `

`=`

`arr[i] `

`-`

`1`

`;`

`if`

`(arr[i] < n `

`and`

`arr[i] !`

`=`

`arr[correctpos]) :`

`# if array element should be lesser than`

`# size and array element should not be at`

`# its correct position then only swap with`

`# its correct position or index value`

`arr[i],arr[correctpos] `

`=`

`arr[correctpos], arr[i]`

`else`

`:`

`# if element is at its correct position`

`# just increment i and check for remaining`

`# array elements`

`i `

`+`

`=`

`1`

`;`

`# check for missing element by comparing elements with their index values`

`for`

`index `

`in`

`range`

`(n) :`

`if`

`(arr[index] !`

`=`

`index `

`+`

`1`

`) :`

`return`

`index `

`+`

`1`

`;`

`return`

`n;`

`# Driver code`

`if`

`__name__ `

`=`

`=`

`"__main__"`

`:`

`arr `

`=`

`[ `

`1`

`, `

`2`

`, `

`3`

`, `

`5`

`];`

`N `

`=`

`len`

`(arr);`

`print`

`(getMissingNo(arr, N));`

`# This Code is Contributed by AnkThon`

## C#

`// C# program to implement`

`// the above approach`

`using`

`System;`

`class`

`GFG {`

`// Function to find the missing number`

`static`

`int`

`getMissingNo(`

`int`

`[] arr, `

`int`

`n)`

`{`

`int`

`i = 0;`

`while`

`(i < n) {`

`// as array is of 1 based indexing so the`

`// correct position or index number of each`

`// element is element-1 i.e. 1 will be at 0th`

`// index similarly 2 correct index will 1 so`

`// on...`

`int`

`correctpos = arr[i] - 1;`

`if`

`(arr[i] < n && arr[i] != arr[correctpos]) {`

`// if array element should be lesser than`

`// size and array element should not be at`

`// its correct position then only swap with`

`// its correct position or index value`

`swap(arr, i, correctpos);`

`}`

`else`

`{`

`// if element is at its correct position`

`// just increment i and check for remaining`

`// array elements`

`i++;`

`}`

`}`

`// check for missing element by comparing elements`

`// with their index values`

`for`

`(`

`int`

`index = 0; index < n; index++) {`

`if`

`(arr[index] != index + 1) {`

`return`

`index + 1;`

`}`

`}`

`return`

`n;`

`}`

`static`

`void`

`swap(`

`int`

`[] arr, `

`int`

`i, `

`int`

`correctpos)`

`{`

`// swap elements with their correct indexes`

`int`

`temp = arr[i];`

`arr[i] = arr[correctpos];`

`arr[correctpos] = temp;`

`}`

`// Driver code`

`public`

`static`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 2, 4, 5, 6 };`

`int`

`N = arr.Length;`

`// Function call`

`int`

`ans = getMissingNo(arr, N);`

`Console.Write(ans);`

`}`

`}`

`// This code is contributed by devendra solunke`

## Javascript

`// javascript program to check missingNo`

`<script>`

`var`

`arr = [ 1, 2, 3, 5 ];`

`var`

`N = arr.length;`

`var`

`ans = getMissingNo(arr, N);`

`console.log(ans);`

`// Function to find the missing number`

`function`

`getMissingNo(arr, n)`

`{`

`var`

`i = 0;`

`while`

`(i < n) {`

`// as array is of 1 based indexing so the`

`// correct position or index number of each`

`// element is element-1 i.e. 1 will be at 0th`

`// index similarly 2 correct index will 1 so`

`// on...`

`var`

`correctpos = arr[i] - 1;`

`if`

`(arr[i] < n && arr[i] != arr[correctpos]) {`

`// if array element should be lesser than`

`// size and array element should not be at`

`// its correct position then only swap with`

`// its correct position or index value`

`swap(arr, i, correctpos);`

`}`

`else`

`{`

`// if element is at its correct position`

`// just increment i and check for remaining`

`// array elements`

`i++;`

`}`

`}`

`// check for missing element by comparing elements with their index values`

`for`

`(`

`var`

`index = 0; index < arr.length; index++) {`

`if`

`(arr[index] != index + 1) {`

`return`

`index + 1;`

`}`

`}`

`return`

`n;`

`}`

`function`

`swap(arr, i, correctpos)`

`{`

`// swap elements with their correct indexes`

`var`

`temp = arr[i];`

`arr[i] = arr[correctpos];`

`arr[correctpos] = temp;`

`}`

`</script>`

`// this code is contributed by devendra solunke`

**Output**

4

**Time Complexity:** O(N), requires (N-1) comparisons**Auxiliary Complexity: **O(1)

** Approach 5 (Use elements as Index and mark the visited places as negative): **Use the below idea to get the approach

Traverse the array. While traversing, use the absolute value of every element as an index and make the value at this index as negative to mark it visited. To find missing, traverse the array again and look for a positive value.

Follow the steps to solve the problem:

- Traverse the given array
- If the absolute value of current element is greater than
**size**of the array, then continue. - else multiply the (absolute value of (current element) – 1)th index with -1.

- If the absolute value of current element is greater than
- Initialize a variable
**ans = size + 1.** - Traverse the array and follow the steps:
- if the value is positive assign
**ans = index + 1**

- if the value is positive assign
- Print
**ans**as the missing value.

Below is the implementation of the above approach:

## C++

`// C++ program to Find the missing element`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`void`

`findMissing(`

`int`

`arr[], `

`int`

`size)`

`{`

`int`

`i;`

`for`

`(i = 0; i < size; i++) {`

`if`

`(`

`abs`

`(arr[i]) - 1 == size){`

`continue`

`;`

`}`

`int`

`ind = `

`abs`

`(arr[i]) - 1;`

`arr[ind] *= -1;`

`}`

`int`

`ans = size + 1;`

`for`

`(i = 0; i < size; i++) {`

`if`

`(arr[i] > 0)`

`ans = i + 1;`

`}`

`cout << ans;`

`}`

`/* Driver code */`

`int`

`main()`

`{`

`int`

`arr[] = { 1, 3, 7, 5, 6, 2 };`

`int`

`n = `

`sizeof`

`(arr) / `

`sizeof`

`(arr[0]);`

`findMissing(arr, n);`

`}`

## Java

`// Java code to implement the approach`

`import`

`java.io.*;`

`import`

`java.util.*;`

`class`

`GFG {`

`// Function to find the missing number`

`public`

`static`

`void`

`findMissing(`

`int`

`arr[], `

`int`

`size)`

`{`

`int`

`i;`

`for`

`(i = `

`0`

`; i < size; i++) {`

`if`

`(Math.abs(arr[i]) - `

`1`

`== size) {`

`continue`

`;`

`}`

`int`

`ind = Math.abs(arr[i]) - `

`1`

`;`

`arr[ind] *= -`

`1`

`;`

`}`

`int`

`ans = size + `

`1`

`;`

`for`

`(i = `

`0`

`; i < size; i++) {`

`if`

`(arr[i] > `

`0`

`)`

`ans = i + `

`1`

`;`

`}`

`System.out.println(ans);`

`}`

`// Driver Code`

`public`

`static`

`void`

`main(String[] args)`

`{`

`int`

`arr[] = { `

`1`

`, `

`3`

`, `

`7`

`, `

`5`

`, `

`6`

`, `

`2`

`};`

`int`

`n = arr.length;`

`// Function call`

`findMissing(arr, n);`

`}`

`}`

`// This code is contributed by aarohirai2616.`

## Python3

`# Function to get the missing number`

`def`

`findMissing(a, size):`

`for`

`i `

`in`

`range`

`(`

`0`

`, n):`

`if`

`(`

`abs`

`(arr[i]) `

`-`

`1`

`=`

`=`

`size):`

`continue`

`ind `

`=`

`abs`

`(arr[i]) `

`-`

`1`

`arr[ind] `

`*`

`=`

`-`

`1`

`ans `

`=`

`size `

`+`

`1`

`for`

`i `

`in`

`range`

`(`

`0`

`, n):`

`if`

`(arr[i] > `

`0`

`):`

`ans `

`=`

`i `

`+`

`1`

`print`

`(ans)`

`# Driver Code`

`if`

`__name__ `

`=`

`=`

`'__main__'`

`:`

`arr `

`=`

`[`

`1`

`, `

`3`

`, `

`7`

`, `

`5`

`, `

`6`

`, `

`2`

`]`

`n `

`=`

`len`

`(arr)`

`# Function call`

`findMissing(arr, n)`

`# This code is contributed by aarohirai2616.`

## C#

`using`

`System;`

`public`

`class`

`GFG {`

`// Function to find the missing number`

`public`

`static`

`void`

`findMissing(`

`int`

`[] arr, `

`int`

`size)`

`{`

`for`

`(`

`int`

`i = 0; i < size; i++) {`

`if`

`(Math.Abs(arr[i]) - 1 == size)`

`continue`

`;`

`int`

`ind = Math.Abs(arr[i]) - 1;`

`arr[ind] *= -1;`

`}`

`int`

`ans = size + 1;`

`for`

`(`

`int`

`i = 0; i < size; i++) {`

`if`

`(arr[i] > 0)`

`ans = i + 1;`

`}`

`Console.WriteLine(ans);`

`}`

`static`

`public`

`void`

`Main()`

`{`

`int`

`[] arr = { 1, 3, 7, 5, 6, 2 };`

`int`

`n = arr.Length;`

`// Function call`

`findMissing(arr, n);`

`}`

`}`

`// This code is contributed by nikhilm2302`

## Javascript

`<script>`

`// Javascript code to implement the approach`

`// Function to find the missing number`

`function`

`findMissing(arr,size){`

`let i;`

`for`

`(i = 0; i < size; i++) {`

`if`

`(Math.abs(arr[i]) - 1 == size) {`

`continue`

`;`

`}`

`let ind = Math.abs(arr[i]) - 1;`

`arr[ind] *= -1;`

`}`

`let ans = size + 1;`

`for`

`(i = 0; i < size; i++) {`

`if`

`(arr[i] > 0)`

`ans = i + 1;`

`}`

`console.log(ans);`

`}`

`// Driver code`

`let arr = [ 1, 3, 7, 5, 6, 2 ];`

`let n = arr.length;`

`// Function call`

`findMissing(arr,n);`

`// This code is contributed by aarohirai2616.`

`</script>`

**Output**

4

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

## FAQs

### How do I find a missing number? ›

...

**Missing Number Problems**

- square = 1,
- square = 4 and then 4 – 1 = 3,
- square = 9,
- square = 16 and then 16 – 1 = 15,
- square = 25,
- square = 36 and then 36 – 1 = 35.
- square = 49.

**How do you find the missing number in Python? ›**

**Approach 1: Using a mathematical formula**

- Calculate the sum of the first n natural numbers as sumtotal= n*(n+1)/2.
- Create a variable sum to store the sum of array elements.
- Traverse the array from start to end.
- Update the value of sum as sum = sum + array[i]
- Print the missing number as sumtotal – sum.

**How do you find the missing number in a integer array of 1 to 100 Javascript? ›**

**Example**

- function findMissedNum(arrayOfNumbers, n = 100) {
- if(arrayOfNumbers. length === n) {
- console. log("no number is missed");
- }
- if(arrayOfNumbers. length < (n - 1) ) {
- console. log("more than one number is missed")
- }
- let totalSum = (n * (n+1)) / 2;

**How do you find a missing number in a range? ›**

...

**Method 2: By using hashing**

- Create a hash table.
- Traverse through the array and update the table.
- Now, again traverse the array, find all the missing elements from the range specified.

**What is a missing number called? ›**

**Number series**

A series or sequence where a number is missed is known as a missing number sequence.

**What is the missing number 73 17? ›**

Summary: The missing number in the equation 73 - 17 = 46 + ? is **10**.

**What is missing number in reasoning? ›**

What is Missing Number Reasoning? Missing number reasoning **involved questions where candidates need to find a missing number in a given series**. Important topics that come under missing number reasoning are Puzzle, Series, Analogy, Order and Ranking and so on. Let us understand each one of them one by one from below.

**How do you find two missing numbers? ›**

To find the two missing numbers, we **take the XOR of all numbers from 1 to N and all the integers present in the array**. The result gives us XOR of the two missing numbers.

**What is the missing number in the series 8214 611? ›**

Therefore, the missing number is **9**.

**Can you find missing numbers 30 45 75? ›**

Expert-Verified Answer

Solution: The given series is: 30, 45, 75, 105, 165, ? On observing carefully, we find that the given numbers in the series are multiples of 15 and prime numbers starting from 2. Hence, **195 will come in place of question mark.**

### How do you find the missing number in subtraction? ›

We can work out a missing number immediately after a subtraction sign by **taking away the number after the equals sign '=' from the number immediately before the subtraction sign '-'**. The missing number will be the answer to the question '8 subtract what number equals 4? '.

**How do you find the missing data in a data set? ›**

**Methods for fixing missing data**

- Identify if the column values are required. Check the target system to determine if the field must have a value. ...
- Insert a constant value. ...
- Use a function. ...
- Copy values from another column. ...
- Delete rows. ...
- Hide the column for now. ...
- Delete the column.

**What is the formula of natural number? ›**

**S _{n} = n(n+1)/2**

Hence, this is the formula to calculate sum of 'n' natural numbers.

**What are 0 to 9 called? ›**

**Digit**. A digit is a single symbol used to make numerals. 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the ten digits we use in everyday numerals.

**What is the missing pattern? ›**

A missing data pattern is said to be **monotone if the variables Yj can be ordered such that if Yj is missing then all variables Yk with k>j are also missing**. This occurs, for example, in longitudinal studies with drop-out. If the pattern is not monotone, it is called non-monotone or general.

**What is 4 digit number called? ›**

What are 4-Digit Numbers? 4-digit numbers are **those numbers that consist of only 4 digits in which the first digit should be 1 or greater than 1 and the rest of the digits can be any number between 0 and 9**. For example, 5693, 1023, and 9825 are four-digit numbers.

**What is the missing number 234 23? ›**

Heya!!! The missing no.is **4** .

**What is the missing number in sequence 9/16 24? ›**

And this pattern is known as sequence. So, it is true and the next number is **43**.

**What are the missing numbers in the sequence 5 10/8/13 blank 16 blank? ›**

What is the missing number in the sequence 5, 10, 8, 13, blank, 16? ANSWER: **11**. The sequence consists of two alternating subsequences, each of which is an arithmetic sequence in which each term is equal to the previous term (of the subsequence) plus 3.

**What is the missing number in the following series 12 144? ›**

The answer is **234**.

### What is the next number in the series 76 69 62? ›

Hence, the next number in the series: 83, 76, 69, 62, 55, 48 is **41**. Note: Whenever finding the next term of a series, we need to look for similarities between the two consecutive terms. This similarity might not always be an arithmetic progression but anything in random.

**What is the next number in the series 7645 5764? ›**

The answer will be **84**.

**What are the missing numbers in the series 15 20 24? ›**

Answer: So, the answer is: **15, 20, 24, 15, 28, 32, 15, 36, 40, 15**, ………………. ...

**What is the missing multiple of 9? ›**

They are: **9, 18, 27, 36, 45, 54, 63, 72, 81, 90 and 99**.

**What are the next numbers of the sequence 15 30 60? ›**

Arithmetic sequence: **15,30,45,60,75,90,105,120**.

**How do you find the missing nth term? ›**

Finding the nth Term of an Arithmetic Sequence

Given an arithmetic sequence with the first term a1 and the common difference d , the nth (or general) term is given by **an=a1+(n−1)d** . Example 1: Find the 27th term of the arithmetic sequence 5,8,11,54,... .

**How do you find the missing and median value? ›**

To find the midpoint of any two numbers, we **add the numbers and then divide by two**. If we let the first missing number be 𝑥, this gives us 62 plus 𝑥 divided by two. As the median is 67, this expression is equal to 67. Multiplying both sides of this equation by two gives us 62 plus 𝑥 is equal to 134.

**What is a missing value in data? ›**

In statistics, missing data, or missing values, **occur when no data value is stored for the variable in an observation**. Missing data are a common occurrence and can have a significant effect on the conclusions that can be drawn from the data.

**What is the sum of infinity? ›**

The sum of infinite for an arithmetic series is undefined since the sum of terms leads to **±∞**. The sum to infinity for a geometric series is also undefined when |r| > 1. If |r| < 1, the sum to infinity of a geometric series can be calculated.

**What is the sum of 1 to infinity? ›**

For those of you who are unfamiliar with this series, which has come to be known as the Ramanujan Summation after a famous Indian mathematician named Srinivasa Ramanujan, it states that if you add all the natural numbers, that is 1, 2, 3, 4, and so on, all the way to infinity, you will find that it is equal to **-1/12**.

### What is nth term? ›

The n t h term is **a formula used to find any term of a sequence, where n stands for the term number**. Formulas: For n t h term of an arithmetic sequence: a n = a + n - 1 d , where a is the first term and d is a common difference.

**What is the first step in finding the missing number? ›**

To find the missing number, we first **identify the rule or formula which is applied in the given missing number series**.

**How do you find the missing number in a number sentence? ›**

To find a missing number in a number sentence where both addition and subtraction are given together, **first add or subtract the given numbers in the order they appear in the question, and then find the missing number to make the number sentence true**.

**What is the best way to find the missing term? ›**

To find the next few terms in an arithmetic sequence, you first need to **find the common difference**, the constant amount of change between numbers in an arithmetic sequence. Once you know the common difference, you can use it to find those next terms!

**How do you explain missing numbers to children? ›**

Support children by **asking them to look at the number before and after the empty space**. When offered the correct answer, emphasise that it is correct, for example, if 10 was the correct missing number, say, yes, that is the number before 11 and comes after 9. So 10 comes in between 9 and 11.