Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Coding puzzle (4)
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00665368
Message ID:
00665754
Vues:
23
Oops, should of read the rest of the thread first.
Already been answered using my solution ;)

I didnt think to check all the way through the replies, figured if there was one, it would be in the top layer or replies.



>>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.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform