srinivasa Avvaru

Greenhorn

Posts: 4

posted 7 years ago

suppose i have sorted list which has integers then write a program that any of 2 integer sum is equal to x(which we give input)?

Ex: i have 1,2,3,4,5 in list i have gave x as 9 then i need to write a program that the number 9 i s equal to any of the integers.Her in that case(4+5=9) so i need this program for this logic?

Ex: i have 1,2,3,4,5 in list i have gave x as 9 then i need to write a program that the number 9 i s equal to any of the integers.Her in that case(4+5=9) so i need this program for this logic?

posted 7 years ago

Hi. The ranch is NotACodeMill. You'll have to DoYourOwnHomeWork. First, show us what you have managed to write.

so i need this program for this logic?

Hi. The ranch is NotACodeMill. You'll have to DoYourOwnHomeWork. First, show us what you have managed to write.

[My Blog]
*All roads lead to JavaRanch*

srinivasa Avvaru

Greenhorn

Posts: 4

posted 7 years ago

You'll need a nested loop. Basically, for each number you combine it with each other number. You can make it a bit faster by using the fact that + is associative*. You'll only need to combine each number with numbers located nearer the end; after all.

* if x + y == z then y + x == z

* if x + y == z then y + x == z

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions