Simple one
2 posters
Page 1 of 1
Simple one
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- Posts : 42
Join date : 2009-01-30
Age : 35
zero
*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
* 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
obama- Posts : 4
Join date : 2009-02-03
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|