Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thealgorithms.datastructures.arrays;

/**
* A utility class to find leaders in an array.
*
* <p>
* A leader is an element greater than all elements to its right.
* The rightmost element is always a leader.
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* Author: https://github.com/VeeruYadav45
*/
public final class LeadersInArray {

// Private constructor to prevent instantiation
private LeadersInArray() {
}

/**
* Prints all leader elements in the array.
*
* @param arr the input array
*/
public static void findLeaders(final int[] arr) {
int n = arr.length;
int maxFromRight = arr[n - 1];
System.out.print("Leaders: " + maxFromRight + " ");

for (int i = n - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
System.out.print(maxFromRight + " ");
}
}
System.out.println();
}

/**
* Example usage.
*
* @param args command line arguments (not used)
*/
@SuppressWarnings("PMD.UselessMainMethod")
public static void main(final String[] args) {
int[] arr = {16, 17, 4, 3, 5, 2};
findLeaders(arr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thealgorithms.datastructures.arrays;

/**
* A utility class to find a peak element in an array.
*
* <p>
* A peak element is an element greater than or equal to its neighbors.
*
* Time Complexity: O(log n) using binary search
* Space Complexity: O(1)
*
* Author: https://github.com/VeeruYadav45
*/
public final class PeakElement {

// Private constructor to prevent instantiation
private PeakElement() {
}

/**
* Finds the index of a peak element using binary search.
*
* @param arr the input array
* @return the index of a peak element
*/
public static int findPeakElement(final int[] arr) {
int n = arr.length;
int low = 0;
int high = n - 1;

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

boolean leftOk = (mid == 0) || (arr[mid] >= arr[mid - 1]);
boolean rightOk = (mid == n - 1) || (arr[mid] >= arr[mid + 1]);

if (leftOk && rightOk) {
return mid;
}

if (mid > 0 && arr[mid - 1] > arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}

/**
* Example usage.
*
* @param args command line arguments (not used)
*/
@SuppressWarnings("PMD.UselessMainMethod")
public static void main(final String[] args) {
int[] arr = {1, 3, 20, 4, 1, 0};
int peakIndex = findPeakElement(arr);
System.out.println("Peak element is " + arr[peakIndex]);
}
}