-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMaximum_In_Array.cpp
97 lines (85 loc) · 2.17 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
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 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.
*/
#include <bits/stdc++.h>
using namespace std;
int max_height_in_array(std::vector<int>, int);
int max_height_in_array(std::vector<int> arr, int size)
{
int max = 0;
// check for even size.
if ((size & 1) == 0)
{
for (int i = 0; i < size; i = i + 2)
{
if (((arr[i] * 12) + arr[i + 1]) > max)
{
max = ((arr[i] * 12) + arr[i + 1]);
}
}
// check for odd size.
}
else
{
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);
}
/*
Time complexity : O(n)
Space complexity : O(n)
*/
/*
Test Case ;
Input:
Enter test cases :
2
Enter the size of an array :
4
Enter the elements in an array :
1 2 2 1
Enter the size of an array :
8
Enter the elements in an array : "
3 5 7 9 5 6 5 5
Output:
Maximum height in the given array is :
25
Maximum height in the given array is :
93
*/