Google Interview - Second Largest Number - 我的博客 - ITeye技术网站
Find the second largest number in a given array. Return 0 if the given array has no second largest number.
http://users.csc.calpoly.edu/~dekhtyar/349-Spring2010/lectures/lec03.349.pdf
Conclusion 1. We can find the second largest element as follows:
1. Find the largest element.
2. Collect all elements of the array that were directly compared to the
largest element.
3. Find the largest element among them.
Step 2. To design an efficient algorithm for finding the second largest element using the observation above, we need an algorithm for finding the largest element, where the largest element is compared to relatively few other elements.
Algorithm FindMaxRecursive(I,J, A[I..J])
begin
if I = J then return A[I]; //base case
max1← FindMaxRecursive(I, I+(J-I)/2, A);
max2← FindMaxRecursive(1+I+(J-I)/2,J, A);
if max1>max2 then return max1
else return max2;
end
Theorem. Finding the largest number in an array of N numbers requires at least N + ⌈log2(N)⌉ − 2 comparisons.
Tournament Method
http://www.seeingwithc.org/topic3html.htmlhttps://sites.google.com/site/mytechnicalcollection/problem-solving/second-largest-problem
http://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/
https://sites.google.com/site/mytechnicalcollection/problem-solving/second-largest-problem
https://blogs.oracle.com/malkit/entry/finding_2nd_minimum_element_in
Read full article from Google Interview - Second Largest Number - 我的博客 - ITeye技术网站
Find the second largest number in a given array. Return 0 if the given array has no second largest number.
http://users.csc.calpoly.edu/~dekhtyar/349-Spring2010/lectures/lec03.349.pdf
Conclusion 1. We can find the second largest element as follows:
1. Find the largest element.
2. Collect all elements of the array that were directly compared to the
largest element.
3. Find the largest element among them.
Step 2. To design an efficient algorithm for finding the second largest element using the observation above, we need an algorithm for finding the largest element, where the largest element is compared to relatively few other elements.
Algorithm FindMaxRecursive(I,J, A[I..J])
begin
if I = J then return A[I]; //base case
max1← FindMaxRecursive(I, I+(J-I)/2, A);
max2← FindMaxRecursive(1+I+(J-I)/2,J, A);
if max1>max2 then return max1
else return max2;
end
Theorem. Finding the largest number in an array of N numbers requires at least N + ⌈log2(N)⌉ − 2 comparisons.
Tournament Method
http://www.seeingwithc.org/topic3html.htmlhttps://sites.google.com/site/mytechnicalcollection/problem-solving/second-largest-problem
- public int secondLargest(int[] arr) {
- if(arr.length<2) return 0;
- int first = Math.max(arr[0], arr[1]);
- int second = Math.min(arr[0], arr[1]);
- for(int i=2; i<arr.length; i++) {
- if(arr[i] > first) {
- second = first;
- first = arr[i];
- } else if(arr[i] > second && arr[i] < first) {
- second = arr[i];
- }
- }
- if(first == second) return 0;
- return second;
- }
http://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/
static
void
print2Smallest(
int
arr[])
{
int
first, second, arr_size = arr.length;
/* There should be atleast two elements */
if
(arr_size <
2
)
{
System.out.println(
" Invalid Input "
);
return
;
}
first = second = Integer.MAX_VALUE;
for
(
int
i =
0
; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if
(arr[i] < first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second
then update second */
else
if
(arr[i] < second && arr[i] != first)
second = arr[i];
}
if
(second == Integer.MAX_VALUE)
System.out.println(
"There is no second"
+
"smallest element"
);
else
System.out.println(
"The smallest element is "
+
first +
" and second Smallest"
+
" element is "
+ second);
}
void largest_and_second_largest(int list[],int n,int &largest,int &slargest) { int largeindex=0,i; largest=list[0]; for (i=1;i<n;i++) //find the largest loop if (list[i]>largest) { largest=list[i]; largeindex=i; //to eliminate the largest later } //we have found the largest, stored in largest and its //index stored in largeindex. Now find the second largest //ignoring the largest. if (largeindex==0) slargest=list[1]; else slargest=list[0]; for (i=0;i<n;i++) if (list[i]>slargest && i!=largeindex) slargest=list[i]; //we have found the second largest }Code For 3rd largest
https://blogs.oracle.com/malkit/entry/finding_2nd_minimum_element_in
Read full article from Google Interview - Second Largest Number - 我的博客 - ITeye技术网站