[go: up one dir, main page]

0% found this document useful (0 votes)
19 views7 pages

Largest Element in An Array

Uploaded by

lonesome.rar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Largest Element in An Array

Uploaded by

lonesome.rar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1/2/25, 12:14 PM Largest Element In An Array

AfterAcademy

Admin AfterAcademy
4 Sep 2020

Largest Element In An Array

Difficulty: Easy

Understanding The Problem


Problem Description

Given an array arr[] of size n , write a program to find the largest


element in it.

https://afteracademy.com/blog/largest-element-in-an-array/ 1/7
1/2/25, 12:14 PM Largest Element In An Array

Example 1:

Input: arr[] = [100, 50, 4]


Output: 100
Explanation: 100 is the largest element in the given array.

Solutions
There are various ways to find the largest element. In each programming
language, there is support for finding the max in the library. However,
internally they work the same. Below are some discussed ways to do that.

1. Sorting : Sort the array in ascending order and the last element will be
at the last index.

2. Traverse : Traverse the array while updating the max value.

You may try this problem here.

1. Sorting
To find the largest element from the array, a simple way is to arrange the
elements in ascending order. After sorting, the first element will represent
the smallest element, the next element will be the second smallest, and
going on, the last element will be the largest element of the array.

Solution Steps

Sort the array.

Return the element at the last index of the array.

Pseudo Code

int largestElement(int[] arr, int size) {


// sort the array in ascending order
arr.sort()
https://afteracademy.com/blog/largest-element-in-an-array/ 2/7
1/2/25, 12:14 PM Largest Element In An Array

largest_element = arr[size-1]
return largest_element
}

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Critical Ideas To Think

Why did we return the last element?

If we would have sorted the array in descending order, then which


element we should have returned?

Sorting the array arranges each and every element and that will help us
to find the largest, 2nd largest, 3rd largest elements and so on in
constant time. But is it okay to do that much computation here when
we just have to find the largest element?

2. Traverse
Take a max variable and initialize it with the first element of the array.
Now start iterating over the array and whenever a larger element
encountered then update the max variable otherwise, move forward.

Solution Steps

1. Create a max variable and initialize it with arr[0]

2. Iterate from the first idx to the last idx of the array:

If arr[idx] > max then update max with arr[idx]

3. Return max
https://afteracademy.com/blog/largest-element-in-an-array/ 3/7
1/2/25, 12:14 PM Largest Element In An Array

Pseudo Code

int largestElement(int[] arr, int size) {


int max = arr[0]
for(int i = 1 to i < size) {
if(max < arr[i]) {
max = arr[i]
}
}
return max
}

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Critical Ideas To Think

Why did we initialize max with arr[0] ?

What if we initialize max with INT_MIN and start the loop from the
0th index. Will that approach would have worked?

What changes do we have to do, when we have to find the 2nd largest
element?

Comparison of Solutions

https://afteracademy.com/blog/largest-element-in-an-array/ 4/7
1/2/25, 12:14 PM Largest Element In An Array

Suggested Problems To Solve


Find the smallest element of the array

Find the 2nd largest element of the array

Find the Kth largest element of the array

Please comment down below if you have a better insight in the above approach.

Happy Coding, Enjoy Algorithms!

Recommended for You

Average of Levels in Reverse First K Elements Of A


Binary Tree Queue
Given a binary tree, write a program to Given an integer K and queue Q of integers.
return the average value of the nodes on Write a program to reverse the order of the
each level in the form of an array. The range first K elements of the queue. The other
of the node's value is in the range of 32-bit elements will be in the same relative order.
signed integer.

Admin AfterAcademy Admin AfterAcademy


5 Nov 2020 5 Oct 2020

https://afteracademy.com/blog/largest-element-in-an-array/ 5/7
1/2/25, 12:14 PM Largest Element In An Array

Surrounded regions Recursive Insertion Sort


Given a 2-D matrix board where every Write a program for the recursive
element is either 'O' or 'X', write a program to implementation of Insertion Sort. Insertion
replace 'O' with 'X' if surrounded by 'X'. It is a Sort is used to sort a given array. This
famous problem based on the concept of problem will sharpen your recursion skills.
flood fill algorithms

Admin AfterAcademy Admin AfterAcademy


23 Sep 2020 23 Sep 2020

When to Convert a 2-D DP Sort List - Merge Sort


array to 1-D DP array Sort a linked list using Merge Sort. This is a
And How? very famous interview problem that
demonstrates the concept of recursion. This
In which situation 2 dimensional DP can be
problem is quite similar to Merge Sort in
dropped to 1 dimension? Is there any
Arrays.
principle or regular pattern? This is a very
important question when it comes to
optimization of DP arrays. Let's find out.

Admin AfterAcademy Admin AfterAcademy


16 Sep 2020 12 Sep 2020

https://afteracademy.com/blog/largest-element-in-an-array/ 6/7
1/2/25, 12:14 PM Largest Element In An Array

Connect With Your Mentors

Janishar Ali Amit Shekhar


Founder | IIT-BHU | 10 Yrs Exp. Founder | IIT-BHU | 10 Yrs Exp.

Copyright 2022, MindOrks Nextgen Private Limited

https://afteracademy.com/blog/largest-element-in-an-array/ 7/7

You might also like