# 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; }```

### 15 thoughts on “LeetCode – Increasing Triplet Subsequence (Java)”

1. Yeah . . . this is a subtle and annoying problem.

The key concept seems to be that it’s not really the value of “small” that matters, but the value of “big”. “big” is always greater than “small”, so once you have values for both and find any value greater than “big”, you know you have an increasing sequence. As you point out, the two variables do not necessarily hold the sequence.

Even with that explanation to myself I still find it annoying, and I feel like I could find a counterexample. But I haven’t.

What’s also frustrating is that the algorithm does not always find the subsequence you may think it should, e.g.

52
4
53
5
56

finds (or seems to find) 4,5,56, not 52,53,56. But, again, the question is whether or not there IS such a subsequence, not what the first one is.

2. wrong my ass you stupid cunt

3. x y order doesn’t matter you idiot, try submit in leetcode to verify the answer first before spraying your ignorance here.

4. 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.

5. 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!!!

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

7. It works for this array.

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

9. They do not need to be consecutive.

10. 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");
}
}

11. 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; } ```

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

13. ``` public class TrippletSubsequenceIncreasing {```

``` private static boolean isIncreasingTrippletSubsequence(int[] a) { int count = 1; for (int i = 1; i a[i - 1]) { count++; } else { count = 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 })); ```

``` } } ```

14. ``` 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 })); } } ```