Find Peak Element
Write a function findPeakElement that takes an array of integers and finds a peak element, and returns its index. A peak element is an element that is greater than its neighbors.
Function Signature
function findPeakElement(nums);
Example 1:
Input:
nums = [1,2,3,1]
Output:2
Explanation: 3 is a peak element and its index is 2.
Example 2:
Input:
nums = [1,2,1,3,5,6,4]
Output:5
Explanation: 6 is a peak element and its index is 5.
Solve this problem
Start your 7-day free trial to solve this problem in our code editor with instant feedback.
Start 7-Day Free Trial to Solve This ProblemRelated Problems
Merge Intervals
mediumWrite a function `mergeIntervals` that takes an array of intervals and merges all overlapping intervals. ## Function Signature ```function mergeIntervals(intervals);``` ### Example 1: > **Input:** `intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]` \ > **Output:** `[[1, 6], [8, 10], [15, 18]]` \ > **Explanation:** Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6]. ### Example 2: > **Input:** `intervals = [[1, 4], [4, 5]]` \ > **Output:** `[[1, 5]]` \ > **Explanation:** Intervals [1, 4] and [4, 5] are considered overlapping.
ArrayBest Time to Buy and Sell Stock
mediumWrite a function `maxProfit` that takes an array where the `i-th` element is the price of a given stock on day `i`, and returns the maximum profit you can achieve by buying and selling one share of the stock. ## Function Signature ```function maxProfit(prices);``` ### Example 1: > **Input:** `prices = [7,1,5,3,6,4]` \ > **Output:** `5` \ > **Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. ### Example 2: > **Input:** `prices = [7,6,4,3,1]` \ > **Output:** `0` \ > **Explanation:** In this case, no transactions are done, i.e. max profit = 0.
ArrayThree Sum
mediumWrite a function `threeSum` that takes an array of integers and returns all unique triplets that sum up to zero. ## Function Signature ```function threeSum(nums);``` ### Example 1: > **Input:** `nums = [-1, 0, 1, 2, -1, -4]` \ > **Output:** `[[-1, -1, 2], [-1, 0, 1]]` \ > **Explanation:** The triplets that sum up to zero are [-1, -1, 2] and [-1, 0, 1]. ### Example 2: > **Input:** `nums = []` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from an empty array. ### Example 3: > **Input:** `nums = [0]` \ > **Output:** `[]` \ > **Explanation:** No triplets can be formed from a single element.
Array