So, you say the problem is that on some inputs you'd run into infinite (non ending) program?Sergiu Dobozi wrote:The non-working case is a hidden case so I have no idea what case where I would have an infinite loop.
Junilu Lacar wrote:Why do you assume it's an infinite loop that's the problem? Any array with less than three elements will probably fail a test because it will result in your sorted() method returning true. I doubt that a zero- or one-element array should be considered as an increasing sequence. To tell if you have a strictly increasing sequence, you need at least two elements, don't you?
I think it's your sorted method that's the problem.
Liutauras Vilda wrote:Thinking a bit in a rush now, but i think you just need to find first element which is smaller than previous, then skip that and check if the rest elements are greater than the one before you skipped, and that is it I think.
[edit] hm.. that might won't work in some cases.. don't know about that now
Junilu Lacar wrote:Liutauras is correct about long arrays. I tried with 50K numbers and it took a few seconds to complete on my Mac. This code will eventually fail for long arrays, depending on how impatient the test harness is.
There are three kinds of actuaries: those who can count, and those who can't.
That's not true.Sergiu Dobozi wrote:This doesn't work for cases like when we have an array such as {1, 2, 3, 4, 99, 5, 6} or {1000, 400, 500, 600} because it would return false rather than true.
So, at best it is O(N), mine described algorithm above at worst O(N).Piet Souris wrote:Another way, find the two biggest elements and write down their indices..And what if these indices are not the highest two indices?..That will give you a O(N) solution
Sergiu Dobozi wrote:
This doesn't work for cases like when we have an array such as {1, 2, 3, 4, 99, 5, 6} or {1000, 400, 500, 600} because it would return false rather than true.
Dave Tolls wrote:
Sergiu Dobozi wrote:
This doesn't work for cases like when we have an array such as {1, 2, 3, 4, 99, 5, 6} or {1000, 400, 500, 600} because it would return false rather than true.
But it's a start point.
There are two situations, both of which can be initially spotted using Liutauras' suggestion.
10,20,30,20,40,50
in which you want to remove the low value, which can be spotted by the fact that 30 < 40, so 20 is the odd one out.
and your example
1, 2, 3, 4, 99, 5, 6
in which you spot the issue on the '5', but because 99 > 6 then it's the 99 that goes.
That should be a pretty simple check.
So, search as per Liutauras' idea, then check either side of the first value you hit where currentValue < previousValue...if previousValue < nextValue then remove currentValue...if previousValue > nextValue, remove previousValue.
There's probably other issues, but it's a start.
Sergiu Dobozi wrote:This is my implementation:
Sergiu Dobozi wrote:
I will have to try out everyone's suggestions as soon as possible.
If there is a case where {1000, 20, 30, 40} and I have to check 1000 with a previous value, I can't do that because I would have to compare with sequence[-1]. So is this method really possible to implement?
Dave Tolls wrote:I always suggest getting the basics down first (Junilu's tests are good for that). Handle the edge cases once you've handled the general case.
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Carey Brown wrote:
What is meant by "strictly"? (I'm assuming I can ignore this.) Does each number have to be 1 more than the one before it? (assume: no)
Does the interval between successive pairs have to be the same? (assume: no) Is a single number, e.g. [1], a "sequence"? (assume: no)
[1, 2, 4, 100, 4] // I disagree w/ Junilu on this one, I'd drop the last '4'[/code]
I'm wondering if anyone disagrees with my test cases? Obviously Junilu and I disagree on one of them.
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|