Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/*Given a sequence of integers as an array(vector), determine whether it is possible to obtain a strictly
 * increasing sequence by removing no more than one element from the array.*/

#include <iostream>
#include <vector>
using namespace std;
bool almostIncreasingSequence(std::vector<int> sequence)
{
    int count=0;
    int i=0;

    while(i<sequence.size())
        {
            if(sequence[i]>sequence[i+1])
                {
                    count++;
                }
            i++;

        }
        if(count>1)
            {
            return false;}
        else
            {
                return true;
            }
            i++;
}


int main()
{
    vector<int>arr{1,6,5,8,9};
    cout<<almostIncreasingSequence(arr)<<endl;


}


What I have tried:

I am trying to compile this code correctly, but I don't understand what is my mistake
Posted
Updated 14-Oct-22 10:00am
Comments
OriginalGriff 25-Oct-17 3:19am    
What's the error?
Where is it showing?
Member 13277493 25-Oct-17 4:33am    
this is a problem in codefights website, they are some tests that this problem does not pass.

i solved using this php code

function solution($sequence) {
$count = count($sequence);
$c_copy = $count;
$i=0;
$step = 0;

while($i<=$count-2){
if($count==1 || $count==2 && $count==$c_copy){
return true;
}
if($sequence[$i]>=$sequence[$i+1]){
if($i+1==$count-1 || $sequence[$i-1]>=$sequence[$i+1]){
unset($sequence[$i+1]);
$temp = array_values($sequence);
$sequence = $temp;
$step++;
$count--;
$i--;
if($step==2){
return false;
}
}
else{unset($sequence[$i]);
$temp = array_values($sequence);
$sequence = $temp;
$step++;
$count--;
$i--;
if($step==2){
return false;
}
}

}
else if($sequence[$i]<$sequence[$i+1]){
$i++;
}
if($i==$count-2 && $sequence[$i]<$sequence[$i+1]){
return true;
}
}
}
 
Share this answer
 
bool almostIncreasingSequence(std::vector<int> sequence) {
if (sequence.size() == 1) return true;

int count = 0;
for (int i = 0; i < sequence.size() - 1; i++)
{
if (sequence[i + 1] <= sequence[i])
{
if (sequence[i+1] > sequence[i-1] || i == 0)
sequence.erase(sequence.begin() + i);
else if (sequence[i+1] <= sequence[i-1])
sequence.erase(sequence.begin() + (i+1));
break;
}
}
for (int i = 0; i < sequence.size() - 1; i++)
{
if (sequence[i + 1] <= sequence[i])
return false;
}

return true;

}
 
Share this answer
 
Comments
Stefan_Lang 27-Apr-21 3:15am    
This question is many years old. Unless you have to add something really important, please do not answer such old postings.

More importantly, a code dump without explanations is not a solution! If you wish to provide any help at all, the most important thing is explain. Providing code is merely an afterthought to help illustrate your explanation.
You immediate problem is
Quote:
while(i<sequence.size())
You have to change it to
C++
while(i < (sequence.size() - 1) )


However that's not enough.
First of all you have to replace > with >= in comparisons (because it should be a strictly increasing sequence), then you shoud try to remove either sequence[i] or sequence[i+1], accepting the first one that preserves locally the incresing property. Lastly 'you can work on points of style' e.g. return false whenever count>1 instead of waiting the end of the loop.
 
Share this answer
 
Comments
Member 13277493 25-Oct-17 5:00am    
#include <iostream>
#include <vector>
using namespace std;
bool almostIncreasingSequence(std::vector<int> sequence)
{
int count=0;
int i=0;

while(i<sequence.size()-1)
{
if(sequence[i]>=sequence[i+1])
{
count++;
sequence.erase(sequence.begin()+(i+1));
}
i++;

if(count>1)
{
return false;}
}

if(count<=1)
{
return true;
}

}


int main()
{
vector<int>arr{1,6,5,8,9};
cout<<almostIncreasingSequence(arr)<<endl;


}
is this the right code?
jeron1 25-Oct-17 10:01am    
Does it work?
Member 13277493 25-Oct-17 12:10pm    
It works, but some tests are not working correctly.
CPallini 25-Oct-17 12:23pm    
Because you always remove the (i+1)th item. AS I stated you should also try to remove the ith item.
Member 13277493 25-Oct-17 13:24pm    
sorry if my question is stupid but I wrote like this, this added true mark for also one test, I don't fully understand what happens with the array
bool almostIncreasingSequence(std::vector<int> sequence)
{
int count=0;
int i=0;

while(i<(sequence.size()-1))
{

if(sequence[i]>sequence[i+1])
{

sequence.erase(sequence.begin()+(i+1));
count++;

}
if(sequence[i]==sequence[i+1])
{
sequence.erase(sequence.begin()+i);
count++;
}
i++;


if(count>1)
{
return false;}
}

if(count<=1)
{
return true;
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900