Skip to content

Commit 5197bc1

Browse files
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

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
public class MaxInArray {
2-
// Method to find the maximum element in the array
3-
static int max(int A[])
4-
{
5-
// Start with the assumption that the first element is the largest
2+
static int max(int A[]) {
3+
// Start with the assumption that the first element is the largest.
64
int max = A[0];
75

8-
// Iterate through the array
96
for (int i = 1; i < A.length; i++)
107
{
118
// Update max if the current element is larger
@@ -17,18 +14,24 @@ static int max(int A[])
1714
// Return the largest element
1815
return max;
1916
}
20-
public static void main(String[] args)
21-
{
22-
// Example array
17+
public static void main(String[] args) {
2318
int[] array = {3, 5, 7, 2, 8, -1, 4, 10, 12};
2419

2520
// Find and print the maximum element
2621
System.out.println("The maximum element in the array is: " + max(array));
2722
}
2823
}
24+
2925
/*
26+
Best case: O(n)
27+
Worst case: O(n)
28+
Average case: O(n)
29+
Space: O(1)
30+
3031
Initialization: The max method initializes the max variable to the first element of the array.
3132
Iteration: It then iterates over the rest of the array starting from index 1.
32-
Comparison: During each iteration, it checks if the current element is greater than max. If it is, it updates the value of max.
33+
Comparison: During each iteration, it checks if the current element is greater than max.
34+
35+
If it is, it updates the value of max.
3336
Return: After the loop completes, the method returns the max value.
34-
*/
37+
*/

0 commit comments

Comments
 (0)