📜  用于不同元素数组中第三大元素的Java程序

📅  最后修改于: 2022-05-13 01:55:38.301000             🧑  作者: Mango

用于不同元素数组中第三大元素的Java程序

给定一个包含 n 个整数的数组,找到第三大元素。数组中的所有元素都是不同的整数。
例子 :

Input: arr[] = {1, 14, 2, 16, 10, 20}
Output: The third Largest element is 14

Explanation: Largest element is 20, second largest element is 16 
and third largest element is 14

Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Output: The third Largest element is 16

Explanation: Largest element is 20, second largest element is 19 
and third largest element is 16

朴素方法任务是首先找到最大的元素,然后是第二大的元素,然后排除它们都找到第三大的元素。基本思想是对数组进行两次迭代,标记最大和第二大元素,然后排除它们都找到第三大元素,即排除最大值和第二大元素的最大元素。

  • 算法:
    1. 首先,遍历数组并找到最大值。
    2. 将此作为第一个最大值与其索引一起存储。
    3. 现在遍历整个数组找到第二个最大值,不包括最大元素。
    4. 最后第三次遍历数组,找到第三个最大的元素,即排除最大值和第二个最大值。
Java
// Java program to find third 
// Largest element in an array
// of distinct elements
  
class GFG
{
static void thirdLargest(int arr[],
                         int arr_size)
{
    /* There should be 
    atleast three elements */
    if (arr_size < 3)
    {
        System.out.printf(" Invalid Input ");
        return;
    }
  
    // Find first 
    // largest element
    int first = arr[0];
    for (int i = 1;
             i < arr_size ; i++)
        if (arr[i] > first)
            first = arr[i];
  
    // Find second 
    // largest element
    int second = Integer.MIN_VALUE;
    for (int i = 0; 
             i < arr_size ; i++)
        if (arr[i] > second && 
            arr[i] < first)
            second = arr[i];
  
    // Find third
    // largest element
    int third = Integer.MIN_VALUE;
    for (int i = 0; 
             i < arr_size ; i++)
        if (arr[i] > third && 
            arr[i] < second)
            third = arr[i];
  
    System.out.printf("The third Largest " + 
                  "element is %d
", third);
}
  
// Driver code
public static void main(String []args)
{
    int arr[] = {12, 13, 1, 
                 10, 34, 16};
    int n = arr.length;
    thirdLargest(arr, n);
}
}
  
// This code is contributed
// by Smitha


Java
// Java program to find third Largest element in an array
class GFG {
  
    static void thirdLargest(int arr[], int arr_size) {
        /* There should be atleast three elements */
        if (arr_size < 3) {
            System.out.printf(" Invalid Input ");
            return;
        }
  
        // Initialize first, second and third Largest element
        int first = arr[0], second = Integer.MIN_VALUE,
                            third = Integer.MIN_VALUE;
  
        // Traverse array elements to find the third Largest
        for (int i = 1; i < arr_size; i++) {
            /* If current element is greater than first,
        then update first, second and third */
            if (arr[i] > first) {
                third = second;
                second = first;
                first = arr[i];
            } /* If arr[i] is in between first and second */
            else if (arr[i] > second) {
                third = second;
                second = arr[i];
            } /* If arr[i] is in between second and third */
            else if (arr[i] > third) {
                third = arr[i];
            }
        }
  
        System.out.printf("The third Largest element is %d
", third);
    }
  
    /* Driver program to test above function */
    public static void main(String []args) {
        int arr[] = {12, 13, 1, 10, 34, 16};
        int n = arr.length;
        thirdLargest(arr, n);
    }
}
//This code is contributed by 29AjayKumar


  • 输出:
The third Largest element is 13
  • 复杂性分析:
    • 时间复杂度: O(n)。
      由于数组迭代三次并在恒定时间内完成
    • 空间复杂度: O(1)。
      不需要额外的空间,因为索引可以存储在常量空间中。

有效方法问题涉及在一次遍历中找到数组中的第三大元素。这个问题可以通过一个类似的问题来解决——找到第二个最大元素。所以想法是从头到尾遍历数组,并跟踪直到该索引的三个最大元素(存储在变量中) 。因此,在遍历整个数组之后,变量将存储数组中三个最大元素的索引(或值)

  • 算法:
    1. 创建三个变量, firstsecondthird ,以存储数组中三个最大元素的索引。 (最初它们都被初始化为最小值)。
    2. 沿着输入数组从开始到结束移动。
    3. 对于每个索引,检查元素是否大于第一个。如果元素较大,则更新first的值,并将first的值分配给second ,将second的值分配给third 。所以最大的元素被更新,之前存储为最大的元素变成第二大元素,第二大元素变成第三大元素。
    4. 否则,如果元素大于第二个,则更新第二个的值,第二大的元素变为第三大。
    5. 如果前两个条件失败,但元素大于第三个,则更新第三个
    6. 从头到尾遍历数组后打印第三个的值

Java

// Java program to find third Largest element in an array
class GFG {
  
    static void thirdLargest(int arr[], int arr_size) {
        /* There should be atleast three elements */
        if (arr_size < 3) {
            System.out.printf(" Invalid Input ");
            return;
        }
  
        // Initialize first, second and third Largest element
        int first = arr[0], second = Integer.MIN_VALUE,
                            third = Integer.MIN_VALUE;
  
        // Traverse array elements to find the third Largest
        for (int i = 1; i < arr_size; i++) {
            /* If current element is greater than first,
        then update first, second and third */
            if (arr[i] > first) {
                third = second;
                second = first;
                first = arr[i];
            } /* If arr[i] is in between first and second */
            else if (arr[i] > second) {
                third = second;
                second = arr[i];
            } /* If arr[i] is in between second and third */
            else if (arr[i] > third) {
                third = arr[i];
            }
        }
  
        System.out.printf("The third Largest element is %d
", third);
    }
  
    /* Driver program to test above function */
    public static void main(String []args) {
        int arr[] = {12, 13, 1, 10, 34, 16};
        int n = arr.length;
        thirdLargest(arr, n);
    }
}
//This code is contributed by 29AjayKumar
  • 输出:
The third Largest element is 13
  • 复杂性分析:
    • 时间复杂度: O(n)。
      由于数组迭代一次并在恒定时间内完成
    • 空间复杂度: O(1)。
      不需要额外的空间,因为索引可以存储在常量空间中。

有关详细信息,请参阅有关不同元素数组中第三大元素的完整文章!