0% found this document useful (0 votes)
4 views

java

The document outlines a Java function 'findMax' designed to find the maximum value in an array of integers, including handling edge cases such as empty arrays and negative integers. It specifies the expected behavior for various test cases and identifies an error in the logic that needs correction. The function should return Integer.MIN_VALUE for empty arrays and correctly identify the maximum value, even when all elements are negative.

Uploaded by

akashkrishna8329
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java

The document outlines a Java function 'findMax' designed to find the maximum value in an array of integers, including handling edge cases such as empty arrays and negative integers. It specifies the expected behavior for various test cases and identifies an error in the logic that needs correction. The function should return Integer.MIN_VALUE for empty arrays and correctly identify the maximum value, even when all elements are negative.

Uploaded by

akashkrishna8329
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

The function "findMax" is intended to find the maximum value in an array of


integers.
However, there is an error in the logic, identify and correct the error to ensure
the function returns the correct maximum value.

***
FAQ
***

Q: What should the function return if the input array is empty?


A: If the input array is empty, the function should return a special value
indicating that no maximum value exists, such as Integer.MIN_VALUE.

Q: Should the function handle negative integers?


A: Yes, the function should handle negative integers. The maximum value could be
negative, so the function should correctly identify it.

Q: Is there a limit on the size of the input array?


A: While there's no specific limit defined, the function should handle input arrays
of reasonable size within the constraints of memory and performance.

Q: What if there are multiple occurrences of the maximum value in the array?
A: The function should return the first occurrence of the maximum value found in
the array.

Q: What if all elements in the array are negative?


A: If all elements in the array are negative, the function should return the
maximum negative value.

**********
Test Cases
**********

Input: [1, 3, 5, 2, 4], Output: 5 (correct)


Input: [10, 6, 8, 12], Output: 12 (correct)
Input: [5, 5, 5, 5], Output: 5 (correct, all elements are the same)
Input: [], Output: Integer.MIN_VALUE (empty array)
Input: [-5, -8, -3, -12, -7], Output: -3 (correct)

*/

package PartB;

public class Challenge3 {

// Method to find the maximum value in an array


public static int findMax(int[] nums) {
int max = 0;
if (nums.length != 0) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] >= 0) {
if (nums[i] > max) {
max = nums[i];
}
} else {
if (i == 0) {
max = nums[i];
} else if (nums[i] > max) {
max = nums[i];
}

}
}
} else {
max = Integer.MIN_VALUE;
}

return max;
}

// Method to run all test cases


public static void runTests() {
assert findMax(new int[] { 1, 3, 5, 2, 4 }) == 5 : "Test case 1 failed";
assert findMax(new int[] { 10, 6, 8, 12 }) == 12 : "Test case 2 failed";
assert findMax(new int[] { 5, 5, 5, 5 }) == 5 : "Test case 3 failed";
assert findMax(new int[] {}) == Integer.MIN_VALUE : "Test case 4 failed";
assert findMax(new int[] { -5, -8, -3, -12, -7 }) == -3 : "Test case 5
failed";

System.out.println("All test cases passed!");


}

public static void main(String[] args) {


runTests();
}
}

You might also like