-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-subsequence-3-linear-time.cpp
60 lines (45 loc) · 1.07 KB
/
sorted-subsequence-3-linear-time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 08 April 2017 -- RC
Find a sorted subsequence of size 3 in linear time*
Given an array of n integers, find the 3 elements such that a[i] < a[j] < a[k] and i < j < k in 0(n) time. If there are multiple such triplets, then print any one of them.
Examples:
Input: arr[] = {12, 11, 10, 5, 6, 2, 30}
Output: 5, 6, 30
Input: arr[] = {1, 2, 3, 4}
Output: 1, 2, 3 OR 1, 2, 4 OR 2, 3, 4
Input: arr[] = {4, 3, 2, 1}
Output: No such triplet
*/
#include <iostream>
#include <climits>
#include <cstdio>
using namespace std;
void print_subsequence(int arr[], int n) {
int i=0, j=INT_MIN, k=INT_MIN;
bool flag = false;
for (int var = 1; var < n; ++var) {
if(j == INT_MIN) {
if(arr[var] > arr[i])
j = var;
else
i = var;
}
else if(j!=INT_MIN && k == INT_MIN) {
if(arr[var] > arr[j]) {
k = var;
flag = true;
break;
}
else
continue;
}
}
if(flag)
printf("%d %d %d\n", arr[i], arr[j], arr[k]);
else
cout<<"No triplet"<<endl;
}
int main() {
int arr[] = {12, 11, 10, 5, 6, 2, 30};
print_subsequence(arr, sizeof(arr)/sizeof(int));
return 0;
}