forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum_In_Array.cpp
83 lines (75 loc) · 2.36 KB
/
Maximum_In_Array.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @author omkarlanghe
* Given an array of type positive integers, having ith element as feet and ith + 1 element as inches.
* Eg: considering arr = [1,2] where 1 equals to feet and 2 equals to inches.
* Find the maximum height, where height is calculated sum of feet and inches after converting feet into inches.
*
* Input:
* First line of input contains number of testcases.
* For each testcase, first line of input N, contains size of an array.
* Next line contains N number of elements (feet for ith index and inches for ith + 1).
*
* Output:
* Output maximum height from array.
*
* Example:
* Input:
* 2
* 4
* 1 2 2 1
* 8
* 3 5 7 9 5 6 5 5
*
* Output:
* 25
* 93
*
* Explanation:
* Testcase 1: (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.
*
* @source: https://practice.geeksforgeeks.org/problems/maximum-in-struct-array/1
*/
#include <iostream>
#include <vector>
int max_height_in_array(std::vector<int>, int);
int max_height_in_array(std::vector<int> arr, int size) {
int max = 0;
if ((size & 1) == 0) { // check for even size.
for (int i = 0 ; i < size ; i = i+2) {
if (((arr[i] * 12) + arr[i+1]) > max) {
max = ((arr[i] * 12) + arr[i+1]);
}
}
} else { // check for odd size.
for (int i = 0 ; i < (size - 1) ; i = i+2) {
if (((arr[i] * 12) + arr[i+1]) > max) {
max = ((arr[i] * 12) + arr[i+1]);
}
}
// special check for last remaining element at odd th index.
if ((arr[size-1] * 12) > max) {
max = (arr[size-1] * 12);
}
}
return (max);
}
int main() {
int test_cases, size, ele;
std::cout << "Enter test cases : " << std::endl;
std::cin >> test_cases;
while (test_cases--) {
std::vector<int> arr;
std::cout << "Enter the size of an array : " << std::endl;
std::cin >> size;
std::cout << "Enter the elements in an array : " << std::endl;
for (int i = 0 ; i < size ; i++) {
std::cin >> ele;
arr.push_back(ele);
}
int result = max_height_in_array(arr, size);
std::cout << "Maximum height in the given array is : " << result
<< std::endl;
}
return (0);
}