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
|
#include <algorithm>
#include <iostream>
template<typename T>
bool solveableSequence(const T& container) {
if (container.size() == 1) {
return true; //depends on whether or not a sequence with 1 element qualfies, otherwise change to false
}
std::size_t operations = 0;
decltype(std::begin(container)) last = std::begin(container);
for (auto begin = last, end = std::prev(std::end(container)); begin != end; ++begin) {
if (!(*last < *std::next(begin))) { //< is for strict ascending, <= is for loose ascending and you might wanna use <= for the return
++operations;
if (operations > 1) {
return false;
}
}
else {
last = begin + 1;
}
}
return operations == 1; // <= for already sorted data;
}
template<typename T>
void solveSequence(const T& container) {
std::cout << std::boolalpha << solveableSequence(container) << std::endl;
}
using initList = std::initializer_list<int>;
int main() {
solveSequence(initList{1, 2, 1, 2});
solveSequence(initList{8, 8, 8, 7, 7, 7});
solveSequence(initList{1, 2, 2, 3});
solveSequence(initList{1, 2, 3, 2, 3});
solveSequence(initList{1, 2, 3, 2, 4});
solveSequence(initList{1, 2, 3, 6, 5});
solveSequence(initList{9, 8, 7});
solveSequence(initList{1, 2, 3, 6, 5, 4});
}
| |