Replace every element with the smallest element on its left side
Given an array of integers, the task is to replace every element with the smallest element on its left side.
Note: Replace the first element with -1 as it has no element in its left.
Examples:
Input: arr[] = {4, 5, 2, 1, 7, 6}
Output: -1 4 4 2 1 1
Since, 4 has no element in its left, so replace it by -1.
For 5, 4 is the smallest element in its left.
For 2, 4 is the smallest element in its left.
For 1, 2 is the smallest element in its left.
For 7, 1 is the smallest element in its left.
For 6, 1 is the smallest element in its left.
Input: arr[] = {3, 2, 5, 7, 1}
Output: -1 3 2 2 2
Approach:
- Maintain a variable min_ele which will store the smallest element.
- Initially, min_ele will be equal to the element at the 0th index.
- First replace arr[0] with -1 then traverse the array
- And then replace the element with the min_ele value and update the min_ele if the element is smaller than min_ele.
- Print the modified array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void ReplaceElements( int arr[], int n)
{
int min_ele = arr[0];
arr[0] = -1;
for ( int i = 1; i < n; ++i) {
if (min_ele < arr[i])
arr[i] = min_ele;
else if (min_ele >= arr[i]) {
int temp = arr[i];
arr[i] = min_ele;
min_ele = temp;
}
}
}
int main()
{
int arr[] = { 4, 5, 2, 1, 7, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
ReplaceElements(arr, n);
for ( int i = 0; i < n; ++i)
cout << arr[i] << " " ;
return 0;
}
|
Java
class GFG {
static void ReplaceElements( int arr[], int n) {
int min_ele = arr[ 0 ];
arr[ 0 ] = - 1 ;
for ( int i = 1 ; i < n; ++i) {
if (min_ele < arr[i]) {
arr[i] = min_ele;
}
else if (min_ele >= arr[i]) {
int temp = arr[i];
arr[i] = min_ele;
min_ele = temp;
}
}
}
public static void main(String[] args) {
int arr[] = { 4 , 5 , 2 , 1 , 7 , 6 };
int n = arr.length;
ReplaceElements(arr, n);
for ( int i = 0 ; i < n; ++i) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
def ReplaceElements(arr, n):
min_ele = arr[ 0 ]
arr[ 0 ] = - 1
for i in range ( 1 , n):
if (min_ele < arr[i]):
arr[i] = min_ele
elif (min_ele > = arr[i]) :
temp = arr[i]
arr[i] = min_ele
min_ele = temp
if __name__ = = "__main__" :
arr = [ 4 , 5 , 2 , 1 , 7 , 6 ]
n = len (arr)
ReplaceElements(arr, n)
for i in range ( n):
print (arr[i], end = " " )
|
C#
using System;
public class GFG {
static void ReplaceElements( int []arr, int n) {
int min_ele = arr[0];
arr[0] = -1;
for ( int i = 1; i < n; ++i) {
if (min_ele < arr[i]) {
arr[i] = min_ele;
}
else if (min_ele >= arr[i]) {
int temp = arr[i];
arr[i] = min_ele;
min_ele = temp;
}
}
}
public static void Main() {
int []arr = {4, 5, 2, 1, 7, 6};
int n = arr.Length;
ReplaceElements(arr, n);
for ( int i = 0; i < n; ++i) {
Console.Write(arr[i] + " " );
}
}
}
|
PHP
<?php
function ReplaceElements( $arr , $n )
{
$min_ele = $arr [0];
$arr [0] = -1;
for ( $i = 1; $i < $n ; ++ $i )
{
if ( $min_ele < $arr [ $i ])
$arr [ $i ] = $min_ele ;
else if ( $min_ele >= $arr [ $i ])
{
$temp = $arr [ $i ];
$arr [ $i ] = $min_ele ;
$min_ele = $temp ;
}
}
return $arr ;
}
$arr = array (4, 5, 2, 1, 7, 6);
$n = sizeof( $arr );
$arr1 = ReplaceElements( $arr , $n );
for ( $i = 0; $i < $n ; ++ $i )
echo $arr1 [ $i ] . " " ;
?>
|
Javascript
<script>
function ReplaceElements(arr, n)
{
var min_ele = arr[0];
arr[0] = -1;
for ( var i = 1; i < n; ++i)
{
if (min_ele < arr[i]) arr[i] = min_ele;
else if (min_ele >= arr[i])
{
var temp = arr[i];
arr[i] = min_ele;
min_ele = temp;
}
}
}
var arr = [4, 5, 2, 1, 7, 6];
var n = arr.length;
ReplaceElements(arr, n);
for ( var i = 0; i < n; ++i) document.write(arr[i] + " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) because constant space is being used for variables
Last Updated :
17 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...