Find closest number in array

Here given code implementation process.

// C Program 
// Find closest number in array
#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 absolute difference of given two numbers
int absoluteValue(int a, int b)
{
	if (a > b)
	{
		return a - b;
	}
	else
	{
		return b - a;
	}
}
//Find the closest value of given element in array
void closestNumber(int arr[], int size, int element)
{
	// Find the difference between a given element and 
	// The first element of the array
	int difference = absoluteValue(arr[0], element);
	int result = arr[0];
	for (int i = 1; i < size; ++i)
	{
		if (difference > absoluteValue(arr[i], element))
		{
			// When get new minimum difference
			difference = absoluteValue(arr[i], element);
			// Get result
			result = arr[i];
		}
	}
	// Display calculated result
	printf(" Closest of element %d is : %d\n", element, result);
}
int main(int argc, char
	const *argv[])
{
	int arr[] = {
		12 , 1 , 4 , -3 , 5 , 8 , 9 , 11 , 16
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	// Display array element
	printf(" Array element \n");
	printArray(arr, size);
	// Test cases
	// element = -2
	closestNumber(arr, size, -2);
	// element = 7
	closestNumber(arr, size, 7);
	// element = 4
	closestNumber(arr, size, 4);
	// element = 10
	closestNumber(arr, size, 10);
	return 0;
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
/*
    Java Program
    Find closest number in array
*/
public class ClosestElement
{
	//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 absolute difference of given two numbers
	public int absoluteValue(int a, int b)
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	public void closestNumber(int[] arr, int size, int element)
	{
		// Find the difference between a given element and 
		// The first element of the array
		int difference = absoluteValue(arr[0], element);
		int result = arr[0];
		for (int i = 1; i < size; ++i)
		{
			if (difference > absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
		}
		// Display calculated result
		System.out.print(" Closest of element " + element + " is : " + result + "\n");
	}
	public static void main(String[] args)
	{
		ClosestElement task = new ClosestElement();
		int[] arr = {
			12 , 1 , 4 , -3 , 5 , 8 , 9 , 11 , 16
		};
		// Get the size
		int size = arr.length;
		// Display array element
		System.out.print(" Array element \n");
		task.printArray(arr, size);
		// Test cases
		// element = -2
		task.closestNumber(arr, size, -2);
		// element = 7
		task.closestNumber(arr, size, 7);
		// element = 4
		task.closestNumber(arr, size, 4);
		// element = 10
		task.closestNumber(arr, size, 10);
	}
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Find closest number in array
*/

class ClosestElement
{
	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 absolute difference of given two numbers
	int absoluteValue(int a, int b)
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	void closestNumber(int arr[], int size, int element)
	{
		// Find the difference between a given element and
		// The first element of the array
		int difference = this->absoluteValue(arr[0], element);
		int result = arr[0];
		for (int i = 1; i < size; ++i)
		{
			if (difference > this->absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = this->absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
		}
		// Display calculated result
		cout << " Closest of element " << element << " is : " << result << "\n";
	}
};
int main()
{
	ClosestElement task = ClosestElement();
	int arr[] = {
		12 , 1 , 4 , -3 , 5 , 8 , 9 , 11 , 16
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	// Display array element
	cout << " Array element \n";
	task.printArray(arr, size);
	// Test cases
	// element = -2
	task.closestNumber(arr, size, -2);
	// element = 7
	task.closestNumber(arr, size, 7);
	// element = 4
	task.closestNumber(arr, size, 4);
	// element = 10
	task.closestNumber(arr, size, 10);
	return 0;
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
// Include namespace system
using System;
/*
    C# Program
    Find closest number in array
*/
public class ClosestElement
{
	//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 absolute difference of given two numbers
	public int absoluteValue(int a, int b)
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	public void closestNumber(int[] arr, int size, int element)
	{
		// Find the difference between a given element and
		// The first element of the array
		int difference = absoluteValue(arr[0], element);
		int result = arr[0];
		for (int i = 1; i < size; ++i)
		{
			if (difference > absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
		}
		// Display calculated result
		Console.Write(" Closest of element " + element + " is : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		ClosestElement task = new ClosestElement();
		int[] arr = {
			12 , 1 , 4 , -3 , 5 , 8 , 9 , 11 , 16
		};
		// Get the size
		int size = arr.Length;
		// Display array element
		Console.Write(" Array element \n");
		task.printArray(arr, size);
		// Test cases
		// element = -2
		task.closestNumber(arr, size, -2);
		// element = 7
		task.closestNumber(arr, size, 7);
		// element = 4
		task.closestNumber(arr, size, 4);
		// element = 10
		task.closestNumber(arr, size, 10);
	}
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
<?php
/*
    Php Program
    Find closest number in array
*/
class ClosestElement
{
	//Display elements of given array
	public	function printArray( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Returns the absolute difference of given two numbers
	public	function absoluteValue($a, $b)
	{
		if ($a > $b)
		{
			return $a - $b;
		}
		else
		{
			return $b - $a;
		}
	}
	// Find the closest value of given element in array
	public	function closestNumber( $arr, $size, $element)
	{
		// Find the difference between a given element and
		// The first element of the array
		$difference = $this->absoluteValue($arr[0], $element);
		$result = $arr[0];
		for ($i = 1; $i < $size; ++$i)
		{
			if ($difference > $this->absoluteValue($arr[$i], $element))
			{
				// When get new minimum difference
				$difference = $this->absoluteValue($arr[$i], $element);
				// Get result
				$result = $arr[$i];
			}
		}
		// Display calculated result
		echo " Closest of element ". $element ." is : ". $result ."\n";
	}
}

function main()
{
	$task = new ClosestElement();
	$arr = array(12, 1, 4, -3, 5, 8, 9, 11, 16);
	// Get the size
	$size = count($arr);
	// Display array element
	echo " Array element \n";
	$task->printArray($arr, $size);
	// Test cases
	// element = -2
	$task->closestNumber($arr, $size, -2);
	// element = 7
	$task->closestNumber($arr, $size, 7);
	// element = 4
	$task->closestNumber($arr, $size, 4);
	// element = 10
	$task->closestNumber($arr, $size, 10);
}
main();

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
/*
    Node Js Program
    Find closest number in array
*/
class ClosestElement
{
	//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 absolute difference of given two numbers
	absoluteValue(a, b)
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	closestNumber(arr, size, element)
	{
		// Find the difference between a given element and
		// The first element of the array
		var difference = this.absoluteValue(arr[0], element);
		var result = arr[0];
		for (var i = 1; i < size; ++i)
		{
			if (difference > this.absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = this.absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
		}
		// Display calculated result
		process.stdout.write(" Closest of element " + element + " is : " + result + "\n");
	}
}

function main()
{
	var task = new ClosestElement();
	var arr = [12, 1, 4, -3, 5, 8, 9, 11, 16];
	// Get the size
	var size = arr.length;
	// Display array element
	process.stdout.write(" Array element \n");
	task.printArray(arr, size);
	// Test cases
	// element = -2
	task.closestNumber(arr, size, -2);
	// element = 7
	task.closestNumber(arr, size, 7);
	// element = 4
	task.closestNumber(arr, size, 4);
	// element = 10
	task.closestNumber(arr, size, 10);
}
main();

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
#  Python 3 Program
#  Find closest number in array

class ClosestElement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		#  Execute loop through by size
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Returns the absolute difference of given two numbers
	def absoluteValue(self, a, b) :
		if (a > b) :
			return a - b
		else :
			return b - a
		
	
	#  Find the closest value of given element in array
	def closestNumber(self, arr, size, element) :
		#  Find the difference between a given element and
		#  The first element of the array
		difference = self.absoluteValue(arr[0], element)
		result = arr[0]
		i = 1
		#  Execute loop through by size
		while (i < size) :
			if (difference > self.absoluteValue(arr[i], element)) :
				#  When get new minimum difference
				difference = self.absoluteValue(arr[i], element)
				#  Get result
				result = arr[i]
			
			i += 1
		
		#  Display calculated result
		print(" Closest of element ", element ," is : ", result )
	

def main() :
	task = ClosestElement()
	arr = [12, 1, 4, -3, 5, 8, 9, 11, 16]
	#  Get the size
	size = len(arr)
	#  Display array element
	print(" Array element ")
	task.printArray(arr, size)
	#  Test cases
	#  element = -2
	task.closestNumber(arr, size, -2)
	#  element = 7
	task.closestNumber(arr, size, 7)
	#  element = 4
	task.closestNumber(arr, size, 4)
	#  element = 10
	task.closestNumber(arr, size, 10)

if __name__ == "__main__": main()

Output

 Array element
   12   1   4   -3   5   8   9   11   16
 Closest of element  -2  is :  -3
 Closest of element  7  is :  8
 Closest of element  4  is :  4
 Closest of element  10  is :  9
#   Ruby Program
#   Find closest number in array

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

		print("\n")
	end

	#  Returns the absolute difference of given two numbers
	def absoluteValue(a, b) 
		if (a > b) 
			return a - b
		else 
			return b - a
		end

	end

	#  Find the closest value of given element in array
	def closestNumber(arr, size, element) 
		#  Find the difference between a given element and
		#  The first element of the array
		difference = self.absoluteValue(arr[0], element)
		result = arr[0]
		i = 1
		#  Execute loop through by size
		while (i < size) 
			if (difference > self.absoluteValue(arr[i], element)) 
				#  When get new minimum difference
				difference = self.absoluteValue(arr[i], element)
				#  Get result
				result = arr[i]
			end

			i += 1
		end

		#  Display calculated result
		print(" Closest of element ", element ," is : ", result ,"\n")
	end

end

def main() 
	task = ClosestElement.new()
	arr = [12, 1, 4, -3, 5, 8, 9, 11, 16]
	#  Get the size
	size = arr.length
	#  Display array element
	print(" Array element \n")
	task.printArray(arr, size)
	#  Test cases
	#  element = -2
	task.closestNumber(arr, size, -2)
	#  element = 7
	task.closestNumber(arr, size, 7)
	#  element = 4
	task.closestNumber(arr, size, 4)
	#  element = 10
	task.closestNumber(arr, size, 10)
end

main()

Output

 Array element 
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
/*
    Scala Program
    Find closest number in array
*/
class ClosestElement
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Returns the absolute difference of given two numbers
	def absoluteValue(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	def closestNumber(arr: Array[Int], size: Int, element: Int): Unit = {
		// Find the difference between a given element and
		// The first element of the array
		var difference: Int = this.absoluteValue(arr(0), element);
		var result: Int = arr(0);
		var i: Int = 1;
		// Execute loop through by size
		while (i < size)
		{
			if (difference > this.absoluteValue(arr(i), element))
			{
				// When get new minimum difference
				difference = this.absoluteValue(arr(i), element);
				// Get result
				result = arr(i);
			}
			i += 1;
		}
		// Display calculated result
		print(" Closest of element " + element + " is : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ClosestElement = new ClosestElement();
		var arr: Array[Int] = Array(12, 1, 4, -3, 5, 8, 9, 11, 16);
		// Get the size
		var size: Int = arr.length;
		// Display array element
		print(" Array element \n");
		task.printArray(arr, size);
		// Test cases
		// element = -2
		task.closestNumber(arr, size, -2);
		// element = 7
		task.closestNumber(arr, size, 7);
		// element = 4
		task.closestNumber(arr, size, 4);
		// element = 10
		task.closestNumber(arr, size, 10);
	}
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9
/*
    Swift 4 Program
    Find closest number in array
*/
class ClosestElement
{
	// Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Returns the absolute difference of given two numbers
	func absoluteValue(_ a: Int, _ b: Int)->Int
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	func closestNumber(_ arr: [Int], _ size: Int, _ element: Int)
	{
		// Find the difference between a given element and
		// The first element of the array
		var difference: Int = self.absoluteValue(arr[0], element);
		var result: Int = arr[0];
		var i: Int = 1;
		// Execute loop through by size
		while (i < size)
		{
			if (difference > self.absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = self.absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
			i += 1;
		}
		// Display calculated result
		print(" Closest of element ", element ," is : ", result );
	}
}
func main()
{
	let task: ClosestElement = ClosestElement();
	let arr: [Int] = [12, 1, 4, -3, 5, 8, 9, 11, 16];
	// Get the size
	let size: Int = arr.count;
	// Display array element
	print(" Array element ");
	task.printArray(arr, size);
	// Test cases
	// element = -2
	task.closestNumber(arr, size, -2);
	// element = 7
	task.closestNumber(arr, size, 7);
	// element = 4
	task.closestNumber(arr, size, 4);
	// element = 10
	task.closestNumber(arr, size, 10);
}
main();

Output

 Array element
   12   1   4   -3   5   8   9   11   16
 Closest of element  -2  is :  -3
 Closest of element  7  is :  8
 Closest of element  4  is :  4
 Closest of element  10  is :  9
/*
    Kotlin Program
    Find closest number in array
*/
class ClosestElement
{
	// Display elements of given array
	fun printArray(arr: Array < Int > , size: Int): Unit
	{
		var i: Int = 0;
		// Execute loop through by size
		while (i < size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Returns the absolute difference of given two numbers
	fun absoluteValue(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a - b;
		}
		else
		{
			return b - a;
		}
	}
	// Find the closest value of given element in array
	fun closestNumber(arr: Array < Int > , size: Int, element: Int): Unit
	{
		// Find the difference between a given element and
		// The first element of the array
		var difference: Int = this.absoluteValue(arr[0], element);
		var result: Int = arr[0];
		var i: Int = 1;
		// Execute loop through by size
		while (i < size)
		{
			if (difference > this.absoluteValue(arr[i], element))
			{
				// When get new minimum difference
				difference = this.absoluteValue(arr[i], element);
				// Get result
				result = arr[i];
			}
			i += 1;
		}
		// Display calculated result
		print(" Closest of element " + element + " is : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: ClosestElement = ClosestElement();
	var arr: Array < Int > = arrayOf(12, 1, 4, -3, 5, 8, 9, 11, 16);
	// Get the size
	var size: Int = arr.count();
	// Display array element
	print(" Array element \n");
	task.printArray(arr, size);
	// Test cases
	// element = -2
	task.closestNumber(arr, size, -2);
	// element = 7
	task.closestNumber(arr, size, 7);
	// element = 4
	task.closestNumber(arr, size, 4);
	// element = 10
	task.closestNumber(arr, size, 10);
}

Output

 Array element
  12  1  4  -3  5  8  9  11  16
 Closest of element -2 is : -3
 Closest of element 7 is : 8
 Closest of element 4 is : 4
 Closest of element 10 is : 9


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