C Program to Find Second Largest and Second Smallest Element in an Array
Finding the second largest and second smallest elements in an array can be done in a single pass through the data — O(n) time, O(1) space — without sorting. The key is maintaining four running extremes: largest, second_largest, smallest, second_smallest. The original program sorted the array first (O(n²) with bubble sort), then accessed fixed indices …