> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/334-increasing-triplet-subsequence.md).

# 334 Increasing Triplet Subsequence

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.

Formally the function should:\
Return true if there exists i, j, k\
such that arr\[i] < arr\[j] < arr\[k] given 0 ≤ i < j < k ≤ n-1 else return false.\
Your algorithm should run in O(n) time complexity and O(1) space complexity.

Examples:

```
Given [1, 2, 3, 4, 5],
return true.

Given [5, 4, 3, 2, 1],
return false.
```

**The Idea:** One way to approach this to two keep two placeholders. One will take the place of the first element that has an increasing sequence, two will take the place of element that follows if it is greater than than the first. If nothing else applies then we can return true, since the third element is greater than both *and* both elements two and three follow to be after one. Otherwise, the system will reset itself as smaller element arrive. We need `<=` for the uniform case; for example `[1,1,1,1...1]`.

**Complexity:** O(n) time and O(1) space

```cpp
bool increasingTriplet(vector<int>& nums) {
    int one = INT_MAX, two = INT_MAX;

    for (int i : nums) {            // sequential search ensures that index(one) < index(two) ...
        if (i <= one) one = i;      // the smallest since first if statement
        else if (i <= two) two = i; // second smallest
        else return true;           // finish
    }

    return false;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/334-increasing-triplet-subsequence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
