Result of Long Positive Integers & Search and element in array
I have two Questions for which I cannot find answers by googling, but I find these questions very important for preparation.. Kindly explain only the logic, I will be able to code.
In Search of Efficient Logic..... in terms of Memory and Time.
WAP to add two long positive integers. What Data structure / data type we can use to store the numbers and result.
What is the best way to search an element from an a开发者_开发问答rray in shortest time. Size of the array could be large enough, and any elements could be stored in the array(i.e. no range).
Thanks.
A simple array is fine for storing long numbers, then the logic for addition follows naturally.
3 byte arrays would work well, two for the numbers to be added and one for the result.
The fastest way to search an element in an array would be some sort of Binary Search, so long as the array is sorted
Since it`s mentioned the numbers are large enough a linked list where each node is based on the indices of the digits within a number. A traversal through a single list can help us with the solution.
If it`s sorted then Binary search would be apt , but if not Hash table would be the best pick as it takes constant time.
精彩评论