Posted on by Kalkicode
Code Mathematics

Two elements whose sum is closest to zero

"Two elements whose sum is closest to zero in an array" means finding any two numbers in the given array whose sum is closest to zero. In other words, the task is to find two numbers in the array whose sum is closest to zero, regardless of whether it is a positive or negative number. For example, if the given array is [4, 2, -1, 5, -7, 1], then the two elements whose sum is closest to zero are -1 and 1 because their sum is zero and they are the closest to zero compared to any other pair of numbers in the array.

``````/*
C program for
Two elements whose sum is closest to zero
*/
#include <stdio.h>

int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void closestZeroSum(int arr[], int n)
{
if (n <= 1)
{
return;
}
// Get value of first element
int x = arr[0];
// Get value of second element
int y = arr[1];
int sum = x + y;
// Execute the loop through by size of array
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (absValue(sum) > absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
}
}
// Display resultant pair
printf("\n (%d) + (%d) = %d", x, y, sum);
}
int main()
{
int arr1[] = {
4 , 7 , 8 , 2 , -6 , 12
};
int arr2[] = {
2 , 6 , 8 , -2 , 5
};
// Test A
int n = sizeof(arr1) / sizeof(arr1[0]);
closestZeroSum(arr1, n);
// Test B
n = sizeof(arr2) / sizeof(arr2[0]);
closestZeroSum(arr2, n);
return 0;
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````/*
Java program for
Two elements whose sum is closest to zero
*/
public class Test
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void closestZeroSum(int[] arr, int n)
{
if (n <= 1)
{
return;
}
// Get value of first element
int x = arr[0];
// Get value of second element
int y = arr[1];
int sum = x + y;
// Execute the loop through by size of array
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (absValue(sum) > absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
}
}
// Display resultant pair
System.out.println(" (" + x + ") + (" + y + ") = " + sum);
}
public static void main(String[] args)
{
int[] arr1 = {
4 , 7 , 8 , 2 , -6 , 12
};
int[] arr2 = {
2 , 6 , 8 , -2 , 5
};
// Test A
int n = arr1.length;
// Test B
n = arr2.length;
}
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Two elements whose sum is closest to zero
*/
class Test
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void closestZeroSum(int arr[], int n)
{
if (n <= 1)
{
return;
}
// Get value of first element
int x = arr[0];
// Get value of second element
int y = arr[1];
int sum = x + y;
// Execute the loop through by size of array
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (this->absValue(sum) > this->absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
}
}
// Display resultant pair
cout << " (" << x << ") + (" << y << ") = " << sum << endl;
}
};
int main()
{
int arr1[] = {
4 , 7 , 8 , 2 , -6 , 12
};
int arr2[] = {
2 , 6 , 8 , -2 , 5
};
// Test A
int n = sizeof(arr1) / sizeof(arr1[0]);
// Test B
n = sizeof(arr2) / sizeof(arr2[0]);
return 0;
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````// Include namespace system
using System;
/*
Csharp program for
Two elements whose sum is closest to zero
*/
public class Test
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void closestZeroSum(int[] arr, int n)
{
if (n <= 1)
{
return;
}
// Get value of first element
int x = arr[0];
// Get value of second element
int y = arr[1];
int sum = x + y;
// Execute the loop through by size of array
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (this.absValue(sum) > this.absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
}
}
// Display resultant pair
Console.WriteLine(" (" + x + ") + (" + y + ") = " + sum);
}
public static void Main(String[] args)
{
int[] arr1 = {
4 , 7 , 8 , 2 , -6 , 12
};
int[] arr2 = {
2 , 6 , 8 , -2 , 5
};
// Test A
int n = arr1.Length;
// Test B
n = arr2.Length;
}
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````package main
import "fmt"
/*
Go program for
Two elements whose sum is closest to zero
*/

