Given an array arr[] having N distinct positive elements, the task is to generate another array B[] such that, for every ith index in the array, arr[], B[i] is the minimum positive number missing from arr[] excluding arr[i].
Examples:
Input: arr[] = {2, 1, 5, 3}
Output: B[] = {2, 1, 4, 3}
Explanation: After excluding the arr[0], the array is {1, 5, 3}, and the minimum positive number which is not present in this array is 2. Therefore, B[0] = 2. Similarly, after excluding arr[1], arr[2], arr[3], the minimum positive numbers which are not present in the array are 1, 4 and 3, respectively. Hence, B[1] = 1, B[2] = 4, B[3] = 3.
Input: arr[] = {1, 9, 2, 4}
Output: B[] = {1, 3, 2, 3}
Naive Approach: The simplest approach to solve this problem is to traverse the array arr[] and for every index i, initialize an array hash[] and for every index j ( where j ? i), update hash[arr[j]] =1. Now traverse array hash[] from index 1 and find the minimum index k for which hash[k] = 0 and update B[i] = k. Finally, print the array B[] after completing the above step.
Time Complexity: O(N2) where N is the length of the given array.
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to calculate MEX of the array arr[] and traverse the array arr[]. If arr[i] is less than MEX of the array arr[] then MEX excluding this element will be arr[i] itself, and if arr[i] is greater than MEX of array A[] then MEX of the array will not change after excluding this element.
Follow the steps below to solve the problem:
- Initialize an array, say hash[], to store whether the value i is present in the array arr[] or not. If i is present hash[i] = 1 else hash[i] = 0.
- Initialize a variable MexOfArr to store MEX of array arr[] and traverse array hash[] from 1 to find the minimum index j for which hash[j] = 0, which implies that the value j is not present in the array arr[] and store MexOfArr = j.
- Now traverse the array, arr[] and if arr[i] is less than MexOfArr, then store B[i] = arr[i] else B[i] = MexOfArr.
- After completing the above steps, print elements of the array B[] as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100001
void constructMEX( int arr[], int N)
{
int hash[MAXN] = { 0 };
for ( int i = 0; i < N; i++) {
hash[arr[i]] = 1;
}
int MexOfArr;
for ( int i = 1; i < MAXN; i++) {
if (hash[i] == 0) {
MexOfArr = i;
break ;
}
}
int B[N];
for ( int i = 0; i < N; i++) {
if (arr[i] < MexOfArr)
B[i] = arr[i];
else
B[i] = MexOfArr;
}
for ( int i = 0; i < N; i++)
cout << B[i] << ' ' ;
}
int main()
{
int arr[] = { 2, 1, 5, 3 };
int N = sizeof (arr)
/ sizeof (arr[0]);
constructMEX(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAXN = 100001 ;
static void constructMEX( int arr[],
int N)
{
int hash[] = new int [MAXN];
for ( int i = 0 ; i < N; i++)
{
hash[i] = 0 ;
}
for ( int i = 0 ; i < N; i++)
{
hash[arr[i]] = 1 ;
}
int MexOfArr = 0 ;
for ( int i = 1 ; i < MAXN; i++)
{
if (hash[i] == 0 )
{
MexOfArr = i;
break ;
}
}
int B[] = new int [N];
for ( int i = 0 ; i < N; i++)
{
if (arr[i] < MexOfArr)
B[i] = arr[i];
else
B[i] = MexOfArr;
}
for ( int i = 0 ; i < N; i++)
System.out.print(B[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 5 , 3 };
int N = arr.length;
constructMEX(arr, N);
}
}
|
Python3
MAXN = 100001
def constructMEX(arr, N):
hash = [ 0 ] * MAXN
for i in range (N):
hash [arr[i]] = 1
MexOfArr = 0
for i in range ( 1 , MAXN):
if ( hash [i] = = 0 ):
MexOfArr = i
break
B = [ 0 ] * N
for i in range (N):
if (arr[i] < MexOfArr):
B[i] = arr[i]
else :
B[i] = MexOfArr
for i in range (N):
print (B[i], end = " " )
if __name__ = = '__main__' :
arr = [ 2 , 1 , 5 , 3 ]
N = len (arr)
constructMEX(arr, N)
|
C#
using System;
class GFG{
static int MAXN = 100001;
static void constructMEX( int [] arr,
int N)
{
int [] hash = new int [MAXN];
for ( int i = 0; i < N; i++)
{
hash[i] = 0;
}
for ( int i = 0; i < N; i++)
{
hash[arr[i]] = 1;
}
int MexOfArr = 0;
for ( int i = 1; i < MAXN; i++)
{
if (hash[i] == 0)
{
MexOfArr = i;
break ;
}
}
int [] B = new int [N];
for ( int i = 0; i < N; i++)
{
if (arr[i] < MexOfArr)
B[i] = arr[i];
else
B[i] = MexOfArr;
}
for ( int i = 0; i < N; i++)
Console.Write(B[i] + " " );
}
public static void Main()
{
int [] arr = { 2, 1, 5, 3 };
int N = arr.Length;
constructMEX(arr, N);
}
}
|
Javascript
<script>
var MAXN = 100001;
function constructMEX(arr, N)
{
var hash = Array(MAXN).fill(0);
for ( var i = 0; i < N; i++) {
hash[arr[i]] = 1;
}
var MexOfArr;
for ( var i = 1; i < MAXN; i++) {
if (hash[i] == 0) {
MexOfArr = i;
break ;
}
}
var B = Array(N);
for ( var i = 0; i < N; i++) {
if (arr[i] < MexOfArr)
B[i] = arr[i];
else
B[i] = MexOfArr;
}
for ( var i = 0; i < N; i++)
document.write( B[i] + ' ' );
}
var arr = [2, 1, 5, 3];
var N = arr.length;
constructMEX(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)