# Distance between two closest minimum elements

Here given code implementation process.

``````// C Program
// Distance between two closest minimum elements
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
// returns the absolute value of subtracting two numbers
int absValue(int a, int b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
void closestMinDistance(int arr[], int size)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
int result = size + 1;
// This is used to check distance of smaller elements
int l1 = -1;
int l2 = -1;
// This is used to find minimum of two smallest
int a = -1;
int b = -1;
// Loop controlling variable
int i = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i++;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > absValue(l1, l2))
{
// Get new distance
result = absValue(l1, l2);
}
i++;
}
}
printf("\n Two smallest (%d,%d)", arr[a], arr[b]);
printf("\n Minimum distance is : %d\n", result);
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr1[] = {
9 , 3 , 6 , 2 , 3 , 5 , 7 , 2 , 3 , 4 , 2 , 6 , 4 , 6 , 2
};
int arr2[] = {
8 , 0 , 8 , 5 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1);
printf(" Array elements \n");
printArray(arr1, size);
closestMinDistance(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2);
printf("\n Array elements \n");
printArray(arr2, size);
closestMinDistance(arr2, size);
return 0;
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````/*
Java Program
Distance between two closest minimum elements
*/
public class Distance
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
}
// returns the absolute value of subtracting two numbers
public int absValue(int a, int b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
public void closestMinDistance(int[] arr, int size)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
int result = size + 1;
// This is used to check distance of smaller elements
int l1 = -1;
int l2 = -1;
// This is used to find minimum of two smallest
int a = -1;
int b = -1;
// Loop controlling variable
int i = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i++;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > absValue(l1, l2))
{
// Get new distance
result = absValue(l1, l2);
}
i++;
}
}
System.out.print("\n Two smallest (" + arr[a] + "," + arr[b] + ")");
System.out.print("\n Minimum distance is : " + result + "\n");
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr1 =
{
9 , 3 , 6 , 2 , 3 , 5 , 7 , 2 , 3 , 4 , 2 , 6 , 4 , 6 , 2
};
int[] arr2 =
{
8 , 0 , 8 , 5 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.length;
System.out.print(" Array elements \n");
// Get the size
size = arr2.length;;
System.out.print("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Distance between two closest minimum elements
*/

class Distance
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
}
// returns the absolute value of subtracting two numbers
int absValue(int a, int b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
void closestMinDistance(int arr[], int size)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
int result = size + 1;
// This is used to check distance of smaller elements
int l1 = -1;
int l2 = -1;
// This is used to find minimum of two smallest
int a = -1;
int b = -1;
// Loop controlling variable
int i = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i++;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > this->absValue(l1, l2))
{
// Get new distance
result = this->absValue(l1, l2);
}
i++;
}
}
cout << "\n Two smallest (" << arr[a] << "," << arr[b] << ")";
cout << "\n Minimum distance is : " << result << "\n";
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
9 , 3 , 6 , 2 , 3 , 5 , 7 , 2 , 3 , 4 , 2 , 6 , 4 , 6 , 2
};
int arr2[] = {
8 , 0 , 8 , 5 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1);
cout << " Array elements \n";
// Get the size
size = sizeof(arr2) / sizeof(arr2);;
cout << "\n Array elements \n";
return 0;
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````// Include namespace system
using System;
/*
C# Program
Distance between two closest minimum elements
*/
public class Distance
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
}
// returns the absolute value of subtracting two numbers
public int absValue(int a, int b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
public void closestMinDistance(int[] arr, int size)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
int result = size + 1;
// This is used to check distance of smaller elements
int l1 = -1;
int l2 = -1;
// This is used to find minimum of two smallest
int a = -1;
int b = -1;
// Loop controlling variable
int i = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i++;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > absValue(l1, l2))
{
// Get new distance
result = absValue(l1, l2);
}
i++;
}
}
Console.Write("\n Two smallest (" + arr[a] + "," + arr[b] + ")");
Console.Write("\n Minimum distance is : " + result + "\n");
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
9 , 3 , 6 , 2 , 3 , 5 , 7 , 2 , 3 , 4 , 2 , 6 , 4 , 6 , 2
};
int[] arr2 = {
8 , 0 , 8 , 5 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.Length;
Console.Write(" Array elements \n");
// Get the size
size = arr2.Length;;
Console.Write("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````<?php
/*
Php Program
Distance between two closest minimum elements
*/
class Distance
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
}
// returns the absolute value of subtracting two numbers
public	function absValue(\$a, \$b)
{
if (\$a > \$b)
{
return \$a - \$b;
}
else
{
return \$b - \$a;
}
}
// This function are find the minimum distance between two smallest element
public	function closestMinDistance( & \$arr, \$size)
{
if (\$size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
\$result = \$size + 1;
// This is used to check distance of smaller elements
\$l1 = -1;
\$l2 = -1;
// This is used to find minimum of two smallest
\$a = -1;
\$b = -1;
// Loop controlling variable
\$i = 0;
// Find two minimum element
// Execute loop through by size
while (\$i < \$size)
{
if (\$a == -1)
{
\$a = \$i;
}
else if (\$arr[\$a] > \$arr[\$i])
{
if (\$b == -1 || \$arr[\$b] > \$arr[\$a])
{
\$b = \$a;
}
\$a = \$i;
}
else if (\$b == -1 || \$arr[\$b] > \$arr[\$i])
{
if (\$arr[\$a] > \$arr[\$b])
{
\$a = \$b;
}
\$b = \$i;
}
\$i++;
}
\$i = 0;
if (\$arr[\$a] == \$arr[\$b])
{
// When both minimum are same
// Execute loop through by size
while (\$i < \$size)
{
if (\$arr[\$i] == \$arr[\$a])
{
if (\$l1 == -1)
{
\$l1 = \$i;
}
else if (\$result > \$i - \$l1)
{
\$result = \$i - \$l1;
\$l1 = \$i;
}
}
\$i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (\$i < \$size)
{
if (\$arr[\$i] == \$arr[\$a])
{
\$l1 = \$i;
}
else if (\$arr[\$i] == \$arr[\$b])
{
\$l2 = \$i;
}
if (\$l1 != -1 && \$l2 != -1 && \$result > \$this->absValue(\$l1, \$l2))
{
// Get new distance
\$result = \$this->absValue(\$l1, \$l2);
}
\$i++;
}
}
echo "\n Two smallest (". \$arr[\$a] .",". \$arr[\$b] .")";
echo "\n Minimum distance is : ". \$result ."\n";
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2);
\$arr2 = array(8, 0, 8, 5, 4, 2, 3, 2, 4);
// Get the size
\$size = count(\$arr1);
echo " Array elements \n";
// Get the size
\$size = count(\$arr2);;
echo "\n Array elements \n";
}
main();``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````/*
Node Js Program
Distance between two closest minimum elements
*/
class Distance
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
}
// returns the absolute value of subtracting two numbers
absValue(a, b)
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
closestMinDistance(arr, size)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
var result = size + 1;
// This is used to check distance of smaller elements
var l1 = -1;
var l2 = -1;
// This is used to find minimum of two smallest
var a = -1;
var b = -1;
// Loop controlling variable
var i = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i++;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i++;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > this.absValue(l1, l2))
{
// Get new distance
result = this.absValue(l1, l2);
}
i++;
}
}
process.stdout.write("\n Two smallest (" + arr[a] + "," + arr[b] + ")");
process.stdout.write("\n Minimum distance is : " + result + "\n");
}
}

