Given an integer array nums
, return true
if there exists a triple of indices (i, j, k)
such that i < j < k
and nums[i] < nums[j] < nums[k]
. If no such indices exists, return false
.
LeetCode Problem - 334: Link | Click Here
class Solution {
public boolean increasingTriplet(int[] nums) {
// Initialize two variables to store the first and second elements of the triplet
int firstElement = Integer.MAX_VALUE;
int midElement = Integer.MAX_VALUE;
// Iterate through the array
for (int num : nums) {
// Check if the current number is less than or equal to the first element
if (num <= firstElement) {
// Update the first element to the current number
firstElement = num;
}
// Check if the current number is less than or equal to the second element
else if (num <= midElement) {
// Update the second element to the current number
midElement = num;
}
// If neither condition is met, it means we have found an increasing triplet
else {
// Return true
return true;
}
}
// If no increasing triplet is found, return false
return false;
}
}