Skip to content

Commit 356b02d

Browse files
Largest Element in Array
1 parent 9b0a0c5 commit 356b02d

3 files changed

Lines changed: 196 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package array.largest_elements_in_array;
2+
3+
/**
4+
* Utility class to find largest and second-largest elements in an array.
5+
*/
6+
public class LargestElement {
7+
8+
/**
9+
* Finds the largest element in the given array.
10+
*
11+
* @param nums The input array of integers.
12+
* @return The largest integer in the array.
13+
* @throws IllegalArgumentException if the array is null or empty.
14+
*/
15+
public int maxElement(int[] nums) {
16+
if (nums == null || nums.length == 0) {
17+
throw new IllegalArgumentException("Array is empty");
18+
}
19+
int max = Integer.MIN_VALUE;
20+
for (var num : nums) {
21+
if (num > max) {
22+
max = num;
23+
}
24+
}
25+
return max;
26+
}
27+
28+
/**
29+
* Finds the second-largest element in the given array.
30+
* Returns -1 if the second-largest element does not exist (e.g., array size < 2 or all elements are equal).
31+
*
32+
* @param nums The input array of integers.
33+
* @return The second-largest integer in the array, or -1 if it doesn't exist.
34+
*/
35+
public int secondLargestElement(int[] nums) {
36+
if (nums == null || nums.length < 2) {
37+
return -1;
38+
}
39+
int max = Integer.MIN_VALUE;
40+
int secondMax = Integer.MIN_VALUE;
41+
for (var num : nums) {
42+
if (num > max) {
43+
secondMax = max;
44+
max = num;
45+
} else if (num > secondMax && num != max) {
46+
secondMax = num;
47+
}
48+
}
49+
50+
if (secondMax == Integer.MIN_VALUE) {
51+
for (var num : nums) {
52+
if (num == secondMax) {
53+
return secondMax;
54+
}
55+
}
56+
return -1;
57+
}
58+
59+
return secondMax;
60+
}
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Contains classes for finding specific elements in an array, such as the largest
3+
* and second-largest elements.
4+
*
5+
* <p>This package provides implementations for:
6+
* <ul>
7+
* <li>Finding the maximum element in an integer array.</li>
8+
* <li>Finding the second-largest distinct element in an integer array.</li>
9+
* </ul>
10+
*
11+
* @since 1.0
12+
*/
13+
package array.largest_elements_in_array;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package array.largest_elements_in_array;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class LargestElementTest {
9+
10+
private final LargestElement solution = new LargestElement();
11+
12+
// Test cases for maxElement()
13+
14+
@Test
15+
@DisplayName("maxElement should find the largest element in a positive integer array")
16+
void testMaxElementPositiveNumbers() {
17+
int[] nums = {1, 8, 3, 10, 4};
18+
assertEquals(10, solution.maxElement(nums));
19+
}
20+
21+
@Test
22+
@DisplayName("maxElement should find the largest element in a negative integer array")
23+
void testMaxElementNegativeNumbers() {
24+
int[] nums = {-5, -2, -10, -1};
25+
assertEquals(-1, solution.maxElement(nums));
26+
}
27+
28+
@Test
29+
@DisplayName("maxElement should find the largest element in an array with mixed positive and negative numbers")
30+
void testMaxElementMixedNumbers() {
31+
int[] nums = {-10, 5, 0, -2, 8};
32+
assertEquals(8, solution.maxElement(nums));
33+
}
34+
35+
@Test
36+
@DisplayName("maxElement should return the element itself for a single-element array")
37+
void testMaxElementSingleElement() {
38+
int[] nums = {42};
39+
assertEquals(42, solution.maxElement(nums));
40+
}
41+
42+
@Test
43+
@DisplayName("maxElement should handle an array containing Integer.MIN_VALUE")
44+
void testMaxElementWithMinValue() {
45+
int[] nums = {-1, -5, Integer.MIN_VALUE};
46+
assertEquals(-1, solution.maxElement(nums));
47+
}
48+
49+
@Test
50+
@DisplayName("maxElement should throw IllegalArgumentException for a null array")
51+
void testMaxElementNullArray() {
52+
assertThrows(IllegalArgumentException.class, () -> solution.maxElement(null));
53+
}
54+
55+
@Test
56+
@DisplayName("maxElement should throw IllegalArgumentException for an empty array")
57+
void testMaxElementEmptyArray() {
58+
int[] nums = {};
59+
assertThrows(IllegalArgumentException.class, () -> solution.maxElement(nums));
60+
}
61+
62+
// Test cases for secondLargestElement()
63+
64+
@Test
65+
@DisplayName("secondLargestElement should find the second-largest element in a standard case")
66+
void testSecondLargestElementStandardCase() {
67+
int[] nums = {1, 8, 3, 10, 4};
68+
assertEquals(8, solution.secondLargestElement(nums));
69+
}
70+
71+
@Test
72+
@DisplayName("secondLargestElement should return -1 if all elements are the same")
73+
void testSecondLargestElementAllElementsSame() {
74+
int[] nums = {5, 5, 5, 5};
75+
assertEquals(-1, solution.secondLargestElement(nums));
76+
}
77+
78+
@Test
79+
@DisplayName("secondLargestElement should handle an array with two distinct elements")
80+
void testSecondLargestElementTwoElements() {
81+
int[] nums = {10, 5};
82+
assertEquals(5, solution.secondLargestElement(nums));
83+
}
84+
85+
@Test
86+
@DisplayName("secondLargestElement should handle negative numbers correctly")
87+
void testSecondLargestElementNegativeNumbers() {
88+
int[] nums = {-5, -2, -10, -1};
89+
assertEquals(-2, solution.secondLargestElement(nums));
90+
}
91+
92+
@Test
93+
@DisplayName("secondLargestElement should return -1 for an array with fewer than two elements")
94+
void testSecondLargestElementLessThanTwoElements() {
95+
int[] nums = {10};
96+
assertEquals(-1, solution.secondLargestElement(nums));
97+
}
98+
99+
@Test
100+
@DisplayName("secondLargestElement should return -1 for a null array")
101+
void testSecondLargestElementNullArray() {
102+
assertEquals(-1, solution.secondLargestElement(null));
103+
}
104+
105+
@Test
106+
@DisplayName("secondLargestElement should return -1 for an empty array")
107+
void testSecondLargestElementEmptyArray() {
108+
int[] nums = {};
109+
assertEquals(-1, solution.secondLargestElement(nums));
110+
}
111+
112+
@Test
113+
@DisplayName("Failing Case: secondLargestElement should handle an array where the second-largest element is Integer.MIN_VALUE")
114+
void testSecondLargestElementFailingCaseWithMinValue() {
115+
// This test exposes a flaw in the logic. The current implementation returns -1
116+
// because it uses Integer.MIN_VALUE as a sentinel for "not found", but it can
117+
// also be a valid second-largest number.
118+
int[] nums = {10, Integer.MIN_VALUE};
119+
assertEquals(Integer.MIN_VALUE, solution.secondLargestElement(nums),
120+
"The method fails to correctly identify Integer.MIN_VALUE as the second-largest element.");
121+
}
122+
}

0 commit comments

Comments
 (0)