Skip to content

Commit 771af8a

Browse files
committed
statement and solution for problem - get min, max element from array
1 parent 56fa577 commit 771af8a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Array/minMaxElementInArray.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Problem link: https://practice.geeksforgeeks.org/problems/find-minimum-and-maximum-element-in-an-array4428/1
3+
*
4+
* Problem statement:
5+
* Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.
6+
*
7+
* Your task:
8+
* You don't need to read input or print anything.
9+
* Your task is to complete the function getMinMax() which takes the array A[] and its size N as inputs and returns the minimum and maximum element of the array.
10+
*
11+
* Expected Time Complexity: O(N)
12+
* Expected Auxiliary Space: O(1)
13+
*/
14+
15+
/**
16+
* Driver code starts here
17+
*/
18+
#include <bits/stdc++.h>
19+
20+
using namespace std;
21+
22+
#define ll long long
23+
24+
pair <long long, long long> getMinMax (long long input[], int n);
25+
26+
int main () {
27+
int t;
28+
cin >> t;
29+
30+
while (t--) {
31+
int n;
32+
cin >> n;
33+
34+
ll input[n];
35+
36+
for (int i = 0; i < n; i++) {
37+
cin >> input[i];
38+
}
39+
40+
pair<ll, ll> result = getMinMax(input, n);
41+
42+
cout << result.first << " " << result.second << endl;
43+
}
44+
45+
return 0;
46+
}
47+
/**
48+
* Driver code ends here
49+
*/
50+
51+
pair <long long, long long> getMinMax (long long input[], int n) {
52+
ll minEl = input[0], maxEl = input[0];
53+
54+
for (int i = 1; i < n; i++) {
55+
minEl = min(input[i], minEl);
56+
maxEl = max(input[i], maxEl);
57+
}
58+
59+
return { minEl, maxEl };
60+
}
61+
62+
/**
63+
* Sample test cases
64+
* 3
65+
* 6
66+
* 3 2 1 56 1000 167
67+
* 1
68+
* 12
69+
* 10
70+
* -1 -10 2 4 23 10000003 434343434 1202129 98 18
71+
*/

0 commit comments

Comments
 (0)