Find all numbers in range [1, N] that are not present in given Array
Last Updated :
24 Mar, 2023
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:
- Iterate over the array, arr[] and for each current element, num perform the following steps:
- Update arr[abs(num)-1] to -abs(arr[abs(num)-1]).
- Iterate over the array, arr[] using the variable i, and print the i+1 if arr[i] is positive.
Below is the implementation of the above approach:
C++
// C++ program for above approach
#include <iostream>
using namespace std;
// Function to find the missing numbers
void 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 Code
int 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 approach
import 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 numbers
def 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 Input
arr = [5, 5, 4, 4, 2]
# Function Call
getMissingNumbers(arr)
C#
// C# program for above approach
using System;
using System.Collections.Generic;
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)
Console.Write(i + 1 + " ");
}
}
// Driver Code
public 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
<script>
// Javascript program for the above approach
// Function to find the missing numbers
function getMissingNumbers(arr){
// Traverse the array arr[]
for(let num of arr)
// Update
arr[(Math.abs(num)-1)] = -(Math.abs(arr[(Math.abs(num)-1)]))
// Traverse the array arr[]
for (pos in arr)
// If Num is not present
if(arr[pos] > 0)
document.write(`${parseInt(pos) + 1} `)
}
// Given Input
let arr = [5, 5, 4, 4, 2]
// Function Call
getMissingNumbers(arr)
// This code is contributed by _saurabh_jaiswal.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find all duplicate and missing numbers in given permutation array of 1 to N Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array. Examples: Input: arr[] = {1, 1, 2, 3, 3, 5}Output: Missing Numbers: [4, 6]Duplicate Numbers: [1, 3]Explanation:As 4 and 6 are
8 min read
Smallest list of ranges that includes all the numbers which are not in array Given a range [l, r] and an array arr[] of unique integers and l <= arr[i] <= r. The task is to find the smallest list of ranges that includes all the numbers that are not in the given array within a certain range. Example: Input: arr = {3, 5, 10, 11, 12, 15} , l = 1, h = 20Output: {1, 2} {4,
9 min read
Find all missing numbers from a given sorted array Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]]. Examples: Input: arr[] = {6, 7, 10, 11, 13}Output: 8 9 12 Explanation: The elements of the array are present in the range of the maximum and minimum array e
13 min read
Count numbers from a given range that are not divisible by any of the array elements Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements. Examples: Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20Output: 6Explanation:The 6 numbers in the range [1, 2
7 min read
Count numbers that does not contain digit N in given range Given integers, N, L, and R, the task is to find the number of integers in the range L to R that does not contain the digit N. print the answer modulo 109 + 7. ( L ? R ? 101000000) Examples: Input: N = 5, L = 1, R = 10Output: 9Explanation: excluding all 5 others from 1 to 10 will be included in the
14 min read
Find elements which are present in first array and not in second Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read