Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Coding puzzle (4)
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00665368
Message ID:
00665752
Views:
18
>For those who know about asymptotic behavior:
>
>Given an array of increasing numbers and a number X, create a linear algorithm that finds whether the array contains two numbers that sum to X (and those two numbers if they exist).
>
>Daniel


How about
LPARAMETER nFind

LOCAL nFront, nBack, nSum

DIMENSION a[10]
a[1]= 3
a[2]= 5
a[3]= 7
a[4]= 9
a[5]= 10
a[6]= 12
a[7]= 14
a[8]= 16 
a[9]= 18 
a[10]= 20

nFront= 1
nBack= ALEN(a)

nSum= a[nFront] + a[nBack]
DO WHILE nSum != nFind AND nFront != nBack
	IF nSum > nFind
		nBack = nBack - 1
	ENDIF
	IF nSum < nFind
		nFront= nFront + 1
	ENDIF
	nSum= a[nFront] + a[nBack]
ENDDO

IF nFront != nBack
	? a[nFront], a[nBack]
ELSE
	? "Not Found"
ENDIF
Keep a pointer to the front, and one to the back, If the sunm is greater, move the back pointer in one, if the sum is less move the front pointer in one.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform