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

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

  1. To find the max value use max_element(). It returns an iterator or address of the largest value in the range.
  2. 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 :

  1. Firstly create two local variable index and max_value
  2. Initialize the index with -1 and max_value with arr[0];
  3. Traverse the array till size-1
    1. Use the max function to find the maximum value between 2 values
    2. Use find function to find index
  4. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top