Find the minimum distance between two numbers

Here given code implementation process.

// C Program 
// Find the minimum distance between two numbers
#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]);
	}
	printf("\n");
}
// Returns the calculated minimum distance
int distance(int e1, int e2, int diff)
{
	if (e1 > e2)
	{
		if (diff == -1 || e1 - e2 < diff)
		{
			return e1 - e2;
		}
	}
	else
	{
		if (diff == -1 || e2 - e1 < diff)
		{
			return e2 - e1;
		}
	}
	return diff;
}
// Finds the minimum distance of given elements (x and y)
// When element is exists in array
void minimumDistance(int arr[], int size, int x, int y)
{
	if (size <= 1)
	{
		// Not more than 2 elements
		return;
	}
	int e1 = -1;
	int e2 = -1;
	int diff = -1;
	for (int i = 0; i < size; ++i)
	{
		if (arr[i] == x)
		{
			// When get a new x element 
			if (x == y)
			{
				// When x and y is similar
				if (e1 == -1)
				{
					// Get first element
					e1 = i;
				}
				else if (e2 == -1)
				{
					// Get second element
					e2 = i;
					// Get distance
					diff = distance(e1, e2, diff);
				}
				else
				{
					e1 = e2;
					e2 = i;
					// Get distance
					diff = distance(e1, e2, diff);
				}
			}
			else
			{
				e1 = i;
				if (e2 != -1)
				{
					// Get distance
					diff = distance(e1, e2, diff);
				}
			}
		}
		else if (arr[i] == y)
		{
			// When get a new y element 
			e2 = i;
			if (e1 != -1)
			{
				// Get distance
				diff = distance(e1, e2, diff);
			}
		}
	}
	if (e1 == -1 || e2 == -1)
	{
		printf("\n Pair (%d,%d) are not exists", x, y);
	}
	else
	{
		printf("  Minimum distance of pair (%d,%d) is : %d \n", x, y, diff);
	}
}
int main(int argc, char const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		4 , 2 , 8 , 2 , 3 , 7 , 1 , 4 , 6 , 1 , 4 , 6 , 8 , 2 , 3
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	printf("  Array Element \n");
	printArray(arr, size);
	// x = 2, y = 6
	minimumDistance(arr, size, 2, 6);
	// x = 4, y = 4
	minimumDistance(arr, size, 4, 4);
	return 0;
}

Output

  Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
  Minimum distance of pair (2,6) is : 2
  Minimum distance of pair (4,4) is : 3
