Difficult computer science problem

This topic is 4367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Does anyone know an algorithm that recieves an unknown amount of numbers(it stops when it recieves -1) and calculates how many of them are above the average without using an array, list, vector, stack, file ect.? Is it even possible?

Share on other sites
so this algorithm can't read/receive the numbers from an array or a file? well then how are you sending the numbers to the program? manual input?

Share on other sites
The term you are looking for is "constant space".

This problem is provably unsolvable in constant space. Assume for a moment that we have inserted all integers between 1 and (N-1) into the program. The last integer we insert can result in any number of integers between N/2 and (N-1) being lower than the average (we can insert, to this effect, any number between 0 and N²). Therefore, the program must store enough information to determine which of the N/2 possibilities is valid, which cannot be done for any N if we restrict ourselves to constant space.

Share on other sites
I'm assuming you can hold variables. Can't you have a variable that's initialised to zero. Then if the inputted number is greater than the average then increase the variable. If it's -1 then quit. Or have I missed an obvious problem?

Share on other sites
This sounds suspiciously like homework. All I'll say is that it can be solved within the constraints listed using a technique that teachers/professors love to set homework problems about.

EDIT: This does depend on the interpretation of the list of constraints.

Σnigma

Share on other sites
The obvious problem is that you need to know all the numbers to get the average.

It can be done if you iterated the sequence twice, once to find the average and again to count how many are higher. Without storing them, you'd have the input the whole sequence twice.

Share on other sites
Quote:
 Original post by DeyjaThe obvious problem is that you need to know all the numbers to get the average.It can be done if you iterated the sequence twice, once to find the average and again to count how many are higher. Without storing them, you'd have the input the whole sequence twice.

Ah, I misread it. I thought it was a given average as opposed to the average of the numbers inputted.

Share on other sites
Well..I'm curious. Maybe the OP can let us know when teh homework gets turned in so someone can tell me what this is all about :)

Share on other sites
Quote:
 Original post by EnigmaThis sounds suspiciously like homework. All I'll say is that it can be solved within the constraints listed using a technique that teachers/professors love to set homework problems about.EDIT: This does depend on the interpretation of the list of constraints.Σnigma

what is "interpretation of the list of constraints"?

Share on other sites
Quote:
 Original post by Semion_Nwhat is "interpretation of the list of constraints"?

It is the interpretation of the ect (sic) in the OP's question. Does his question mean to rule out any memory storage (constant space solving, which is provably impossible)? Does his question mean to rule out only the standard imperative storage approaches (hinting at an easily expressed recursive solution)?

I've interpreted it as the first option, since a recursive solution technically uses a stack (unless it uses tail recursion, but then the problem becomes provably unsolvable again). If it's homework, then the recursive solution would be a good answer, though.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 33
• 13
• 13
• 10
• Forum Statistics

• Total Topics
632582
• Total Posts
3007216

×