GeeksforGeeks App
Open App
Browser
Continue

# Find all numbers in range [1, N] that are not present in given Array

Given an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array.

Examples:

Input: arr[ ] = {5, 5, 4, 4, 2}
Output: 1 3
Explanation:
For all numbers in the range [1, 5], 1 and 3 are not present in the array.

Input: arr[ ] = {3, 2, 3, 1}
Output: 4

Naive Approach: The simplest approach is to hash every array element using any data structure like the dictionary and then iterate over the range [1, N] and print all numbers not present in the hash.

d

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

Approach: The above approach can be optimized further by marking the number at position arr[i] – 1, negative to mark i is present in the array. Then print all positions of the array elements that are positive as they are missing. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for above approach#include using namespace std;  // Function to find the missing numbersvoid getMissingNumbers(int arr[], int N){    // traverse the array arr[]    for (int i = 0; i < N; i++) {        // Update        arr[abs(arr[i]) - 1] = -(abs(arr[abs(arr[i]) - 1]));    }    // Traverse the array arr[]    for (int i = 0; i < N; i++) {        // If Num is not present        if (arr[i] > 0)            cout << i + 1 << " ";    }}  // Driver Codeint main(){      // Given Input    int N = 5;    int arr[] = { 5, 5, 4, 4, 2 };      // Function Call    getMissingNumbers(arr, N);    return 0;}// This codeis contributed by dwivediyash

## Java

 // Java program for the above approachimport java.io.*;  class GFG {        // Function to find the missing numbers    static void getMissingNumbers(int arr[], int N)    {                // traverse the array arr[]        for (int i = 0; i < N; i++)         {                        // Update            arr[(Math.abs(arr[i]) - 1)]                = -(Math.abs(arr[(Math.abs(arr[i]) - 1)]));        }                // Traverse the array arr[]        for (int i = 0; i < N; i++)         {                        // If Num is not present            if (arr[i] > 0)                System.out.print(i + 1 + " ");        }    }      // Driver Code    public static void main(String[] args)    {                // Given Input        int N = 5;        int arr[] = { 5, 5, 4, 4, 2 };          // Function Call        getMissingNumbers(arr, N);    }}  // This code is contributed by Potta Lokesh

## Python3

 # Python program for the above approach  # Function to find the missing numbersdef getMissingNumbers(arr):      # Traverse the array arr[]    for num in arr:          # Update        arr[abs(num)-1] = -(abs(arr[abs(num)-1]))      # Traverse the array arr[]    for pos, num in enumerate(arr):          # If Num is not present        if num > 0:            print(pos + 1, end =' ')    # Given Inputarr = [5, 5, 4, 4, 2]  # Function CallgetMissingNumbers(arr)

## C#

 // C# program for above approachusing System;using System.Collections.Generic;  class GFG{  // Function to find the missing numbersstatic void getMissingNumbers(int []arr, int N){        // traverse the array arr[]    for (int i = 0; i < N; i++)     {                // Update        arr[(Math.Abs(arr[i]) - 1)] = -(Math.Abs(arr[(Math.Abs(arr[i]) - 1)]));    }        // Traverse the array arr[]    for (int i = 0; i < N; i++)    {                // If Num is not present        if (arr[i] > 0)          Console.Write(i + 1 + " ");    }}  // Driver Codepublic static void Main(){      // Given Input    int N = 5;    int []arr = { 5, 5, 4, 4, 2 };      // Function Call    getMissingNumbers(arr, N);}}  // This code is contributed by ipg2016107.

## Javascript



Output

1 3

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

My Personal Notes arrow_drop_up