C++ Program to Find Largest Element in an Array Last Updated : 03 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to find the greatest element in an array is to simply traverse the whole list and maintain a variable max to store the maximum element so far and compare this max element with the current element and update the max if any element greater than max is encountered. AlgorithmCreate a local variable max to store the maximum among the list.Initialize max with the first element initially, to start the comparison.Then traverse the given array from the second element till the end, and for each element:Compare the current element with the maxIf the current element is greater than max, then replace the value of max with the current element.In the end, return and print the value of the largest element of the array stored in max.C++ Program to Find Largest Number in an Array C++ // C++ program to find maximum // in arr[] of size n #include <bits/stdc++.h> using namespace std; // Function to find the largest // number in array int largest(int arr[], int n) { int i; // Initialize maximum element int max = arr[0]; // Traverse array elements // from second and compare // every element with current max for (i = 1; i < n; i++) if (arr[i] > max) max = arr[i]; return max; } // Driver Code int main() { int arr[] = { 10, 324, 45, 90, 9808 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Largest in given array is " << largest(arr, n); return 0; } OutputLargest in given array is 9808Complexity AnalysisTime complexity: O(N), to traverse the Array completely.Auxiliary Space: O(1), as only an extra variable is created, which will take O(1) space. Refer to the complete article Program to find largest element in an Array for optimized methods to find the largest element in an array. Comment More infoAdvertise with us Next Article Largest element in an Array K kartik Follow Improve Article Tags : C++ Order-Statistics Arrays Practice Tags : CPPArrays Similar Reads Find Second largest element in an array | Set 2 Given an array arr[] consisting of N integers, the task is to find the second largest element in the given array using N+log2(N) - 2 comparisons. Examples: Input : arr[] = {22, 33, 14, 55, 100, 12}Output : 55 Input : arr[] = {35, 23, 12, 35, 19, 100}Output : 35 Sorting and Two-Traversal Approach: Re 9 min read Find k largest elements in an array Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai 15+ min read Largest element in an Array Given an array arr. The task is to find the largest element in the given array. Examples: Input: arr[] = [10, 20, 4]Output: 20Explanation: Among 10, 20 and 4, 20 is the largest. Input: arr[] = [20, 10, 20, 4, 100]Output: 100Table of ContentIterative Approach - O(n) Time and O(1) SpaceRecursive Appro 6 min read Kth Largest Element in an Array Given an integer array arr[] of size n elements and a positive integer K, the task is to return the kth largest element in the given array (not the Kth distinct element).Examples:Input: [1, 23, 12, 9, 30, 2, 50], K = 3Output: 23Input: [12, 3, 5, 7, 19], K = 2Output: 12Table of Content[Naive Approach 15 min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Assembly language program to find largest number in an array Problem - Determine largest number in an array of n elements. Value of n is stored at address 2050 and array starts from address 2051. Result is stored at address 3050. Starting address of program is taken as 2000. Example: Algorithm: We are taking first element of array in AComparing A with other e 3 min read Like