From 54cc684bf2a1950a1c8d024f0d762aab7b9a5347 Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Fri, 17 Oct 2025 09:13:21 +0530 Subject: [PATCH 1/4] added some array datastructre algoritms --- .../datastructures/arrays/LeadersInArray.java | 46 +++++++++++++++ .../datastructures/arrays/PeakElement.java | 59 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java create mode 100644 src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java new file mode 100644 index 000000000000..b7bfa7bc487e --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java @@ -0,0 +1,46 @@ +package com.thealgorithms.datastructures.arrays; + +/** + * A program to find leaders in an array. + * + * A leader is an element that is greater than all the elements to its right. + * The rightmost element is always a leader. + * + * Example: + * Input: [16, 17, 4, 3, 5, 2] + * Output: Leaders are 17, 5, 2 + * + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * Author: https://github.com/VeeruYadav45 + */ +public class LeadersInArray { + + /** + * Prints all leader elements in the array. + * + * @param arr the input array + */ + public static void findLeaders(int[] arr) { + int n = arr.length; + + // The rightmost element is always a leader + int maxFromRight = arr[n - 1]; + System.out.print("Leaders: " + maxFromRight + " "); + + // Traverse the array from right to left + for (int i = n - 2; i >= 0; i--) { + if (arr[i] > maxFromRight) { + maxFromRight = arr[i]; // Update the new leader + System.out.print(maxFromRight + " "); + } + } + } + + // Example usage + public static void main(String[] args) { + int[] arr = { 16, 17, 4, 3, 5, 2 }; + findLeaders(arr); + } +} diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java new file mode 100644 index 000000000000..04ff20fb30db --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java @@ -0,0 +1,59 @@ +package com.thealgorithms.datastructures.arrays; + +/** + * A program to find a peak element in an array. + * + *

+ * A peak element is an element that is greater than or equal to its neighbors. + * For corner elements, we need to consider only one neighbor. + * + * Example: + * Input: [1, 3, 20, 4, 1, 0] + * Output: Peak element is 20 + * + * Time Complexity: O(log n) using binary search + * Space Complexity: O(1) + * + * Author: https://github.com/VeeruYadav45 + */ +public class PeakElement { + + /** + * Finds a peak element in the array using binary search. + * + * @param arr the input array + * @return the index of any one peak element + */ + public static int findPeakElement(int[] arr) { + int n = arr.length; + int low = 0, high = n - 1; + + while (low <= high) { + int mid = low + (high - low) / 2; + + // Check if mid is a peak + 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 left neighbor is greater, move left + if (mid > 0 && arr[mid - 1] > arr[mid]) { + high = mid - 1; + } else { // Otherwise move right + low = mid + 1; + } + } + + return -1; // Should never reach here if input is valid + } + + // Example usage + public static void main(String[] args) { + int[] arr = { 1, 3, 20, 4, 1, 0 }; + int peakIndex = findPeakElement(arr); + System.out.println("Peak element is " + arr[peakIndex]); + } +} From f1c0d8600eb01f4d1f44eab5b18b20d1ea525912 Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Fri, 17 Oct 2025 09:19:33 +0530 Subject: [PATCH 2/4] ADDED DATA STRUCTURE ARRAY ALGORITHMS --- .../com/thealgorithms/datastructures/arrays/LeadersInArray.java | 1 + .../com/thealgorithms/datastructures/arrays/PeakElement.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java index b7bfa7bc487e..acdfe8ba0900 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java @@ -2,6 +2,7 @@ /** * A program to find leaders in an array. + * https://www.geeksforgeeks.org/dsa/find-a-peak-in-a-given-array/ * * A leader is an element that is greater than all the elements to its right. * The rightmost element is always a leader. diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java index 04ff20fb30db..9fdbbf331dbc 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java @@ -2,6 +2,7 @@ /** * A program to find a peak element in an array. + * https://www.geeksforgeeks.org/dsa/leaders-in-an-array/ * *

* A peak element is an element that is greater than or equal to its neighbors. From c2585ef95fa314784b546bd1bc83b824fea450ff Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Fri, 17 Oct 2025 09:34:12 +0530 Subject: [PATCH 3/4] Fix formatting and Checkstyle issues --- .../datastructures/arrays/LeadersInArray.java | 34 ++++++++-------- .../datastructures/arrays/PeakElement.java | 40 ++++++++++--------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java index acdfe8ba0900..e4cd3a79981b 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java @@ -2,46 +2,48 @@ /** * A program to find leaders in an array. - * https://www.geeksforgeeks.org/dsa/find-a-peak-in-a-given-array/ - * + * + *

* A leader is an element that is greater than all the elements to its right. * The rightmost element is always a leader. * - * Example: - * Input: [16, 17, 4, 3, 5, 2] - * Output: Leaders are 17, 5, 2 - * * Time Complexity: O(n) * Space Complexity: O(1) - * + * * Author: https://github.com/VeeruYadav45 */ -public class LeadersInArray { +public final class LeadersInArray { + + // Prevent instantiation of this utility class + private LeadersInArray() { + } /** * Prints all leader elements in the array. * * @param arr the input array */ - public static void findLeaders(int[] arr) { + public static void findLeaders(final int[] arr) { int n = arr.length; - - // The rightmost element is always a leader int maxFromRight = arr[n - 1]; System.out.print("Leaders: " + maxFromRight + " "); - // Traverse the array from right to left for (int i = n - 2; i >= 0; i--) { if (arr[i] > maxFromRight) { - maxFromRight = arr[i]; // Update the new leader + maxFromRight = arr[i]; System.out.print(maxFromRight + " "); } } + System.out.println(); } - // Example usage - public static void main(String[] args) { - int[] arr = { 16, 17, 4, 3, 5, 2 }; + /** + * Example usage. + * + * @param args command line arguments (not used) + */ + public static void main(final String[] args) { + int[] arr = {16, 17, 4, 3, 5, 2}; findLeaders(arr); } } diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java index 9fdbbf331dbc..2ea6623e7897 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java @@ -2,37 +2,36 @@ /** * A program to find a peak element in an array. - * https://www.geeksforgeeks.org/dsa/leaders-in-an-array/ * *

* A peak element is an element that is greater than or equal to its neighbors. - * For corner elements, we need to consider only one neighbor. + * For corner elements, only one neighbor is considered. * - * Example: - * Input: [1, 3, 20, 4, 1, 0] - * Output: Peak element is 20 - * - * Time Complexity: O(log n) using binary search + * Time Complexity: O(n) * Space Complexity: O(1) * * Author: https://github.com/VeeruYadav45 */ -public class PeakElement { +public final class PeakElement { + + // Prevent instantiation of this utility class + private PeakElement() { + } /** - * Finds a peak element in the array using binary search. + * Finds the index of any peak element in the array. * * @param arr the input array - * @return the index of any one peak element + * @return the index of a peak element */ - public static int findPeakElement(int[] arr) { + public static int findPeakElement(final int[] arr) { int n = arr.length; - int low = 0, high = n - 1; + int low = 0; + int high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; - // Check if mid is a peak boolean leftOk = (mid == 0) || (arr[mid] >= arr[mid - 1]); boolean rightOk = (mid == n - 1) || (arr[mid] >= arr[mid + 1]); @@ -40,20 +39,23 @@ public static int findPeakElement(int[] arr) { return mid; } - // If left neighbor is greater, move left if (mid > 0 && arr[mid - 1] > arr[mid]) { high = mid - 1; - } else { // Otherwise move right + } else { low = mid + 1; } } - return -1; // Should never reach here if input is valid + return -1; } - // Example usage - public static void main(String[] args) { - int[] arr = { 1, 3, 20, 4, 1, 0 }; + /** + * Example usage. + * + * @param args command line arguments (not used) + */ + 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]); } From 565663797f5b6e77e7bbc5c78dd81d84fa3e1610 Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Fri, 17 Oct 2025 09:41:24 +0530 Subject: [PATCH 4/4] Again Fixed formatting and Checkstyle issues --- .../datastructures/arrays/LeadersInArray.java | 7 ++++--- .../datastructures/arrays/PeakElement.java | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java index e4cd3a79981b..84ad0403fd33 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/LeadersInArray.java @@ -1,10 +1,10 @@ package com.thealgorithms.datastructures.arrays; /** - * A program to find leaders in an array. + * A utility class to find leaders in an array. * *

- * A leader is an element that is greater than all the elements to its right. + * A leader is an element greater than all elements to its right. * The rightmost element is always a leader. * * Time Complexity: O(n) @@ -14,7 +14,7 @@ */ public final class LeadersInArray { - // Prevent instantiation of this utility class + // Private constructor to prevent instantiation private LeadersInArray() { } @@ -42,6 +42,7 @@ public static void findLeaders(final int[] arr) { * * @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); diff --git a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java index 2ea6623e7897..639d5492bb72 100644 --- a/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java +++ b/src/main/java/com/thealgorithms/datastructures/arrays/PeakElement.java @@ -1,25 +1,24 @@ package com.thealgorithms.datastructures.arrays; /** - * A program to find a peak element in an array. + * A utility class to find a peak element in an array. * *

- * A peak element is an element that is greater than or equal to its neighbors. - * For corner elements, only one neighbor is considered. + * A peak element is an element greater than or equal to its neighbors. * - * Time Complexity: O(n) + * Time Complexity: O(log n) using binary search * Space Complexity: O(1) * * Author: https://github.com/VeeruYadav45 */ public final class PeakElement { - // Prevent instantiation of this utility class + // Private constructor to prevent instantiation private PeakElement() { } /** - * Finds the index of any peak element in the array. + * Finds the index of a peak element using binary search. * * @param arr the input array * @return the index of a peak element @@ -54,6 +53,7 @@ public static int findPeakElement(final int[] arr) { * * @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);