-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_21.cpp
60 lines (43 loc) · 1.14 KB
/
question_21.cpp
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
/*
==> Maximum in Struct Array <==
Question Link:- https://practice.geeksforgeeks.org/problems/maximum-in-struct-array/1?page=2&difficulty[]=-1&category[]=Arrays&sortBy=submissions
Given a struct array of type Height, having two elements feet and inches. Find the maximum
height among n heights, where height is calculated sum of feet and inches after converting
feet into inches.
Example 1:
Input:
n = 2
h1 -> 1 2
h2 -> 2 1
Output:
25
Explanation:
(1, 2) and (2, 1) are respective given heights.
After converting both heigths in to inches,
we get 14 and 25 as respective heights.
So, 25 is the maximum height.
*/
#include <iostream>
using namespace std;
struct Height {
int feet;
int inches;
};
int toInch(Height h) {
return h.feet*12 + h.inches;
}
int findMax(Height arr[], int n) {
int mx = 0;
for ( int i = 0; i < n; i++ ) mx = max( mx, toInch(arr[i]) );
return mx;
}
int main () {
Height h1, h2;
h1.feet = 1;
h1.inches = 2;
h2.feet = 2;
h2.inches = 1;
Height arr [] = { h1, h2 };
cout << findMax(arr, 2) << endl;
return 0;
}