Tutorialspoint
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
    .
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.
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)
ArraysDropboxTutorix
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Initialize a variable max with the first element of the array to establish a baseline for comparison.
 Step 2: Start a loop from the second element (index 1) to iterate through the remaining elements of the array.
 Step 3: For each element in the array, compare it with the current maximum value stored in the max variable.
 Step 4: If the current element is greater than the max, update the max variable with the current element's value.
 Step 5: Continue the iteration until all elements in the array have been compared.
 Step 6: After the loop completes, the max variable will contain the largest element in the array.
 Step 7: Return the maximum value as the result.

Submitted Code :