Program to find largest element in an array using Dynamic Memory Allocation - GeeksforGeeks

17 Sep, 2020
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.
Take N elements and a pointer to store the address of N elements
Allocate 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++
Java
Python3
C#
// C program for the above approach#include <stdio.h>#include <stdlib.h> // Function to find the largest element// using dynamic memory allocationvoid 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 Codeint 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++ program for the above approach#include <iostream>using namespace std; // Function to find the largest element// using dynamic memory allocationvoid 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 Codeint 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 program for the above approachimport java.util.*;class GFG{ // Function to find the largest element// using dynamic memory allocationstatic 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 Codepublic 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 program for# the above approach # Function to find the largest element# using dynamic memory allocationdef 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 Codeif __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# program for the above approachusing System;class GFG{ // Function to find the largest// element using dynamic memory allocationstatic 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 Codepublic 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
20








 
Time Complexity: O(N)Auxiliary Space: O(1)
shikhasingrajput
Rajput-Ji
C-Pointers
Dynamic Memory Allocation
Arrays
C Programs
C++ Programs
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Reversal algorithm for array rotation
Window Sliding Technique
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Strings in C
Arrow operator -> in C/C++ with Examples
Header files in C/C++ and its uses
C Program to read contents of Whole File
Basics of File Handling in C