Manafall Daily Quest

Find Peak Element

Function Name: findPeakElement

Description

Given an array of integers nums, write a function 'findPeakElement' that returns the index of any peak element. A peak element is an element that is greater than its neighbors. For the purposes of this problem, consider that the 'nums' array has -Infinity as neighbors past its bounds, meaning that if the first or last element is greater than their single neighbor (inside the array), they could be considered peak elements as well.

You must write an algorithm that runs in O(log n) time complexity.

Requirements

  • The function must accept an array of integers as a parameter.
  • The function should return the index of any peak element, not necessarily the first or the largest.
  • The function's runtime complexity should be O(log n).
  • You may assume that the input array 'nums' will have at least one element.
  • If the array has multiple peaks, returning the index of any peak is acceptable.
  • Do not use built-in functions that trivialize the problem (such as finding a maximum value with a single function call).

Examples

Example 1:Input: nums = [1, 2, 3, 1]Output: 2Explanation: 3 is a peak element and your function should return index number 2.Example 2:Input: nums = [1, 2, 1, 3, 5, 6, 4]Output: 1 or 5Explanation: Both 2 and 6 are peak elements. Your function can return either index 1 or index 5.

Links

https://en.wikipedia.org/wiki/Peak_detection

Frequency Counter

Wed Nov 13 2024

Write a function named calculateFrequency that takes a string as its parameter. The function should return a dictionary or an object that maps each character in the string to the number of times it appears in the string. The frequency count should be case-sensitive, meaning that 'a' and 'A' should be counted as two different characters. Non-alphabetic characters should be included in the count as well. The output should be sorted by the character in ascending order.

Prev Quest

Sort Colors

Fri Nov 15 2024

Given an array consisting of n objects with '0's, '1's, and '2's, sort the array in-place so that objects of the same color are adjacent, with the colors in the order of '0', '1', and '2'.You are not allowed to use the library's sort function and should solve this problem with a linear complexity O(n).The function should modify the array in-place and does not need to return anything.

Next Quest