LeetCode – Increasing Triplet Subsequence (Java)

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

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

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

Analysis

This problem can be formalized as finding a sequence x, y and z, such that x < y < z .

Java Solution

public boolean increasingTriplet(int[] nums) {
    int small = Integer.MAX_VALUE;
    int big = Integer.MAX_VALUE;
 
    for (int num: nums) {
        if (num <= small) {
            small = num;// update x to be a smaller value
        } else if (num <= big) {
            big = num; // update y to be a smaller value
        } else {
            return true;
        }
    }
 
    return false;
}
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Bonsai

    The solution still works for this input. The increasing subsequence is 2,3 and 4. The solution will lead small=1, big=3, which is not the subsequence. However, note that the problem does not ask for the real sequence, but check its existence only.

  • Bonsai

    The cached values are not necessarily the increasing subsequence. The problem does not ask for the sequence, but check if the subsequence exists. It’s similar to https://www.programcreek.com/2014/04/leetcode-longest-increasing-subsequence-java/.

  • alexwest11

    Wrong!!!!!!!!!

    such sloppy solution!

    don’t work in case of x-smallest after y-smaller in array

    after loop, need so check if x after y, then find from 0..upto y , any number smaller than y!!!

    see geeks for reference!!!

  • afj

    test case: [2,3,1,4] ?

  • ryanlr

    It works for this array.

  • ryanlr

    This is not correct. For example, given the array {5,1,5,5,2,5,4}, it should return true. You code return false.

  • ryanlr

    They do not need to be consecutive.

  • Samir Vasani

    public class IncreaseSubSequence {

    public static void main(String[] a){
    int[] num={7,8,1,4,5};
    int count=1;
    for(int i=0;i<num.length;i++){
    int j=i+1;
    if(j<=2 && i<2){
    if(num[i] < num[j]){
    count++;
    if(count == 3){
    break;
    }
    }else{
    count=0;
    break;
    }
    }
    }

    if(count == 3){
    System.out.println("true");
    }else{
    System.out.println("false");
    }
    }

  • kash

    C++ solution

    bool increasing_triplet(int arr[], int n) {
    for (int i = 2; i arr[i-1] && arr[i-1] > arr[i-2])
    return true;
    }
    return false;
    }

  • Ashiq Imran

    You program will not work for [1,7,6,4,5] input?

  • Ankit Shah
  • Ankit Shah

    public class TrippletSubsequenceIncreasing {

    private static boolean isIncreasingTrippletSubsequence(int[] a) {
    int current = 0;
    int count = 1;
    for (int i = 1; i < a.length; i++) {
    if (a[i] == a[current] + 1) {
    count++;
    } else {
    count = 1;
    }
    current = current + 1;

    if (count == 3) {
    return true;
    }
    }

    return false;
    }

    public static void main(String[] args) {

    System.out.println(isIncreasingTrippletSubsequence(new int[] { 1, 2, 3, 4, 5 }));
    System.out.println(isIncreasingTrippletSubsequence(new int[] { 5, 4, 3, 2, 1 }));
    System.out.println(isIncreasingTrippletSubsequence(new int[] { 5, 6, 7, 8, 10 }));
    System.out.println(isIncreasingTrippletSubsequence(new int[] { 3, 4, 6, 7, 9 }));

    }

    }