function main()
{
// Define array of integer elements
var arr1 = [9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2];
var arr2 = [8, 0, 8, 5, 4, 2, 3, 2, 4];
// Get the size
var size = arr1.length;
process.stdout.write(" Array elements \n");
// Get the size
size = arr2.length;;
process.stdout.write("\n Array elements \n");
}
main();``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````#  Python 3 Program
#  Distance between two closest minimum elements

class Distance :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

#  returns the absolute value of subtracting two numbers
def absValue(self, a, b) :
if (a > b) :
return a - b
else :
return b - a

#  This function are find the minimum distance between two smallest element
def closestMinDistance(self, arr, size) :
if (size <= 1) :
#  When less than 2 elements exist
return

#  Set max difference
result = size + 1
#  This is used to check distance of smaller elements
l1 = -1
l2 = -1
#  This is used to find minimum of two smallest
a = -1
b = -1
#  Loop controlling variable
i = 0
#  Find two minimum element
#  Execute loop through by size
while (i < size) :
if (a == -1) :
a = i

elif(arr[a] > arr[i]) :
if (b == -1 or arr[b] > arr[a]) :
b = a

a = i

elif(b == -1 or arr[b] > arr[i]) :
if (arr[a] > arr[b]) :
a = b

b = i

i += 1

i = 0
if (arr[a] == arr[b]) :
#  When both minimum are same
#  Execute loop through by size
while (i < size) :
if (arr[i] == arr[a]) :
if (l1 == -1) :
l1 = i

