ask for a algorithm about visit array
-
There is a int array consists of 1001 elements. Inside it, only two element's values are equal. I mean that there are two items have the same value, and the other items are different. Design a algorithm according to the following commands: 1 every item of the array can only be visited once 2 find out the same value many thanks
-
There is a int array consists of 1001 elements. Inside it, only two element's values are equal. I mean that there are two items have the same value, and the other items are different. Design a algorithm according to the following commands: 1 every item of the array can only be visited once 2 find out the same value many thanks
Good luck. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
There is a int array consists of 1001 elements. Inside it, only two element's values are equal. I mean that there are two items have the same value, and the other items are different. Design a algorithm according to the following commands: 1 every item of the array can only be visited once 2 find out the same value many thanks
"every item of the array can only be visited once" I'll be interested to see if anyone comes up with something.
Mark Brock Click here to view my blog
-
"every item of the array can only be visited once" I'll be interested to see if anyone comes up with something.
Mark Brock Click here to view my blog
-
There is an easy way from the pov of the algorithm but depending on what they mean by integer it may be quite painful. Think tagging bits.
I agree: it strongly depends on their integer size.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I agree: it strongly depends on their integer size.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
There is a int array consists of 1001 elements. Inside it, only two element's values are equal. I mean that there are two items have the same value, and the other items are different. Design a algorithm according to the following commands: 1 every item of the array can only be visited once 2 find out the same value many thanks
Build a linked list (or tree, but for 1001 elements it would be overkill), visit the array one time for each element (or at least until you find the matching elements), build an entry containing the index of the value and the value at that index, link it in the linked list at the tail of the list (because you need to traverse the entire existing list checking for a match) and stop when you find a match. You then have the index of both values (one in the list and the last entry built) and the value, and have only accessed the list one time for each element.
-
What does it mean? Can you explain it in more details or make a simple example? many thanks
What is the range of your integers?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke