?
Solved

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

Posted on 2011-09-26
3
Medium Priority
?
314 Views
Last Modified: 2013-12-13
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
Comment
Question by:dvcphp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36650705
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
 
LVL 6

Accepted Solution

by:
neorush earned 2000 total points
ID: 36653420
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
 

Author Closing Comment

by:dvcphp
ID: 36658073
Thanks so much.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question