Posted on by Kalkicode
Code Array

Find minimum absolute difference in array

Here given code implementation process.

``````/*
C Program
Find minimum absolute difference in array
*/
#include <stdio.h>
#include <limits.h>

// Returns the absolute value
int absoluteValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
void absoluteDiff(int arr[], int n)
{
if (n < 2)
{
return;
}
int result = INT_MAX;
// Use to get element location
int a = 0;
int b = 0;
// Outer loop through by size
for (int i = 0; i < n; ++i)
{
// Inner loop
for (int j = i + 1; j < n; ++j)
{
if (absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = absoluteValue(arr[i] - arr[j]);
}
}
}
// Display calculated result
printf(" Element : (%d,%d)", arr[a], arr[b]);
printf("\n Minimum difference : %d\n", result);
}
int main(int argc, char const *argv[])
{
// Define array of integer elements
int arr[] = {
5 , 2 , -2 , 12 , 9 , -4 , 15
};
// Get the size of array
int n = sizeof(arr) / sizeof(arr[0]);
// Find absolute difference
absoluteDiff(arr, n);
return 0;
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````/*
Java program
Find minimum absolute difference in array
*/
public class Difference
{
// Returns the absolute value
public int absoluteValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
public void absoluteDiff(int[] arr, int n)
{
if (n < 2)
{
return;
}
int result = Integer.MAX_VALUE;;
// Use to get element location
int a = 0;
int b = 0;
// Outer loop through by size
for (int i = 0; i < n; ++i)
{
// Inner loop
for (int j = i + 1; j < n; ++j)
{
if (absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = absoluteValue(arr[i] - arr[j]);
}
}
}
// Display calculated result
System.out.print(" Element : (" + arr[a] + "," + arr[b] + ")");
System.out.print("\n Minimum difference : " + result + "\n");
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr = {
5 , 2 , -2 , 12 , 9 , -4 , 15
};
// Get the size of array
int n = (arr.length);
// Find absolute difference
}
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````// Include header file
#include <iostream>
#include <limits.h>

using namespace std;
/*
C++ program
Find minimum absolute difference in array
*/
class Difference
{
public:
// Returns the absolute value
int absoluteValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
void absoluteDiff(int arr[], int n)
{
if (n < 2)
{
return;
}
int result = INT_MAX;;
// Use to get element location
int a = 0;
int b = 0;
// Outer loop through by size
for (int i = 0; i < n; ++i)
{
// Inner loop
for (int j = i + 1; j < n; ++j)
{
if (this->absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = this->absoluteValue(arr[i] - arr[j]);
}
}
}
// Display calculated result
cout << " Element : (" << arr[a] << "," << arr[b] << ")";
cout << "\n Minimum difference : " << result << "\n";
}
};
int main()
{
// Define array of integer elements
int arr[] = {
5 , 2 , -2 , 12 , 9 , -4 , 15
};
// Get the size of array
int n = (sizeof(arr) / sizeof(arr[0]));
// Find absolute difference
return 0;
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````// Include namespace system
using System;
/*
C# program
Find minimum absolute difference in array
*/
public class Difference
{
// Returns the absolute value
public int absoluteValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
public void absoluteDiff(int[] arr, int n)
{
if (n < 2)
{
return;
}
int result = int.MaxValue;;
// Use to get element location
int a = 0;
int b = 0;
// Outer loop through by size
for (int i = 0; i < n; ++i)
{
// Inner loop
for (int j = i + 1; j < n; ++j)
{
if (absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = absoluteValue(arr[i] - arr[j]);
}
}
}
// Display calculated result
Console.Write(" Element : (" + arr[a] + "," + arr[b] + ")");
Console.Write("\n Minimum difference : " + result + "\n");
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr = {
5 , 2 , -2 , 12 , 9 , -4 , 15
};
// Get the size of array
int n = (arr.Length);
// Find absolute difference
}
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````<?php
/*
Php program
Find minimum absolute difference in array
*/
class Difference
{
// Returns the absolute value
public  function absoluteValue(\$num)
{
if (\$num < 0)
{
return -\$num;
}
return \$num;
}
// Find the minimum absolute difference in an array
public  function absoluteDiff( & \$arr, \$n)
{
if (\$n < 2)
{
return;
}
\$result = PHP_INT_MAX;;
// Use to get element location
\$a = 0;
\$b = 0;
// Outer loop through by size
for (\$i = 0; \$i < \$n; ++\$i)
{
// Inner loop
for (\$j = \$i + 1; \$j < \$n; ++\$j)
{
if (\$this->absoluteValue(\$arr[\$i] - \$arr[\$j]) < \$result)
{
// Get element location
\$a = \$i;
\$b = \$j;
// Get new difference
\$result = \$this->absoluteValue(\$arr[\$i] - \$arr[\$j]);
}
}
}
// Display calculated result
echo " Element : (". \$arr[\$a] .",". \$arr[\$b] .")";
echo "\n Minimum difference : ". \$result ."\n";
}
}

function main()
{
// Define array of integer elements
\$arr = array(5, 2, -2, 12, 9, -4, 15);
// Get the size of array
\$n = (count(\$arr));
// Find absolute difference
}
main();``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````/*
Node Js program
Find minimum absolute difference in array
*/
class Difference
{
// Returns the absolute value
absoluteValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
absoluteDiff(arr, n)
{
if (n < 2)
{
return;
}
var result = Number.MAX_VALUE;;
// Use to get element location
var a = 0;
var b = 0;
// Outer loop through by size
for (var i = 0; i < n; ++i)
{
// Inner loop
for (var j = i + 1; j < n; ++j)
{
if (this.absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = this.absoluteValue(arr[i] - arr[j]);
}
}
}
// Display calculated result
process.stdout.write(" Element : (" + arr[a] + "," + arr[b] + ")");
process.stdout.write("\n Minimum difference : " + result + "\n");
}
}

function main()
{
// Define array of integer elements
var arr = [5, 2, -2, 12, 9, -4, 15];
// Get the size of array
var n = (arr.length);
// Find absolute difference
}
main();``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````import sys
#   Python 3 program
#   Find minimum absolute difference in array

class Difference :
#  Returns the absolute value
def absoluteValue(self, num) :
if (num < 0) :
return -num

return num

#  Find the minimum absolute difference in an array
def absoluteDiff(self, arr, n) :
if (n < 2) :
return

result = sys.maxsize
#  Use to get element location
a = 0
b = 0
#  Outer loop through by size
i = 0
while (i < n) :
#  Inner loop
j = i + 1
while (j < n) :
if (self.absoluteValue(arr[i] - arr[j]) < result) :
#  Get element location
a = i
b = j
#  Get new difference
result = self.absoluteValue(arr[i] - arr[j])

j += 1

i += 1

#  Display calculated result
print(" Element : (", arr[a] ,",", arr[b] ,")", end = "")
print("\n Minimum difference : ", result )

def main() :
#  Define array of integer elements
arr = [5, 2, -2, 12, 9, -4, 15]
#  Get the size of array
n = (len(arr))
#  Find absolute difference

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

Output

`````` Element : ( -2 , -4 )
Minimum difference :  2``````
``````#   Ruby program
#   Find minimum absolute difference in array

class Difference
#  Returns the absolute value
def absoluteValue(num)
if (num < 0)
return -num
end

return num
end

#  Find the minimum absolute difference in an array
def absoluteDiff(arr, n)
if (n < 2)
return
end

result = (2 ** (0. size * 8 - 2))
#  Use to get element location
a = 0
b = 0
#  Outer loop through by size
i = 0
while (i < n)
#  Inner loop
j = i + 1
while (j < n)
if (self.absoluteValue(arr[i] - arr[j]) < result)
#  Get element location
a = i
b = j
#  Get new difference
result = self.absoluteValue(arr[i] - arr[j])
end

j += 1
end

i += 1
end

#  Display calculated result
print(" Element : (", arr[a] ,",", arr[b] ,")")
print("\n Minimum difference : ", result ,"\n")
end

end

def main()
#  Define array of integer elements
arr = [5, 2, -2, 12, 9, -4, 15]
#  Get the size of array
n = (arr.length)
#  Find absolute difference
end

main()``````

Output

`````` Element : (-2,-4)
Minimum difference : 2
``````
``````/*
Scala program
Find minimum absolute difference in array
*/
class Difference
{
// Returns the absolute value
def absoluteValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
def absoluteDiff(arr: Array[Int], n: Int): Unit = {
if (n < 2)
{
return;
}
var result: Int = Int.MaxValue;;
// Use to get element location
var a: Int = 0;
var b: Int = 0;
// Outer loop through by size
var i: Int = 0;
while (i < n)
{
// Inner loop
var j: Int = i + 1;
while (j < n)
{
if (this.absoluteValue(arr(i) - arr(j)) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = this.absoluteValue(arr(i) - arr(j));
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Element : (" + arr(a) + "," + arr(b) + ")");
print("\n Minimum difference : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Difference = new Difference();
// Define array of integer elements
var arr: Array[Int] = Array(5, 2, -2, 12, 9, -4, 15);
// Get the size of array
var n: Int = (arr.length);
// Find absolute difference
}
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````
``````/*
Swift 4 program
Find minimum absolute difference in array
*/
class Difference
{
// Returns the absolute value
func absoluteValue(_ num: Int)->Int
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
func absoluteDiff(_ arr: [Int], _ n: Int)
{
if (n < 2)
{
return;
}
var result: Int = Int.max;
// Use to get element location
var a: Int = 0;
var b: Int = 0;
// Outer loop through by size
var i: Int = 0;
while (i < n)
{
// Inner loop
var j: Int = i + 1;
while (j < n)
{
if (self.absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = self.absoluteValue(arr[i] - arr[j]);
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Element : (", arr[a] ,",", arr[b] ,")", terminator: "");
print("\n Minimum difference : ", result );
}
}
func main()
{
// Define array of integer elements
let arr: [Int] = [5, 2, -2, 12, 9, -4, 15];
// Get the size of array
let n: Int = (arr.count);
// Find absolute difference
}
main();``````

Output

`````` Element : ( -2 , -4 )
Minimum difference :  2``````
``````/*
Kotlin program
Find minimum absolute difference in array
*/
class Difference
{
// Returns the absolute value
fun absoluteValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
// Find the minimum absolute difference in an array
fun absoluteDiff(arr: Array < Int > , n: Int): Unit
{
if (n < 2)
{
return;
}
var result: Int = Int.MAX_VALUE;
// Use to get element location
var a: Int = 0;
var b: Int = 0;
// Outer loop through by size
var i: Int = 0;
while (i < n)
{
// Inner loop
var j: Int = i + 1;
while (j < n)
{
if (this.absoluteValue(arr[i] - arr[j]) < result)
{
// Get element location
a = i;
b = j;
// Get new difference
result = this.absoluteValue(arr[i] - arr[j]);
}
j += 1;
}
i += 1;
}
// Display calculated result
print(" Element : (" + arr[a] + "," + arr[b] + ")");
print("\n Minimum difference : " + result + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
var arr: Array < Int > = arrayOf(5, 2, -2, 12, 9, -4, 15);
// Get the size of array
var n: Int = (arr.count());
// Find absolute difference
}``````

Output

`````` Element : (-2,-4)
Minimum difference : 2``````

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