forked from nani0231/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminMaxElementInArray.cpp
More file actions
71 lines (56 loc) · 1.45 KB
/
minMaxElementInArray.cpp
File metadata and controls
71 lines (56 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Problem link: https://practice.geeksforgeeks.org/problems/find-minimum-and-maximum-element-in-an-array4428/1
*
* Problem statement:
* Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.
*
* Your task:
* You don't need to read input or print anything.
* 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.
*
* Expected Time Complexity: O(N)
* Expected Auxiliary Space: O(1)
*/
/**
* Driver code starts here
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
pair <long long, long long> getMinMax (long long input[], int n);
int main () {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll input[n];
for (int i = 0; i < n; i++) {
cin >> input[i];
}
pair<ll, ll> result = getMinMax(input, n);
cout << result.first << " " << result.second << endl;
}
return 0;
}
/**
* Driver code ends here
*/
pair <long long, long long> getMinMax (long long input[], int n) {
ll minEl = input[0], maxEl = input[0];
for (int i = 1; i < n; i++) {
minEl = min(input[i], minEl);
maxEl = max(input[i], maxEl);
}
return { minEl, maxEl };
}
/**
* Sample test cases
* 3
* 6
* 3 2 1 56 1000 167
* 1
* 12
* 10
* -1 -10 2 4 23 10000003 434343434 1202129 98 18
*/