Peak Elements

This commit is contained in:
Imran Alam 2024-02-20 15:35:40 +05:30
parent 536ffe9106
commit e62e1d88e6
6 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.baeldung.peakelements;
import java.util.ArrayList;
import java.util.List;
public class MultiplePeakFinder {
public static List<Integer> findPeaks(int[] arr) {
List<Integer> peaks = new ArrayList<>();
if (arr == null || arr.length == 0) {
return peaks;
}
findPeakElement(arr, 0, arr.length - 1, peaks);
return peaks;
}
private static void findPeakElement(int[] arr, int low, int high, List<Integer> peaks) {
if (low > high) {
return;
}
int mid = low + (high - low) / 2;
if ((mid == 0 || arr[mid] >= arr[mid - 1]) && (mid == arr.length - 1 || arr[mid] >= arr[mid + 1])) {
peaks.add(arr[mid]);
}
if (mid > 0) {
findPeakElement(arr, low, mid - 1, peaks);
}
if (mid < arr.length - 1) {
findPeakElement(arr, mid + 1, high, peaks);
}
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.peakelements;
import java.util.ArrayList;
import java.util.List;
public class PeakElementFinder {
public List<Integer> findPeakElement(int[] arr) {
int n = arr.length;
List<Integer> peaks = new ArrayList<>();
if (n == 0) {
return peaks;
}
for (int i = 0; i < n; i++) {
if (isPeak(arr, i, n)) {
peaks.add(i);
}
while (i < n - 1 && arr[i] == arr[i + 1]) {
i++;
}
}
return peaks;
}
private boolean isPeak(int[] arr, int index, int n) {
if (index == 0) {
return n > 1 ? arr[index] >= arr[index + 1] : true;
} else if (index == n - 1) {
return arr[index] >= arr[index - 1];
}
return arr[index] >= arr[index - 1] && arr[index] >= arr[index + 1];
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.peakelements;
public class SinglePeakFinder {
public static int findSinglePeak(int[] arr) {
int n = arr.length;
for (int i = 1; i < n - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
return arr[i];
}
}
if (arr[0] > arr[1]) {
return arr[0];
}
if (arr[n - 1] > arr[n - 2]) {
return arr[n - 1];
}
return -1;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.peakelements;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class MultiplePeakFinderUnitTest {
@Test
void findPeaks_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeaks() {
MultiplePeakFinder finder = new MultiplePeakFinder();
int[] array = {1, 13, 7, 0, 4, 1, 4, 45, 50};
List<Integer> peaks = finder.findPeaks(array);
assertEquals(3, peaks.size());
assertTrue(peaks.contains(4));
assertTrue(peaks.contains(13));
assertTrue(peaks.contains(50));
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.peakelements;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class PeakElementFinderUnitTest {
@Test
void findPeakElement_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeakIndex() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {5, 8, 7, 6, 12};
List<Integer> peaks = finder.findPeakElement(array);
assertEquals(2, peaks.size() );
}
@Test
void findPeakElement_givenArrayOfIntegers_whenNoPeaks_thenReturnsMinusOne() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {5, 8, 7, 6, 12};
List<Integer> peaks = finder.findPeakElement(array);
assertEquals(2, peaks.size());
}
@Test
void findPeakElement_givenArrayOfIntegers_whenPeaksAtExtremes_thenReturnsCorrectPeakIndex() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {5, 2, 1, 3, 4};
List<Integer> peaks = finder.findPeakElement(array);
assertEquals(2, peaks.size());
}
@Test
void findPeakElement_givenArrayOfIntegers_whenPlateaus_thenReturnsCorrectPeakIndex() {
PeakElementFinder finder = new PeakElementFinder();
int[] array = {1, 2, 2, 2, 3, 4, 5};
List<Integer> peaks = finder.findPeakElement(array);
assertEquals(2, peaks.size());
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.peakelements;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class SinglePeakFinderUnitTest {
@Test
void findSinglePeak_givenArrayOfIntegers_whenValidInput_thenReturnsCorrectPeak() {
int[] arr = {0, 10, 2, 4, 5, 1};
assertEquals(10, SinglePeakFinder.findSinglePeak(arr));
}
}