- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find a triplet nums[i] < nums[k] < nums[j] from a list nums in C++
Suppose we have a list of numbers called nums, we have to check whether there are triplets (i, j, k) such that i < j < k and nums[i] < nums[k] < nums[j].
So, if the input is like nums = [2, 12, 1, 4, 4], then the output will be True, as [2, 12, 4] matches the criteria because 2 < 4 < 12.
To solve this, we will follow these steps −
n := size of nums
Define an array left of size n
left[0] := nums[0]
for initialize i := 1, when i < n, update (increase i by 1), do −
left[i] := minimum of nums[i] and left[i - 1]
Define one stack st
for initialize i := n - 1, when i >= 1, update (decrease i by 1), do −
x := left[i - 1]
while st is not empty and top of st <= x, do −
pop from st
if st is not empty and x < nums[i] and nums[i] > top of st, then −
return true
push nums[i] into st
return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: bool solve(vector<int>& nums) { int n = nums.size(); vector<int> left(n); left[0] = nums[0]; for (int i = 1; i < n; i++) { left[i] = min(nums[i], left[i - 1]); } stack<int> st; for (int i = n - 1; i >= 1; i--) { int x = left[i - 1]; while (!st.empty() && st.top() <= x) st.pop(); if (!st.empty() && x < nums[i] && nums[i] > st.top()) return true; st.push(nums[i]); } return false; } }; bool solve(vector<int>& nums) { return (new Solution())->solve(nums); } int main(){ vector<int> v = {2, 12, 1, 4, 4}; cout << solve(v); }
Input
{2, 12, 1, 4, 4}
Output
1
- Related Articles
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
- Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ≥ 1}
- Python program to find list of triplets for which i+j+k is not same as n
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Construct a Turing machine for L = {aibjck | i>j>k; k ≥ 1}
- Construct a Turing machine for L = {aibjck | i< j< k; i ≥ 1}
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Program to rotate a linked list by k places in C++
- Program to find number of subsequences with i, j and k number of x, y, z letters in Python
