-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from MahiTyagi30/issue-71
Adding Finding Peak Element Problem code in java in DSA Folder #71
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import java.util.Scanner; | ||
|
||
public class BestTimeToBuyAndSellStock { | ||
|
||
// Function to calculate maximum profit | ||
public static int maxProfit(int[] prices) { | ||
if (prices == null || prices.length == 0) return 0; | ||
|
||
int minPrice = Integer.MAX_VALUE; | ||
int maxProfit = 0; | ||
|
||
for (int i = 0; i < prices.length; i++) { | ||
if (prices[i] < minPrice) { | ||
minPrice = prices[i]; | ||
} | ||
int profit = prices[i] - minPrice; | ||
if (profit > maxProfit) { | ||
maxProfit = profit; | ||
} | ||
} | ||
return maxProfit; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
// Prompt user for the number of days | ||
System.out.print("Enter the number of days: "); | ||
int n = scanner.nextInt(); | ||
|
||
// Create an array to store the prices | ||
int[] prices = new int[n]; | ||
|
||
// Input stock prices from user | ||
System.out.println("Enter the stock prices for " + n + " days:"); | ||
for (int i = 0; i < n; i++) { | ||
prices[i] = scanner.nextInt(); | ||
} | ||
|
||
// Call the maxProfit function and print the result | ||
int result = maxProfit(prices); | ||
System.out.println("Maximum Profit: " + result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import java.util.Scanner; | ||
|
||
public class PeakElementFinder { | ||
|
||
public static int findPeakElement(int[] arr) { | ||
return findPeakUtil(arr, 0, arr.length - 1); | ||
} | ||
|
||
private static int findPeakUtil(int[] arr, int left, int right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
// Check if mid is a peak | ||
if ((mid == 0 || arr[mid - 1] < arr[mid]) && (mid == arr.length - 1 || arr[mid] > arr[mid + 1])) { | ||
return mid; | ||
} | ||
|
||
// If the left neighbor is greater, then the peak lies on the left side | ||
if (mid > 0 && arr[mid - 1] > arr[mid]) { | ||
return findPeakUtil(arr, left, mid - 1); | ||
} | ||
|
||
// If the right neighbor is greater, then the peak lies on the right side | ||
return findPeakUtil(arr, mid + 1, right); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.print("Enter the length of the array: "); | ||
int n = scanner.nextInt(); | ||
|
||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements of the array:"); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = scanner.nextInt(); | ||
} | ||
|
||
int peakIndex = findPeakElement(arr); | ||
|
||
System.out.println("Index of a peak element: " + peakIndex); | ||
System.out.println("Peak element: " + arr[peakIndex]); | ||
|
||
scanner.close(); // Close the scanner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// File: protocol.java | ||
|
||
/* | ||
* Problem Statement: | ||
* Given an array of integers, find a peak element. A peak element is an element that is greater | ||
* than its neighbors. In case of a boundary element, it only needs to be greater than its one neighbor. | ||
* The goal is to find the index of one peak element in the array using an efficient approach. | ||
* | ||
* A peak element can be located anywhere in the array, and there can be multiple peak elements. | ||
* The function should return the index of one such peak. | ||
*/ | ||
|
||
/* | ||
* Approach: | ||
* 1. The problem can be solved using a binary search-like approach. | ||
* 2. The `findPeakElement` function uses recursion to find the peak element. | ||
* 3. In the recursive helper function `findPeakUtil`, we calculate the middle index: | ||
* - Check if the middle element is greater than its neighbors (i.e., it's a peak). | ||
* - If the element on the left side of mid is greater, search in the left half of the array. | ||
* - If the element on the right side of mid is greater, search in the right half of the array. | ||
* 4. This approach ensures that a peak is found in O(log n) time. | ||
*/ | ||
|
||
import java.util.Scanner; | ||
|
||
public class PeakElementFinder { | ||
|
||
// Method to find a peak element in the array | ||
public static int findPeakElement(int[] arr) { | ||
return findPeakUtil(arr, 0, arr.length - 1); // Call helper function | ||
} | ||
|
||
// Recursive function to find peak element using binary search logic | ||
private static int findPeakUtil(int[] arr, int left, int right) { | ||
int mid = left + (right - left) / 2; // Find the middle index | ||
|
||
// Check if mid is a peak element | ||
if ((mid == 0 || arr[mid - 1] < arr[mid]) && (mid == arr.length - 1 || arr[mid] > arr[mid + 1])) { | ||
return mid; // Mid is a peak element | ||
} | ||
|
||
// If the left neighbor is greater, the peak must be on the left side | ||
if (mid > 0 && arr[mid - 1] > arr[mid]) { | ||
return findPeakUtil(arr, left, mid - 1); | ||
} | ||
|
||
// Otherwise, the peak must be on the right side | ||
return findPeakUtil(arr, mid + 1, right); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
// Input the length of the array | ||
System.out.print("Enter the length of the array: "); | ||
int n = scanner.nextInt(); | ||
|
||
// Create and input elements for the array | ||
int[] arr = new int[n]; | ||
System.out.println("Enter the elements of the array:"); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = scanner.nextInt(); | ||
} | ||
|
||
// Find the peak element index and print it | ||
int peakIndex = findPeakElement(arr); | ||
System.out.println("Index of a peak element: " + peakIndex); | ||
System.out.println("Peak element: " + arr[peakIndex]); | ||
|
||
scanner.close(); // Close the scanner to avoid resource leak | ||
} | ||
} | ||
|