VOOZH about

URL: https://www.geeksforgeeks.org/dsa/length-longest-strict-bitonic-subsequence/

⇱ Length of longest strict bitonic subsequence - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Length of longest strict bitonic subsequence

Last Updated : 13 Nov, 2025

Given an array arr[] of integers, find the length of the longest strict bitonic subsequence.

A subsequence is strict bitonic if it first strictly increases and then strictly decreases, such that the absolute difference between every pair of consecutive elements is exactly 1 both in the increasing and decreasing parts.

A fully increasing or fully decreasing subsequence (with consecutive differences of 1) is also considered bitonic.

Examples:

Input: arr[] = [4, 5, 6, 5, 4, 3]
Output: 6
Explanation: The longest strict bitonic subsequence is [4, 5, 6, 5, 4, 3]. Both the increasing [4, 5, 6] and decreasing [6, 5, 4, 3] parts have consecutive differences of 1.

Input: arr[] = [10, 9, 8, 7]
Output: 4
Explanation: The sequence is fully decreasing, which is allowed. Consecutive differences are 1: [10, 9, 8, 7].

[Naive Approach] Using Recursion - O(2n) Time and O(n) Space

The idea is to use recursion to generate all subsequences of the array. For each subsequence, check if it first strictly increases with consecutive differences of 1 and then strictly decreases with consecutive differences of 1. The length of the longest valid subsequence is the answer.


Output
6

[Better Approach] Using Two Arrays - O(n2) Time and O(n) Space

We can solve this efficiently using the idea of longest bitonic subsequence instead of checking all subsequences.

First, for each index i, compute inc[i] as the length of the longest increasing subsequence ending at i, where consecutive elements differ by exactly 1. Similarly, compute dec[i] as the length of the longest decreasing subsequence starting at i with the same consecutive difference constraint.

The length of a strict bitonic subsequence with peak at i is inc[i] + dec[i] - 1. The final answer is the maximum of these values over all indices.


Output
6

[Expected Approach] Using HashMap - O(n) Time and O(n) Space

 The idea is to use hashmaps to quickly find the longest increasing and decreasing subsequences that follow the consecutive difference rule (difference of 1).

  • Traverse the array from left to right to fill inc[i], where inc[i] = 1 + inc of the previous element arr[i] - 1 (if it exists).
  • Traverse from right to left to fill dec[i], where dec[i] = 1 + dec of the next element arr[i] - 1 (if it exists).
  • Use hashmaps to store the best length seen so far for each value, ensuring O(1) average lookup.
  • Finally, the longest strict bitonic subsequence passing through index i is inc[i] + dec[i] - 1.

The answer is the maximum of this value across all indices.


Output
6


Comment
Article Tags: