Open In App

How to Find Largest Number in an Array in C++?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++.

For Example,

Input:
myVector = {1, 3, 10, 7, 8, 5, 4}

Output:
Largest Number = 10

Find the Largest Number in an Array in C++

To find the largest number in an array, we can use the std::max_element() function that takes the range in which it has to search for the max element as the arguments.

The syntax of max_element() is:

max_element(begin, end);

Here, the begin is the pointer or iterator to the start of the range and the end is the pointer or iterator to the end of the range.

This function will return the pointer or iterator to the maximum element in the range.

C++ Program to Find the Largest Number in an Array in C++

C++




// C++ program to find the largest number in an array
// without using std::max_element
#include <algorithm>
#include <iostream>
  
using namespace std;
  
int main()
{
    // Initialize the array
    int arr[] = { 1, 45, 54, 71, 76, 12 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Find the largest element in the array
    int max_num = *max_element(arr, arr + n);
  
    // Output the result
    cout << "Largest number in the array is: " << max_num
         << endl;
  
    return 0;
}


Output

Largest number in the array is: 76

Time complexity: O(n), where n is the number of elements in the array.
Space complexity: O(1)


Similar Reads

Find the largest and second largest value in a Linked List
Given a Linked List, the task is to find the largest and second largest value in a Linked List.Example: Input: LL = 10 -&gt; 15 -&gt; 5 -&gt; 20 -&gt; 7 -&gt; 9 Output: Largest = 20 Second Largest = 15Input: LL = 0 -&gt; 5 -&gt; 52 -&gt; 21 Output: Largest = 52 Second Largest = 21 Approach: Store the maximum of first two nodes in a variable max.Sto
9 min read
Find the largest composite number that divides N but is strictly lesser than N
Given a composite number N, the task is to find the largest composite number that divides N and is strictly lesser than N. If there is no such number exist print -1.Examples: Input: N = 16 Output: 8 Explanation: All numbers that divide 16 are { 1, 2, 4, 8, 16 } out of which 8 is largest composite number(lesser than 16) that divides 16. Input: N = 1
6 min read
Minimum N-Digit number required to obtain largest N-digit number after performing given operations
Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number: Convert the number to its Binary Coded Decimal form.Concatenate all the resulting nibbles to form a binary number.Remove the least significant N bits from the
6 min read
C++ Program to Print the Largest Possible Prime Number From a Given Number
Given an integer, the task is to find the largest prime number that can be made out of that. If we consider the integer as a string of digits, then the prime number can be a substring of any length. The examples given below will clarify the idea of the problem. Example: Input: 12691Output: 691 Approach:Create a string of the given numberCompute all
2 min read
Given an array and two integers l and r, find the kth largest element in the range [l, r]
Given an unsorted array arr[] of n integers and an integer k, the task is to find the kth largest element in the given index range [l, r]Examples: Input: arr[] = {5, 3, 2, 4, 1}, k = 4, l = 1, r = 5 Output: 4 4 will be the 4th element when arr[0...4] is sorted.Input: arr[] = {1, 4, 2, 3, 5, 7, 6}, k = 3, l = 3, r = 6 Output: 5 Approach: A naive sol
11 min read
Program to find largest element in an array using Dynamic Memory Allocation
Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanation:The largest element present in the given array
5 min read
Find a pair in Array with second largest product
Given an array arr[] of N integers, where N &gt; 2, the task is to find the second largest product pair from the given array. Examples: Input: arr[] = {10, 20, 12, 40, 50}Output: 20 50Explanation:A pair of array elements = [(10, 20), (10, 12), (10, 40), (10, 50), (20, 12), (20, 40), (20, 50), (12, 40), (12, 50), (40, 50)]If do product of each pair
16 min read
C++ Program to Find Largest Element in an Array
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 tr
2 min read
C++ Program to Find the Second Largest Element in an Array
In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The largest element is 56 and the second largest eleme
3 min read
Mean of given array after removal of K percent of smallest and largest array elements
Given an array arr[] and an integer K, the task is to remove K % percent array elements from the smallest and largest array elements and calculate the mean of the remaining array. Examples: Input: arr[] = {6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0}, K = 5Output: 4.00000Explanation:There are 20 elements in the array. Therefore, 5%
6 min read
Article Tags :
Practice Tags :