# Find the first, second and third minimum elements in an array in C++ program

C++Server Side ProgrammingProgramming

Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.

Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.

## Example

#include<iostream>
using namespace std;
int getThreeMins(int arr[], int n) {
int first = INT_MAX, sec = INT_MAX, third = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] < first) {
third = sec;
sec = first;
first = arr[i];
} else if (arr[i] < sec) {
third = sec;
sec = arr[i];
} else if (arr[i] < third)
third = arr[i];
}
cout << "First min = " << first << endl;
cout << "Second min = " << sec << endl;
cout << "Third min = " << third << endl;
}
int main() {
int array[] = {4, 9, 18, 32, 12};
int n = sizeof(array) / sizeof(array);
getThreeMins(array, n);
}

## Output

First min = 4
Second min = 9
Third min = 12
{"ft_lang_label":"__label__en","ft_lang_prob":0.82577145,"math_prob":0.9922388,"size":1039,"snap":"2021-43-2021-49","text_gpt3_token_len":292,"char_repetition_ratio":0.16135266,"word_repetition_ratio":0.049261082,"special_character_ratio":0.34359962,"punctuation_ratio":0.16346154,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9989428,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-25T17:22:53Z\",\"WARC-Record-ID\":\"<urn:uuid:0298c6aa-50f6-49cf-ade7-bc76011545a2>\",\"Content-Length\":\"32093\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:38222160-1d1f-4927-a0ea-b440e2d194e9>\",\"WARC-Concurrent-To\":\"<urn:uuid:381a2b62-0640-4184-82e4-d0d7bcb407e8>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/find-the-first-second-and-third-minimum-elements-in-an-array-in-cplusplus-program\",\"WARC-Payload-Digest\":\"sha1:WE7VHQONGQ6QG53RMSKTG5SKAUIED6DL\",\"WARC-Block-Digest\":\"sha1:JMC2U6NEDGOSAJYO6VXO4YITOWXDE2EZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587719.64_warc_CC-MAIN-20211025154225-20211025184225-00023.warc.gz\"}"}