Find Missing Number in an Array in O(1) Time Complexity



Problem Description

We are given an array that contains numbers from 1 to n, and one number is missing between the given numbers from 1 to n. We have to return the missing number in O(1) time complexity. In this problem, we are going to discuss how we can find the missing number in an array.

Example 1

  • Input: array = {1, 2, 3, 5, 6, 7}
  • Output: 4

Explanation

From 1 to 7, the number 4 is missing in the given array.

Example 2

  • Input: array = {1, 2, 3, 4, 5, 6, 7, 8, 10}
  • Output: 9

Explanation

From 1 to 10, the number 9 is missing in the given array.

Example 3

  • Input: array = {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  • Output: 8

Explanation

From 1 to 20, the number 8 is missing in the given array.

Direct Formula Approach

This is a simple and direct approach to finding missing numbers in a given array in reduced time complexity of O(N). In this approach, we first find the sum of the first n numbers from 1 to n using the formula for the sum of the first n natural numbers: n * (n + 1) / 2. Now, we find the sum of all elements in the given array. Subtract the sum of n numbers from the sum of all elements in the given array to get the missing number of the array.

C++ Implementation

#include <iostream>
#include <vector>
using namespace std;

int missingNumber(vector<int>& arr) {
    int n = arr.size() + 1;
    int totalSum = n * (n + 1) / 2;
    int arraySum = 0;

    for(int i : arr) {
        arraySum += i;
    }

    return totalSum - arraySum;
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 6, 7};
    int ans = missingNumber(arr);
    cout << ans << endl;
    return 0;
}

Output

5

Java Implementation

import java.util.*;

public class MissingNumber {
    public static int missingNumber(List<Integer> arr) {
        int n = arr.size() + 1;
        int totalSum = n * (n + 1) / 2;
        int arraySum = 0;

        for (int num : arr) {
            arraySum += num;
        }

        return totalSum - arraySum;
    }

    public static void main(String[] args) {
        List<Integer> arr = Arrays.asList(1, 2, 3, 4, 6, 7);
        int ans = missingNumber(arr);
        System.out.println(ans);
    }
}

Python Implementation

def missing_number(arr):
    n = len(arr) + 1
    total_sum = n * (n + 1) // 2
    array_sum = sum(arr)

    return total_sum - array_sum

if __name__ == "__main__":
    arr = [1, 2, 3, 4, 6, 7]
    ans = missing_number(arr)
    print(ans)

JavaScript Implementation

<script>
function missingNumber(arr) {
    const n = arr.length + 1;
    const totalSum = (n * (n + 1)) / 2;
    const arraySum = arr.reduce((sum, num) => sum + num, 0);

    return totalSum - arraySum;
}

const arr = [1, 2, 3, 4, 6, 7];
const ans = missingNumber(arr);
document.write(ans);
</script>

PHP Implementation

<?php
function missingNumber($arr) {
    $n = count($arr) + 1;
    $totalSum = $n * ($n + 1) / 2;
    $arraySum = array_sum($arr);
    return $totalSum - $arraySum;
}

$arr = [1, 2, 3, 4, 6, 7];
$ans = missingNumber($arr);
echo $ans;
?>

C# Implementation

using System;
using System.Collections.Generic;

class MissingNumber {
    public static int MissingNumberFunc(List<int> arr) {
        int n = arr.Count + 1;
        int totalSum = n * (n + 1) / 2;
        int arraySum = 0;

        foreach (int num in arr) {
            arraySum += num;
        }

        return totalSum - arraySum;
    }

    static void Main(string[] args) {
        List<int> arr = new List<int> { 1, 2, 3, 4, 6, 7 };
        int ans = MissingNumberFunc(arr);
        Console.WriteLine(ans);
    }
}

Time Complexity: O(n)

Space Complexity: O(1)

Updated on: 2024-12-30T19:22:27+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements