Find the Maximum Element in an Array
Problem Statement
Given an array of integers, the task is to find the maximum (largest) element present in the array.
This problem is fundamental in array processing and is frequently used as a base for solving optimization problems such as finding the second largest element, maximum subarray sum, and range-based queries.
Why This Problem Matters
Finding the maximum element is commonly required in:
- Data analysis and statistics
- Competitive programming
- Real-world systems like leaderboards, rankings, and analytics
- Preprocessing steps for more complex algorithms
It also helps reinforce the concept of array traversal and comparison logic.
Input and Output Format
Input
- An array arr of size n
Output
- A single integer representing the maximum element
Example
Input: arr = [12, 45, 7, 89, 23]
Output: 89
Approach 1: Linear Scan (Standard Method)
Idea
Traverse the array once and keep track of the largest value encountered so far.
Step-by-Step Algorithm
- Initialize maxElement with the first element of the array
- Traverse the array from index 1 to n-1
- Compare the current element with maxElement
- If current element is greater, update maxElement
- After traversal, return maxElement
Pseudocode
maxElement = arr[0]
for i from 1 to n-1:
if arr[i] > maxElement:
maxElement = arr[i]
return maxElement
Dry Run Example
arr = [12, 45, 7, 89, 23]
Step 1: max = 12
Step 2: compare 45 → max = 45
Step 3: compare 7 → max remains 45
Step 4: compare 89 → max = 89
Step 5: compare 23 → max remains 89
Final Answer: 89
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
_1769009041.png)
Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {12, 45, 7, 89, 23};
int n = sizeof(arr) / sizeof(arr[0]);
int maxElement = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > maxElement) {
maxElement = arr[i];
}
}
printf("Maximum Element = %d", maxElement);
return 0;
}
Output
Maximum Element = 89
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {12, 45, 7, 89, 23};
int n = sizeof(arr) / sizeof(arr[0]);
int maxElement = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > maxElement) {
maxElement = arr[i];
}
}
cout << "Maximum Element = " << maxElement;
return 0;
}
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {12, 45, 7, 89, 23};
int maxElement = arr[0];
for(int i = 1; i < arr.length; i++) {
if(arr[i] > maxElement) {
maxElement = arr[i];
}
}
System.out.println("Maximum Element = " + maxElement);
}
}
Python Implementation
arr = [12, 45, 7, 89, 23]
max_element = arr[0]
for num in arr:
if num > max_element:
max_element = num
print("Maximum Element =", max_element)
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {12, 45, 7, 89, 23};
int maxElement = arr[0];
for(int i = 1; i < arr.Length; i++) {
if(arr[i] > maxElement) {
maxElement = arr[i];
}
}
Console.WriteLine("Maximum Element = " + maxElement);
}
}
JavaScript Implementation
let arr = [12, 45, 7, 89, 23];
let maxElement = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
}
console.log("Maximum Element =", maxElement);
Alternative Approach (Built-in Functions)
Some languages provide built-in utilities:
- Python: max(arr)
- JavaScript: Math.max(...arr)
However, in interviews, manual traversal is preferred to demonstrate understanding.
Edge Cases to Consider
- Array with one element
- Array containing all negative numbers
- Very large arrays (performance check)
- Duplicate maximum values
- Empty array (should be validated before processing)
Common Mistakes
- Initializing maxElement with 0 instead of first element
- Ignoring negative values
- Incorrect loop starting index
- Not handling empty arrays
Interview Notes
Interviewers look for:
- Correct initialization
- Efficient traversal
- Clear comparison logic
Follow-up questions often include:
- Second largest element
- Maximum and minimum together
- Maximum subarray sum
Summary
Finding the maximum element in an array is a fundamental operation that reinforces traversal and comparison logic. Despite its simplicity, it is a critical building block for many advanced array-based problems. Mastering this ensures correctness and confidence when tackling more complex optimization challenges.
