|
| 1 | +--- |
| 2 | +title: Second Smallest and Second Largest Element in an Array |
| 3 | +--- |
| 4 | +## Problem Statement |
| 5 | + |
| 6 | +I am given an array of integers of size `n`. |
| 7 | +My task is to find: |
| 8 | + |
| 9 | +- the **second smallest** element |
| 10 | +- the **second largest** element |
| 11 | + |
| 12 | +If either does not exist, I should return `-1` for that value. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +### `Example 1` |
| 19 | + |
| 20 | +Input: [1, 2, 4, 7, 7, 5] |
| 21 | +Output: Second Smallest = 2, Second Largest = 5 |
| 22 | + |
| 23 | +`### Example 2` |
| 24 | + |
| 25 | +Input: [1] |
| 26 | +Output: -1 -1 |
| 27 | + |
| 28 | +`### Example 3` |
| 29 | + |
| 30 | +Input: [5, 5, 5] |
| 31 | +Output: -1 -1 |
| 32 | + |
| 33 | + |
| 34 | +Complexity Analysis |
| 35 | + |
| 36 | + Time Complexity: O(n) |
| 37 | + |
| 38 | + Space Complexity: O(1) |
| 39 | + |
| 40 | +This is optimal since I must inspect every element at least once. |
| 41 | +Edge Cases to Remember |
| 42 | + |
| 43 | + Array size < 2 → no second elements |
| 44 | + |
| 45 | + All elements equal → no second smallest or largest |
| 46 | + |
| 47 | + Duplicate largest/smallest values should be ignored |
| 48 | + |
| 49 | +--- |
| 50 | +## Intuition |
| 51 | +To find the second smallest and second largest elements, I need **distinct values**. |
| 52 | +A naive idea would be: |
| 53 | +- sort the array |
| 54 | +- take the second element from start and end |
| 55 | + |
| 56 | +However, sorting costs **O(n log n)** time, which is unnecessary. |
| 57 | + |
| 58 | +I can do this more efficiently using a **single traversal**. |
| 59 | + |
| 60 | +--- |
| 61 | +## Key Observations |
| 62 | +- The **smallest** and **largest** elements are easy to track. |
| 63 | +- The **second smallest** must be: |
| 64 | + - greater than the smallest |
| 65 | + - smaller than all other candidates |
| 66 | +- The **second largest** must be: |
| 67 | + - smaller than the largest |
| 68 | + - greater than all other candidates |
| 69 | +--- |
| 70 | +## Optimal Approach (Single Pass) |
| 71 | + |
| 72 | +### Strategy |
| 73 | + |
| 74 | +I maintain four variables: |
| 75 | +- `smallest` |
| 76 | +- `secondSmallest` |
| 77 | +- `largest` |
| 78 | +- `secondLargest` |
| 79 | + |
| 80 | +I iterate through the array once and update these values carefully. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## Algorithm |
| 85 | + |
| 86 | +1. Initialize: |
| 87 | + - `smallest = +∞` |
| 88 | + - `secondSmallest = +∞` |
| 89 | + - `largest = -∞` |
| 90 | + - `secondLargest = -∞` |
| 91 | + |
| 92 | +2. For each element `x` in the array: |
| 93 | + - If `x < smallest`: |
| 94 | + - `secondSmallest = smallest` |
| 95 | + - `smallest = x` |
| 96 | + - Else if `x > smallest` and `x < secondSmallest`: |
| 97 | + - `secondSmallest = x` |
| 98 | + |
| 99 | + - If `x > largest`: |
| 100 | + - `secondLargest = largest` |
| 101 | + - `largest = x` |
| 102 | + - Else if `x < largest` and `x > secondLargest`: |
| 103 | + - `secondLargest = x` |
| 104 | + |
| 105 | +1. If `secondSmallest` or `secondLargest` was never updated, return `-1`. |
| 106 | + |
| 107 | +--- |
| 108 | +## Code (Optimal – O(n)) |
| 109 | + |
| 110 | +```cpp |
| 111 | +#include <climits> |
| 112 | +#include <vector> |
| 113 | +using namespace std; |
| 114 | + |
| 115 | +pair<int, int> secondSmallestAndLargest(vector<int> &arr) { |
| 116 | + int n = arr.size(); |
| 117 | + if (n < 2) return {-1, -1}; |
| 118 | + |
| 119 | + int smallest = INT_MAX, secondSmallest = INT_MAX; |
| 120 | + int largest = INT_MIN, secondLargest = INT_MIN; |
| 121 | + |
| 122 | + for (int x : arr) { |
| 123 | + // For smallest |
| 124 | + if (x < smallest) { |
| 125 | + secondSmallest = smallest; |
| 126 | + smallest = x; |
| 127 | + } |
| 128 | + else if (x > smallest && x < secondSmallest) { |
| 129 | + secondSmallest = x; |
| 130 | + } |
| 131 | + |
| 132 | + // For largest |
| 133 | + if (x > largest) { |
| 134 | + secondLargest = largest; |
| 135 | + largest = x; |
| 136 | + } |
| 137 | + else if (x < largest && x > secondLargest) { |
| 138 | + secondLargest = x; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (secondSmallest == INT_MAX || secondLargest == INT_MIN) |
| 143 | + return {-1, -1}; |
| 144 | + |
| 145 | + return {secondSmallest, secondLargest}; |
| 146 | +} |
| 147 | +``` |
| 148 | +--- |
| 149 | +
|
| 150 | +## Complexity Analysis |
| 151 | +
|
| 152 | +- **Time Complexity:** O(n) |
| 153 | + |
| 154 | +- **Space Complexity:** O(1) |
| 155 | + |
| 156 | +
|
| 157 | +This is optimal since I must inspect every element at least once. |
| 158 | +
|
| 159 | +--- |
| 160 | +
|
| 161 | +## Edge Cases to Remember |
| 162 | +
|
| 163 | +- Array size < 2 → no second elements |
| 164 | + |
| 165 | +- All elements equal → no second smallest or largest |
| 166 | + |
| 167 | +- Duplicate largest/smallest values should be ignored |
| 168 | + |
| 169 | +
|
| 170 | +--- |
0 commit comments