But as mentioned in the question, space complexity is not the issue. but list should be transversed only once. And using two pointer that can't be done.
We can do this in one pass using a pointer and a queue of length 5
function 5th_from_tail( head )
s = queue length 5
s = Null
ptr = head
while ptr != Null
ptr = ptr -> next
element = s.pop_from_tail()
if element == Null
return "not enough elements"
External sorting algorithm, which sorts chunks that each fit in RAM, then merges the sorted chunks together. For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:
Read 100 MB of the data in main memory and sort by some conventional method, like quicksort.
Write the sorted data to disk.
Repeat steps 1 and 2 until all of the data is in sorted 100 MB chunks (there are 900MB / 100MB = 9 chunks), which now need to be merged into one single output file.
Read the first 10 MB (= 100MB / (9 chunks + 1)) of each sorted chunk into input buffers in main memory and allocate the remaining 10 MB for an output buffer. (In practice, it might provide better performance to make the output buffer larger and the input buffers slightly smaller.)
Perform a 9-way merge and store the result in the output buffer. If the output buffer is full, write it to the final sorted file, and empty it. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk until no more data from the chunk is available. This is the key step that makes external merge sort work externally -- because the merge algorithm only makes one pass sequentially through each of the chunks, each chunk does not have to be loaded completely; rather, sequential parts of the chunk can be loaded as needed.
courtesy :- wikipedia (en.wikipedia.org/wiki/External_sorting )
if list == :
pivot = list
lesser = quick_sort([x for x in list[1:] if x < pivot])
greater = quick_sort([x for x in list[1:] if x >= pivot])
return lesser + [pivot] + greater
sort_list = quick_sort(list)
It is explained very well here....
Pseudo code for Post Order Traversal without Recursion.....
Using Single Stack and single variable...
stack stc = empty
prev = null
curr = stc.top()
if curr == NULL
//reached at leaf Node
elseif curr->left == NULL && curr->right == NULL
//left child is already visited so push right
elseif curr->left == prev
//left & right both child visited so print value
elseif curr->right == prev
prev = curr
You are having here unreachable code. Because you have return statement in your conditional statement & it will satisfy anyone of them & calls return. So code after else statement will never gona execute.
I think this is simple Change problem, which can be solved by DP or recursion.
pseudo code for this will be....
Change(array, size, target)
best_change = 0
for c in target
best_change[c] = infinite
for i in size
if c > array[i]
best_change[c-array[i]] + 1 < best_change[c]
best_change[c] = best_change[c- array[i]] + 1
and array need not to be sorted....
Array....- Aks August 26, 2012
a long array...where index will be used as hashing.
and it will also take care of duplicate elements, because for that we can increase array[number]
1. enqueue and dequeue will be O(1)---> array[number]++ for enqueue; array[number]-- for dequeue
2. IsNumberPresent --> array[number] > 0 present else not
3. delete--> array[number] =0 to remove all duplicate of this number and itself