Algorithm PLEASE help :)

Hi I'm new in Java and I need to find algorithm for these questions :

1. Write a program that reads a number n and sequence of n numbers and finds in the sequence the longest area of adjacent equal elements . Example :

n = 14

sequence = {7,3,3,3,7,7,7,7,3,3,3,2,7,2}

result = {7,7,7,7}

2. Write a program that reads from the console a sequence of numbers and finds the longest subsequence of growing numbers. Example :

sequence = {3, 1, 2, 4, 6, 2, 3, 5, 4, 5}

result = {1, 2, 3, 4, 5}

I'll be very thankful if you write me one of the algorithms

[605 byte] By [n3m0a] at [2007-11-27 1:21:38]
# 1
> I'll be very thankful if you write me one of the> algorithmsI will write them both for you. I need to fit them into my schedule so when do you need them by?
sabre150a at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 2
The second one is at least an interesting problem. I'm thinking dynamic programming.
YAT_Archivista at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 3

> The second one is at least an interesting problem.

> I'm thinking dynamic programming.

I was just thinking 'brute force'. Am I missing something?

Edit: I just realized I was missing something! Not as easy as I thought.

Further edit: It is as easy as I thought but not the same as I originally thought. Brute force will still not be too bad. N factorial comparisons.

Message was edited by:

sabre150

sabre150a at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 4

It's deceptively hard. I keep feeling it should be possible in O(n) but spotting errors in my ideas for doing it linearly. And I've just spotted a flaw in my idea for a O(n lg n) solution.

The idea there was one forward pass, in which you record for each index the index of the largest (that's the flaw) smaller element already encountered, and the chain length given by that link. Then work backwards down the longest chain.

The brute force algorithm is quadratic, not factorial. Each iteration of the outer loop extends the section of the array under consideration by one.

YAT_Archivista at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 5

> The brute force algorithm is quadratic, not

> factorial. Each iteration of the outer loop extends

> the section of the array under consideration by one.

You are right! Quadratic!

Edit: It is actually quite tricky. My first attempt did not work because I assumed that no backtracking was needed. Backtracking is needed (at least in my approach it is)! Ho Hum!

Message was edited by:

sabre150

sabre150a at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 6

Construct a map of the longest sequences with the least last added value, so if you have

6421

321

And encounter 5, you find 321 in lgM, where M is the size of the table, insert 5321, but when inserting it look at the next greater node, see whether it's the same length, and remove it if so, as any value which would extend it would also extend the sequence with the lower last added value you just added. Adjacent nodes must be be one number longer, otherwise there would be an intermediate sequence. You also look at the next least sequence, and if it's last value is your value -1 then delete it, as there can't be a sequence which extends it by one which has a lower next value:

6421

321

+5 =>

6421

+5321

321

eliminate above as same length (above will always be same length, if you've added one value to the longest sequence whose last added value is less than the new value)

=>

5321

321

+4 =>

5321

+4321

321

eliminate above, and eliminate below as consecutive values

=>

4321

So that would be O(N lgM), where M is the maximum size of the table. You can also reject any values less than or equal to the last value in the least long sequence as they won't contribute, so run time will typically be better, but I'm not about to calculate by how much. You do need to maintain all of the best sequences whose last value is less than a value which can be extended to be longer than any other sequence extended by adding a given new value, so I don't think O(N) is possible. If you have two numbers remaining, you don't know to choose between 54321 and 754321 until you get 6,7 or 8,3. The question is what size M is - if the sequence is increasing by one each time, M=1. If it's the same value repeated, or decreasing, M=1. For the sample input sequence, M gets up to 3:

+3 = {3}

+1 = {3},{1} -> {1}

+2 = {2,1},{1} -> {2,1}

+4 = {4,2,1},{2,1}

+6 = {6,4,2,1},{4,2,1},{2,1}

+2 = {6,4,2,1},{4,2,1},{2,1}

+3 = {6,4,2,1},{4,2,1},{3,2,1},{2,1} -> {6,4,2,1},{3,2,1}

+5 = {6,4,2,1},{5,3,2,1},{3,2,1} -> {5,3,2,1},{3,2,1}

+4 = {5,3,2,1},{4,3,2,1},{3,2,1} -> {4,3,2,1}

+5 = {5,4,3,2,1},{4,3,2,1} -> {5,4,3,2,1}

Since M is increased for every value encountered greater than the previous greatest value, and decreased for every value adjacent to an existing value. You'd need to know the range of possible values to determine it, but at worst case M = N for sequences such as a[i] = 2*i.

pm_kirkhama at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 7
My stored sequences are reversed in the above, if you hadn't noticed and were wondering. (it's easiest to build them by pushing to a singly linked list)
pm_kirkhama at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 8
Just thought you folks should also see this crosspost by the OP,and the interesting interaction between the OP and the volunteer helpers: http://forum.java.sun.com/thread.jspa?threadID=5161687&tstart=0
KathyMcDonnella at 2007-7-11 23:59:33 > top of Java-index,Other Topics,Algorithms...
# 9

> Just thought you folks should also see this crosspost by the OP,

> and the interesting interaction between the OP

> and the volunteer helpers:

> ...

I think all will be OK. The people currently posting in this thread are hardcore algorithm/math cracks and they're probably not posting here to give the little ****** a hand, but are interested in the problem.

So if (pseudo) code will be posted by any of them here, then:

- the OP will definitely not understand it;

- the OP's teacher will see in the blink of an eye (might he copy-n-paste it) he copied it from somewhere.

prometheuzza at 2007-7-11 23:59:34 > top of Java-index,Other Topics,Algorithms...
# 10

> Just thought you folks should also see this crosspost

> by the OP,

> and the interesting interaction between the OP

> and the volunteer helpers:

>

> http://forum.java.sun.com/thread.jspa?threadID=5161687

> &tstart=0

Kathy - I have played this game many times before. From your posts I know you are bright enough to understand.

sabre150a at 2007-7-11 23:59:34 > top of Java-index,Other Topics,Algorithms...