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
313 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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…
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.

728 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