Skip to content

Commit 54cc684

Browse files
committed
added some array datastructre algoritms
1 parent e1773e9 commit 54cc684

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.datastructures.arrays;
2+
3+
/**
4+
* A program to find leaders in an array.
5+
*
6+
* A leader is an element that is greater than all the elements to its right.
7+
* The rightmost element is always a leader.
8+
*
9+
* Example:
10+
* Input: [16, 17, 4, 3, 5, 2]
11+
* Output: Leaders are 17, 5, 2
12+
*
13+
* Time Complexity: O(n)
14+
* Space Complexity: O(1)
15+
*
16+
* Author: https://github.com/VeeruYadav45
17+
*/
18+
public class LeadersInArray {
19+
20+
/**
21+
* Prints all leader elements in the array.
22+
*
23+
* @param arr the input array
24+
*/
25+
public static void findLeaders(int[] arr) {
26+
int n = arr.length;
27+
28+
// The rightmost element is always a leader
29+
int maxFromRight = arr[n - 1];
30+
System.out.print("Leaders: " + maxFromRight + " ");
31+
32+
// Traverse the array from right to left
33+
for (int i = n - 2; i >= 0; i--) {
34+
if (arr[i] > maxFromRight) {
35+
maxFromRight = arr[i]; // Update the new leader
36+
System.out.print(maxFromRight + " ");
37+
}
38+
}
39+
}
40+
41+
// Example usage
42+
public static void main(String[] args) {
43+
int[] arr = { 16, 17, 4, 3, 5, 2 };
44+
findLeaders(arr);
45+
}
46+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.datastructures.arrays;
2+
3+
/**
4+
* A program to find a peak element in an array.
5+
*
6+
* <p>
7+
* A peak element is an element that is greater than or equal to its neighbors.
8+
* For corner elements, we need to consider only one neighbor.
9+
*
10+
* Example:
11+
* Input: [1, 3, 20, 4, 1, 0]
12+
* Output: Peak element is 20
13+
*
14+
* Time Complexity: O(log n) using binary search
15+
* Space Complexity: O(1)
16+
*
17+
* Author: https://github.com/VeeruYadav45
18+
*/
19+
public class PeakElement {
20+
21+
/**
22+
* Finds a peak element in the array using binary search.
23+
*
24+
* @param arr the input array
25+
* @return the index of any one peak element
26+
*/
27+
public static int findPeakElement(int[] arr) {
28+
int n = arr.length;
29+
int low = 0, high = n - 1;
30+
31+
while (low <= high) {
32+
int mid = low + (high - low) / 2;
33+
34+
// Check if mid is a peak
35+
boolean leftOk = (mid == 0) || (arr[mid] >= arr[mid - 1]);
36+
boolean rightOk = (mid == n - 1) || (arr[mid] >= arr[mid + 1]);
37+
38+
if (leftOk && rightOk) {
39+
return mid;
40+
}
41+
42+
// If left neighbor is greater, move left
43+
if (mid > 0 && arr[mid - 1] > arr[mid]) {
44+
high = mid - 1;
45+
} else { // Otherwise move right
46+
low = mid + 1;
47+
}
48+
}
49+
50+
return -1; // Should never reach here if input is valid
51+
}
52+
53+
// Example usage
54+
public static void main(String[] args) {
55+
int[] arr = { 1, 3, 20, 4, 1, 0 };
56+
int peakIndex = findPeakElement(arr);
57+
System.out.println("Peak element is " + arr[peakIndex]);
58+
}
59+
}

0 commit comments

Comments
 (0)