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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now