2. Find Maximum Element in Array

EasyArrayArray

Given an integer array nums, find and return the maximum element in the array.

You must solve it without using any built-in max functions.

Input: An integer array nums of length n.

Output: A single integer — the maximum element.

Examples

Example 1
Input: [3,1,4,1,5,9,2,6]
Output: 9
Explanation: Start max=3. Check 4>3 → max=4. Check 5>4 → max=5. Check 9>5 → max=9. No bigger found. Return 9.
Example 2
Input: [-5,-1,-3,-2]
Output: -1
Explanation: Start max=-5. Check -1>-5 → max=-1. -3 and -2 are less than -1. Return -1.
Example 3
Input: [42]
Output: 42
Explanation: Only one element. It is trivially the maximum. Return 42.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →