sequence
algorithm
binary search
monotonic
data structures

Finding an number in montonically increasing and then decreasing sequencecera

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In this article, we explore the search problem within a specific type of sequence known as a bitonic sequence. A bitonic sequence is characterized by elements that are first sorted in increasing order, followed by elements in a decreasing order. We aim to efficiently find a target number in such a sequence, leveraging its distinct properties.

Understanding Bitonic Sequences

A bitonic sequence can be defined as a sequence of numbers that consists of two parts:

  • A monotonically increasing subsequence.
  • A monotonically decreasing subsequence.

For example, consider the sequence: [1, 3, 5, 7, 8, 6, 4, 2]. Here, the sequence increases to a maximum (8) and then decreases.

Problem Definition

Given a bitonic sequence, the task is to find whether a target number exists in the sequence, and if so, its index within the sequence. Given the nature of the sequence, we can utilize an algorithm that finds the solution efficiently, similar in complexity to binary search.

Strategy for Finding the Target

To find a target number in a bitonic sequence, we can use a modified binary search. The entire sequence can be divided into two parts:

  1. Locate the bitonic point (peak): The first step is to identify the index where the sequence changes from increasing to decreasing. This peak is also known as the bitonic point.
  2. Binary search in each segment:
    • Perform a binary search on the increasing sequence from the start to the peak.
    • If the target number is not found, perform a binary search on the decreasing sequence from the peak to the end.

The combined complexity of this approach is O(logn)O(\log n), analogous to traditional binary search.

Algorithm Implementation

Below is a pseudo-code representation of the algorithm to find a target number in a bitonic sequence:

  • Input: `arr = [1, 3, 8, 12, 9, 5, 2], target = 9`
  • Output: `Index = 4`
  • If the sequence length is less than 2, handle base cases directly since a bitonic sequence requires at least one element for direction change.
  • If all elements are equal, consider the search unsuccessful for any non-matching target.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.