Use the start method to find an item in the list where some of the adjacent elements are less than 72. If there are many such elements, then find the largest of them; if such an element does not exist - output the corresponding information.
#include <iostream>
#include <cstdlib>
using namespace std;
const int SPECIAL = 72;
void start(int* arr, int len) {
int counter = 0;
int max;
for (int i = 0; i < len; i++) {
if (
(i == 0 && arr[i + 1] < SPECIAL) ||
(i == len - 1 && arr[i - 1] < SPECIAL) ||
(arr[i - 1] < SPECIAL || arr[i + 1] < SPECIAL)
) {
if (counter == 0) max = arr[i];
else if (arr[i] > max) max = arr[i];
counter++;
}
}
if (counter == 0) cout << "Element does not exist!" << endl;
else cout << "Element - " << max << endl;
}
int main() {
srand(time(NULL));
int randomLength = rand() % 11 + 10; // 10-20
int* arr = new int[randomLength];
cout << "List:" << endl;
for (int i = 0; i < randomLength; i++) {
arr[i] = rand() % 100; // 0-99
cout << arr[i] << " ";
}
cout << endl;
start(arr, randomLength);
return 0;
}
Comments
Leave a comment