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

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

PL/SQL: Find Peak Element in Sorted Array

PL/SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION find_peak_element (
2p_nums IN SYS.ODCINUMBERLIST
3) RETURN NUMBER
4IS
5v_low PLS_INTEGER := 1;
6v_high PLS_INTEGER;
7v_mid PLS_INTEGER;
8v_array_size PLS_INTEGER;
9BEGIN
10-- Handle edge case: empty or null array
11IF p_nums IS NULL OR p_nums.COUNT = 0 THEN
12RETURN -1; -- Indicate no peak element found
13END IF;
14
15v_array_size := p_nums.COUNT;
16v_high := v_array_size;
17
18-- Handle edge case: array with only one element
19IF v_array_size = 1 THEN
20RETURN 1; -- The single element is a peak
21END IF;
22
23-- Binary search for a peak element
24WHILE v_low <= v_high LOOP
25v_mid := TRUNC((v_low + v_high) / 2);
26
27-- Check if v_mid is a peak element
28-- Condition 1: v_mid is not the first element AND element at v_mid-1 is greater than element at v_mid
29IF v_mid > 1 AND p_nums(v_mid - 1) > p_nums(v_mid) THEN
30v_high := v_mid - 1; -- Peak must be in the left half
31-- Condition 2: v_mid is not the last element AND element at v_mid+1 is greater than element at v_mid
32ELSIF v_mid < v_array_size AND p_nums(v_mid + 1) > p_nums(v_mid) THEN
33v_low := v_mid + 1; -- Peak must be in the right half
34-- Condition 3: Element at v_mid is greater than both neighbors (or is at a boundary)
35ELSE
36RETURN v_mid; -- Found a peak element
37END IF;
38END LOOP;
39
40-- This part should ideally not be reached if the array is guaranteed to have a peak
41-- but as a fallback, return -1.
42RETURN -1;
43END;
Algorithm description viewbox

PL/SQL: Find Peak Element in Sorted Array

Algorithm description:

This PL/SQL function finds a peak element in a bitonic array (an array that first increases and then decreases). A peak element is defined as an element that is strictly greater than its adjacent elements. This function is useful in scenarios where you need to find the maximum value in such a structured array efficiently, like in signal processing or optimization problems.

Algorithm explanation:

The function employs a binary search algorithm to efficiently locate a peak element. It initializes `low` to the first index and `high` to the last index of the array. In each iteration, it calculates the middle index `mid`. It then checks if the element at `mid` is a peak by comparing it with its neighbors. If the element to the left (`mid-1`) is greater, the peak must lie in the left half, so `high` is updated to `mid-1`. If the element to the right (`mid+1`) is greater, the peak must lie in the right half, so `low` is updated to `mid+1`. If neither neighbor is greater, then the element at `mid` is a peak, and its index is returned. Edge cases such as an empty array or an array with a single element are handled explicitly. The time complexity is O(log N) due to the binary search, and the space complexity is O(1) as it uses a constant amount of extra space.

Pseudocode:

FUNCTION find_peak_element(nums):
  IF nums is NULL or empty THEN
    RETURN -1
  END IF

  low = 1
  high = length(nums)

  IF length(nums) == 1 THEN
    RETURN 1
  END IF

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

    IF mid > 1 AND nums[mid-1] > nums[mid] THEN
      high = mid - 1
    ELSIF mid < length(nums) AND nums[mid+1] > nums[mid] THEN
      low = mid + 1
    ELSE
      RETURN mid
    END IF
  END WHILE

  RETURN -1