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 not in arr[] Therefore they are missing and 1 is repeating two times and 3 is repeating two times so they are duplicate numbers.Input: arr[] = {1, 2, 2, 2, 4, 5, 7}
Output:
Missing Numbers: [3, 6]
Duplicate Numbers: [2]
Approach: The given problem can be solved using the idea discussed in this article where only one element is repeating and the other is duplicate. Follow the steps below to solve the given problem:
- Initialize an array, say missing[] that stores the missing elements.
- Initialize a set, say duplicate that stores the duplicate elements.
-
Traverse the given array arr[] using the variable i and perform the following steps:
- If the value of arr[i] != arr[arr[i] – 1] is true, then the current element is not equal to the place where it is supposed to be if all numbers were present from 1 to N. So swap arr[i] and arr[arr[i] – 1].
- Otherwise, it means the same element is present at arr[arr[i] – 1].
- Traverse the given array arr[] using the variable i and if the value of arr[i] is not the same as (i + 1) then the missing element is (i + 1) and the duplicate element is arr[i].
- After completing the above steps, print the elements stored in missing[] and duplicate[] as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the duplicate and // the missing elements over the range // [1, N] void findElements( int arr[], int N)
{ int i = 0;
// Stores the missing and duplicate
// numbers in the array arr[]
vector< int > missing;
set< int > duplicate;
// Making an iterator for set
set< int >::iterator it;
// Traverse the given array arr[]
while (i != N) {
cout << i << " # " ;
// Check if the current element
// is not same as the element at
// index arr[i] - 1, then swap
if (arr[i] != arr[arr[i] - 1]) {
swap(arr[i], arr[arr[i] - 1]);
}
// Otherwise, increment the index
else {
i++;
}
}
// Traverse the array again
for (i = 0; i < N; i++) {
// If the element is not at its
// correct position
if (arr[i] != i + 1) {
// Stores the missing and the
// duplicate elements
missing.push_back(i + 1);
duplicate.insert(arr[i]);
}
}
// Print the Missing Number
cout << "Missing Numbers: " ;
for ( auto & it : missing)
cout << it << ' ' ;
// Print the Duplicate Number
cout << "\nDuplicate Numbers: " ;
for ( auto & it : duplicate)
cout << it << ' ' ;
} // Driver code int main()
{ int arr[] = { 1, 2, 2, 2, 4, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findElements(arr, N);
return 0;
} |
// Java program for the above approach import java.util.ArrayList;
import java.util.HashSet;
class GFG {
// Function to find the duplicate and
// the missing elements over the range
// [1, N]
static void findElements( int arr[], int N) {
int i = 0 ;
// Stores the missing and duplicate
// numbers in the array arr[]
ArrayList<Integer> missing = new ArrayList<Integer>();
HashSet<Integer> duplicate = new HashSet<Integer>();
// Traverse the given array arr[]
while (i != N) {
// Check if the current element
// is not same as the element at
// index arr[i] - 1, then swap
if (arr[i] != arr[arr[i] - 1 ]) {
int temp = arr[i];
arr[i] = arr[arr[i] - 1 ];
arr[temp - 1 ] = temp;
}
// Otherwise, increment the index
else {
i++;
}
}
// Traverse the array again
for (i = 0 ; i < N; i++) {
// If the element is not at its
// correct position
if (arr[i] != i + 1 ) {
// Stores the missing and the
// duplicate elements
missing.add(i + 1 );
duplicate.add(arr[i]);
}
}
// Print the Missing Number
System.out.print( "Missing Numbers: " );
for (Integer itr : missing)
System.out.print(itr + " " );
// Print the Duplicate Number
System.out.print( "\nDuplicate Numbers: " );
for (Integer itr : duplicate)
System.out.print(itr + " " );
}
// Driver code
public static void main(String args[]) {
int arr[] = { 1 , 2 , 2 , 2 , 4 , 5 , 7 };
int N = arr.length;
findElements(arr, N);
}
} // This code is contributed by gfgking. |
# Python3 program for the above approach # Function to find the duplicate and # the missing elements over the range # [1, N] def findElements(arr, N) :
i = 0 ;
# Stores the missing and duplicate
# numbers in the array arr[]
missing = [];
duplicate = set ();
# Traverse the given array arr[]
while (i ! = N) :
# Check if the current element
# is not same as the element at
# index arr[i] - 1, then swap
if (arr[i] ! = arr[arr[i] - 1 ]) :
t = arr[i]
arr[i] = arr[arr[i] - 1 ]
arr[t - 1 ] = t
# Otherwise, increment the index
else :
i + = 1 ;
# Traverse the array again
for i in range (N) :
# If the element is not at its
# correct position
if (arr[i] ! = i + 1 ) :
# Stores the missing and the
# duplicate elements
missing.append(i + 1 );
duplicate.add(arr[i]);
# Print the Missing Number
print ( "Missing Numbers: " ,end = "");
for it in missing:
print (it,end = " " );
# Print the Duplicate Number
print ( "\nDuplicate Numbers: " ,end = "");
for it in list (duplicate) :
print (it, end = ' ' );
# Driver code if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ];
N = len (arr);
findElements(arr, N);
# This code is contributed by AnkThon
|
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG {
// Function to find the duplicate and
// the missing elements over the range
// [1, N]
static void findElements( int [] arr, int N)
{
int i = 0;
// Stores the missing and duplicate
// numbers in the array arr[]
List< int > missing = new List< int >();
HashSet< int > duplicate = new HashSet< int >();
// Traverse the given array arr[]
while (i != N) {
// Check if the current element
// is not same as the element at
// index arr[i] - 1, then swap
if (arr[i] != arr[arr[i] - 1]) {
int temp = arr[i];
arr[i] = arr[arr[i] - 1];
arr[temp - 1] = temp;
}
// Otherwise, increment the index
else {
i++;
}
}
// Traverse the array again
for (i = 0; i < N; i++) {
// If the element is not at its
// correct position
if (arr[i] != i + 1) {
// Stores the missing and the
// duplicate elements
missing.Add(i + 1);
duplicate.Add(arr[i]);
}
}
// Print the Missing Number
Console.Write( "Missing Numbers: " );
foreach ( int itr in missing)
Console.Write(itr + " " );
// Print the Duplicate Number
Console.Write( "\nDuplicate Numbers: " );
foreach ( int itr in duplicate)
Console.Write(itr + " " );
}
// Driver code
public static void Main()
{
int [] arr = { 1, 2, 2, 2, 4, 5, 7 };
int N = arr.Length;
findElements(arr, N);
}
} // This code is contributed by ukasp. |
<script> // Javascript program for the above approach // Function to find the duplicate and // the missing elements over the range // [1, N] function findElements(arr, N) {
let i = 0;
// Stores the missing and duplicate
// numbers in the array arr[]
let missing = [];
let duplicate = new Set();
// Traverse the given array arr[]
while (i != N)
{
// Check if the current element
// is not same as the element at
// index arr[i] - 1, then swap
if (arr[i] != arr[arr[i] - 1]) {
t = arr[i];
arr[i] = arr[arr[i] - 1];
arr[t - 1] = t;
}
// Otherwise, increment the index
else {
i += 1;
}
}
// Traverse the array again
for (let i = 0; i < N; i++)
{
// If the element is not at its
// correct position
if (arr[i] != i + 1)
{
// Stores the missing and the
// duplicate elements
missing.push(i + 1);
duplicate.add(arr[i]);
}
}
// Print the Missing Number
document.write( "Missing Numbers: " );
for (it of missing) document.write(it + " " );
// Print the Duplicate Number
document.write( "<br>Duplicate Numbers: " );
for (let it of [...duplicate]) document.write(it + " " );
} // Driver code let arr = [1, 2, 2, 2, 4, 5, 7]; let N = arr.length; findElements(arr, N); // This code is contributed by _saurabh_jaiswal </script> |
Missing Numbers: 3 6 Duplicate Numbers: 2
Time Complexity: O(N)
Auxiliary Space: O(N)