开发者

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.

  1. WAP to add two long positive integers. What Data structure / data type we can use to store the numbers and result.

  2. 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


  1. 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.

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