Program to find largest element in an array using Dynamic Memory Allocation Last Updated : 21 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanation:The largest element present in the given array is 12. Approach: The idea here is to use Dynamic Memory for searching the largest element in the given array. Follow the steps below to solve the problem: Take N elements and a pointer to store the address of N elementsAllocate memory dynamically for N elements.Store the elements in the allocated memory.Traverse the array arr[] to find the largest element among all the numbers by comparing the values using pointers. Below is the implementation of the above approach: 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 JavaScript // Javascript program for the above approach // Function to find the largest element // using dynamic memory allocation function findLargest(arr, N) { // Traverse the array arr[] for (let i = 1; i < N; i++) { // Update the largest element if (arr[0] < (arr[i])) { arr[0] = (arr[i]); } } // Print the largest number console.log(arr[0]); } // Driver Code let N = 4; let arr = []; // Memory allocation to arr arr = new Array(N); // Condition for no memory // allocation if (arr.length < N) { console.log("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 Saurabh Jaiswal Output: 20 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to find largest element in an array using Dynamic Memory Allocation pushkar_s Follow Improve Article Tags : C Programs C++ Programs DSA Arrays C-Pointers Dynamic Memory Allocation +2 More Practice Tags : Arrays Similar Reads C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin 2 min read C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h 3 min read C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 3 min read C++ Program to Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara 4 min read C++ Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example :  Input: arr[] = {1, 14, 2, 16, 10, 20} Output: The third Largest element is 14 Explanation: Largest element is 20, second largest element is 16 and third largest element is 1 5 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 3 min read C++ Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 3 min read C++ Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : The approach is very simple. The idea is to run the loop for no_of_rows. Check each 2 min read C++ Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input:stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}k = 3Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on to 7 min read Like