Dada una array arr[] que consta de N enteros, la tarea es encontrar el elemento más grande en la array dada utilizando la asignación dinámica de memoria .
Ejemplos:
Entrada: arr[] = {4, 5, 6, 7}
Salida: 7
Explicación:
El elemento más grande presente en la array dada es 7.Entrada: arr[] = {8, 9, 10, 12}
Salida: 12
Explicación:
El elemento más grande presente en la array dada es 12.
Enfoque: la idea aquí es usar la memoria dinámica para buscar el elemento más grande en la array dada . Siga los pasos a continuación para resolver el problema:
- Tome N elementos y un puntero para almacenar la dirección de N elementos
- Asigne memoria dinámicamente para N elementos.
- Almacene los elementos en la memoria asignada.
- Recorra la array arr[] para encontrar el elemento más grande entre todos los números comparando los valores usando punteros.
A continuación se muestra la implementación del enfoque anterior:
C
// C program for the above approach #include <stdio.h> #include <stdlib.h> // Function to find the largest element // using dynamic memory allocation void findLargest(int* arr, int N) { int i; // Traverse the array arr[] for (i = 1; i < N; i++) { // Update the largest element if (*arr < *(arr + i)) { *arr = *(arr + i); } } // Print the largest number printf("%d ", *arr); } // Driver Code int main() { int i, N = 4; int* arr; // Memory allocation to arr arr = (int*)calloc(N, sizeof(int)); // Condition for no memory // allocation if (arr == NULL) { printf("No memory allocated"); exit(0); } // Store the elements *(arr + 0) = 14; *(arr + 1) = 12; *(arr + 2) = 19; *(arr + 3) = 20; // Function Call findLargest(arr, N); return 0; }
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to find the largest element // using dynamic memory allocation void findLargest(int* arr, int N) { // Traverse the array arr[] for (int i = 1; i < N; i++) { // Update the largest element if (*arr < *(arr + i)) { *arr = *(arr + i); } } // Print the largest number cout << *arr; } // Driver Code int main() { int N = 4; int* arr; // Memory allocation to arr arr = new int[N]; // Condition for no memory // allocation if (arr == NULL) { cout << "No memory allocated"; } // Store the elements *(arr + 0) = 14; *(arr + 1) = 12; *(arr + 2) = 19; *(arr + 3) = 20; // Function Call findLargest(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the largest element // using dynamic memory allocation static void findLargest(int []arr, int N) { // Traverse the array arr[] for (int i = 1; i < N; i++) { // Update the largest element if (arr[0] < (arr[i])) { arr[0] = (arr[i]); } } // Print the largest number System.out.print(arr[0]); } // Driver Code public static void main(String[] args) { int N = 4; int []arr; // Memory allocation to arr arr = new int[N]; // Condition for no memory // allocation if (arr.length < N) { System.out.print("No memory allocated"); } // Store the elements arr[0] = 14; arr[1] = 12; arr[2] = 19; arr[3] = 20; // Function Call findLargest(arr, N); } } // This code is contributed by shikhasingrajput
Python3
# Python3 program for # the above approach # Function to find the largest element # using dynamic memory allocation def findLargest(arr, N): # Traverse the array arr for i in range(1, N): # Update the largest element if (arr[0] < (arr[i])): arr[0] = (arr[i]); # Print largest number print(arr[0]); # Driver Code if __name__ == '__main__': N = 4; # Memory allocation to arr arr = [0] * N; # Condition for no memory # allocation if (len(arr) < N): print("No memory allocated"); # Store the elements arr[0] = 14; arr[1] = 12; arr[2] = 19; arr[3] = 20; # Function Call findLargest(arr, N); # This code is contributed by shikhasingrajput
C#
// C# program for the above approach using System; class GFG{ // Function to find the largest // element using dynamic memory allocation static void findLargest(int []arr, int N) { // Traverse the array []arr for (int i = 1; i < N; i++) { // Update the largest element if (arr[0] < (arr[i])) { arr[0] = (arr[i]); } } // Print the largest number Console.Write(arr[0]); } // Driver Code public static void Main(String[] args) { int N = 4; int []arr; // Memory allocation to arr arr = new int[N]; // Condition for no memory // allocation if (arr.Length < N) { Console.Write("No memory allocated"); } // Store the elements arr[0] = 14; arr[1] = 12; arr[2] = 19; arr[3] = 20; // Function Call findLargest(arr, N); } } // This code is contributed by Rajput-Ji
Producción:
20
Complejidad temporal: O(N)
Espacio auxiliar: O(1)