|
| 1 | +--- |
| 2 | +Difficulty: Easy |
| 3 | +Source: 160 Days of Problem Solving |
| 4 | +Tags: |
| 5 | + - Arrays |
| 6 | + - Searching |
| 7 | +--- |
| 8 | + |
| 9 | +# _Day 1. Second Largest_ |
| 10 | + |
| 11 | +The problem can be found at the following link: [Problem Link](https://www.geeksforgeeks.org/problems/second-largest3735/1) |
| 12 | + |
| 13 | +## 💡 **Problem Description:** |
| 14 | + |
| 15 | +Given an array of positive integers `arr[]`, return the second largest element from the array. If the second largest element doesn't exist, return `-1`. |
| 16 | + |
| 17 | +**Note:** The second largest element should not be equal to the largest element. |
| 18 | + |
| 19 | +## 🔍 **Example Walkthrough:** |
| 20 | + |
| 21 | +**Input:** |
| 22 | + |
| 23 | +``` |
| 24 | +arr[] = [12, 35, 1, 10, 34, 1] |
| 25 | +``` |
| 26 | + |
| 27 | +**Output:** |
| 28 | + |
| 29 | +``` |
| 30 | +34 |
| 31 | +``` |
| 32 | + |
| 33 | +**Explanation:** |
| 34 | +The largest element of the array is 35, and the second largest element is 34. |
| 35 | + |
| 36 | +**Input:** |
| 37 | + |
| 38 | +``` |
| 39 | +arr[] = [10, 5, 10] |
| 40 | +``` |
| 41 | + |
| 42 | +**Output:** |
| 43 | + |
| 44 | +``` |
| 45 | +5 |
| 46 | +``` |
| 47 | + |
| 48 | +**Explanation:** |
| 49 | +The largest element of the array is 10, and the second largest element is 5. |
| 50 | + |
| 51 | +**Input:** |
| 52 | + |
| 53 | +``` |
| 54 | +arr[] = [10, 10, 10] |
| 55 | +``` |
| 56 | + |
| 57 | +**Output:** |
| 58 | + |
| 59 | +``` |
| 60 | +-1 |
| 61 | +``` |
| 62 | + |
| 63 | +**Explanation:** |
| 64 | +The largest element of the array is 10, and there is no distinct second largest element. |
| 65 | + |
| 66 | +### Constraints: |
| 67 | + |
| 68 | +- $\(2 \leq \text{arr.size()} \leq 10^5\)$ |
| 69 | +- $\(1 \leq \text{arr[i]} \leq 10^5\)$ |
| 70 | + |
| 71 | +## 🎯 **My Approach:** |
| 72 | + |
| 73 | +1. **Tracking Largest and Second Largest:** |
| 74 | + |
| 75 | + - Initialize two variables, `first` and `second`, to the smallest possible values. |
| 76 | + - Traverse through the array: |
| 77 | + - If the current element is greater than `first`, update `second` to `first`, and then set `first` to the current element. |
| 78 | + - If the current element is less than `first` but greater than `second`, update `second`. |
| 79 | + - If no valid second largest element is found, return `-1`. |
| 80 | + |
| 81 | +2. **Edge Cases:** |
| 82 | + - If all elements are the same, return `-1`. |
| 83 | + - If the array has only two distinct elements, return the smaller of the two if it’s not equal to the largest. |
| 84 | + |
| 85 | +## 🕒 **Time and Auxiliary Space Complexity**📝 |
| 86 | + |
| 87 | +- **Expected Time Complexity:** O(n), where `n` is the number of elements in the array. The algorithm makes a single pass through the array to determine the largest and second largest elements. |
| 88 | +- **Expected Auxiliary Space Complexity:** O(1), as we only use a constant amount of additional space to store `first` and `second`. |
| 89 | + |
| 90 | +## 📝 **Solution Code** |
| 91 | + |
| 92 | +## Code (C++) |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + int getSecondLargest(const std::vector<int>& arr) { |
| 98 | + int first = INT_MIN, second = INT_MIN; |
| 99 | + |
| 100 | + for (int num : arr) { |
| 101 | + if (num > first) { |
| 102 | + second = first; |
| 103 | + first = num; |
| 104 | + } else if (num > second && num < first) { |
| 105 | + second = num; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return second == INT_MIN ? -1 : second; |
| 110 | + } |
| 111 | +}; |
| 112 | +``` |
| 113 | + |
| 114 | +<details> |
| 115 | + <summary><h2 align='center'>👨💻 Alternative Approaches</h2></summary> |
| 116 | + |
| 117 | +### Alternative Approach (Using Sorting) |
| 118 | + |
| 119 | +```cpp |
| 120 | +class Solution { |
| 121 | +public: |
| 122 | + int getSecondLargest(std::vector<int>& arr) { |
| 123 | + std::sort(arr.rbegin(), arr.rend()); |
| 124 | + int first = arr[0]; |
| 125 | + for (int num : arr) { |
| 126 | + if (num < first) return num; |
| 127 | + } |
| 128 | + return -1; |
| 129 | + } |
| 130 | +}; |
| 131 | +``` |
| 132 | +
|
| 133 | +</details> |
| 134 | +
|
| 135 | +## Code (Java) |
| 136 | +
|
| 137 | +```java |
| 138 | +class Solution { |
| 139 | + public int getSecondLargest(int[] arr) { |
| 140 | + int first = Integer.MIN_VALUE; |
| 141 | + int second = Integer.MIN_VALUE; |
| 142 | +
|
| 143 | + for (int num : arr) { |
| 144 | + if (num > first) { |
| 145 | + second = first; |
| 146 | + first = num; |
| 147 | + } else if (num > second && num < first) { |
| 148 | + second = num; |
| 149 | + } |
| 150 | + } |
| 151 | +
|
| 152 | + return second == Integer.MIN_VALUE ? -1 : second; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Code (Python) |
| 158 | + |
| 159 | +```python |
| 160 | +class Solution: |
| 161 | + def getSecondLargest(self, arr): |
| 162 | + first = float('-inf') |
| 163 | + second = float('-inf') |
| 164 | + |
| 165 | + for num in arr: |
| 166 | + if num > first: |
| 167 | + second = first |
| 168 | + first = num |
| 169 | + elif num > second and num < first: |
| 170 | + second = num |
| 171 | + |
| 172 | + return -1 if second == float('-inf') else second |
| 173 | +``` |
| 174 | + |
| 175 | +# 🎯 **Contribution and Support:** |
| 176 | + |
| 177 | +For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: [Any Questions](https://www.linkedin.com/in/het-patel-8b110525a/). Let’s make this learning journey more collaborative! |
| 178 | + |
| 179 | +⭐ If you find this helpful, please give this repository a star! ⭐ |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +<div align="center"> |
| 184 | + <h3><b>📍Visitor Count</b></h3> |
| 185 | +</div> |
| 186 | + |
| 187 | +<p align="center"> |
| 188 | + <img src="https://profile-counter.glitch.me/Hunterdii/count.svg" /> |
| 189 | +</p> |
0 commit comments