Given an array of positive integers arr[], return the second largest element from the array. If the second largest element doesn't exist then return -1.
Note: The second largest element should not be equal to the largest element.
Examples:
Input: arr[] = [12, 35, 1, 10, 34, 1]
Output: 34
Explanation: The largest element of the array is 35 and the second largest element is 34.
Input: arr[] = [10, 5, 10]
Output: 5
Explanation: The largest element of the array is 10 and the second largest element is 5.
Input: arr[] = [10, 10, 10]
Output: -1
Explanation: The largest element of the array is 10 and the second largest element does not exist.
Constraints:
2 ≤ arr.size() ≤ 105
1 ≤ arr[i] ≤ 105
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve the problem of finding the second largest distinct element in an array of positive integers, we can follow an efficient approach that traverses the array only once. Here's a step-by-step explanation along with the Python implementation:
Approach:
Initialize Two Variables:
first: To keep track of the largest element in the array.
second: To keep track of the second largest distinct element.
Traverse the Array:
For each element in the array:
If the current element is greater than first:
Update second to be the previous first.
Update first to be the current element.
Else if the current element is different from first and greater than second:
Update second to be the current element.
Final Check:
After traversing the array, if second remains unchanged (i.e., no second distinct largest element was found), return -1.
Otherwise, return the value of second.
This approach ensures that we find the second largest distinct element in a single pass with a time complexity of O(n), which is efficient for large input sizes (up to 10^5 elements as per the constraints).
Python Implementation:
python
def second_largest(arr):
"""
Returns the second largest distinct element in the array.
If no such element exists, returns -1.
Parameters:
arr (list): A list of positive integers.
Returns:
int: The second largest distinct element or -1 if it doesn't exist.
"""
if len(arr) < 2:
# If the array has fewer than 2 elements, return -1
return -1
# Initialize first and second to the smallest possible integer
first = second = -1 # Since all elements are positive, -1 works
for num in arr:
if num > first:
second = first
first = num
elif first > num > second:
second = num
# After traversal, check if second was updated
return second if second != -1 else -1
# Example Usage:
if __name__ == "__main__":
# Test Case 1
arr1 = [12, 35, 1, 10, 34, 1]
print("Second Largest in {}: {}".format(arr1, second_largest(arr1))) # Output: 34
# Test Case 2
arr2 = [10, 5, 10]
print("Second Largest in {}: {}".format(arr2, second_largest(arr2))) # Output: 5
# Test Case 3
arr3 = [10, 10, 10]
print("Second Largest in {}: {}".format(arr3, second_largest(arr3))) # Output: -1
# Additional Test Case: Large Array
# arr4 = [1] * 100000 # Should return -1
# print("Second Largest in large array: {}".format(second_largest(arr4)))
Explanation:
Initialization:
We start by initializing two variables, first and second, to -1. Since all elements in the array are positive integers (as per the constraints), -1 serves as a sentinel value indicating that second hasn't been updated yet.
Traversing the Array:
For each number num in the array:
Update first and second:
If num is greater than first, it means we've found a new largest number. In this case, we update second to be the previous first, and then update first to be num.
Update second Only:
If num is not greater than first but is greater than second, and also distinct from first, we update second to be num.
Final Check:
After traversing the entire array, we check if second has been updated from its initial value. If second is still -1, it means there was no second distinct largest element in the array, and we return -1. Otherwise, we return the value of second.
Handling Edge Cases:
All Elements Are the Same:
If all elements in the array are identical, second will remain -1, and the function correctly returns -1.
Array Size Less Than 2:
If the array has fewer than 2 elements, it's impossible to have a second largest element, so the function returns -1.
Large Arrays:
The function efficiently handles large arrays (up to 10^5 elements) since it operates in linear time.
Output of Example Test Cases:
code
Second Largest in [12, 35, 1, 10, 34, 1]: 34
Second Largest in [10, 5, 10]: 5
Second Largest in [10, 10, 10]: -1
This implementation ensures that the problem is solved optimally while adhering to the given constraints.