Find closest higher value for every element in array

Here given code implementation process.

// C Program 
// Find closest higher value for every element in array
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// When higher element not exist then returning value of -1
int findClosest(int arr[], int size, int element)
{
	int result = -1;
	for (int i = 0; i < size; ++i)
	{
		if (arr[i] > element)
		{
			if (result == -1 || arr[result] > arr[i])
			{
				// When get higher value
				result = i;
			}
		}
	}
	return result;
}
// Finding higher closest element of each elements
void findClosestHigher(int arr[], int size)
{
	if (size <= 1)
	{
		// When have less than 2 elements
		return;
	}
	// Display array elements
	printf("\n  Given Array \n");
	display(arr, size);
	int location = -1;
	printf("  Closest higher is \n");
	// Execute loop through by array size
	for (int i = 0; i < size; ++i)
	{
		location = findClosest(arr, size, arr[i]);
		if (location == -1)
		{
			// When no higher closest
			printf("  _");
		}
		else
		{
			// Print higher value
			printf("  %d", arr[location]);
		}
	}
}
int main(int argc, char const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		1 , 4 , 7 , 9 , 14 , 6 , 8 , 5 , 8 , 2
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	findClosestHigher(arr, size);
	return 0;
}

Output

  Given Array
  1  4  7  9  14  6  8  5  8  2
  Closest higher is
  2  5  8  14  _  7  9  6  9  4
/*
  Java Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("   " + arr[i] );
		}
		System.out.print("\n");
	}
	// When higher element not exist then returning value of -1
	public int findClosest(int[] arr, int size, int element)
	{
		int result = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
		}
		return result;
	}
	// Finding higher closest element of each elements
	public void findClosestHigher(int[] arr, int size)
	{
		if (size <= 1)
		{
			// When have less than 2 elements
			return;
		}
		// Display array elements
		System.out.print("\n Given Array \n");
		display(arr, size);
		int location = -1;
		System.out.print(" Closest higher is \n");
		// Execute loop through by array size
		for (int i = 0; i < size; ++i)
		{
			location = findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				System.out.print("   _");
			} 
			else
			{
				// Print higher value
				System.out.print("   " + arr[location] );
			}
		}
	}
	public static void main(String[] args)
	{
		Closest task = new Closest();
		// Define array of integer elements
		int[] arr = {
			1 , 4 , 7 , 9 , 14 , 6 , 8 , 5 , 8 , 2
		};
		// Get the size
		int size = arr.length;
		// Test
		task.findClosestHigher(arr, size);
	}
}

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Find closest higher value for every element in array
*/

