Replace every element with the smallest of all other array elements
Last Updated :
28 May, 2021
Given an array arr[] which consist of N integers, the task is to replace every element by the smallest of all other elements present in the array.
Examples:
Input: arr[] = {1, 1, 1, 2}
Output: 1 1 1 1
Input: arr[] = {4, 2, 1, 3}
Output: 1 1 2 1
Naive Approach:
The simplest approach is to find the smallest of all remaining elements for every element with the help of a nested loop.
Time Complexity: O(N2)
Efficient Approach:
The idea is to maintain prefix and suffix min arrays. Maintain leftMin[] and rightMin[] arrays which stores the minimum on the left and right subarrays for every array element. Once computed, replace every ith index of the original array by storing the minimum of leftMin[i] and rightMin[i].
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void ReplaceElements( int arr[], int n)
{
if (n < 2)
{
cout << ( " Invalid Input " );
return ;
}
int leftMin[n];
leftMin[0] = INT_MAX;
int rightMin[n];
rightMin[n - 1] = INT_MAX;
for ( int i = 1; i < n; i++)
{
leftMin[i] = min(leftMin[i - 1], arr[i - 1]);
rightMin[n - 1 - i] = min(rightMin[n - 1 - i + 1],
arr[n - 1 - i + 1]);
}
for ( int i = 0; i < n; i++)
{
arr[i] = min(leftMin[i], rightMin[i]);
}
for ( int i = 0; i < n; ++i)
{
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
ReplaceElements(arr, n);
}
|
Java
import java.util.*;
class GFG {
static void ReplaceElements( int [] arr, int n)
{
if (n < 2 ) {
System.out.println( " Invalid Input " );
return ;
}
int [] leftMin = new int [n];
leftMin[ 0 ] = Integer.MAX_VALUE;
int [] rightMin = new int [n];
rightMin[n - 1 ] = Integer.MAX_VALUE;
for ( int i = 1 ; i < n; i++) {
leftMin[i] = Math.min(leftMin[i - 1 ],
arr[i - 1 ]);
rightMin[n - 1 - i] = Math.min(
rightMin[n - 1 - i + 1 ],
arr[n - 1 - i + 1 ]);
}
for ( int i = 0 ; i < n; i++) {
arr[i] = Math.min(leftMin[i],
rightMin[i]);
}
for ( int i = 0 ; i < n; ++i) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 2 };
int n = arr.length;
ReplaceElements(arr, n);
}
}
|
Python3
import sys
def ReplaceElements(arr, n):
if (n < 2 ):
print ( " Invalid Input " )
return
leftMin = [ 0 ] * n
leftMin[ 0 ] = sys.maxsize
rightMin = [ 0 ] * n
rightMin[n - 1 ] = sys.maxsize
for i in range ( 1 , n):
leftMin[i] = min (leftMin[i - 1 ],
arr[i - 1 ])
rightMin[n - 1 - i] = min (rightMin[n - 1 -
i + 1 ],
arr[n - 1 -
i + 1 ])
for i in range (n):
arr[i] = min (leftMin[i],
rightMin[i])
print ( * arr, sep = " " )
arr = [ 1 , 2 , 3 , 2 ]
n = len (arr)
ReplaceElements(arr, n)
|
C#
using System;
class GFG{
static void ReplaceElements( int [] arr, int n)
{
if (n < 2)
{
Console.Write( " Invalid Input " );
return ;
}
int [] leftMin = new int [n];
leftMin[0] = Int32.MaxValue;
int [] rightMin = new int [n];
rightMin[n - 1] = Int32.MaxValue;
for ( int i = 1; i < n; i++)
{
leftMin[i] = Math.Min(leftMin[i - 1],
arr[i - 1]);
rightMin[n - 1 - i] = Math.Min(
rightMin[n - 1 - i + 1],
arr[n - 1 - i + 1]);
}
for ( int i = 0; i < n; i++)
{
arr[i] = Math.Min(leftMin[i],
rightMin[i]);
}
for ( int i = 0; i < n; ++i)
{
Console.Write(arr[i] + " " );
}
}
public static void Main()
{
int []arr = { 1, 2, 3, 2 };
int n = arr.Length;
ReplaceElements(arr, n);
}
}
|
Javascript
<script>
function ReplaceElements(arr, n)
{
if (n < 2)
{
document.write( " Invalid Input " );
return ;
}
var leftMin = Array(n);
leftMin[0] = 1000000000;
var rightMin = Array(n);
rightMin[n - 1] = 10000000000;
for ( var i = 1; i < n; i++)
{
leftMin[i] = Math.min(leftMin[i - 1], arr[i - 1]);
rightMin[n - 1 - i] = Math.min(rightMin[n - 1 - i + 1],
arr[n - 1 - i + 1]);
}
for ( var i = 0; i < n; i++)
{
arr[i] = Math.min(leftMin[i], rightMin[i]);
}
for ( var i = 0; i < n; ++i)
{
document.write( arr[i] + " " );
}
}
var arr = [1, 2, 3, 2];
var n = arr.length;
ReplaceElements(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Another Efficient Approach: The idea is to find the smallest and second smallest element in the array by traversing it. Then, traverse the array one more time:
- If the current element is the smallest element, then replace it with second smallest element.
- Else replace the current element with the smallest element
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ReplaceElements( int arr[], int n)
{
if (n < 2)
{
cout << " Invalid Input " ;
return ;
}
int first = INT_MAX;
int second = INT_MAX;
for ( int i = 0; i < n; i++)
{
if (arr[i] < first)
{
second = first;
first = arr[i];
}
else if (arr[i] < second &&
arr[i] != first)
second = arr[i];
}
for ( int i = 0; i < n; i++)
{
arr[i] = (arr[i] == first) ?
second : first;
}
for ( int i = 0; i < n; ++i)
{
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
ReplaceElements(arr, n);
}
|
Java
import java.util.*;
class GFG {
static void ReplaceElements( int [] arr, int n)
{
if (n < 2 ) {
System.out.println(
" Invalid Input " );
return ;
}
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
if (arr[i] < first) {
second = first;
first = arr[i];
}
else if (arr[i] < second
&& arr[i] != first)
second = arr[i];
}
for ( int i = 0 ; i < n; i++) {
arr[i] = (arr[i] == first)
? second
: first;
}
for ( int i = 0 ; i < n; ++i) {
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 2 };
int n = arr.length;
ReplaceElements(arr, n);
}
}
|
Python3
import sys
def ReplaceElements(arr, n):
if (n < 2 ):
print ( " Invalid Input " )
return
first = sys.maxsize
second = sys.maxsize
for i in range (n):
if (arr[i] < first):
second = first
first = arr[i]
elif (arr[i] < second and
arr[i] ! = first):
second = arr[i]
for i in range (n):
if (arr[i] = = first):
arr[i] = second
else :
arr[i] = first
for i in range (n):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 2 ]
n = len (arr)
ReplaceElements(arr, n)
|
C#
using System;
class GFG{
static void ReplaceElements( int [] arr, int n)
{
if (n < 2)
{
Console.WriteLine( " Invalid Input " );
return ;
}
int first = Int32.MaxValue;
int second = Int32.MaxValue;
for ( int i = 0; i < n; i++)
{
if (arr[i] < first)
{
second = first;
first = arr[i];
}
else if (arr[i] < second &&
arr[i] != first)
second = arr[i];
}
for ( int i = 0; i < n; i++)
{
arr[i] = (arr[i] == first) ?
second : first;
}
for ( int i = 0; i < n; ++i)
{
Console.Write(arr[i] + " " );
}
}
static void Main()
{
int [] arr = { 1, 2, 3, 2 };
int n = arr.Length;
ReplaceElements(arr, n);
}
}
|
Javascript
<script>
function ReplaceElements(arr, n)
{
if (n < 2)
{
document.write( " Invalid Input " );
return ;
}
var first = Number.MAX_VALUE;
var second = Number.MAX_VALUE;
for ( var i = 0; i < n; i++)
{
if (arr[i] < first)
{
second = first;
first = arr[i];
}
else if (arr[i] < second && arr[i] != first)
second = arr[i];
}
for ( var i = 0; i < n; i++)
{
arr[i] = (arr[i] == first) ? second : first;
}
for ( var i = 0; i < n; ++i)
{
document.write( arr[i] + " " );
}
}
var arr = [ 1, 2, 3, 2 ];
var n = arr.length;
ReplaceElements(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...