# 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 converted to be finding if there is a sequence such that `the_smallest_so_far < the_second_smallest_so_far < current`. We use x, y and z to denote the 3 number respectively.

Java Solution

```public boolean increasingTriplet(int[] nums) { int x = Integer.MAX_VALUE; int y = Integer.MAX_VALUE;   for (int i = 0; i < nums.length; i++) { int z = nums[i];   if (x >= z) { x = z;// update x to be a smaller value } else if (y >= z) { y = z; // 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>
```
1. Ankit Shah on 2016-3-24

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

2. Ankit Shah on 2016-3-24

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

``` } } ```

3. Ashiq Imran on 2016-4-12

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

4. kash on 2016-5-14

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

5. Samir Vasani on 2016-8-1

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

6. ryanlr on 2016-8-1

They do not need to be consecutive.

7. ryanlr on 2016-8-1

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

8. ryanlr on 2016-8-1

It works for this array.

9. afj on 2016-8-12

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