/*
    Java Program
    Find the minimum distance between two numbers
*/
public class FindDistance
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Returns the calculated minimum distance
	public int distance(int e1, int e2, int diff)
	{
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	public void minimumDistance(int[] arr, int size, int x, int y)
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		int e1 = -1;
		int e2 = -1;
		int diff = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == x)
			{
				// When get a new x element 
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = distance(e1, e2, diff);
					}
				}
			}
			else if (arr[i] == y)
			{
				// When get a new y element 
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = distance(e1, e2, diff);
				}
			}
		}
		if (e1 == -1 || e2 == -1)
		{
			System.out.print("\n Pair (" + x + "," + y + ") are not exists");
		}
		else
		{
			System.out.print(" Minimum distance of pair (" + x + "," + y + ") is : " + diff + " \n");
		}
	}
	public static void main(String args[])
	{
		FindDistance task = new FindDistance();
		// Define array of integer elements
		int[] arr = {
			4 , 2 , 8 , 2 , 3 , 7 , 1 , 4 , 6 , 1 , 4 , 6 , 8 , 2 , 3
		};
		// Get the size
		int size = arr.length;
		System.out.print(" Array Element \n");
		task.printArray(arr, size);
		// x = 2, y = 6
		task.minimumDistance(arr, size, 2, 6);
		// x = 4, y = 4
		task.minimumDistance(arr, size, 4, 4);
	}
}

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Returns the calculated minimum distance
	int distance(int e1, int e2, int diff)
	{
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	void minimumDistance(int arr[], int size, int x, int y)
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		int e1 = -1;
		int e2 = -1;
		int diff = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = this->distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = this->distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = this->distance(e1, e2, diff);
					}
				}
			}
			else if (arr[i] == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = this->distance(e1, e2, diff);
				}
			}
		}
		if (e1 == -1 || e2 == -1)
		{
			cout << "\n Pair (" << x << "," << y << ") are not exists";
		}
		else
		{
			cout << " Minimum distance of pair (" << x << "," << y << ") is : " << diff << " \n";
		}
	}
};
int main()
{
	FindDistance task = FindDistance();
	// Define array of integer elements
	int arr[] = {
		4 , 2 , 8 , 2 , 3 , 7 , 1 , 4 , 6 , 1 , 4 , 6 , 8 , 2 , 3
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << " Array Element \n";
	task.printArray(arr, size);
	// x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6);
	// x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4);
	return 0;
}

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
// Include namespace system
using System;
/*
    C# Program
    Find the minimum distance between two numbers
*/
public class FindDistance
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Returns the calculated minimum distance
	public int distance(int e1, int e2, int diff)
	{
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	public void minimumDistance(int[] arr, int size, int x, int y)
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		int e1 = -1;
		int e2 = -1;
		int diff = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = distance(e1, e2, diff);
					}
				}
			}
			else if (arr[i] == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = distance(e1, e2, diff);
				}
			}
		}
		if (e1 == -1 || e2 == -1)
		{
			Console.Write("\n Pair (" + x + "," + y + ") are not exists");
		}
		else
		{
			Console.Write(" Minimum distance of pair (" + x + "," + y + ") is : " + diff + " \n");
		}
	}
	public static void Main(String []args)
	{
		FindDistance task = new FindDistance();
		// Define array of integer elements
		int[] arr = {
			4 , 2 , 8 , 2 , 3 , 7 , 1 , 4 , 6 , 1 , 4 , 6 , 8 , 2 , 3
		};
		// Get the size
		int size = arr.Length;
		Console.Write(" Array Element \n");
		task.printArray(arr, size);
		// x = 2, y = 6
		task.minimumDistance(arr, size, 2, 6);
		// x = 4, y = 4
		task.minimumDistance(arr, size, 4, 4);
	}
}

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
<?php
/*
    Php Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Returns the calculated minimum distance
	public	function distance($e1, $e2, $diff)
	{
		if ($e1 > $e2)
		{
			if ($diff == -1 || $e1 - $e2 < $diff)
			{
				return $e1 - $e2;
			}
		}
		else
		{
			if ($diff == -1 || $e2 - $e1 < $diff)
			{
				return $e2 - $e1;
			}
		}
		return $diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	public	function minimumDistance( & $arr, $size, $x, $y)
	{
		if ($size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		$e1 = -1;
		$e2 = -1;
		$diff = -1;
		for ($i = 0; $i < $size; ++$i)
		{
			if ($arr[$i] == $x)
			{
				// When get a new x element
				if ($x == $y)
				{
					// When x and y is similar
					if ($e1 == -1)
					{
						// Get first element
						$e1 = $i;
					}
					else if ($e2 == -1)
					{
						// Get second element
						$e2 = $i;
						// Get distance
						$diff = $this->distance($e1, $e2, $diff);
					}
					else
					{
						$e1 = $e2;
						$e2 = $i;
						// Get distance
						$diff = $this->distance($e1, $e2, $diff);
					}
				}
				else
				{
					$e1 = $i;
					if ($e2 != -1)
					{
						// Get distance
						$diff = $this->distance($e1, $e2, $diff);
					}
				}
			}
			else if ($arr[$i] == $y)
			{
				// When get a new y element
				$e2 = $i;
				if ($e1 != -1)
				{
					// Get distance
					$diff = $this->distance($e1, $e2, $diff);
				}
			}
		}
		if ($e1 == -1 || $e2 == -1)
		{
			echo "\n Pair (". $x .",". $y .") are not exists";
		}
		else
		{
			echo " Minimum distance of pair (". $x .",". $y .") is : ". $diff ." \n";
		}
	}
}

function main()
{
	$task = new FindDistance();
	// Define array of integer elements
	$arr = array(4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3);
	// Get the size
	$size = count($arr);
	echo " Array Element \n";
	$task->printArray($arr, $size);
	// x = 2, y = 6
	$task->minimumDistance($arr, $size, 2, 6);
	// x = 4, y = 4
	$task->minimumDistance($arr, $size, 4, 4);
}
main();

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
/*
    Node Js Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	// Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Returns the calculated minimum distance
	distance(e1, e2, diff)
	{
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	minimumDistance(arr, size, x, y)
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		var e1 = -1;
		var e2 = -1;
		var diff = -1;
		for (var i = 0; i < size; ++i)
		{
			if (arr[i] == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
			}
			else if (arr[i] == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = this.distance(e1, e2, diff);
				}
			}
		}
		if (e1 == -1 || e2 == -1)
		{
			process.stdout.write("\n Pair (" + x + "," + y + ") are not exists");
		}
		else
		{
			process.stdout.write(" Minimum distance of pair (" + x + "," + y + ") is : " + diff + " \n");
		}
	}
}

function main()
{
	var task = new FindDistance();
	// Define array of integer elements
	var arr = [4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3];
	// Get the size
	var size = arr.length;
	process.stdout.write(" Array Element \n");
	task.printArray(arr, size);
	// x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6);
	// x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4);
}
main();

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
#  Python 3 Program
#  Find the minimum distance between two numbers

class FindDistance :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Returns the calculated minimum distance
	def distance(self, e1, e2, diff) :
		if (e1 > e2) :
			if (diff == -1 or e1 - e2 < diff) :
				return e1 - e2
			
		else :
			if (diff == -1 or e2 - e1 < diff) :
				return e2 - e1
			
		
		return diff
	
	#  Finds the minimum distance of given elements (x and y)
	#  When element is exists in array
	def minimumDistance(self, arr, size, x, y) :
		if (size <= 1) :
			#  Not more than 2 elements
			return
		
		e1 = -1
		e2 = -1
		diff = -1
		i = 0
		while (i < size) :
			if (arr[i] == x) :
				#  When get a new x element
				if (x == y) :
					#  When x and y is similar
					if (e1 == -1) :
						#  Get first element
						e1 = i
					
					elif(e2 == -1) :
						#  Get second element
						e2 = i
						#  Get distance
						diff = self.distance(e1, e2, diff)
					else :
						e1 = e2
						e2 = i
						#  Get distance
						diff = self.distance(e1, e2, diff)
					
				else :
					e1 = i
					if (e2 != -1) :
						#  Get distance
						diff = self.distance(e1, e2, diff)
					
				
			
			elif(arr[i] == y) :
				#  When get a new y element
				e2 = i
				if (e1 != -1) :
					#  Get distance
					diff = self.distance(e1, e2, diff)
				
			
			i += 1
		
		if (e1 == -1 or e2 == -1) :
			print("\n Pair (", x ,",", y ,") are not exists", end = "")
		else :
			print(" Minimum distance of pair (", x ,",", y ,") is : ", diff ," ")
		
	

def main() :
	task = FindDistance()
	#  Define array of integer elements
	arr = [4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3]
	#  Get the size
	size = len(arr)
	print(" Array Element ")
	task.printArray(arr, size)
	#  x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6)
	#  x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4)

if __name__ == "__main__": main()

Output

 Array Element
   4   2   8   2   3   7   1   4   6   1   4   6   8   2   3
 Minimum distance of pair ( 2 , 6 ) is :  2
 Minimum distance of pair ( 4 , 4 ) is :  3
#  Ruby Program
#  Find the minimum distance between two numbers

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

		print("\n")
	end

	#  Returns the calculated minimum distance
	def distance(e1, e2, diff) 
		if (e1 > e2) 
			if (diff == -1 || e1 - e2 < diff) 
				return e1 - e2
			end

		else 
			if (diff == -1 || e2 - e1 < diff) 
				return e2 - e1
			end

		end

		return diff
	end

	#  Finds the minimum distance of given elements (x and y)
	#  When element is exists in array
	def minimumDistance(arr, size, x, y) 
		if (size <= 1) 
			#  Not more than 2 elements
			return
		end

		e1 = -1
		e2 = -1
		diff = -1
		i = 0
		while (i < size) 
			if (arr[i] == x) 
				#  When get a new x element
				if (x == y) 
					#  When x and y is similar
					if (e1 == -1) 
						#  Get first element
						e1 = i
					elsif(e2 == -1) 
						#  Get second element
						e2 = i
						#  Get distance
						diff = self.distance(e1, e2, diff)
					else 
						e1 = e2
						e2 = i
						#  Get distance
						diff = self.distance(e1, e2, diff)
					end

				else 
					e1 = i
					if (e2 != -1) 
						#  Get distance
						diff = self.distance(e1, e2, diff)
					end

				end

			elsif(arr[i] == y) 
				#  When get a new y element
				e2 = i
				if (e1 != -1) 
					#  Get distance
					diff = self.distance(e1, e2, diff)
				end

			end

			i += 1
		end

		if (e1 == -1 || e2 == -1) 
			print("\n Pair (", x ,",", y ,") are not exists")
		else 
			print(" Minimum distance of pair (", x ,",", y ,") is : ", diff ," \n")
		end

	end

end

def main() 
	task = FindDistance.new()
	#  Define array of integer elements
	arr = [4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3]
	#  Get the size
	size = arr.length
	print(" Array Element \n")
	task.printArray(arr, size)
	#  x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6)
	#  x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4)
end

main()

Output

 Array Element 
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2 
 Minimum distance of pair (4,4) is : 3 
/*
    Scala Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	//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;
		}
		print("\n");
	}
	// Returns the calculated minimum distance
	def distance(e1: Int, e2: Int, diff: Int): Int = {
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	def minimumDistance(arr: Array[Int], size: Int, x: Int, y: Int): Unit = {
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		var e1: Int = -1;
		var e2: Int = -1;
		var diff: Int = -1;
		var i: Int = 0;
		while (i < size)
		{
			if (arr(i) == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
			}
			else if (arr(i) == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = this.distance(e1, e2, diff);
				}
			}
			i += 1;
		}
		if (e1 == -1 || e2 == -1)
		{
			print("\n Pair (" + x + "," + y + ") are not exists");
		}
		else
		{
			print(" Minimum distance of pair (" + x + "," + y + ") is : " + diff + " \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: FindDistance = new FindDistance();
		// Define array of integer elements
		var arr: Array[Int] = Array(4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3);
		// Get the size
		var size: Int = arr.length;
		print(" Array Element \n");
		task.printArray(arr, size);
		// x = 2, y = 6
		task.minimumDistance(arr, size, 2, 6);
		// x = 4, y = 4
		task.minimumDistance(arr, size, 4, 4);
	}
}

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3
/*
    Swift 4 Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Returns the calculated minimum distance
	func distance(_ e1: Int, _ e2: Int, _ diff: Int)->Int
	{
		if (e1 > e2)
		{
			if (diff == -1 || e1 - e2 < diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1 < diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	func minimumDistance(_ arr: [Int], _ size: Int, _ x: Int, _ y: Int)
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		var e1: Int = -1;
		var e2: Int = -1;
		var diff: Int = -1;
		var i: Int = 0;
		while (i < size)
		{
			if (arr[i] == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = self.distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = self.distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = self.distance(e1, e2, diff);
					}
				}
			}
			else if (arr[i] == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = self.distance(e1, e2, diff);
				}
			}
			i += 1;
		}
		if (e1 == -1 || e2 == -1)
		{
			print("\n Pair (", x ,",", y ,") are not exists", terminator: "");
		}
		else
		{
			print(" Minimum distance of pair (", x ,",", y ,") is : ", diff ," ");
		}
	}
}
func main()
{
	let task: FindDistance = FindDistance();
	// Define array of integer elements
	let arr: [Int] = [4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3];
	// Get the size
	let size: Int = arr.count;
	print(" Array Element ");
	task.printArray(arr, size);
	// x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6);
	// x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4);
}
main();

Output

 Array Element
   4   2   8   2   3   7   1   4   6   1   4   6   8   2   3
 Minimum distance of pair ( 2 , 6 ) is :  2
 Minimum distance of pair ( 4 , 4 ) is :  3
/*
    Kotlin Program
    Find the minimum distance between two numbers
*/
class FindDistance
{
	//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;
		}
		print("\n");
	}
	// Returns the calculated minimum distance
	fun distance(e1: Int, e2: Int, diff: Int): Int
	{
		if (e1>e2)
		{
			if (diff == -1 || e1 - e2<diff)
			{
				return e1 - e2;
			}
		}
		else
		{
			if (diff == -1 || e2 - e1<diff)
			{
				return e2 - e1;
			}
		}
		return diff;
	}
	// Finds the minimum distance of given elements (x and y)
	// When element is exists in array
	fun minimumDistance(arr: Array<Int>, size: Int, x: Int, y: Int): Unit
	{
		if (size <= 1)
		{
			// Not more than 2 elements
			return;
		}
		var e1: Int = -1;
		var e2: Int = -1;
		var diff: Int = -1;
		var i: Int = 0;
		while (i<size)
		{
			if (arr[i] == x)
			{
				// When get a new x element
				if (x == y)
				{
					// When x and y is similar
					if (e1 == -1)
					{
						// Get first element
						e1 = i;
					}
					else
					if (e2 == -1)
					{
						// Get second element
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
					else
					{
						e1 = e2;
						e2 = i;
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
				else
				{
					e1 = i;
					if (e2 != -1)
					{
						// Get distance
						diff = this.distance(e1, e2, diff);
					}
				}
			}
			else
			if (arr[i] == y)
			{
				// When get a new y element
				e2 = i;
				if (e1 != -1)
				{
					// Get distance
					diff = this.distance(e1, e2, diff);
				}
			}
			i += 1;
		}
		if (e1 == -1 || e2 == -1)
		{
			print("\n Pair (" + x + "," + y + ") are not exists");
		}
		else
		{
			print(" Minimum distance of pair (" + x + "," + y + ") is : " + diff + " \n");
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: FindDistance = FindDistance();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(4, 2, 8, 2, 3, 7, 1, 4, 6, 1, 4, 6, 8, 2, 3);
	// Get the size
	var size: Int = arr.count();
	print(" Array Element \n");
	task.printArray(arr, size);
	// x = 2, y = 6
	task.minimumDistance(arr, size, 2, 6);
	// x = 4, y = 4
	task.minimumDistance(arr, size, 4, 4);
}

Output

 Array Element
  4  2  8  2  3  7  1  4  6  1  4  6  8  2  3
 Minimum distance of pair (2,6) is : 2
 Minimum distance of pair (4,4) is : 3

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.

New Comment







© 2021, kalkicode.com, All rights reserved