func absValue(num int) int {
if num < 0 {
return -num
}
return num
}
func closestZeroSum(arr[] int, n int) {
if n <= 1 {
return
}
// Get value of first element
var x int = arr[0]
// Get value of second element
var y int = arr[1]
var sum int = x + y
// Execute the loop through by size of array
for i := 0 ; i < n ; i++ {
for j := i + 1 ; j < n ; j++ {
if absValue(sum) > absValue(arr[i] + arr[j]) {
// Get pair of closest to zero
x = arr[i]
y = arr[j]
// Change sum
sum = x + y
}
}
}
// Display resultant pair
fmt.Println(" (", x, ") + (", y, ") = ", sum)
}
func main() {

var arr1 = [] int {4, 7, 8, 2, -6, 12}
var arr2 = [] int {2, 6, 8, -2, 5}
// Test A
var n int = len(arr1)
closestZeroSum(arr1, n)
// Test B
n = len(arr2)
closestZeroSum(arr2, n)
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````<?php
/*
Php program for
Two elements whose sum is closest to zero
*/
class Test
{
public	function absValue(\$num)
{
if (\$num < 0)
{
return -\$num;
}
return \$num;
}
public	function closestZeroSum(\$arr, \$n)
{
if (\$n <= 1)
{
return;
}
// Get value of first element
\$x = \$arr[0];
// Get value of second element
\$y = \$arr[1];
\$sum = \$x + \$y;
// Execute the loop through by size of array
for (\$i = 0; \$i < \$n; ++\$i)
{
for (\$j = \$i + 1; \$j < \$n; ++\$j)
{
if (\$this->absValue(\$sum) >
\$this->absValue(\$arr[\$i] + \$arr[\$j]))
{
// Get pair of closest to zero
\$x = \$arr[\$i];
\$y = \$arr[\$j];
// Change sum
\$sum = \$x + \$y;
}
}
}
// Display resultant pair
echo(" (".\$x.
") + (".\$y.
") = ".\$sum.
"\n");
}
}

function main()
{
\$arr1 = array(4, 7, 8, 2, -6, 12);
\$arr2 = array(2, 6, 8, -2, 5);
// Test A
\$n = count(\$arr1);
// Test B
\$n = count(\$arr2);
}
main();``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````/*
Node JS program for
Two elements whose sum is closest to zero
*/
class Test
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
closestZeroSum(arr, n)
{
if (n <= 1)
{
return;
}
// Get value of first element
var x = arr[0];
// Get value of second element
var y = arr[1];
var sum = x + y;
// Execute the loop through by size of array
for (var i = 0; i < n; ++i)
{
for (var j = i + 1; j < n; ++j)
{
if (this.absValue(sum) > this.absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
}
}
// Display resultant pair
console.log(" (" + x + ") + (" + y + ") = " + sum);
}
}

function main()
{
var arr1 = [4, 7, 8, 2, -6, 12];
var arr2 = [2, 6, 8, -2, 5];
// Test A
var n = arr1.length;
// Test B
n = arr2.length;
}
main();``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````#    Python 3 program for
#    Two elements whose sum is closest to zero
class Test :
def absValue(self, num) :
if (num < 0) :
return -num

return num

def closestZeroSum(self, arr, n) :
if (n <= 1) :
return

#  Get value of first element
x = arr[0]
#  Get value of second element
y = arr[1]
sum = x + y
i = 0
#  Execute the loop through by size of list
while (i < n) :
j = i + 1
while (j < n) :
if (self.absValue(sum) > self.absValue(arr[i] + arr[j])) :
#  Get pair of closest to zero
x = arr[i]
y = arr[j]
#  Change sum
sum = x + y

j += 1

i += 1

#  Display resultant pair
print(" (", x ,") + (", y ,") = ", sum)

def main() :
arr1 = [4, 7, 8, 2, -6, 12]
arr2 = [2, 6, 8, -2, 5]
#  Test A
n = len(arr1)
#  Test B
n = len(arr2)

if __name__ == "__main__": main()``````

Output

`````` ( 7 ) + ( -6 ) =  1
( 2 ) + ( -2 ) =  0``````
``````#    Ruby program for
#    Two elements whose sum is closest to zero
class Test
def absValue(num)
if (num < 0)
return -num
end

return num
end

def closestZeroSum(arr, n)
if (n <= 1)
return
end

#  Get value of first element
x = arr[0]
#  Get value of second element
y = arr[1]
sum = x + y
i = 0
#  Execute the loop through by size of array
while (i < n)
j = i + 1
while (j < n)
if (self.absValue(sum) > self.absValue(arr[i] + arr[j]))
#  Get pair of closest to zero
x = arr[i]
y = arr[j]
#  Change sum
sum = x + y
end

j += 1
end

i += 1
end

#  Display resultant pair
print(" (", x ,") + (", y ,") = ", sum, "\n")
end

end

def main()
arr1 = [4, 7, 8, 2, -6, 12]
arr2 = [2, 6, 8, -2, 5]
#  Test A
n = arr1.length
#  Test B
n = arr2.length
end

main()``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0
``````
``````/*
Scala program for
Two elements whose sum is closest to zero
*/
class Test()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def closestZeroSum(arr: Array[Int], n: Int): Unit = {
if (n <= 1)
{
return;
}
// Get value of first element
var x: Int = arr(0);
// Get value of second element
var y: Int = arr(1);
var sum: Int = x + y;
var i: Int = 0;
// Execute the loop through by size of array
while (i < n)
{
var j: Int = i + 1;
while (j < n)
{
if (absValue(sum) > absValue(arr(i) + arr(j)))
{
// Get pair of closest to zero
x = arr(i);
y = arr(j);
// Change sum
sum = x + y;
}
j += 1;
}
i += 1;
}
// Display resultant pair
println(" (" + x + ") + (" + y + ") = " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Test = new Test();
var arr1: Array[Int] = Array(4, 7, 8, 2, -6, 12);
var arr2: Array[Int] = Array(2, 6, 8, -2, 5);
// Test A
var n: Int = arr1.length;
// Test B
n = arr2.length;
}
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````
``````import Foundation;
/*
Swift 4 program for
Two elements whose sum is closest to zero
*/
class Test
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func closestZeroSum(_ arr: [Int], _ n: Int)
{
if (n <= 1)
{
return;
}
// Get value of first element
var x: Int = arr[0];
// Get value of second element
var y: Int = arr[1];
var sum: Int = x + y;
var i: Int = 0;
// Execute the loop through by size of array
while (i < n)
{
var j: Int = i + 1;
while (j < n)
{
if (self.absValue(sum) > self.absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
j += 1;
}
i += 1;
}
// Display resultant pair
print(" (", x ,") + (", y ,") = ", sum);
}
}
func main()
{
let arr1: [Int] = [4, 7, 8, 2, -6, 12];
let arr2: [Int] = [2, 6, 8, -2, 5];
// Test A
var n: Int = arr1.count;
// Test B
n = arr2.count;
}
main();``````

Output

`````` ( 7 ) + ( -6 ) =  1
( 2 ) + ( -2 ) =  0``````
``````/*
Kotlin program for
Two elements whose sum is closest to zero
*/
class Test
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun closestZeroSum(arr: Array < Int > , n: Int): Unit
{
if (n <= 1)
{
return;
}
// Get value of first element
var x: Int = arr[0];
// Get value of second element
var y: Int = arr[1];
var sum: Int = x + y;
var i: Int = 0;
// Execute the loop through by size of array
while (i < n)
{
var j: Int = i + 1;
while (j < n)
{
if (this.absValue(sum) > this.absValue(arr[i] + arr[j]))
{
// Get pair of closest to zero
x = arr[i];
y = arr[j];
// Change sum
sum = x + y;
}
j += 1;
}
i += 1;
}
// Display resultant pair
println(" (" + x + ") + (" + y + ") = " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val arr1: Array < Int > = arrayOf(4, 7, 8, 2, -6, 12);
val arr2: Array < Int > = arrayOf(2, 6, 8, -2, 5);
// Test A
var n: Int = arr1.count();
// Test B
n = arr2.count();
}``````

Output

`````` (7) + (-6) = 1
(2) + (-2) = 0``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post