Reemplace cada elemento con el elemento más grande en su lado izquierdo

Dada una array de enteros, la tarea es reemplazar cada elemento con el elemento mayor en su lado izquierdo. 
Nota: Reemplace el primer elemento con -1 ya que no tiene ningún elemento a la izquierda.
Ejemplos: 
 

Input: arr[] = {4, 5, 2, 1, 7, 6}
Output: -1 4 5 5 5 7
Since, 4 has no element in its left, so replace it by -1.
For 5, 4 is the greatest element in its left.
For 2, 5 is the greatest element in its left.
For 1, 5 is the greatest element in its left.
For 7, 5 is the greatest element in its left.
For 6, 7 is the greatest element in its left.

Input: arr[] = {3, 2, 5, 7, 1}
Output: -1 3 3 5 7

Acercarse: 
 

  1. Mantenga una variable max_ele que almacenará el elemento más grande.
  2. Inicialmente, max_ele será igual al elemento en el índice 0.
  3. Primero reemplace arr[0] con -1 y luego recorra la array
  4. Y luego reemplace el elemento con el valor max_ele y actualice max_ele si el elemento es mayor que max_ele.
  5. Imprime la array modificada.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to Replace every
// element with the greater element
// on its left side
#include <bits/stdc++.h>
using namespace std;
 
// Function to replace the elements
void ReplaceElements(int arr[], int n)
{
    // Max value initialised
    // to element at 0th index
    int max_ele = arr[0];
    arr[0] = -1;
 
    for (int i = 1; i < n; ++i) {
 
        // If max_ele is greater than arr[i]
        // then just replace arr[i] with max_ele
        if (max_ele > arr[i])
            arr[i] = max_ele;
 
        // Else if update the max_ele also
        else if (max_ele <= arr[i]) {
            int temp = arr[i];
            arr[i] = max_ele;
            max_ele = temp;
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 4, 5, 2, 1, 7, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Replace the elements
    // with the smaller element
    // on its left side
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
 
    return 0;
}

Java

// Java program to Replace every
// element with the greater element
// on its left side
import java.util.*;
 
class Geeks {
 
 
// Function to replace the elements
static void ReplaceElements(int arr[], int n)
{
    // Max value initialised
    // to element at 0th index
    int max_ele = arr[0];
    arr[0] = -1;
 
    for (int i = 1; i < n; ++i) {
 
        // If max_ele is greater than arr[i]
        // then just replace arr[i] with max_ele
        if (max_ele > arr[i])
            arr[i] = max_ele;
 
        // Else if update the max_ele also
        else if (max_ele <= arr[i]) {
            int temp = arr[i];
            arr[i] = max_ele;
            max_ele = temp;
        }
    }
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 4, 5, 2, 1, 7, 6 };
    int n = arr.length;
 
    // Replace the elements
    // with the smaller element
    // on its left side
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; ++i)
        System.out.println(arr[i]);
}
}
 
// This code is contributed by ankita_saini

Python3

# Python3 program to Replace every
# element with the greater element
# on its left side
 
# Function to replace the elements
def ReplaceElements(arr, n):
 
    # Max value initialised
    # to element at 0th index
    max_ele = arr[0]
    arr[0] = -1
 
    for i in range(1, n):
 
        # If max_ele is greater than arr[i]
        # then just replace arr[i] with max_ele
        if (max_ele > arr[i]):
            arr[i] = max_ele
 
        # Else if update the max_ele also
        elif (max_ele <= arr[i]):
            temp = arr[i]
            arr[i] = max_ele
            max_ele = temp
 
# Driver code
if __name__ == "__main__":
 
    arr = [4, 5, 2, 1, 7, 6 ]
    n = len(arr)
 
    # Replace the elements
    # with the smaller element
    # on its left side
    ReplaceElements(arr, n)
 
    # Print the modified array
    for i in range (n):
        print( arr[i], end = " ")
 
# This code is contributed
# by ChitraNayal

C#

// C# program to Replace every
// element with the greater element
// on its left side
using System;
public class GFG {
 
// Function to replace the elements
static void ReplaceElements(int []arr, int n)
{
    // Max value initialised
    // to element at 0th index
    int max_ele = arr[0];
    arr[0] = -1;
 
    for (int i = 1; i < n; ++i) {
 
        // If max_ele is greater than arr[i]
        // then just replace arr[i] with max_ele
        if (max_ele > arr[i])
            arr[i] = max_ele;
 
        // Else if update the max_ele also
        else if (max_ele <= arr[i]) {
            int temp = arr[i];
            arr[i] = max_ele;
            max_ele = temp;
        }
    }
}
 
// Driver code
public static void Main()
{
    int []arr = { 4, 5, 2, 1, 7, 6 };
    int n = arr.Length;
 
    // Replace the elements
    // with the smaller element
    // on its left side
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (int i = 0; i < n; ++i) {
        Console.Write(arr[i]+" ");
    }
}
}
 
// This code is contributed by Rajput-Ji

PHP

<?php
// PHP program to Replace every
// element with the greater element
// on its left side
 
// Function to replace the elements
function ReplaceElements($arr, $n)
{
    // Max value initialised
    // to element at 0th index
    $max_ele = $arr[0];
    $arr[0] = -1;
 
    for ($i = 1; $i < $n; ++$i)
    {
 
        // If max_ele is greater than arr[i]
        // then just replace arr[i] with max_ele
        if ($max_ele > $arr[$i])
            $arr[$i] = $max_ele;
 
        // Else if update the max_ele also
        else if ($max_ele <= $arr[$i])
        {
            $temp = $arr[$i];
            $arr[$i] = $max_ele;
            $max_ele = $temp;
        }
    }
    return $arr;
}
 
// Driver code
$arr = array(4, 5, 2, 1, 7, 6);
$n = sizeof($arr);
 
// Replace the elements
// with the smaller element
// on its left side
$arr1 = ReplaceElements($arr, $n);
 
// Print the modified array
for ($i = 0; $i < $n; ++$i)
    echo $arr1[$i] . " ";
 
// This code is contributed
// by Akanksha Rai

Javascript

<script>
// Java script program to Replace every
// element with the greater element
// on its left side
 
// Function to replace the elements
function ReplaceElements(arr,n)
{
 
    // Max value initialised
    // to element at 0th index
    let max_ele = arr[0];
    arr[0] = -1;
 
    for (let i = 1; i < n; ++i) {
 
        // If max_ele is greater than arr[i]
        // then just replace arr[i] with max_ele
        if (max_ele > arr[i])
            arr[i] = max_ele;
 
        // Else if update the max_ele also
        else if (max_ele <= arr[i]) {
            let temp = arr[i];
            arr[i] = max_ele;
            max_ele = temp;
        }
    }
}
 
// Driver code
    let arr = [ 4, 5, 2, 1, 7, 6 ];
    let n = arr.length;
 
    // Replace the elements
    // with the smaller element
    // on its left side
    ReplaceElements(arr, n);
 
    // Print the modified array
    for (let i = 0; i < n; ++i)
        document.write(arr[i]+" ");
 
// This code is contributed by sravan kumar G
</script>
Producción: 

-1 4 5 5 5 7

 

Complejidad de tiempo: O(N)
 

Publicación traducida automáticamente

Artículo escrito por imdhruvgupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *