-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestElement.java
More file actions
37 lines (32 loc) · 976 Bytes
/
Copy pathLargestElement.java
File metadata and controls
37 lines (32 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package array;
public class LargestElement {
static int findLargestElement(int[] nums) {
int start = 0;
int end = nums.length - 1;
int largestElement = Integer.MIN_VALUE;
int count = 0;
while (start < end) {
if (nums[start] > nums[end] && nums[start] > largestElement) {
largestElement = nums[start];
count++;
start++;
end--;
}
if (nums[end] > nums[start] && nums[end] > largestElement) {
largestElement = nums[end];
count++;
start++;
end--;
}
count++;
start++;
end--;
}
System.out.println(count);
return largestElement;
}
public static void main(String[] args) {
int[] arr = {2, 5, 1, 28, 3, 0, 11, -12, 19, 56};
System.out.println(findLargestElement(arr));
}
}