Suppose there is an unsorted array. We have to check whether an increasing subsequence of length 3 exists or not in that 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.
To solve this, we will follow these steps −
small := infinity, big := infinity
for each element i in array
if i <= small, then small := i, otherwise when i <= big, then big := i, otherwise return true
Let us see the following implementation to get better understanding −
class Solution(object): def increasingTriplet(self, nums): """ :type nums: List[int] :rtype: bool """ small,big = 100000000000000000000,100000000000000000000 for i in nums: if i <= small: small = i elif i<=big: big = i else : return True return False