elif(result > i - l1) :
result = i - l1
l1 = i

i += 1

else :
#  When two smaller are not same
#  Execute loop through by size
while (i < size) :
if (arr[i] == arr[a]) :
l1 = i

elif(arr[i] == arr[b]) :
l2 = i

if (l1 != -1 and l2 != -1 and result > self.absValue(l1, l2)) :
#  Get new distance
result = self.absValue(l1, l2)

i += 1

print("\n Two smallest (", arr[a] ,",", arr[b] ,")", end = "")
print("\n Minimum distance is : ", result )

def main() :
#  Define array of integer elements
arr1 = [9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2]
arr2 = [8, 0, 8, 5, 4, 2, 3, 2, 4]
#  Get the size
size = len(arr1)
print(" Array elements ")
#  Get the size
size = len(arr2)
print("\n Array elements ")

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

#### Output

`````` Array elements
9   3   6   2   3   5   7   2   3   4   2   6   4   6   2
Two smallest ( 2 , 2 )
Minimum distance is :  3

Array elements
8   0   8   5   4   2   3   2   4
Two smallest ( 0 , 2 )
Minimum distance is :  4``````
``````#  Ruby Program
#  Distance between two closest minimum elements

class Distance
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

end

#  returns the absolute value of subtracting two numbers
def absValue(a, b)
if (a > b)
return a - b
else
return b - a
end

end

#  This function are find the minimum distance between two smallest element
def closestMinDistance(arr, size)
if (size <= 1)
#  When less than 2 elements exist
return
end

#  Set max difference
result = size + 1
#  This is used to check distance of smaller elements
l1 = -1
l2 = -1
#  This is used to find minimum of two smallest
a = -1
b = -1
#  Loop controlling variable
i = 0
#  Find two minimum element
#  Execute loop through by size
while (i < size)
if (a == -1)
a = i
elsif(arr[a] > arr[i])
if (b == -1 || arr[b] > arr[a])
b = a
end

a = i
elsif(b == -1 || arr[b] > arr[i])
if (arr[a] > arr[b])
a = b
end

b = i
end

i += 1
end

i = 0
if (arr[a] == arr[b])
#  When both minimum are same
#  Execute loop through by size
while (i < size)
if (arr[i] == arr[a])
if (l1 == -1)
l1 = i
elsif(result > i - l1)
result = i - l1
l1 = i
end

end

i += 1
end

else
#  When two smaller are not same
#  Execute loop through by size
while (i < size)
if (arr[i] == arr[a])
l1 = i
elsif(arr[i] == arr[b])
l2 = i
end

if (l1 != -1 && l2 != -1 && result > self.absValue(l1, l2))
#  Get new distance
result = self.absValue(l1, l2)
end

i += 1
end

end

print("\n Two smallest (", arr[a] ,",", arr[b] ,")")
print("\n Minimum distance is : ", result ,"\n")
end

end

def main()
#  Define array of integer elements
arr1 = [9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2]
arr2 = [8, 0, 8, 5, 4, 2, 3, 2, 4]
#  Get the size
size = arr1.length
print(" Array elements \n")
#  Get the size
size = arr2.length
print("\n Array elements \n")
end

