Find minimum value and its index in an Array in C++

In this article, we will discuss different ways to find the minimum 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 minimum value and its index position.

Input

int arr[] = {12, 56, 823, 7, 1023};

Output

Minimum value in given array is 7 and its index is 3

Let’s see how we can do it using C++. There are three method to find the min value in an array in C++.

Find Min value in Array using Linear traversal -(Iterative Method)

In this method , we will iterate the over whole array.

These are following steps :

  1. Firstly create two local variable index and min.
  2. Initialize the index with -1 and min with INT_MAX
  3. Traverse the whole array
  4. If the current value is less than min then replace the current value with min.
  5. Along with replace the index value.
  6. Print the min value and index

Time Complexity: O(n)
Space Complexity: O(1)

Example

// C++ program to find minimum 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 min and index
    int min = INT_MAX;
    int index = -1;

    // Iterate the array
    for(int i=0; i < n; i++)
    {
        if(arr[i] < min)
        {
            //If current value is less than min value
            // then replace it with min value
            min = arr[i];
            index = i;
        }
    }

    cout << "Minimum value in given array is ";
    cout << min <<" and its index is "<< index <<endl;

    return 0;
}

Output

Minimum value in given array is 7 and its index is 3

Find Min value in Array using STL function

In this method we use STL functions to find the minimum value and its index value. We use 2 following STL functions

  1. To find the min value use min_element(). It returns an iterator or address of the smallest value in the range.
  2. To find the index position of an element use find().

First find the smallest element using min_element() and then look for its index position using find().

Time Complexity: O(n)
Space Complexity: O(1)

Example

// STL Function to find minimum 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]);

    // *min_element() will return the min value in array
    int min = *(min_element(arr,arr+n));

    // now min variable contain minimum value

    // Now we have minimum value so we will find the 
    // index of this min value by using find() function
    int index = find(arr,arr+n,min)-arr;

    cout << "Minimum value in given array is ";
    cout << min<<" and its index is "<< index <<endl;

    return 0;
}

Output

Minimum value in given array is 7 and its index is 3

Find Min value in Array using min() and find()

In this method , we iterate the array till just before array size (n-1).

These are the steps :

  1. Firstly create two local varaible index and min_value
  2. Intialize the index with -1 and min_value with arr[0];
  3. Traverse the array till size-1
    1. Use the min() function to find the minimum value between 2 values
    2. Use the find() function to find index.
  4. Print the min value and index

Time Complexity: O(n)
Space Complexity: O(1)

Example

// C++ program to find minimum 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 min_value = arr[0];
    int index = -1;


    // Iterate the array
    for(int i=0;i<n-1;i++)
    {
        min_value = min(min_value ,arr[i+1]);
    }

    // Finding index using find function
    index = find(arr, arr+n, min_value) - arr;

    cout << "Minimum value in given array is ";
    cout << min_value << " and its index is "<< index <<endl;

    return 0;
}

Output

Minimum value in given array is 7 and its index is 3

Summary

We have seen three method to find minimum value and its index. One is the naive solution. Another are using 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