Program to find largest element in an array using Dynamic Memory Allocation
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: 7
Explanation:
The largest element present in the given array is 7.
Input: arr[] = {8, 9, 10, 12}
Output: 12
Explanation:
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 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
#include <stdio.h>
#include <stdlib.h>
void findLargest( int * arr, int N)
{
int i;
for (i = 1; i < N; i++) {
if (*arr < *(arr + i)) {
*arr = *(arr + i);
}
}
printf ( "%d " , *arr);
}
int main()
{
int i, N = 4;
int * arr;
arr = ( int *) calloc (N, sizeof ( int ));
if (arr == NULL) {
printf ( "No memory allocated" );
exit (0);
}
*(arr + 0) = 14;
*(arr + 1) = 12;
*(arr + 2) = 19;
*(arr + 3) = 20;
findLargest(arr, N);
return 0;
}
|
C++
#include <iostream>
using namespace std;
void findLargest( int * arr, int N)
{
for ( int i = 1; i < N; i++) {
if (*arr < *(arr + i)) {
*arr = *(arr + i);
}
}
cout << *arr;
}
int main()
{
int N = 4;
int * arr;
arr = new int [N];
if (arr == NULL) {
cout << "No memory allocated" ;
}
*(arr + 0) = 14;
*(arr + 1) = 12;
*(arr + 2) = 19;
*(arr + 3) = 20;
findLargest(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findLargest( int []arr, int N)
{
for ( int i = 1 ; i < N; i++)
{
if (arr[ 0 ] < (arr[i]))
{
arr[ 0 ] = (arr[i]);
}
}
System.out.print(arr[ 0 ]);
}
public static void main(String[] args)
{
int N = 4 ;
int []arr;
arr = new int [N];
if (arr.length < N)
{
System.out.print( "No memory allocated" );
}
arr[ 0 ] = 14 ;
arr[ 1 ] = 12 ;
arr[ 2 ] = 19 ;
arr[ 3 ] = 20 ;
findLargest(arr, N);
}
}
|
Python3
def findLargest(arr, N):
for i in range ( 1 , N):
if (arr[ 0 ] < (arr[i])):
arr[ 0 ] = (arr[i]);
print (arr[ 0 ]);
if __name__ = = '__main__' :
N = 4 ;
arr = [ 0 ] * N;
if ( len (arr) < N):
print ( "No memory allocated" );
arr[ 0 ] = 14 ;
arr[ 1 ] = 12 ;
arr[ 2 ] = 19 ;
arr[ 3 ] = 20 ;
findLargest(arr, N);
|
C#
using System;
class GFG{
static void findLargest( int []arr,
int N)
{
for ( int i = 1; i < N; i++)
{
if (arr[0] < (arr[i]))
{
arr[0] = (arr[i]);
}
}
Console.Write(arr[0]);
}
public static void Main(String[] args)
{
int N = 4;
int []arr;
arr = new int [N];
if (arr.Length < N)
{
Console.Write( "No memory allocated" );
}
arr[0] = 14;
arr[1] = 12;
arr[2] = 19;
arr[3] = 20;
findLargest(arr, N);
}
}
|
Javascript
function findLargest(arr, N)
{
for (let i = 1; i < N; i++)
{
if (arr[0] < (arr[i])) {
arr[0] = (arr[i]);
}
}
console.log(arr[0]);
}
let N = 4;
let arr = [];
arr = new Array(N);
if (arr.length < N) {
console.log( "No memory allocated" );
}
arr[0] = 14;
arr[1] = 12;
arr[2] = 19;
arr[3] = 20;
findLargest(arr, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...