ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

CQL: Find Peak Element in Array

Cassandra CQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION find_peak_element(
2nums list<int>
3)
4RETURNS int
5AS $$
6DECLARE
7n int;
8low int;
9high int;
10mid int;
11BEGIN
12n = list_size(nums);
13
14-- Handle edge cases: empty list and single element list
15IF n = 0 THEN
16RETURN NULL; -- Or throw an error, depending on requirements
17END IF;
18IF n = 1 THEN
19RETURN nums[0];
20END IF;
21
22low = 0;
23high = n - 1;
24
25WHILE low < high DO
26mid = low + (high - low) / 2;
27
28-- Check if mid is a peak
29-- If nums[mid] > nums[mid+1], then a peak must exist in the left half (including mid)
30IF nums[mid] > nums[mid + 1] THEN
31high = mid;
32-- If nums[mid] < nums[mid+1], then a peak must exist in the right half (excluding mid)
33ELSE
34low = mid + 1;
35END IF;
36END WHILE;
37
38-- At the end of the loop, low == high, which points to a peak element
39RETURN nums[low];
40END; $$
41
42-- Explanation of why this works:
43-- The binary search invariant is that a peak element is guaranteed to exist
44-- within the range [low, high].
45-- When nums[mid] > nums[mid + 1], it means that either nums[mid] is a peak,
46-- or there's an element to its left that's even larger (forming a peak further left).
47-- So, we can safely discard the right half (mid+1 to high) and search in [low, mid].
48-- When nums[mid] < nums[mid + 1], it means that nums[mid+1] is greater than nums[mid].
49-- This implies that either nums[mid+1] is a peak, or there's an element to its right
50-- that's even larger. So, we can safely discard the left half (low to mid) and search in [mid+1, high].
51-- The loop terminates when low == high, and this single element must be a peak.
52
53-- Example Usage:
54-- SELECT find_peak_element([1, 2, 3, 1]); -- Expected: 3
55-- SELECT find_peak_element([1, 2, 1, 3, 5, 6, 4]); -- Expected: 2 or 6
56-- SELECT find_peak_element([1, 2, 3, 4, 5]); -- Expected: 5 (monotonic increasing)
57-- SELECT find_peak_element([5, 4, 3, 2, 1]); -- Expected: 5 (monotonic decreasing)
58-- SELECT find_peak_element([5]); -- Expected: 5
59-- SELECT find_peak_element([]); -- Expected: NULL
Algorithm description viewbox

CQL: Find Peak Element in Array

Algorithm description:

This Cassandra CQL function finds a peak element in a list of integers using a binary search approach. A peak element is defined as an element that is greater than or equal to its neighbors. The function handles edge cases such as empty lists, single-element lists, and monotonic arrays. This is useful for optimization problems and data analysis where identifying local maxima is important.

Algorithm explanation:

The `find_peak_element` function employs a binary search strategy to efficiently locate a peak element. A peak element is one greater than or equal to its adjacent elements. The algorithm initializes `low` to 0 and `high` to `n-1` (where `n` is the list size). In each iteration, it calculates the middle index `mid`. If `nums[mid]` is greater than `nums[mid+1]`, it implies a peak exists in the left half (including `mid`), so `high` is updated to `mid`. Otherwise, a peak must exist in the right half (excluding `mid`), and `low` is updated to `mid+1`. The loop continues until `low` equals `high`, at which point `nums[low]` is guaranteed to be a peak. Time complexity is O(log N) due to binary search. Space complexity is O(1) as it uses a constant amount of extra space. Edge cases handled: empty list (returns NULL), single-element list (returns that element), and monotonic lists (returns the end element).

Pseudocode:

FUNCTION find_peak_element(nums):
  n = size of nums

  IF n is 0 THEN
    RETURN NULL
  END IF
  IF n is 1 THEN
    RETURN nums[0]
  END IF

  low = 0
  high = n - 1

  WHILE low < high:
    mid = low + (high - low) / 2

    IF nums[mid] > nums[mid + 1] THEN
      high = mid
    ELSE
      low = mid + 1
    END IF
  END WHILE

  RETURN nums[low]
END FUNCTION