From e62e1d88e6052863a01818ff95986834db4570f8 Mon Sep 17 00:00:00 2001 From: Imran Alam Date: Tue, 20 Feb 2024 15:35:40 +0530 Subject: [PATCH] Peak Elements --- .../peakelements/MultiplePeakFinder.java | 40 ++++++++++++++++++ .../peakelements/PeakElementFinder.java | 38 +++++++++++++++++ .../peakelements/SinglePeakFinder.java | 25 +++++++++++ .../MultiplePeakFinderUnitTest.java | 24 +++++++++++ .../PeakElementFinderUnitTest.java | 42 +++++++++++++++++++ .../SinglePeakFinderUnitTest.java | 13 ++++++ 6 files changed, 182 insertions(+) create mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/PeakElementFinder.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/SinglePeakFinder.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/MultiplePeakFinderUnitTest.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/PeakElementFinderUnitTest.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/SinglePeakFinderUnitTest.java diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java new file mode 100644 index 0000000000..29956ba027 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/MultiplePeakFinder.java @@ -0,0 +1,40 @@ +package com.baeldung.peakelements; + +import java.util.ArrayList; +import java.util.List; + +public class MultiplePeakFinder { + + public static List findPeaks(int[] arr) { + + List 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 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); + } + + } +} diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/PeakElementFinder.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/PeakElementFinder.java new file mode 100644 index 0000000000..963f505be5 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/PeakElementFinder.java @@ -0,0 +1,38 @@ +package com.baeldung.peakelements; + +import java.util.ArrayList; +import java.util.List; + +public class PeakElementFinder { + public List findPeakElement(int[] arr) { + int n = arr.length; + List 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]; + } +} + diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/SinglePeakFinder.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/SinglePeakFinder.java new file mode 100644 index 0000000000..7b8cd76075 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/peakelements/SinglePeakFinder.java @@ -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; + } +} + diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/MultiplePeakFinderUnitTest.java b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/MultiplePeakFinderUnitTest.java new file mode 100644 index 0000000000..76564a2395 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/MultiplePeakFinderUnitTest.java @@ -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 peaks = finder.findPeaks(array); + + assertEquals(3, peaks.size()); + assertTrue(peaks.contains(4)); + assertTrue(peaks.contains(13)); + assertTrue(peaks.contains(50)); + } +} + diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/PeakElementFinderUnitTest.java b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/PeakElementFinderUnitTest.java new file mode 100644 index 0000000000..8ed6c9a991 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/PeakElementFinderUnitTest.java @@ -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 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 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 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 peaks = finder.findPeakElement(array); + assertEquals(2, peaks.size()); + } +} diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/SinglePeakFinderUnitTest.java b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/SinglePeakFinderUnitTest.java new file mode 100644 index 0000000000..a847afa504 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/peakelements/SinglePeakFinderUnitTest.java @@ -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)); + } +}