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
312 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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