previous_mid
previous_mid
one. Since these are practice problems for an exam, I’ll provide solutions
in C++ that leverage the concepts you’ve been studying (`sort`,
`reverse`, `find`, `substr`, `bool`, `vector`, `pair`, etc.). I’ll also keep the
solutions concise, efficient, and well-commented for your cheat sheet.
Let’s go through each problem systematically.
---
#### Solution
We can use the `reverse` function from the `<algorithm>` library, which
you’ve already studied.
```cpp
#include <iostream>
#include <algorithm>
int main() {
reverseArray(arr, n);
return 0;
```
#### Notes
```cpp
```
---
**Task**: Check if an array reads the same forwards and backwards (e.g.,
`[2, 3, 5, 5, 3, 2]` is a palindrome).
#### Solution
We can compare elements from the start and end, moving inward.
Alternatively, we can create a copy of the array, reverse it, and compare.
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
return true;
int main() {
return 0;
```
#### Notes
```cpp
reverse(copy.begin(), copy.end());
return true;
```
---
**Task**: Split an array into two halves (e.g., `[1, 2, 3, 4, 5, 6, 7, 8]` splits
into `[1, 2, 3, 4]` and `[5, 6, 7, 8]`).
#### Solution
We’ll split the array into two parts at the middle index. Since the problem
doesn’t specify output format, we’ll print the two halves.
```cpp
#include <iostream>
// First half
// Second half
int main() {
splitArray(arr, n);
// Output:
// First half: 1 2 3 4
// Second half: 5 6 7 8
return 0;
```
#### Notes
- **If \(n\) is odd**: The first half gets \(\lfloor n/2 \rfloor\) elements, the
second gets the rest.
```cpp
```
---
### **Problem 4: Rotate Array Left by \(d\) Positions**
#### Solution
```cpp
#include <iostream>
#include <algorithm>
d = d % n; // Handle d > n
if (d == 0) return;
int main() {
int d = 2;
rotateLeft(arr, n, d);
return 0;
```
#### Notes
---
**Task**: Find two elements in an array whose sum is closest to zero (e.g.,
`[5, -5, 10, -15, 20]` → `-5, 5`).
#### Solution
Sort the array and use two pointers to find the pair with the smallest
absolute sum.
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
minSum = sum;
return result;
int main() {
return 0;
```
#### Notes
- **Test Cases**:
---
#### Solution
Use two arrays to store the maximum height to the left and right of each
bar, then calculate trapped water.
```cpp
#include <iostream>
leftMax[0] = arr[0];
int water = 0;
return water;
int main() {
return 0;
```
#### Notes
- **Test Cases**:
- `[7, 4, 0, 9]` → 10
- `[6, 9, 9]` → 0
- `[3, 0, 0, 2, 0, 4]` → 10
---
### **Problem 7: Roadtrip Adventure**
**Task**: Given locations and gas costs, calculate how many cycles you
can complete with a 50-liter gas tank, starting from a chosen index,
moving rightwards in a cyclic array.
#### Solution
```cpp
#include <iostream>
int cycles = 0;
cout << "Number of cycles completed: " << cycles << endl;
cout << "Gas left: " << tank << " Litres" << endl;
}
int main() {
int n = 5;
return 0;
```
#### Notes
- **Output for Example** (with costs `[10, 20, 15, 25, 30]`, start at index
2):
```
starting index: 2
Starting at location: C
Final Location: B
```
```cpp
int tank;
```cpp
return;
```
---