Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1).

Examples:

Input: arr[] = {10, 4, 3, 50, 23, 90} Output: 90, 50, 23

Source: http://qa.geeksforgeeks.org/1019/array-number-elements-given-largest-numbers-array-sorting

Below is algorithm:

1) Initialize the largest three elements as minus infinite. first = second = third = -∞ 2) Iterate through all elements of array. a) Let current array element be x. b) If (x > first) { // This order of assignment is important third = second second = first first = x } c) Else if (x > second) { third = second second = x } d) Else if (x > third) { third = x } 3) Print first, second and third.

Below is the implementation of above algorithm.

## C

#include <stdio.h> #include <limits.h> /* For INT_MIN */ /* Function to print three largest elements */ void print2largest(int arr[], int arr_size) { int i, first, second, third; /* There should be atleast two elements */ if (arr_size < 3) { printf(" Invalid Input "); return; } third = first = second = INT_MIN; for (i = 0; i < arr_size ; i ++) { /* If current element is smaller than first*/ if (arr[i] > first) { third = second; second = first; first = arr[i]; } /* If arr[i] is in between first and second then update second */ else if (arr[i] > second) { third = second; second = arr[i]; } else if (arr[i] > third) third = arr[i]; } printf("Three largest elements are %d %d %d\n", first, second, third); } /* Driver program to test above function */ int main() { int arr[] = {12, 13, 1, 10, 34, 1}; int n = sizeof(arr)/sizeof(arr[0]); print2largest(arr, n); return 0; }

## Java

// Java code to find largest three elements // in an array class PrintLargest { /* Function to print three largest elements */ static void print2largest(int arr[], int arr_size) { int i, first, second, third; /* There should be atleast two elements */ if (arr_size < 3) { System.out.print(" Invalid Input "); return; } third = first = second = Integer.MIN_VALUE; for (i = 0; i < arr_size ; i ++) { /* If current element is smaller than first*/ if (arr[i] > first) { third = second; second = first; first = arr[i]; } /* If arr[i] is in between first and second then update second */ else if (arr[i] > second) { third = second; second = arr[i]; } else if (arr[i] > third) third = arr[i]; } System.out.println("Three largest elements are " + first + " " + second + " " + third); } /* Driver program to test above function*/ public static void main (String[] args) { int arr[] = {12, 13, 1, 10, 34, 1}; int n = arr.length; print2largest(arr, n); } } /*This code is contributed by Prakriti Gupta*/

## Python3

# Python3 code to find largest three # elements in an array import sys # Function to print three largest # elements def print2largest(arr, arr_size): # There should be atleast two # elements if (arr_size < 3): print(" Invalid Input ") return third = first = second = -sys.maxsize for i in range(0, arr_size): # If current element is smaller # than first if (arr[i] > first): third = second second = first first = arr[i] # If arr[i] is in between first # and second then update second elif (arr[i] > second): third = second second = arr[i] elif (arr[i] > third): third = arr[i] print("Three largest elements are", first, second, third) # Driver program to test above function arr = [12, 13, 1, 10, 34, 1] n = len(arr) print2largest(arr, n) # This code is contributed by Smitha Dinesh Semwal

Output:

Three largest elements are 34 13 12

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above