
Problem
Solution
Submissions
First Non-Repeating Character
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C# program to find the maximum element in an array of integers without using any built-in functions like Max(), Array.Max(), or LINQ methods. You need to iterate through the array and compare each element to find the largest one.
Example 1
- Input: arr = [3, 7, 2, 9, 1, 5]
- Output: 9
- Explanation:
- Start with the first element 3 as the maximum.
- Compare 3 with 7, since 7 > 3, update maximum to 7.
- Compare 7 with 2, since 2 < 7, maximum remains 7.
- Compare 7 with 9, since 9 > 7, update maximum to 9.
- Compare 9 with 1, since 1 < 9, maximum remains 9.
- Compare 9 with 5, since 5 < 9, maximum remains 9.
- The maximum element is 9
- Start with the first element 3 as the maximum.
Example 2
- Input: arr = [15, 8, 23, 4, 12]
- Output: 23
- Explanation:
- Start with the first element 15 as the maximum.
- Compare 15 with 8, since 8 < 15, maximum remains 15.
- Compare 15 with 23, since 23 > 15, update maximum to 23.
- Compare 23 with 4, since 4 < 23, maximum remains 23.
- Compare 23 with 12, since 12 < 23, maximum remains 23.
- The maximum element is 23.
- Start with the first element 15 as the maximum.
Constraints
- 1 ≤ arr.length ≤ 10^4
- -10^9 ≤ arr[i] ≤ 10^9
- You are not allowed to use any built-in functions like Max(), Array.Max(), or LINQ
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Initialize a variable to store the maximum value with the first element of the array
- Start iterating from the second element of the array
- Compare each element with the current maximum value
- If the current element is greater than the maximum, update the maximum
- Continue this process until you reach the end of the array
- Return the maximum value found