main()``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4
``````
``````/*
Scala Program
Distance between two closest minimum elements
*/
class Distance
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
}
// returns the absolute value of subtracting two numbers
def absValue(a: Int, b: Int): Int = {
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
def closestMinDistance(arr: Array[Int], size: Int): Unit = {
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
var result: Int = size + 1;
// This is used to check distance of smaller elements
var l1: Int = -1;
var l2: Int = -1;
// This is used to find minimum of two smallest
var a: Int = -1;
var b: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr(a) > arr(i))
{
if (b == -1 || arr(b) > arr(a))
{
b = a;
}
a = i;
}
else if (b == -1 || arr(b) > arr(i))
{
if (arr(a) > arr(b))
{
a = b;
}
b = i;
}
i += 1;
}
i = 0;
if (arr(a) == arr(b))
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr(i) == arr(a))
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i += 1;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr(i) == arr(a))
{
l1 = i;
}
else if (arr(i) == arr(b))
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > this.absValue(l1, l2))
{
// Get new distance
result = this.absValue(l1, l2);
}
i += 1;
}
}
print("\n Two smallest (" + arr(a) + "," + arr(b) + ")");
print("\n Minimum distance is : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Distance = new Distance();
// Define array of integer elements
var arr1: Array[Int] = Array(9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2);
var arr2: Array[Int] = Array(8, 0, 8, 5, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.length;
print(" Array elements \n");
// Get the size
size = arr2.length;;
print("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````
``````/*
Swift 4 Program
Distance between two closest minimum elements
*/
class Distance
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
// returns the absolute value of subtracting two numbers
func absValue(_ a: Int, _ b: Int)->Int
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
func closestMinDistance(_ arr: [Int], _ size: Int)
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
var result: Int = size + 1;
// This is used to check distance of smaller elements
var l1: Int = -1;
var l2: Int = -1;
// This is used to find minimum of two smallest
var a: Int = -1;
var b: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i += 1;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i += 1;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1  != -1 && l2  != -1 && result > self.absValue(l1, l2))
{
// Get new distance
result = self.absValue(l1, l2);
}
i += 1;
}
}
print("\n Two smallest (", arr[a] ,",", arr[b] ,")", terminator: "");
print("\n Minimum distance is : ", result );
}
}
func main()
{
// Define array of integer elements
let arr1: [Int] = [9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2];
let arr2: [Int] = [8, 0, 8, 5, 4, 2, 3, 2, 4];
// Get the size
var size: Int = arr1.count;
print(" Array elements ");
// Get the size
size = arr2.count;
print("\n Array elements ");
}
main();``````

#### Output

`````` Array elements
9   3   6   2   3   5   7   2   3   4   2   6   4   6   2
Two smallest ( 2 , 2 )
Minimum distance is :  3

Array elements
8   0   8   5   4   2   3   2   4
Two smallest ( 0 , 2 )
Minimum distance is :  4``````
``````/*
Kotlin Program
Distance between two closest minimum elements
*/
class Distance
{
//Display elements of given array
fun printArray(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
}
// returns the absolute value of subtracting two numbers
fun absValue(a: Int, b: Int): Int
{
if (a > b)
{
return a - b;
}
else
{
return b - a;
}
}
// This function are find the minimum distance between two smallest element
fun closestMinDistance(arr: Array <Int> , size: Int): Unit
{
if (size <= 1)
{
// When less than 2 elements exist
return;
}
// Set max difference
var result: Int = size + 1;
// This is used to check distance of smaller elements
var l1: Int = -1;
var l2: Int = -1;
// This is used to find minimum of two smallest
var a: Int = -1;
var b: Int = -1;
// Loop controlling variable
var i: Int = 0;
// Find two minimum element
// Execute loop through by size
while (i < size)
{
if (a == -1)
{
a = i;
}
else if (arr[a] > arr[i])
{
if (b == -1 || arr[b] > arr[a])
{
b = a;
}
a = i;
}
else if (b == -1 || arr[b] > arr[i])
{
if (arr[a] > arr[b])
{
a = b;
}
b = i;
}
i += 1;
}
i = 0;
if (arr[a] == arr[b])
{
// When both minimum are same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
if (l1 == -1)
{
l1 = i;
}
else if (result > i - l1)
{
result = i - l1;
l1 = i;
}
}
i += 1;
}
}
else
{
// When two smaller are not same
// Execute loop through by size
while (i < size)
{
if (arr[i] == arr[a])
{
l1 = i;
}
else if (arr[i] == arr[b])
{
l2 = i;
}
if (l1 != -1 && l2 != -1 && result > this.absValue(l1, l2))
{
// Get new distance
result = this.absValue(l1, l2);
}
i += 1;
}
}
print("\n Two smallest (" + arr[a] + "," + arr[b] + ")");
print("\n Minimum distance is : " + result + "\n");
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr1: Array <Int> = arrayOf(9, 3, 6, 2, 3, 5, 7, 2, 3, 4, 2, 6, 4, 6, 2);
var arr2: Array <Int> = arrayOf(8, 0, 8, 5, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.count();
print(" Array elements \n");
// Get the size
size = arr2.count();
print("\n Array elements \n");
}``````

#### Output

`````` Array elements
9  3  6  2  3  5  7  2  3  4  2  6  4  6  2
Two smallest (2,2)
Minimum distance is : 3

Array elements
8  0  8  5  4  2  3  2  4
Two smallest (0,2)
Minimum distance is : 4``````

## 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.