Back to Cosmos

README

code/languages/cpp/largest-element-in-an-array/README.md

latest1.0 KB
Original Source
<h3>Finding largest element in an array</h3>

We are given an integer array of size N or we can say number of elements is equal to N. We have to find the largest/ maximum element in an array.

Approach Firstly, program asks the user to input the values. We assign the first element value to the largest variable. Then we compare largest with all other elements inside a loop. If the largest is larger than all other elements, Then we return largest which store first element value as largest element. But if the element is larger than the largest, Then we store that element value into largest. This way largest will always store the largest value. Then we return largest. Time complexity: O(N) where there are N elements in the array Space complexity: O(1)

<p align="center"> For more insights, refer to <a href="https://iq.opengenus.org/largest-element-in-an-array/">Scheduling to finding largest element in an array</a> </p>
<p align="center"> A massive collaborative effort by <a href="https://github.com/OpenGenus/cosmos">OpenGenus Foundation</a> </p>