Dada una array de enteros, reemplace cada elemento con el siguiente elemento más grande (el elemento más grande en el lado derecho) en la array. Como no hay ningún elemento al lado del último elemento, reemplácelo con -1. Por ejemplo, si la array es {16, 17, 4, 3, 5, 2}, debe modificarse a {17, 5, 5, 5, 2, -1}.
Le recomendamos encarecidamente que haga clic aquí y lo practique antes de pasar a la solución.
La pregunta es muy similar a esta publicación y las soluciones también son similares.
Un método ingenuo es ejecutar dos bucles. El bucle exterior seleccionará uno por uno los elementos de la array de izquierda a derecha. El bucle interior encontrará el mayor elemento presente después del elemento elegido. Finalmente, el bucle exterior reemplazará el elemento seleccionado con el elemento más grande encontrado por el bucle interior. La complejidad temporal de este método será O(n*n).
C++
// C++ Program to replace every element with the greatest // element on right side #include <bits/stdc++.h> using namespace std; /* Function to replace every element with the next greatest element */ void nextGreatest(int arr[], int size) { //Initialise maxFromRight with -1 int maxFromRight = -1; int n = arr.length; // run loop from last and replace maxFromRight with the element in the array for(int i= n-1; i>=0;i--) { int temp = maxFromRight; if(arr[i]> maxFromRight){ //replacing only array element with maxFromRight in case element is bigger maxFromRight = arr[i]; } arr[i] = temp; } return arr; } /* A utility Function that prints an array */ void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } /* Driver program to test above function */ int main() { int arr[] = {16, 17, 4, 3, 5, 2}; int size = sizeof(arr)/sizeof(arr[0]); nextGreatest (arr, size); cout << "The modified array is: \n"; printArray (arr, size); return (0); } // This is code is contributed by rathbhupendra
C
// C Program to replace every element with the greatest // element on right side #include <stdio.h> /* Function to replace every element with the next greatest element */ void nextGreatest(int arr[], int size) { // Initialize the next greatest element int max_from_right = arr[size-1]; // The next greatest element for the rightmost element // is always -1 arr[size-1] = -1; // Replace all other elements with the next greatest for(int i = size-2; i >= 0; i--) { // Store the current element (needed later for updating // the next greatest element) int temp = arr[i]; // Replace current element with the next greatest arr[i] = max_from_right; // Update the greatest element, if needed if(max_from_right < temp) max_from_right = temp; } } /* A utility Function that prints an array */ void printArray(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } /* Driver program to test above function */ int main() { int arr[] = {16, 17, 4, 3, 5, 2}; int size = sizeof(arr)/sizeof(arr[0]); nextGreatest (arr, size); printf ("The modified array is: \n"); printArray (arr, size); return (0); }
Java
// Java Program to replace every element with the // greatest element on right side import java.io.*; class NextGreatest { /* Function to replace every element with the next greatest element */ static void nextGreatest(int arr[]) { int size = arr.length; // Initialize the next greatest element int max_from_right = arr[size-1]; // The next greatest element for the rightmost // element is always -1 arr[size-1] = -1; // Replace all other elements with the next greatest for (int i = size-2; i >= 0; i--) { // Store the current element (needed later for // updating the next greatest element) int temp = arr[i]; // Replace current element with the next greatest arr[i] = max_from_right; // Update the greatest element, if needed if(max_from_right < temp) max_from_right = temp; } } /* A utility Function that prints an array */ static void printArray(int arr[]) { for (int i=0; i < arr.length; i++) System.out.print(arr[i]+" "); } public static void main (String[] args) { int arr[] = {16, 17, 4, 3, 5, 2}; nextGreatest (arr); System.out.println("The modified array:"); printArray (arr); } } /*This code is contributed by Devesh Agrawal*/
Python3
# Python Program to replace every element with the # greatest element on right side # Function to replace every element with the next greatest # element def nextGreatest(arr): size = len(arr) # Initialize the next greatest element max_from_right = arr[size-1] # The next greatest element for the rightmost element # is always -1 arr[size-1] = -1 # Replace all other elements with the next greatest for i in range(size-2,-1,-1): # Store the current element (needed later for updating # the next greatest element) temp = arr[i] # Replace current element with the next greatest arr[i]=max_from_right # Update the greatest element, if needed if max_from_right< temp: max_from_right=temp # Utility function to print an array def printArray(arr): for i in range(0,len(arr)): print (arr[i],end=" ") # Driver function to test above function arr = [16, 17, 4, 3, 5, 2] nextGreatest(arr) print ("Modified array is") printArray(arr) # This code is contributed by Devesh Agrawal
C#
// C# Program to replace every element with the // greatest element on right side using System; class GFG { /* Function to replace every element with the next greatest element */ static void nextGreatest(int []arr) { int size = arr.Length; // Initialize the next greatest element int max_from_right = arr[size-1]; // The next greatest element for the // rightmost element is always -1 arr[size-1] = -1; // Replace all other elements with the // next greatest for (int i = size-2; i >= 0; i--) { // Store the current element (needed // later for updating the next // greatest element) int temp = arr[i]; // Replace current element with // the next greatest arr[i] = max_from_right; // Update the greatest element, if // needed if(max_from_right < temp) max_from_right = temp; } } /* A utility Function that prints an array */ static void printArray(int []arr) { for (int i=0; i < arr.Length; i++) Console.Write(arr[i]+" "); } public static void Main () { int []arr = {16, 17, 4, 3, 5, 2}; nextGreatest (arr); Console.WriteLine("The modified array:"); printArray (arr); } } /* This code is contributed by vt_m.*/
PHP
<?php // PHP Program to replace every element with // the greatest element on right side /* Function to replace every element with the next greatest element */ function nextGreatest(&$arr, $size) { // Initialize the next greatest element $max_from_right = $arr[$size - 1]; // The next greatest element for the // rightmost element is always -1 $arr[$size - 1] = -1; // Replace all other elements with // the next greatest for($i = $size - 2; $i >= 0; $i--) { // Store the current element (needed // later for updating the next // greatest element) $temp = $arr[$i]; // Replace current element with the // next greatest $arr[$i] = $max_from_right; // Update the greatest element, // if needed if($max_from_right < $temp) $max_from_right = $temp; } } // A utility Function that prints an array function printArray($arr, $size) { for ($i = 0; $i < $size; $i++) echo $arr[$i] . " "; echo "\n"; } // Driver Code $arr = array(16, 17, 4, 3, 5, 2); $size = count($arr); nextGreatest ($arr, $size); echo "The modified array is: \n"; printArray ($arr, $size); // This code is contributed by // rathbhupendra ?>
Javascript
<script> // JavaScript Program to replace every element with the greatest // element on right side /* Function to replace every element with the next greatest element */ function nextGreatest(arr,size) { // Initialize the next greatest element max_from_right = arr[size-1]; // The next greatest element for the rightmost element // is always -1 arr[size-1] = -1; // Replace all other elements with the next greatest for(let i = size-2; i >= 0; i--) { // Store the current element (needed later for updating // the next greatest element) temp = arr[i]; // Replace current element with the next greatest arr[i] = max_from_right; // Update the greatest element, if needed if(max_from_right < temp) max_from_right = temp; } } /* A utility Function that prints an array */ function printArray(arr,size) { var i; for ( let i = 0; i < size; i++) document.write(arr[i] + " " ); } /* Driver program to test above function */ arr = new Array (16, 17, 4, 3, 5, 2); size = arr.length; nextGreatest (arr, size); document.write ("The modified array is: " + "<br>"+ " \n"); printArray (arr, size); // This code is contributed by simranarora5sos </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA