Answer:
A Simple Solution is to sort the array in increasing order. The first two elements in sorted array would be two smallest elements. Time complexity of this solution is O(n Log n).
A Better Solution is to scan the array twice. In first traversal find the minimum element. Let this element be x. In second traversal, find the smallest element greater than x. Time complexity of this solution is O(n).
The above solution requires two traversals of input array.
An Efficient Solution can find the minimum two elements in one traversal. Below is complete algorithm.
Algorithm:
Explanation:
<span>o,t,t,f,f,s,S,E,N,T,E,T </span>
<span>It's the first letter of all the numbers starting with one, two, three, four,... </span>
<span>The second pattern I might guess would be move the O back two spaces then move one space back, then repeat. </span>
<span>JKLMNO, JKLOMN, JKOLMN, OJKLMN, JKLMON, JKOLMN, ... </span>
<span>The second one I'm not sure about because there are only three starting terms to work with. It's hard to see the real pattern with that little to work with. </span>
<span>Hope this helps. </span>