Click here to Skip to main content
15,063,556 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 26-Apr-21 9:02am
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.

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.
   
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;
}

}
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;

}
   
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.

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