CSInterviewing
Problems
Register
Login
Problems
Register
Login
menu
Maximum Element in Array
array
# Maximum Element in Array ## Difficulty Easy ## Tags `array` ## Problem Statement Given a non-empty array of integers `nums`, find the maximum value in the array. ## Constraints - 1 ≤ nums.length ≤ 10⁴ - -10⁴ ≤ nums[i] ≤ 10⁴ ## Signatures ### Python ```python def find_max(nums: List[int]) -> int: pass ``` ### Java ```java public class Solution { public int find_max(List<Integer> nums) { return null; } } ``` ### Csharp ```csharp public class Solution { public int find_max(IList<int> nums) { throw new NotImplementedException(); } } ``` ### Cpp ```cpp class Solution { public: int find_max(vector<int> nums) { } }; ``` ### C ```c int find_max(int* nums) { } ``` ### Ruby ```ruby def find_max(nums) end ``` ### Perl ```perl sub find_max { my ($self, @args) = @_; } ``` ### Scala ```scala object Solution { def find_max(nums: List[Int]): Int = { } } ``` ### Haskell ```haskell find_max :: ... -> ... find_max args = undefined ``` ### Clojure ```clojure (defn find_max [nums] ) ``` ### Scheme ```scheme (define (find_max nums) ) ``` ## Examples ### Example 1 **Input:** ``` nums = [3, 1, 4, 1, 5, 9, 2, 6] ``` **Output:** ``` 9 ``` ### Example 2 **Input:** ``` nums = [-5, -2, -10, -1] ``` **Output:** ``` -1 ``` **Explanation:** -1 is the largest among all negative numbers. ### Example 3 **Input:** ``` nums = [42] ``` **Output:** ``` 42 ``` ### Example 4 **Input:** ``` nums = [5, 5, 5, 5] ``` **Output:** ``` 5 ``` ## Hints 1. Initialize `max_val` to the first element or negative infinity. 2. Iterate through the array, updating `max_val` if current element is larger. 3. Return `max_val` after traversing the entire array.
Login required
Please log in to view the solution editor and submit code.
Login
Register