Skip to content

Commit c2585ef

Browse files
committed
Fix formatting and Checkstyle issues
1 parent f1c0d86 commit c2585ef

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@
22

33
/**
44
* A program to find leaders in an array.
5-
* https://www.geeksforgeeks.org/dsa/find-a-peak-in-a-given-array/
6-
*
5+
*
6+
* <p>
77
* A leader is an element that is greater than all the elements to its right.
88
* The rightmost element is always a leader.
99
*
10-
* Example:
11-
* Input: [16, 17, 4, 3, 5, 2]
12-
* Output: Leaders are 17, 5, 2
13-
*
1410
* Time Complexity: O(n)
1511
* Space Complexity: O(1)
16-
*
12+
*
1713
* Author: https://github.com/VeeruYadav45
1814
*/
19-
public class LeadersInArray {
15+
public final class LeadersInArray {
16+
17+
// Prevent instantiation of this utility class
18+
private LeadersInArray() {
19+
}
2020

2121
/**
2222
* Prints all leader elements in the array.
2323
*
2424
* @param arr the input array
2525
*/
26-
public static void findLeaders(int[] arr) {
26+
public static void findLeaders(final int[] arr) {
2727
int n = arr.length;
28-
29-
// The rightmost element is always a leader
3028
int maxFromRight = arr[n - 1];
3129
System.out.print("Leaders: " + maxFromRight + " ");
3230

33-
// Traverse the array from right to left
3431
for (int i = n - 2; i >= 0; i--) {
3532
if (arr[i] > maxFromRight) {
36-
maxFromRight = arr[i]; // Update the new leader
33+
maxFromRight = arr[i];
3734
System.out.print(maxFromRight + " ");
3835
}
3936
}
37+
System.out.println();
4038
}
4139

42-
// Example usage
43-
public static void main(String[] args) {
44-
int[] arr = { 16, 17, 4, 3, 5, 2 };
40+
/**
41+
* Example usage.
42+
*
43+
* @param args command line arguments (not used)
44+
*/
45+
public static void main(final String[] args) {
46+
int[] arr = {16, 17, 4, 3, 5, 2};
4547
findLeaders(arr);
4648
}
4749
}

src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,60 @@
22

33
/**
44
* A program to find a peak element in an array.
5-
* https://www.geeksforgeeks.org/dsa/leaders-in-an-array/
65
*
76
* <p>
87
* A peak element is an element that is greater than or equal to its neighbors.
9-
* For corner elements, we need to consider only one neighbor.
8+
* For corner elements, only one neighbor is considered.
109
*
11-
* Example:
12-
* Input: [1, 3, 20, 4, 1, 0]
13-
* Output: Peak element is 20
14-
*
15-
* Time Complexity: O(log n) using binary search
10+
* Time Complexity: O(n)
1611
* Space Complexity: O(1)
1712
*
1813
* Author: https://github.com/VeeruYadav45
1914
*/
20-
public class PeakElement {
15+
public final class PeakElement {
16+
17+
// Prevent instantiation of this utility class
18+
private PeakElement() {
19+
}
2120

2221
/**
23-
* Finds a peak element in the array using binary search.
22+
* Finds the index of any peak element in the array.
2423
*
2524
* @param arr the input array
26-
* @return the index of any one peak element
25+
* @return the index of a peak element
2726
*/
28-
public static int findPeakElement(int[] arr) {
27+
public static int findPeakElement(final int[] arr) {
2928
int n = arr.length;
30-
int low = 0, high = n - 1;
29+
int low = 0;
30+
int high = n - 1;
3131

3232
while (low <= high) {
3333
int mid = low + (high - low) / 2;
3434

35-
// Check if mid is a peak
3635
boolean leftOk = (mid == 0) || (arr[mid] >= arr[mid - 1]);
3736
boolean rightOk = (mid == n - 1) || (arr[mid] >= arr[mid + 1]);
3837

3938
if (leftOk && rightOk) {
4039
return mid;
4140
}
4241

43-
// If left neighbor is greater, move left
4442
if (mid > 0 && arr[mid - 1] > arr[mid]) {
4543
high = mid - 1;
46-
} else { // Otherwise move right
44+
} else {
4745
low = mid + 1;
4846
}
4947
}
5048

51-
return -1; // Should never reach here if input is valid
49+
return -1;
5250
}
5351

54-
// Example usage
55-
public static void main(String[] args) {
56-
int[] arr = { 1, 3, 20, 4, 1, 0 };
52+
/**
53+
* Example usage.
54+
*
55+
* @param args command line arguments (not used)
56+
*/
57+
public static void main(final String[] args) {
58+
int[] arr = {1, 3, 20, 4, 1, 0};
5759
int peakIndex = findPeakElement(arr);
5860
System.out.println("Peak element is " + arr[peakIndex]);
5961
}

0 commit comments

Comments
 (0)