I\'m just practicing some MIT java assignments. But, I\'m not sure how to find the second largest number. http://ocw.csail.mit.edu/f/13
public class Marath
Generally speaking:
Have two values -- "largest" and "notQuite".
Initialize both to -9999 or whatever.
Scan through your list. If the number is larger than "largest", set "largest" to that number. But before you do that, copy the old "largest" value to "notQuite".
If, on the other hand, the number is smaller than "largest" but is larger than "notQuite", set "notQuite" to that number.
When you're done examining all the numbers, "notQuite" contains the second-largest.
And note that, as you fill in the above numbers, you can also keep a "largestIndex" and "notQuiteIndex" and fill those in with the corresponding array index values, so you can identify the "winning" value. Unfortunately, though, if there are multiple identical "largest" or "secondLargest" values the simple index scheme doesn't work and you need to keep a list of some sort.
public void findMax(int a[]) {
int large = Integer.MIN_VALUE;
int secondLarge = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
if (large < a[i]) {
secondLarge = large;
large = a[i];
} else if (a[i] > secondLarge) {
if (a[i] != large) {
secondLarge = a[i];
}
}
}
System.out.println("Large number " + large + " Second Large number " + secondLarge);
}
The above code has been tested with integer arrays having duplicate entries, negative values. Largest number and second largest number are retrieved in one pass. This code only fails if array only contains multiple copy of same number like {8,8,8,8} or having only one number.
int largest=time[0];
int secondLargest=largest;
for(int i=0;i<time.length;i++){
if(time[i]>largest){
secondLargest=largest;
largest=time[i];
}
else if(secondLargest<time[i] && time[i]<largest || secondLargest>=largest)
secondLargest=time[i];
}
return secondLargest;
Find the second largest element in the given array:
public static void findSecondMax(){
int[] arr = {3, 2, 20, 4, 1, 9, 6, 3, 8};
int max = 0;
int secondMax = 0;
for(int i =0; i< arr.length; i++) {
if(max < arr[i]) max = arr[i];
if((max > arr[i]) && (secondMax < arr[i])) secondMax = arr[i];
}
System.out.println(secondMax);
}
Instead of resorting to sorting the array, you can simply do the following:
largestValue
and a secondLargestValue
largestValue
:
largestValue
to secondLargestValue
, then assign the current element to largestValue
(think of it as shifting everything down by 1)secondLargestValue
secondLargestValue
O(n) run time
O(1) space requirement
Sorting the array simply to find an order statistics is too wasteful. You can find the second largest element by following an algorithm that resembles the one that you already have, with an additional variable representing the second largest number.
Currently, the next element could be larger than the max or equal to/smaller than the max, hence a single if
is sufficient:
if (times[i] > maxValue) {
maxValue = times[i];
}
With two variables to consider, the next element could be
A special care must be taken about the initial state. Look at the first two items, and assign the larger one to the max
and the smaller to the second largest; start looping at the element number three, if there is one.
Here is how you can code it:
if (times[i] > maxValue) {
secondLargest = maxValue;
maxValue = times[i];
} else if (times[i] > secondLargest) {
secondLargest = times[i];
}