Tercer elemento más grande en una array de elementos distintos

Dada una array de n enteros, encuentre el tercer elemento más grande. Todos los elementos de la array son enteros distintos. 
Ejemplo : 
 

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

C++

// C++ program to find third Largest
// element in an array of distinct elements
#include <bits/stdc++.h>
 
void thirdLargest(int arr[], int arr_size)
{
    /* There should be atleast three elements */
    if (arr_size < 3) {
        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 = INT_MIN;
    for (int i = 0; i < arr_size; i++)
        if (arr[i] > second && arr[i] < first)
            second = arr[i];
 
    // Find third largest element
    int third = INT_MIN;
    for (int i = 0; i < arr_size; i++)
        if (arr[i] > third && arr[i] < second)
            third = arr[i];
 
    printf("The third Largest element is %d\n", third);
}
 
/* Driver program to test above function */
int main()
{
    int arr[] = { 12, 13, 1, 10, 34, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
    thirdLargest(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C program to find third Largest element in an array of
// distinct elements
#include <limits.h>
#include <stdio.h>
 
void thirdLargest(int arr[], int arr_size)
{
    /* There should be atleast three elements */
    if (arr_size < 3) {
        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 = INT_MIN;
    for (int i = 0; i < arr_size; i++)
        if (arr[i] > second && arr[i] < first)
            second = arr[i];
 
    // Find third largest element
    int third = INT_MIN;
    for (int i = 0; i < arr_size; i++)
        if (arr[i] > third && arr[i] < second)
            third = arr[i];
 
    printf("The third Largest element is %d\n", third);
}
 
/* Driver program to test above function */
int main()
{
    int arr[] = { 12, 13, 1, 10, 34, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
    thirdLargest(arr, n);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

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\n", 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

Python3

# Python 3 program to find
# third Largest element in
# an array of distinct elements
import sys
def thirdLargest(arr, arr_size):
 
    # There should be
    # atleast three elements
    if (arr_size < 3):
     
        print(" Invalid Input ")
        return
     
 
    # Find first
    # largest element
    first = arr[0]
    for i in range(1, arr_size):
        if (arr[i] > first):
            first = arr[i]
 
    # Find second
    # largest element
    second = -sys.maxsize
    for i in range(0, arr_size):
        if (arr[i] > second and
            arr[i] < first):
            second = arr[i]
 
    # Find third
    # largest element
    third = -sys.maxsize
    for i in range(0, arr_size):
        if (arr[i] > third and
            arr[i] < second):
            third = arr[i]
 
    print("The Third Largest",
          "element is", third)
 
# Driver Code
arr = [12, 13, 1,
       10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
 
# This code is contributed
# by Smitha

C#

// C# program to find third
// Largest element in an array
// of distinct elements
using System;
 
class GFG
{
static void thirdLargest(int []arr,
                         int arr_size)
{
    /* There should be
    atleast three elements */
    if (arr_size < 3)
    {
        Console.Write(" 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 = -int.MaxValue;
    for (int i = 0;
             i < arr_size ; i++)
        if (arr[i] > second &&
            arr[i] < first)
            second = arr[i];
 
    // Find third
    // largest element
    int third = -int.MaxValue;
    for (int i = 0;
             i < arr_size ; i++)
        if (arr[i] > third &&
            arr[i] < second)
            third = arr[i];
 
    Console.Write("The third Largest " +
                  "element is "+ third);
}
 
// Driver code
public static void Main()
{
    int []arr = {12, 13, 1,    
                 10, 34, 16};
    int n = arr.Length;
    thirdLargest(arr, n);
}
}
 
// This code is contributed by Smitha

PHP

<?php
// PHP program to find third
// Largest element in an array
// of distinct elements
 
function thirdLargest($arr, $arr_size)
{
    /* There should be atleast
    three elements */
    if ($arr_size < 3)
    {
        echo " Invalid Input ";
        return;
    }
 
    // Find first largest element
    $first = $arr[0];
    for ($i = 1; $i < $arr_size ; $i++)
        if ($arr[$i] > $first)
            $first = $arr[$i];
 
    // Find second largest element
    $second = PHP_INT_MIN;
    for ($i = 0; $i < $arr_size ; $i++)
        if ($arr[$i] > $second &&
               $arr[$i] < $first)
            $second = $arr[$i];
 
    // Find third largest element
    $third = PHP_INT_MIN;
    for ($i = 0; $i < $arr_size ; $i++)
        if ($arr[$i] > $third &&
              $arr[$i] < $second)
            $third = $arr[$i];
 
    echo "The third Largest element is ",
                             $third,"\n";
}
 
// Driver Code
$arr = array(12, 13, 1,
             10, 34, 16);
$n = sizeof($arr);
thirdLargest($arr, $n);
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
// JavaScript program to find third
// Largest element in an array
// of distinct elements
 
function thirdLargest(arr, arr_size)
{
    /* There should be
    atleast three elements */
    if (arr_size < 3)
    {
        document.write(" Invalid Input ");
        return;
    }
   
    // Find first
    // largest element
    let first = arr[0];
    for (let i = 1;
             i < arr_size ; i++)
        if (arr[i] > first)
            first = arr[i];
   
    // Find second
    // largest element
    let second = Number.MIN_VALUE;
    for (let i = 0;
             i < arr_size ; i++)
        if (arr[i] > second &&
            arr[i] < first)
            second = arr[i];
   
    // Find third
    // largest element
    let third = Number.MIN_VALUE;
    for (let i = 0;
             i < arr_size ; i++)
        if (arr[i] > third &&
            arr[i] < second)
            third = arr[i];
   
    document.write("The third Largest " +
                  "element is ", third);
}
 
// Driver Code
 
    let arr = [12, 13, 1,
                 10, 34, 16];
    let n = arr.length;
    thirdLargest(arr, n);
                         
</script>

C++

// C++ program to find third
// Largest element in an array
#include <bits/stdc++.h>
 
void thirdLargest(int arr[], int arr_size)
{
    /* There should be atleast three elements */
    if (arr_size < 3)
    {
        printf(" Invalid Input ");
        return;
    }
 
    // Initialize first, second and third Largest element
    int first = arr[0], second = INT_MIN, third = INT_MIN;
 
    // 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];
    }
 
    printf("The third Largest element is %d\n", third);
}
 
/* Driver program to test above function */
int main()
{
    int arr[] = {12, 13, 1, 10, 34, 16};
    int n = sizeof(arr)/sizeof(arr[0]);
    thirdLargest(arr, n);
    return 0;
}

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\n", 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

Python3

# Python3 program to find
# third Largest element in
# an array
import sys
def thirdLargest(arr, arr_size):
 
    # There should be
    # atleast three elements
    if (arr_size < 3):
     
        print(" Invalid Input ")
        return
     
    # Initialize first, second
    # and third Largest element
    first = arr[0]
    second = -sys.maxsize
    third = -sys.maxsize
 
    # Traverse array elements
    # to find the third Largest
    for i in range(1, arr_size):
     
        # 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
        elif (arr[i] > second):
         
            third = second
            second = arr[i]
         
        # If arr[i] is in between
        # second and third
        elif (arr[i] > third):
            third = arr[i]
     
    print("The third Largest" ,
                  "element is", third)
 
# Driver Code
arr = [12, 13, 1,
       10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
 
# This code is contributed
# by Smitha

C#

// C# program to find third Largest element in an array
using System;
class GFG {
 
static void thirdLargest(int[] arr, int arr_size)
{
    /* There should be atleast three elements */
    if (arr_size < 3)
    {
        Console.Write(" Invalid Input ");
        return;
         
    }
 
    // Initialize first, second and third Largest element
    int first = arr[0], second = int.MinValue,
                            third = int.MinValue;
 
    // 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];
        }
    }
 
    Console.Write("The third Largest element is "+ third);
}
 
/* Driver program to test above function */
public static void Main() {
        int[] arr = {12, 13, 1, 10, 34, 16};
        int n = arr.Length;
        thirdLargest(arr, n);
    }
}
 
// This code is contributed
// by Ita_c

PHP

<?php
// PHP program to find third
// Largest element in an array
 
function thirdLargest($arr, $arr_size)
{
    /* There should be atleast
       three elements */
    if ($arr_size < 3)
    {
        echo " Invalid Input ";
        return;
    }
 
    // Initialize first, second and
    // third Largest element
    $first = $arr[0];
    $second = PHP_INT_MIN;
    $third = PHP_INT_MIN;
 
    // Traverse array elements to
    // find the third Largest
    for ($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];
    }
 
    echo "The third Largest element is ",
                                  $third;
}
 
// Driver Code
$arr = array (12, 13, 1,
              10, 34, 16);
$n = sizeof($arr);
thirdLargest($arr, $n);
 
// This code is contributed by jit_t
?>

Javascript

<script>
 
// JavaScript program to find third
// Largest element in an array
 
function thirdLargest(arr, arr_size)
{
    /* There should be atleast three elements */
    if (arr_size < 3)
    {
        document.write(" Invalid Input ");
        return;
    }
 
    // Initialize first, second and third Largest element
    var first = arr[0], second = -1000000000, third = -1000000000;
 
    // Traverse array elements to find the third Largest
    for (var 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];
    }
 
    document.write("The third Largest element is "+ third);
}
 
/* Driver program to test above function */
var arr = [12, 13, 1, 10, 34, 16];
var n = arr.length;
thirdLargest(arr, n);
 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *