Largest Number in an Array
Problem Statement:β
Given an arrayΒ arr[].Β The task is to find theΒ largestΒ element and return it.
-
Example:
Examples:
Input: arr[] = [1, 8, 7, 56, 90]
Output: 90
Explanation: The largest element of the given array is 90.
Input: arr[] = [5, 5, 5, 5]
Output: 5
Explanation: The largest element of the given array is 5.
Input: arr[] = [10]
Output: 10
Explanation: There is only one element which is the largest.
β Solution 1: Brute Forceβ
int largest(vector<int> &arr) {
int max_ele = INT_MIN;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] > max_ele) {
max_ele = arr[i];
}
}
return max_ele;
}
β Solution 2: Sorting Approachβ
int largest(vector<int> &arr) {
sort(arr.begin(), arr.end()); // Sorts the array in increasing order
return arr.back(); // Last element is the maximum
}
β Solution 3: Recursive Approachβ
class Solution {
public:
int FindMax(vector<int> &arr, int i){
if(i == arr.size() - 1) return arr[i];
int max_ele = FindMax(arr, i + 1);
return max(arr[i], max_ele);
}
int largest(vector<int> &arr) {
return FindMax(arr, 0);
}
};
π How It Worksβ
- Brute Force: Linear scan to compare all elements and track the maximum so far.
- Sorting: Sort the entire array and return the last element (maximum).
- Recursion: Divide problem by computing max of rest of array and compare with current.
π§© Key Formulaβ
- Brute Force:
max = max(arr[i], max) - Recursion:
max(i) = max(arr[i], max(i+1))
β±οΈ Time & Space Complexityβ
| Approach | Time | Space |
|---|---|---|
| Brute Force β | O(N) | O(1) |
| Sorting | O(N log N) | O(1) or O(N) (depending on sort) |
| Recursion | O(N) | O(N) β call stack |
β οΈ Edge Casesβ
- Empty array β should return error/INT_MIN or handle explicitly.
- All elements same β returns that element.
- All negative numbers β works as usual.
π‘ Other Approachesβ
| Approach | Notes |
|---|---|
| Divide and Conquer | Useful for parallel processing |
| Segment Tree | If you need to do multiple range queries |
π Related Problemsβ
- GFG: Largest element in array
- LeetCode 162: Find Peak Element
- GFG: Second largest element
- Min/Max in recursive array calls
π οΈ Other Notes (Optional)β
- β Brute force is most optimal for one-time scan.
- β οΈ Avoid recursion for large inputs due to stack overflow risk.
- β Sorting is overkill just to find a single max value.
π¬