O(nlogn)
1. precompute the minimum of [ i+1 ....N] and store in b[i]
2. Now do a binary search for a[i] in b[i+1] in the range of  b[i+1..... N]

On Wed, Jul 27, 2011 at 12:27 PM, salvador_cerinza <
vishwakarma.ii...@gmail.com> wrote:

> Let say stack S.
> 1.insert elements in S of A[] from right to left.
> 2.int val = S.top();
> 3.S.pop();
> 4.now check val with S.top() until u find any element smaller than val.
> 5.Note down the element pop it from stack
> 6.if step 4 is true , the push val in stack S and all elements which were
> popped in the order they were popped except the last matched candidate
> element.
>
> Yeah..dis algo is not very efficient..
>
>
> On Wed, Jul 27, 2011 at 12:20 PM, Pankaj <jatka.oppimi...@gmail.com>wrote:
>
>> Can you please elaborate a little about your stack based solution. I was
>> thinking of using queue but was unable to make a perfect algo.
>>
>>
>> On Wed, Jul 27, 2011 at 12:18 PM, salvador_cerinza <
>> vishwakarma.ii...@gmail.com> wrote:
>>
>>> i m  suggesting stack  not just for best case only .
>>>
>>>
>>> On Wed, Jul 27, 2011 at 12:16 PM, Pankaj <jatka.oppimi...@gmail.com>wrote:
>>>
>>>> Even in array best case can be O(n). Why use stack?
>>>> On Wed, Jul 27, 2011 at 12:14 PM, salvador_cerinza <
>>>> vishwakarma.ii...@gmail.com> wrote:
>>>>
>>>>> Best case : O(n)
>>>>> Worst case : O(n^2)
>>>>> can be done using stack.
>>>>>
>>>>> Thinking of better solution. .
>>>>>
>>>>>
>>>>> On Wed, Jul 27, 2011 at 11:50 AM, ankit sambyal <
>>>>> ankitsamb...@gmail.com> wrote:
>>>>>
>>>>>> O(n^2) algo is trivial. Can anybody think of a better approach ???
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Algorithm Geeks" group.
>>>>>> To post to this group, send email to algogeeks@googlegroups.com.
>>>>>> To unsubscribe from this group, send email to
>>>>>> algogeeks+unsubscr...@googlegroups.com.
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>
>>>>>
>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Algorithm Geeks" group.
>>>>> To post to this group, send email to algogeeks@googlegroups.com.
>>>>> To unsubscribe from this group, send email to
>>>>> algogeeks+unsubscr...@googlegroups.com.
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to algogeeks@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> algogeeks+unsubscr...@googlegroups.com.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
regards,
chinna.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to