

Find maximum value and its index in an Array in C++
source link: https://thispointer.com/find-maximum-value-and-its-index-in-an-array-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find maximum value and its index in an Array in C++
In this article, we will discuss different ways to find the maximum value in an array and also its index position.
Table Of Contents
Problem Description
Here we are given an array and we have to find the maximum value in it and its index position in array.
Input
Advertisements
int arr[] = {12, 56, 823, 7, 1023};
Output
Maximum value in given array is 1023 and its index is 4
Let’s see how we can do it using C++. There are three method to find the max value in an array in c++
Find Maximum value in Array using Linear traversal : Iterative Method
In this method , we will iterate over the whole array.
These are following steps :
- Firstly create two local variable index and max
- Initialize the index with -1 and max with INT_MIN
- Traverse the whole array
- If the current value is greater than max then replace the current value with max.
- Along with it, replace the index value.
- Print the max value and index
Time Complexity: O(n)
Space Complexity: O(1)
Example
// C++ program to find maximum value and its index #include <iostream> #include <bits/stdc++.h> using namespace std; // Driver Code int main() { int arr[] = {12, 56, 823, 7, 1023}; // n is the size of array int n = sizeof(arr) / sizeof(arr[0]); //Intialize the value of max and index int max = INT_MIN; int index = -1; // Iterate the array for(int i=0;i<n;i++) { if(arr[i]>max) { // If current value is greater than max // value then replace it with max value max = arr[i]; index = i; } } cout << "Maximum value in given array is "; cout << max<<" and its index is "<< index <<endl; return 0; }
Output
Maximum value in given array is 1023 and its index is 4
Find Maximum value in Array using STL function max_element() and find()
In this method we use two STL functions to find the maximum value and its index value. we use 2 following STL function
- To find the max value use max_element(). It returns an iterator or address of the largest value in the range.
- To find the index position of an element use find().
First find the largest element using max_element() and then look for its index position using find().
Time Complexity: O(n)
Space Complexity: O(1)
Example
// STL Function to find maximum value and its index #include <iostream> #include <algorithm> using namespace std; // Driver Code int main() { int arr[] = {12, 56, 823, 7, 1023}; int n = sizeof(arr) / sizeof(arr[0]); // *max_element() will return the max value in array int max = *max_element(arr,arr+n); // now max variable contain maximum value // Now we have maximum value so we will find index of this max value by using find() function int index = find(arr, arr+n, max) - arr; cout << "Maximum value in given array is "; cout << max<<" and its index is "<< index <<endl; return 0; }
Output
Maximum value in given array is 1023 and its index is 4
Find Maximum value in Array using max() & find()
In this method , we will iterate over the array till just before array size (n-1).
These are the steps :
- Firstly create two local variable index and max_value
- Initialize the index with -1 and max_value with arr[0];
- Traverse the array till size-1
- Use the max function to find the maximum value between 2 values
- Use find function to find index
- Print the max value and index
Time Complexity: O(n)
Space Complexity: O(1)
Example
// C++ program to find maximum value and its index #include <iostream> #include <algorithm> using namespace std; // Driver Code int main() { int arr[] = {12, 56, 823, 7, 1023}; int n = sizeof(arr) / sizeof(arr[0]); int max_value = arr[0]; int index = -1; // Iterate the array for(int i=0;i<n-1;i++) { max_value = max(max_value, arr[i+1]); } // Finding index using find function index = find(arr, arr+n, max_value) - arr; cout << "Maximum value in given array is "; cout << max_value<<" and its index is "<< index << endl; return 0; }
Output
Maximum value in given array is 1023 and its index is 4
Summary
We have seen three method to find the maximum value in an array and also its index position. One is naive solution. Another are using the STL functions. Every Method has its own Time complexity and space complexity.
Do you want to Learn Modern C++ from best?
We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.
Check Detailed Reviews of Best Modern C++ Courses
Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK