📜  在数组中查找第二大元素|套装2

📅  最后修改于: 2021-05-17 03:43:35             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是使用N + log 2 (N)– 2个比较在给定数组中找到第二大元素。

例子:

排序和两次遍历方法:请参阅查找数组中的第二大元素以获取使用排序以及遍历数组两次的解决方案。

高效方法:
请按照以下步骤解决问题:

  • 从给定的数组中找到最大的元素,并与最大的元素进行比较,跟踪所有元素。
    • 将当前数组拆分为两个相等长度的子数组。
    • 对于每个子数组,递归地找到最大的元素,并返回一个数组,其中第一个索引包含该数组的长度,第二个索引元素包含最大的元素,其余的数组包含与最大元素进行比较的元素。
    • 现在,从两个子数组返回的两个数组中,比较两个子数组的最大元素,然后返回包含两个子数组中最大的一个的数组。
  • findLargest()返回的最终数组包含其在第一个索引中的大小,该数组在第二个索引处的最大元素以及与其余索引中的最大元素相比的元素。使用findLargest()重复上述步骤,从比较的元素列表中找到数组的第二大元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the largest
// element in the array arr[]
vector findLargest(int beg, int end,
                vector arr, int n)
{
     
    // Base Condition
    if (beg == end)
    {
         
        // Initialize an empty list
        vector compared(n, 0);
        compared[0] = 1;
        compared[1] = arr[beg];
        return compared;
    }
 
    // Divide the array into two equal
    // length subarrays and recursively
    // find the largest among the two
    vector compared1 = findLargest(
                            beg, (beg + end) / 2,
                            arr, n);
 
    vector compared2 = findLargest(
                            (beg + end) / 2 + 1,
                            end, arr, n);
 
    if (compared1[1] > compared2[1])
    {
        int k = compared1[0] + 1;
 
        // Store length of compared1[]
        // in the first index
        compared1[0] = k;
 
        // Store the maximum element
        compared1[k] = compared2[1];
 
        // Return compared1 which
        // contains the maximum element
        return compared1;
    }
    else
    {
        int k = compared2[0] + 1;
 
        // Store length of compared2[]
        // in the first index
        compared2[0] = k;
 
        // Store the maximum element
        compared2[k] = compared1[1];
 
        // Return compared2[] which
        // contains the maximum element
        return compared2;
    }
}
         
// Function to print the second largest
// element in the array arr[]
void findSecondLargest(int end, vector arr)
{
 
    // Find the largest element in arr[]
    vector compared1 = findLargest(
                            0, end - 1, arr, end);
 
    // Find the second largest element
    // in arr[]
    vector compared2 = findLargest(
                            2, compared1[0] + 2,
                            compared1,
                            compared1[0]);
 
    // Print the second largest element
    cout << compared2[1];
}
 
