|
| 1 | +--- |
| 2 | +id: second-largest-element |
| 3 | +title: Second Largest Element |
| 4 | +sidebar_label: Second Largest Element |
| 5 | +tags: |
| 6 | + - Easy |
| 7 | + - Array |
| 8 | + - DSA |
| 9 | +description: "This tutorial covers the solution to the Second Largest Distinct Element problem from the GeeksforGeeks." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given an array `arr` of size `n`, print the second largest distinct element from the array. If the second largest element doesn't exist, return `-1`. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | +``` |
| 20 | +Input: arr = [10, 5, 10] |
| 21 | +Output: 5 |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | +``` |
| 26 | +Input: arr = [10, 10, 10] |
| 27 | +Output: -1 |
| 28 | +``` |
| 29 | + |
| 30 | +## Your Task |
| 31 | + |
| 32 | +You don't need to read input or print anything. Your task is to complete the function `print2largest()` which takes the array `arr` and its size `n` as input parameters and returns the second largest distinct element from the array. If no such element exists, return `-1`. |
| 33 | + |
| 34 | +Expected Time Complexity: $O(N)$, where N is the number of elements in the array. |
| 35 | + |
| 36 | +Expected Auxiliary Space: $O(1)$ |
| 37 | + |
| 38 | +## Constraints |
| 39 | + |
| 40 | +* `1 ≤ n ≤ 10^5` |
| 41 | +* `1 ≤ arr[i] ≤ 10^5` |
| 42 | + |
| 43 | +## Problem Explanation |
| 44 | + |
| 45 | +To solve this problem, you need to find the second largest distinct element in the array. This can be achieved by keeping track of the largest and the second largest distinct elements while iterating through the array. |
| 46 | + |
| 47 | +## Code Implementation |
| 48 | + |
| 49 | +<Tabs> |
| 50 | + <TabItem value="Python" label="Python" default> |
| 51 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 52 | + |
| 53 | + ```python |
| 54 | + class Solution: |
| 55 | + def print2largest(self, arr, n): |
| 56 | + if n < 2: |
| 57 | + return -1 |
| 58 | + |
| 59 | + first = second = -1 |
| 60 | + for num in arr: |
| 61 | + if num > first: |
| 62 | + second = first |
| 63 | + first = num |
| 64 | + elif num > second and num != first: |
| 65 | + second = num |
| 66 | + |
| 67 | + return second |
| 68 | + |
| 69 | + # Example usage |
| 70 | + if __name__ == "__main__": |
| 71 | + solution = Solution() |
| 72 | + arr = [10, 5, 10] |
| 73 | + print(solution.print2largest(arr, len(arr))) # Expected output: 5 |
| 74 | + ``` |
| 75 | + |
| 76 | + </TabItem> |
| 77 | + <TabItem value="C++" label="C++"> |
| 78 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 79 | + |
| 80 | + ```cpp |
| 81 | + //{ Driver Code Starts |
| 82 | + #include <bits/stdc++.h> |
| 83 | + using namespace std; |
| 84 | + |
| 85 | + // } Driver Code Ends |
| 86 | + class Solution { |
| 87 | + public: |
| 88 | + // Function returns the second largest element |
| 89 | + int print2largest(int arr[], int n) { |
| 90 | + int first = -1, second = -1; |
| 91 | + for (int i = 0; i < n; i++) { |
| 92 | + if (arr[i] > first) { |
| 93 | + second = first; |
| 94 | + first = arr[i]; |
| 95 | + } else if (arr[i] > second && arr[i] != first) { |
| 96 | + second = arr[i]; |
| 97 | + } |
| 98 | + } |
| 99 | + return second; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + //{ Driver Code Starts. |
| 104 | + |
| 105 | + int main() { |
| 106 | + int t; |
| 107 | + cin >> t; |
| 108 | + while (t--) { |
| 109 | + int n; |
| 110 | + cin >> n; |
| 111 | + int arr[n]; |
| 112 | + for (int i = 0; i < n; i++) { |
| 113 | + cin >> arr[i]; |
| 114 | + } |
| 115 | + Solution ob; |
| 116 | + auto ans = ob.print2largest(arr, n); |
| 117 | + cout << ans << "\n"; |
| 118 | + } |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + // } Driver Code Ends |
| 123 | + ``` |
| 124 | +
|
| 125 | + </TabItem> |
| 126 | +
|
| 127 | + <TabItem value="Javascript" label="Javascript" default> |
| 128 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 129 | +
|
| 130 | + ```javascript |
| 131 | + class Solution { |
| 132 | + print2largest(arr, n) { |
| 133 | + if (n < 2) return -1; |
| 134 | + let first = second = -1; |
| 135 | + for (let num of arr) { |
| 136 | + if (num > first) { |
| 137 | + second = first; |
| 138 | + first = num; |
| 139 | + } else if (num > second && num != first) { |
| 140 | + second = num; |
| 141 | + } |
| 142 | + } |
| 143 | + return second; |
| 144 | + } |
| 145 | +} |
| 146 | +
|
| 147 | + ``` |
| 148 | + |
| 149 | + </TabItem> |
| 150 | + |
| 151 | + <TabItem value="Typescript" label="Typescript" default> |
| 152 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 153 | + |
| 154 | + ```typescript |
| 155 | + class Solution { |
| 156 | + print2largest(arr: number[], n: number): number { |
| 157 | + if (n < 2) return -1; |
| 158 | + let first: number = second: number = -1; |
| 159 | + for (let num of arr) { |
| 160 | + if (num > first) { |
| 161 | + second = first; |
| 162 | + first = num; |
| 163 | + } else if (num > second && num != first) { |
| 164 | + second = num; |
| 165 | + } |
| 166 | + } |
| 167 | + return second; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | + ``` |
| 172 | + |
| 173 | + </TabItem> |
| 174 | + |
| 175 | + <TabItem value="Java" label="Java" default> |
| 176 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 177 | + |
| 178 | + ```java |
| 179 | + public class Solution { |
| 180 | + public int print2largest(int[] arr, int n) { |
| 181 | + if (n < 2) return -1; |
| 182 | + int first = Integer.MIN_VALUE; |
| 183 | + int second = Integer.MIN_VALUE; |
| 184 | + for (int num : arr) { |
| 185 | + if (num > first) { |
| 186 | + second = first; |
| 187 | + first = num; |
| 188 | + } else if (num > second && num != first) { |
| 189 | + second = num; |
| 190 | + } |
| 191 | + } |
| 192 | + return second; |
| 193 | + } |
| 194 | +} |
| 195 | +public class Main { |
| 196 | + public static void main(String[] args) { |
| 197 | + Solution solution = new Solution(); |
| 198 | + int[] arr = {10, 5, 10}; |
| 199 | + System.out.println(solution.print2largest(arr, arr.length)); // Expected output: 5 |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | + ``` |
| 204 | + |
| 205 | + </TabItem> |
| 206 | +</Tabs> |
| 207 | + |
| 208 | + |
| 209 | +## Solution Logic: |
| 210 | + |
| 211 | +1. Iterate through the array and keep track of the largest and the second largest distinct elements. |
| 212 | +2. Return the second largest distinct element or `-1` if it doesn't exist. |
| 213 | + |
| 214 | +## Time Complexity |
| 215 | + |
| 216 | +* The function visits each element once, so the time complexity is $O(n)$, where n is the length of the array. This is because we are iterating through the array once. |
| 217 | + |
| 218 | +* The space complexity of the solution is O(1), which means the space required does not change with the size of the input array. This is because we are using a fixed amount of space to store the variables first, second, and num. |
0 commit comments