/
sorted_subsequence_of_size_3_without_space.c
73 lines (63 loc) · 1.55 KB
/
sorted_subsequence_of_size_3_without_space.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Date: 2018-09-28
*
* Description:
* Given an array of n integers, find the 3 elements such that
* a[i] < a[j] < a[k] and i < j < k. If there are multiple such triplets, then
* print any one of them.
*
* Approach:
* In first loop find a smaller(min) and second smaller(mid) number, If a more
* than both appears, break the loop. Now we have mid and max element we need
* to find a min element which on left side of mid and max. Again run a loop
* till i to find element smaller than larger.
*
* Complexity:
* O(N) Time
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0, j = 0;
int n = 0;
int *A = NULL;
int small = 1 << 30;
int large = 1 << 30;
printf("Enter number of elements: ");
scanf("%d", &n);
A = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &A[i]);
}
for (i = 0; i < n; i++) {
if (small >= A[i]) // Track smallest element
small = A[i];
else if (large >= A[i]) // Track second largest element
large = A[i];
else
break;
}
if (i == n) return -1;
for (j = 0; j <= i; j++) {
// Find smaller element which appears on left of larger and A[i]
if (large >= A[j]) {
small = A[j];
break;
}
}
printf("3 sorted values are: %d %d %d\n", small, large, A[i]);
return 0;
}
/*
* Output:
* ----------------
* Enter number of elements: 5
* Enter element[0]: 4
* Enter element[1]: 7
* Enter element[2]: 2
* Enter element[3]: 9
* Enter element[4]: 1
*
* 3 sorted elements are: 4 7 9
*/