| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
![]() Hi guys, I am supposed to find the maximum element of an array. If there are more than one then get rid of all of them. And of course teh rest of the data is supposed to left shift. I can't work it out.... inpu : 78, 3, 45, 89, 34, 89, 2, 3, 12 ouyput : 78, 3, 45, 34, 2, 3, 12 |
|
#2
|
|||
|
|||
|
Hey neo_darko!
Look, It occurs to me, that you could work out a recursive function (or a simple loop). Code:
1) Get the biggest number 2) search for it to check if there's more than one of them 2.1) if found more than 1 2.1.1) delete them from the array (and shift the numbers) 2.1.2) Call function (recursive) 2.2) else 2.2.1) Inform the array IF I understood correctly, that's what you should do...the code, I leave it up to you. I only suggest you use some extra functions to get the number, search for it, and to erase it. Good Luck... Anibal. |
|
#3
|
|||
|
|||
|
Quote:
The bold text is not clear. You mean to remove elements with same values? |
|
#4
|
|||
|
|||
|
Hey Cirus!
I assumed that. I mean, for instances: the 89 is the maximum number, but since it apeares twice in the array, it must be removed. If another 78 was to be found, It would be removed as well. I think, it's like highlander: there can be only one (maximum) |
|
#5
|
|||
|
|||
|
Quote:
Hello Anibal. One off-track question. If user gives a big array , supposingly of 1000 numbers, don't you think the algo will be time consuming. I mean suppose if two identical numbers appear, but their positions are far from each other. Say one occuring at 2nd position and next occuring at 998th position. With the current approach, lots of time will be wasted. Can you suggest a better method?? PS: Do not take my words otherwise. I am not saying wrong to our approach. |
|
#6
|
|||
|
|||
|
I suggest to scan array using shell sort & bucket sort combination.
i.e: 1. Use shell sort to arrange numbers. 2. Re-sort the same using bucket sort. 3. Count the number of occurances of each number.If |bucket| > 1, remove the number. GoTo step1. Else print result. My approach will fail in case when each item is unique.At then lots of space will be wasted. Any comments Anibal and others??????? |
|
#7
|
|||
|
|||
|
Hey Cirus! Yes..the algorithm is in deed ineficient. The problem is that I don't know if you're allowed to sort the array. For what I saw, you're not allowed to do that. If you're not allowed to reorder the array, there's nothing left to do but what I have suggested.
If the array could be ordered, that's a different story! I would order the array with any method (each has it's own flaw...quicksort, bubblesort,...etc). Then, Remove those elements which are at the beginning of the array (should they be the biggest). That's as simple as it gets!. I agree that eficient algorithms are allways best..but sometimes, given the processing power of todays computers (and the magnitud of the proyect, which happens to be a simple school asignment), it more a waste of time, to try and optimize it!. Anyway, You were absolutly right about it and it's good that you presented an optimization of the algorithm. Just a piece of advice (no ofense): a modification which causes the algorithm to fail in its ability to function in all cases for which it was designed, is not an optimization. Nevertheless, thanks for the remark!! Good Luck and keep it up!! Anibal. |
|
#8
|
|||
|
|||
|
Yes, I agree. But the algo. would perform better if there are repeated occurances of more than one items.
Thanks for discussing this. |
|
#9
|
|||
|
|||
|
Any time man! Discussing is the path to understanding and therefore....perfection!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Multiple Max Element |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|