class Closest
{
	public:
		//Function which is display array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "   " << arr[i];
			}
			cout << "\n";
		}
	// When higher element not exist then returning value of -1
	int findClosest(int arr[], int size, int element)
	{
		int result = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
		}
		return result;
	}
	// Finding higher closest element of each elements
	void findClosestHigher(int arr[], int size)
	{
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		cout << "\n Given Array \n";
		this->display(arr, size);
		int location = -1;
		cout << " Closest higher is \n";
		// Execute loop through by array size
		for (int i = 0; i < size; ++i)
		{
			location = this->findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				cout << "   _";
			}
			else
			{
				// Print higher value
				cout << "   " << arr[location];
			}
		}
	}
};
int main()
{
	Closest task = Closest();
	// Define array of integer elements
	int arr[] = {
		1 , 4 , 7 , 9 , 14 , 6 , 8 , 5 , 8 , 2
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	// Test
	task.findClosestHigher(arr, size);
	return 0;
}

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
// Include namespace system
using System;
/*
  C# Program for
  Find closest higher value for every element in array
*/
public class Closest
{
	//Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("   " + arr[i]);
		}
		Console.Write("\n");
	}
	// When higher element not exist then returning value of -1
	public int findClosest(int[] arr, int size, int element)
	{
		int result = -1;
		for (int i = 0; i < size; ++i)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
		}
		return result;
	}
	// Finding higher closest element of each elements
	public void findClosestHigher(int[] arr, int size)
	{
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		Console.Write("\n Given Array \n");
		display(arr, size);
		int location = -1;
		Console.Write(" Closest higher is \n");
		// Execute loop through by array size
		for (int i = 0; i < size; ++i)
		{
			location = findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				Console.Write("   _");
			}
			else
			{
				// Print higher value
				Console.Write("   " + arr[location]);
			}
		}
	}
	public static void Main(String[] args)
	{
		Closest task = new Closest();
		// Define array of integer elements
		int[] arr = {
			1 , 4 , 7 , 9 , 14 , 6 , 8 , 5 , 8 , 2
		};
		// Get the size
		int size = arr.Length;
		// Test
		task.findClosestHigher(arr, size);
	}
}

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
<?php
/*
  Php Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	public	function display( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "   ". $arr[$i];
		}
		echo "\n";
	}
	// When higher element not exist then returning value of -1
	public	function findClosest( $arr, $size, $element)
	{
		$result = -1;
		for ($i = 0; $i < $size; ++$i)
		{
			if ($arr[$i] > $element)
			{
				if ($result == -1 || $arr[$result] > $arr[$i])
				{
					// When get higher value
					$result = $i;
				}
			}
		}
		return $result;
	}
	// Finding higher closest element of each elements
	public	function findClosestHigher( $arr, $size)
	{
		// When have less than 2 elements
		if ($size <= 1)
		{
			return;
		}
		// Display array elements
		echo "\n Given Array \n";
		$this->display($arr, $size);
		$location = -1;
		echo " Closest higher is \n";
		// Execute loop through by array size
		for ($i = 0; $i < $size; ++$i)
		{
			$location = $this->findClosest($arr, $size, $arr[$i]);
			if ($location == -1)
			{
				// When no higher closest
				echo "   _";
			}
			else
			{
				// Print higher value
				echo "   ". $arr[$location];
			}
		}
	}
}

function main()
{
	$task = new Closest();
	// Define array of integer elements
	$arr = array(1, 4, 7, 9, 14, 6, 8, 5, 8, 2);
	// Get the size
	$size = count($arr);
	// Test
	$task->findClosestHigher($arr, $size);
}
main();

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
/*
  Node Js Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("   " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// When higher element not exist then returning value of -1
	findClosest(arr, size, element)
	{
		var result = -1;
		for (var i = 0; i < size; ++i)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
		}
		return result;
	}
	// Finding higher closest element of each elements
	findClosestHigher(arr, size)
	{
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		process.stdout.write("\n Given Array \n");
		this.display(arr, size);
		var location = -1;
		process.stdout.write(" Closest higher is \n");
		// Execute loop through by array size
		for (var i = 0; i < size; ++i)
		{
			location = this.findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				process.stdout.write("   _");
			}
			else
			{
				// Print higher value
				process.stdout.write("   " + arr[location]);
			}
		}
	}
}

function main()
{
	var task = new Closest();
	// Define array of integer elements
	var arr = [1, 4, 7, 9, 14, 6, 8, 5, 8, 2];
	// Get the size
	var size = arr.length;
	// Test
	task.findClosestHigher(arr, size);
}
main();

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
#   Python 3 Program for
#   Find closest higher value for every element in array

class Closest :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  When higher element not exist then returning value of -1
	def findClosest(self, arr, size, element) :
		result = -1
		i = 0
		while (i < size) :
			if (arr[i] > element) :
				if (result == -1 or arr[result] > arr[i]) :
					#  When get higher value
					result = i
				
			
			i += 1
		
		return result
	
	#  Finding higher closest element of each elements
	def findClosestHigher(self, arr, size) :
		#  When have less than 2 elements
		if (size <= 1) :
			return
		
		#  Display array elements
		print("\n Given Array ")
		self.display(arr, size)
		location = -1
		print(" Closest higher is ")
		i = 0
		#  Execute loop through by array size
		while (i < size) :
			location = self.findClosest(arr, size, arr[i])
			if (location == -1) :
				#  When no higher closest
				print("  _", end = "")
			else :
				#  Print higher value
				print("  ", arr[location], end = "")
			
			i += 1
		
	

def main() :
	task = Closest()
	#  Define array of integer elements
	arr = [1, 4, 7, 9, 14, 6, 8, 5, 8, 2]
	#  Get the size
	size = len(arr)
	#  Test
	task.findClosestHigher(arr, size)

if __name__ == "__main__": main()

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14  _   7   9   6   9   4
#   Ruby Program for
#   Find closest higher value for every element in array

class Closest 
	# Function which is display array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print("   ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  When higher element not exist then returning value of -1
	def findClosest(arr, size, element) 
		result = -1
		i = 0
		while (i < size) 
			if (arr[i] > element) 
				if (result == -1 || arr[result] > arr[i]) 
					#  When get higher value
					result = i
				end

			end

			i += 1
		end

		return result
	end

	#  Finding higher closest element of each elements
	def findClosestHigher(arr, size) 
		#  When have less than 2 elements
		if (size <= 1) 
			return
		end

		#  Display array elements
		print("\n Given Array \n")
		self.display(arr, size)
		location = -1
		print(" Closest higher is \n")
		i = 0
		#  Execute loop through by array size
		while (i < size) 
			location = self.findClosest(arr, size, arr[i])
			if (location == -1) 
				#  When no higher closest
				print("   _")
			else 
				#  Print higher value
				print("   ", arr[location])
			end

			i += 1
		end

	end

end

def main() 
	task = Closest.new()
	#  Define array of integer elements
	arr = [1, 4, 7, 9, 14, 6, 8, 5, 8, 2]
	#  Get the size
	size = arr.length
	#  Test
	task.findClosestHigher(arr, size)
end

main()

Output

 Given Array 
   1   4   7   9   14   6   8   5   8   2
 Closest higher is 
   2   5   8   14   _   7   9   6   9   4
/*
  Scala Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("   " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// When higher element not exist then returning value of -1
	def findClosest(arr: Array[Int], size: Int, element: Int): Int = {
		var result: Int = -1;
		var i: Int = 0;
		while (i < size)
		{
			if (arr(i) > element)
			{
				if (result == -1 || arr(result) > arr(i))
				{
					// When get higher value
					result = i;
				}
			}
			i += 1;
		}
		return result;
	}
	// Finding higher closest element of each elements
	def findClosestHigher(arr: Array[Int], size: Int): Unit = {
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		print("\n Given Array \n");
		this.display(arr, size);
		var location: Int = -1;
		print(" Closest higher is \n");
		var i: Int = 0;
		// Execute loop through by array size
		while (i < size)
		{
			location = this.findClosest(arr, size, arr(i));
			if (location == -1)
			{
				// When no higher closest
				print("   _");
			}
			else
			{
				// Print higher value
				print("   " + arr(location));
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Closest = new Closest();
		// Define array of integer elements
		var arr: Array[Int] = Array(1, 4, 7, 9, 14, 6, 8, 5, 8, 2);
		// Get the size
		var size: Int = arr.length;
		// Test
		task.findClosestHigher(arr, size);
	}
}

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
/*
  Swift 4 Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// When higher element not exist then returning value of -1
	func findClosest(_ arr: [Int], _ size: Int, _ element: Int)->Int
	{
		var result: Int = -1;
		var i: Int = 0;
		while (i < size)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
			i += 1;
		}
		return result;
	}
	// Finding higher closest element of each elements
	func findClosestHigher(_ arr: [Int], _ size: Int)
	{
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		print("\n Given Array ");
		self.display(arr, size);
		var location: Int = -1;
		print(" Closest higher is ");
		var i: Int = 0;
		// Execute loop through by array size
		while (i < size)
		{
			location = self.findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				print("   _", terminator: "");
			}
			else
			{
				// Print higher value
				print("  ", arr[location], terminator: "");
			}
			i += 1;
		}
	}
}
func main()
{
	let task: Closest = Closest();
	// Define array of integer elements
	let arr: [Int] = [1, 4, 7, 9, 14, 6, 8, 5, 8, 2];
	// Get the size
	let size: Int = arr.count;
	// Test
	task.findClosestHigher(arr, size);
}
main();

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4
/*
  Kotlin Program for
  Find closest higher value for every element in array
*/
class Closest
{
	//Function which is display array elements
	fun display(arr: Array<Int> , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print("   " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// When higher element not exist then returning value of -1
	fun findClosest(arr: Array<Int> , size: Int, element: Int): Int
	{
		var result: Int = -1;
		var i: Int = 0;
		while (i < size)
		{
			if (arr[i] > element)
			{
				if (result == -1 || arr[result] > arr[i])
				{
					// When get higher value
					result = i;
				}
			}
			i += 1;
		}
		return result;
	}
	// Finding higher closest element of each elements
	fun findClosestHigher(arr: Array < Int > , size: Int): Unit
	{
		// When have less than 2 elements
		if (size <= 1)
		{
			return;
		}
		// Display array elements
		print("\n Given Array \n");
		this.display(arr, size);
		var location: Int;
		print(" Closest higher is \n");
		var i: Int = 0;
		// Execute loop through by array size
		while (i < size)
		{
			location = this.findClosest(arr, size, arr[i]);
			if (location == -1)
			{
				// When no higher closest
				print("   _");
			}
			else
			{
				// Print higher value
				print("   " + arr[location]);
			}
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Closest = Closest();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(1, 4, 7, 9, 14, 6, 8, 5, 8, 2);
	// Get the size
	var size: Int = arr.count();
	// Test
	task.findClosestHigher(arr, size);
}

Output

 Given Array
   1   4   7   9   14   6   8   5   8   2
 Closest higher is
   2   5   8   14   _   7   9   6   9   4


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