Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the 2 largest numbers in a list using a single pass?

Tags:

c#

algorithm

max

Is it possible to find the 2 largest numbers in an array, and only loop through the collection once?

I had this as an interview question and didn't really get it in time.

like image 985
codecompleting Avatar asked Oct 18 '11 14:10

codecompleting


People also ask

How do you find the second largest number in an array?

Take two variables and initiliaze them with zero. Iterate through each element of the array and compare each number against these two number. If current number is greater than maxOne then maxOne = number and maxTwo = maxOne. Otherwise if it only greater than maxTwo then we only update maxTwo with current number.

How do you find the second largest number without an array?

Logic To Find First and Second Biggest Number in N Numbers, without using Arrays. First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the while loop until limit value is 0.

How do you find the largest number in a list using for loops in Python?

The max() Function — Find the Largest Element of a List. In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.


2 Answers

It seems pretty straightforward..

int[] nums = { 3, 1, 4, 1, 5, 9, 2, 6 };

int max1 = -1;
int max2 = -1;
foreach (int num in nums)
{
  if (num > max1) { max2 = max1; max1 = num; }
  else if (num > max2) { max2 = num; }
}    

For example:

// 3: max2 = -1; max1 = 3;
// 1: max2 = 1;
// 4: max2 = 3; max1 = 4;

Quick explanation:

  • Define -1 as a placeholder, could use int.MinValue, or better yet a separate bool to indicate no-match
  • If the value tested is bigger than the current maximum (max1), assign current maximum to max2, new value to max1
  • Else if must be smaller, but if it's bigger than second-maximum, assign new value to max2
like image 147
Kieren Johnstone Avatar answered Oct 07 '22 21:10

Kieren Johnstone


In general, you can find the K largest (or smallest) numbers in an array using a single pass for any K. The total time complexity will be O(NK), where N is the size of the array:

Keep a sorted list of numbers that has at most K elements. Walk though the array and for each item:

  1. if there list is not yet full, insert the item
  2. otherwise, if the item is bigger than the smallest item in the list, insert this item and remove the smallest one.

In the end, the list will contain K largest items, which is what we wanted.

This solution is quite slow, though. Using a self-balancing binary search tree or a skip list, you could get to O(N log K). (Since it's impossible to sort faster than O(N log N) in general case, and this method can be used for sorting the whole array if we set K = N, this looks like the best we can get.)

In the case of K = 2, you don't need all this heavy machinery. Just two variables representing the two positions in the list are enough.

like image 27
svick Avatar answered Oct 07 '22 22:10

svick