1. Pick 2 elements(a, b), compare them. (say a > b)
2. Update min by comparing (min, b)
3. Update max by comparing (max, a)
This way you would do 3 comparisons for 2 elements, amounting to 3N/2
total comparisons for N
elements.
More Related Contents:
- Print two-dimensional array in spiral order
- In-place array reordering?
- How to find repeating sequence of characters in a given array?
- maximize the top element of the stack after performing exactly K operations
- Algorithm for all subsets of Array
- How to sort in-place using the merge sort algorithm?
- Searching array reports “not found” even though it’s found
- Finding three elements in an array whose sum is closest to a given number
- How to find the kth smallest element in the union of two sorted arrays?
- Algorithm to find k smallest numbers in array of n items
- Can min/max of moving window achieve in O(N)?
- Weighted random selection from array
- Find the Smallest Integer Not in a List
- How to find a duplicate element in an array of shuffled consecutive integers?
- Binary Search in Array
- Find the majority element in array
- Given an array, find out the next smaller element for each element
- Finding neighbours in a two-dimensional array
- Binary Search algorithm implementations
- Algorithm to determine if array contains n…n+m?
- Find the 2nd largest element in an array with minimum number of comparisons
- Algorithm to rotate an array in linear time
- Find all subsets of length k in an array
- Median of 5 sorted arrays
- find pair of numbers in array that add to given sum
- Regarding in-place merge in an array
- Interview question: three arrays and O(N*N)
- Maximum sum of non consecutive elements
- calculating the number of “inversions” in a permutation
- Find the first element in a sorted array that is greater than the target