Commit 5197bc1
committed
feat: Implement MaxInArray to compute largest element in integer array
WHAT the code does:
Introduces a MaxInArray class with a static max() method.
Iterates through an integer array to determine the maximum element.
main() demonstrates usage by passing a sample array and printing the result.
WHY this matters:
Demonstrates a fundamental algorithm for finding the maximum value in a collection.
Reinforces array traversal concepts, conditional checks, and iteration with for loops.
Highlights a real-world pattern: reducing a sequence to a single aggregate result.
Forms the basis for extending into more advanced algorithms like finding min/max pairs or stream-based reductions.
HOW it works:
max() initializes the maximum value with the first element.
Iterates from the second element (index 1) to the end of the array.
Compares each element with the current max and updates when a larger value is found.
Returns the final maximum after traversal.
main() runs the function on {3, 5, 7, 2, 8, -1, 4, 10, 12} and prints 12.
Tips and gotchas:
Assumes the array is non-empty; otherwise A[0] would throw an exception.
Time complexity is O(n) with a single traversal of the array.
For multi-threaded or large datasets, consider parallel streams or divide-and-conquer approaches.
For negative-only arrays, logic still holds since the first element initializes max.
Use-cases:
Basic algorithm practice for beginners in Java.
Building block for statistics (e.g., finding max, min, average).
Useful in competitive programming and interview questions.
Foundation for more advanced data structure operations (like heaps or priority queues).
Short key: class-max in array array-traversal find-maximum.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent fb20c7b commit 5197bc1
1 file changed
+13
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 2 | + | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | | - | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
| |||
17 | 14 | | |
18 | 15 | | |
19 | 16 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 17 | + | |
23 | 18 | | |
24 | 19 | | |
25 | 20 | | |
26 | 21 | | |
27 | 22 | | |
28 | 23 | | |
| 24 | + | |
29 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | | - | |
| 37 | + | |
0 commit comments