## DEV Community

β¨ thetealpickle π±

Posted on

# [C++ CODE] Almost Increasing Sum

THE TEAL PICKLE CODING CHALLENGE!! Determine whether the sequence is almost increasing. I solved this problem with bae (C++). TRY IT π

Check out my solution and share yours!! ~ π» π»

Jaydeep Pipaliya
#include <vector>
#include <iostream>

bool canBeIncreasing(std::vector<int>& sequence) {
int count = 0;
for (int i = 1; i < sequence.size(); ++i) {
if (sequence[i] <= sequence[i - 1]) {
count++;
if (count > 1) {
return false;
}
if (i > 1 && i < sequence.size() - 1 && sequence[i - 2] >= sequence[i] && sequence[i - 1] >= sequence[i + 1]) {
return false;
}
}
}
return true;
}

int main() {
std::vector<int> sequence1 = {1, 3, 2, 1};
std::vector<int> sequence2 = {1, 3, 2};
std::vector<int> sequence3 = {1, 2, 1, 2};
std::vector<int> sequence4 = {10, 1, 2, 3};

std::cout << canBeIncreasing(sequence1) << std::endl; // Output: 0 (false)
std::cout << canBeIncreasing(sequence2) << std::endl; // Output: 1 (true)
std::cout << canBeIncreasing(sequence3) << std::endl; // Output: 0 (false)
std::cout << canBeIncreasing(sequence4) << std::endl; // Output: 1 (true)

return 0;
}

Initial Solution: O(n2) time complexity due to nested loops and repetitive checks.
Optimized Solution: O(n) time complexity due to a single pass through the sequence and constant time checks.