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.

### Frequently Asked:

- How to convert a string to char array in C++?
- Check if Any value in Array is greater than a Value in C++
- How to initialize an Array with same value in C++?
- Check if All elements in Array are equal to value in C++

These are following steps :

- Firstly create two local variable index and min.
- Initialize the index with -1 and min with INT_MAX
- Traverse the whole array
- If the current value is less than min then replace the current value with min.
- Along with replace the index value.
- 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

- To find the min value use min_element(). It returns an iterator or address of the smallest value in the range.
- 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 :

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

- 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.