• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Give an array of integers, return a boolean to indicate there exists two numbers in that array that would sum to zero".

HI Experts,
I need help in solving this question using PHP.

Give an array of integers, return a boolean to indicate there exists two numbers in that array that would sum to zero".
0
dvcphp
Asked:
dvcphp
1 Solution
 
Hugh McCurdyCommented:
Is this a homework assignment?  If so, I'm happy to help you but first you need to do some work.  Make an attempt at writing the code and post it here and we can help you along the way.
0
 
neorushCommented:
This sounds a little like your homework...I hope its not as you should learn to do this yourself.  You just have to see if there are two numbers that are inverses of each other, since this is the only way they can add up to zero.  e.g. The number you are looking for times negative 1
<?php

function inverseExists($int_array){
	foreach($int_array as $value){
		if(in_array($value*(-1), $int_array)) return true;
	}
	return false;
}
$my_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, -6);
if(inverseExists($my_array)) echo 'yes!';
else echo 'no!';

?>

Open in new window

0
 
dvcphpAuthor Commented:
Thanks so much.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now