// Driver code
int main()
{
    int N = 10;
 
    vector arr{ 20, 1990, 12, 1110, 1,
                    59, 12, 15, 120, 1110};
 
    findSecondLargest(N, arr);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the largest
// element in the array arr[]
static int[] findLargest(int beg, int end,
                        int []arr, int n)
{
// Base Condition
if (beg == end)
{
    // Initialize an empty list
    int []compared = new int[n];
    compared[0] = 1;
    compared[1] = arr[beg];
    return compared;
}
 
// Divide the array into two equal
// length subarrays and recursively
// find the largest among the two
int []compared1 = findLargest(beg,
                            (beg + end) /
                                2, arr, n);
 
int []compared2 = findLargest((beg + end) /
                                2 + 1,
                                end, arr, n);
 
if (compared1[1] > compared2[1])
{
    int k = compared1[0] + 1;
 
    // Store length of compared1[]
    // in the first index
    compared1[0] = k;
 
    // Store the maximum element
    compared1[k] = compared2[1];
 
    // Return compared1 which
    // contains the maximum element
    return compared1;
}
else
{
    int k = compared2[0] + 1;
 
    // Store length of compared2[]
    // in the first index
    compared2[0] = k;
 
    // Store the maximum element
    compared2[k] = compared1[1];
 
    // Return compared2[] which
    // contains the maximum element
    return compared2;
}
}
 
// Function to print the second largest
// element in the array arr[]
static void findSecondLargest(int end,
                            int []arr)
{
    // Find the largest element in arr[]
    int []compared1 = findLargest(0, end - 1,
                                arr, end);
 
    // Find the second largest element
    // in arr[]
    int []compared2 = findLargest(2, compared1[0] + 2,
                                compared1,
                                compared1[0]);
 
    // Print the second largest element
    System.out.print(compared2[1]);
}
 
// Driver code
public static void main(String[] args)
{
  int N = 10;
  int []arr ={20, 1990, 12, 1110, 1,
              59, 12, 15, 120, 1110};
  findSecondLargest(N, arr);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 Program to implement
# the above approach
 
# Function to find the largest
# element in the array arr[]
def findLargest(beg, end, arr, n):
 
    # Base Condition
    if(beg == end):
 
        # Initialize an empty list
        compared = [0]*n
        compared[0] = 1
        compared[1] = arr[beg]
        return compared
 
    # Divide the array into two equal
    # length subarrays and recursively
    # find the largest among the two
    compared1 = findLargest(beg, (beg+end)//2,
                            arr, n)
 
    compared2 = findLargest((beg+end)//2+1, end,
                            arr, n)
 
    if(compared1[1] > compared2[1]):
        k = compared1[0]+1
 
    # Store length of compared1[]
    # in the first index
        compared1[0] = k
 
    # Store the maximum element
        compared1[k] = compared2[1]
 
        # Return compared1 which
        # contains the maximum element
        return compared1
 
    else:
        k = compared2[0]+1
 
    # Store length of compared2[]
    # in the first index
        compared2[0] = k
 
    # Store the maximum element
        compared2[k] = compared1[1]
 
        # Return compared2[] which
        # contains the maximum element
        return compared2
 
 
# Function to print the second largest
# element in the array arr[]
def findSecondLargest(end, arr):
 
    # Find the largest element in arr[]
    compared1 = findLargest(0, end-1, arr, end)
 
    # Find the second largest element
    # in arr[]
    compared2 = findLargest(2, compared1[0]+2,
                            compared1,
                            compared1[0])
 
    # Print the second largest element
    print(compared2[1])
 
 
# Driver Code
N = 10
 
arr = [20, 1990, 12, 1110, 1, 59, 12, 15, 120, 1110]
 
findSecondLargest(N, arr)


C#
// C# program to implement
// the above approach
using System;
  
class GFG{
     
// Function to find the largest
// element in the array arr[]
static int[] findLargest(int beg, int end,
                         int []arr, int n)
{
     
    // Base Condition
    if (beg == end)
    {
         
        // Initialize an empty list
        int []compared = new int[n];
        compared[0] = 1;
        compared[1] = arr[beg];
        return compared;
    }
     
    // Divide the array into two equal
    // length subarrays and recursively
    // find the largest among the two
    int []compared1 = findLargest(beg,
                                 (beg + end) /
                                  2, arr, n);
     
    int []compared2 = findLargest((beg + end) /
                                     2 + 1,
                                  end, arr, n);
     
    if (compared1[1] > compared2[1])
    {
        int k = compared1[0] + 1;
         
        // Store length of compared1[]
        // in the first index
        compared1[0] = k;
         
        // Store the maximum element
        compared1[k] = compared2[1];
         
        // Return compared1 which
        // contains the maximum element
        return compared1;
    }
    else
    {
        int k = compared2[0] + 1;
         
        // Store length of compared2[]
        // in the first index
        compared2[0] = k;
         
        // Store the maximum element
        compared2[k] = compared1[1];
         
        // Return compared2[] which
        // contains the maximum element
        return compared2;
    }
}
  
// Function to print the second largest
// element in the array arr[]
static void findSecondLargest(int end,
                              int []arr)
{
     
    // Find the largest element in arr[]
    int []compared1 = findLargest(0, end - 1,
                                  arr, end);
  
    // Find the second largest element
    // in arr[]
    int []compared2 = findLargest(2, compared1[0] + 2,
                                     compared1,
                                     compared1[0]);
  
    // Print the second largest element
   Console.WriteLine(compared2[1]);
}
 
// Driver code
static public void Main ()
{
    int N = 10;
    int []arr = { 20, 1990, 12, 1110, 1,
                  59, 12, 15, 120, 1110 };
                   
    findSecondLargest(N, arr);
}
}
 
// This code is contributed by offbeat


输出:
1110






时间复杂度: O(N)
辅助空间: O(logN)