mockers
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Simple one

2 posters

Go down

Simple one Empty Simple one

Post  shivang Wed Feb 25, 2009 6:52 pm

Let there be n integers in ascending order.design an O(n) algorithm such that this algorithm finds two integers from this list that adds upto zero otherwise it will say that there is no such pair of integers.
shivang
shivang

Posts : 42
Join date : 2009-01-30
Age : 35

Back to top Go down

Simple one Empty zero

Post  obama Sun Mar 01, 2009 10:46 am

*take two integers assign fist index of array to one integer say i & assign last index to another integer say j
* take the sum of values at these position sum=a[i]+a[j]
while(i<=j)
******if(sum<0)
******* then move forward means increment in i
******if(sum>0)
******* then move backward mean decrement in j
****** if(sum==0)
******* return i,j
*return false
Evil or Very Mad

obama

Posts : 4
Join date : 2009-02-03

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum