Assign Mice to Holes

Here given code implementation process.

import java.util.Arrays;
/*
    Java program for
    Assign Mice to Holes
*/
public class MiceHoles
{
	int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void assignHole(int[] mices, int[] holes, int n, int m)
	{
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		int result = 0;
		// Sort given data
		Arrays.sort(holes);
		Arrays.sort(mices);
		// Find max absolute distance
		for (int i = 0; i < n; ++i)
		{
			if (result < absValue(mices[i] - holes[i]))
			{
				result = absValue(mices[i] - holes[i]);
			}
		}
		System.out.println(result);
	}
	public static void main(String[] args)
	{
		MiceHoles task = new MiceHoles();
		// Position of mouses  
		int mices[] = {
			-3 , 7 , 5 , 9 , 16
		};
		// Position of holes
		int holes[] = {
			1 , 9 , 15 , 4 , 14
		};
		int n = mices.length;
		int m = holes.length;
		/*     
		      -3  -2  -1  0  1     4          9        14  15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉                      ♉    ♉  ♉                ♉
		       
		      -3                      5    7  9               16
		                             Mics position   

		    ---------------------------------------------------

		      Step ➀ position -3  Mice are enter in nearest holes

		      -3  -2  -1  0  1     4          9         14 15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
		       
		      -3   ← Mics position   

		      
		      Time to move : 4

		    ---------------------------------------------------

		      Step ➁ position 5 Mice are enter in nearest holes 4

		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────☉──────────☉─────────☉──☉─────
		                           └  ♉    ♉  ♉                ♉
		       
		                              5 ← Mics position   

		      
		      Time to move : 1

		    ---------------------------------------------------

		       mice ➂ position 7 are enter in nearest holes 9

		      -3  -2  -1  0  1     4          9        14  15
		    ──────────────────────────────────☉─────────☉──☉─────
		                                  ♉ - ┘               ♉
		                                      ♉      
		                                  7 ← Mics position   

		      
		      Time to move : 2

		    ---------------------------------------------------

		       mice ➃ position 9 are enter in nearest holes 14

		      -3  -2  -1  0  1     4          9        14  15
		    ────────────────────────────────────────────☉───☉─────
		                                      ♉ -- - -  ┘      ♉
		                                            
		                                      9 ← Mics position   

		      
		      Time to move : 5

		    --------------------------------------------------

		       mice ➄ position 16 are enter in nearest holes 15

		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────────────────────────────☉─────
		                                                   └  ♉
		                                            
		                                    Mics position->16  

		      
		      Time to move : 1
		    ----------------------------------------------------

		    Result = 5

		*/
		task.assignHole(mices, holes, n, m);
	}
}

Output

5
// Include header file
#include <iostream>
#include <algorithm>

using namespace std;
/*
    C++ program for
    Assign Mice to Holes
*/
class MiceHoles
{
	public: int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	void assignHole(int mices[], int holes[], int n, int m)
	{
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		int result = 0;
		// Sort given data
		sort(holes, holes + n);
		sort(mices, mices + m);
		// Find max absolute distance
		for (int i = 0; i < n; ++i)
		{
			if (result < this->absValue(mices[i] - holes[i]))
			{
				result = this->absValue(mices[i] - holes[i]);
			}
		}
		cout << result << endl;
	}
};
int main()
{
	MiceHoles *task = new MiceHoles();
	// Position of mouses
	int mices[] = {
		-3 , 7 , 5 , 9 , 16
	};
	// Position of holes
	int holes[] = {
		1 , 9 , 15 , 4 , 14
	};
	int n = sizeof(mices) / sizeof(mices[0]);
	int m = sizeof(holes) / sizeof(holes[0]);
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	task->assignHole(mices, holes, n, m);
	return 0;
}

Output

5
// Include namespace system
using System;
/*
    Csharp program for
    Assign Mice to Holes
*/
public class MiceHoles
{
	int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void assignHole(int[] mices, int[] holes, int n, int m)
	{
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		int result = 0;
		// Sort given data
		Array.Sort(holes);
		Array.Sort(mices);
		// Find max absolute distance
		for (int i = 0; i < n; ++i)
		{
			if (result < this.absValue(mices[i] - holes[i]))
			{
				result = this.absValue(mices[i] - holes[i]);
			}
		}
		Console.WriteLine(result);
	}
	public static void Main(String[] args)
	{
		MiceHoles task = new MiceHoles();
		// Position of mouses
		int[] mices = {
			-3 , 7 , 5 , 9 , 16
		};
		// Position of holes
		int[] holes = {
			1 , 9 , 15 , 4 , 14
		};
		int n = mices.Length;
		int m = holes.Length;
		/*
		      -3  -2  -1  0  1     4          9        14  15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉                      ♉    ♉  ♉                ♉
		       
		      -3                      5    7  9               16
		                             Mics position   
		    ---------------------------------------------------
		      Step ➀ position -3  Mice are enter in nearest holes
		      -3  -2  -1  0  1     4          9         14 15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
		       
		      -3   ← Mics position   
		      
		      Time to move : 4
		    ---------------------------------------------------
		      Step ➁ position 5 Mice are enter in nearest holes 4
		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────☉──────────☉─────────☉──☉─────
		                           └  ♉    ♉  ♉                ♉
		       
		                              5 ← Mics position   
		      
		      Time to move : 1
		    ---------------------------------------------------
		       mice ➂ position 7 are enter in nearest holes 9
		      -3  -2  -1  0  1     4          9        14  15
		    ──────────────────────────────────☉─────────☉──☉─────
		                                  ♉ - ┘               ♉
		                                      ♉      
		                                  7 ← Mics position   
		      
		      Time to move : 2
		    ---------------------------------------------------
		       mice ➃ position 9 are enter in nearest holes 14
		      -3  -2  -1  0  1     4          9        14  15
		    ────────────────────────────────────────────☉───☉─────
		                                      ♉ -- - -  ┘      ♉
		                                            
		                                      9 ← Mics position   
		      
		      Time to move : 5
		    --------------------------------------------------
		       mice ➄ position 16 are enter in nearest holes 15
		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────────────────────────────☉─────
		                                                   └  ♉
		                                            
		                                    Mics position->16  
		      
		      Time to move : 1
		    ----------------------------------------------------
		    Result = 5
		*/
		task.assignHole(mices, holes, n, m);
	}
}

Output

5
package main
import "sort"
import "fmt"
/*
    Go program for
    Assign Mice to Holes
*/

func absValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func assignHole(mices[] int, holes[] int, n int, m int) {
	if n != m {
		// When mice and holes are not equal
		return
	}
	var result int = 0
	// Sort given data
	sort.Ints(holes)
	sort.Ints(mices)
	// Find max absolute distance
	for i := 0 ; i < n ; i++ {
		if result < absValue(mices[i] - holes[i]) {
			result = absValue(mices[i] - holes[i])
		}
	}
	fmt.Println(result)
}
func main() {
	
	// Position of mouses
	var mices = [] int {-3 , 7 , 5 , 9 , 16}
	// Position of holes
	var holes = [] int {1 , 9 , 15 , 4 , 14}
	var n int = len(mices)
	var m int = len(holes)
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	assignHole(mices, holes, n, m)
}

Output

5
<?php
/*
    Php program for
    Assign Mice to Holes
*/
class MiceHoles
{
	function absValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function assignHole($mices, $holes, $n, $m)
	{
		if ($n != $m)
		{
			// When mice and holes are not equal
			return;
		}
		$result = 0;
		// Sort given data
		sort($holes);
		sort($mices);
		// Find max absolute distance
		for ($i = 0; $i < $n; ++$i)
		{
			if ($result < $this->absValue($mices[$i] - $holes[$i]))
			{
				$result = $this->absValue($mices[$i] - $holes[$i]);
			}
		}
		echo($result.
			"\n");
	}
}

function main()
{
	$task = new MiceHoles();
	// Position of mouses
	$mices = array(-3, 7, 5, 9, 16);
	// Position of holes
	$holes = array(1, 9, 15, 4, 14);
	$n = count($mices);
	$m = count($holes);
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	$task->assignHole($mices, $holes, $n, $m);
}
main();

Output

5
/*
    Node JS program for
    Assign Mice to Holes
*/
class MiceHoles
{
	absValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	assignHole(mices, holes, n, m)
	{
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		var result = 0;
		// Sort given data
		holes.sort(function(a, b)
		{
			return a - b;
		});
		mices.sort(function(a, b)
		{
			return a - b;
		});
		// Find max absolute distance
		for (var i = 0; i < n; ++i)
		{
			if (result < this.absValue(mices[i] - holes[i]))
			{
				result = this.absValue(mices[i] - holes[i]);
			}
		}
		console.log(result);
	}
}

function main()
{
	var task = new MiceHoles();
	// Position of mouses
	var mices = [-3, 7, 5, 9, 16];
	// Position of holes
	var holes = [1, 9, 15, 4, 14];
	var n = mices.length;
	var m = holes.length;
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	task.assignHole(mices, holes, n, m);
}
main();

Output

5
#    Python 3 program for
#    Assign Mice to Holes
class MiceHoles :
	def absValue(self, x) :
		if (x < 0) :
			return -x
		
		return x
	
	def assignHole(self, mices, holes, n, m) :
		if (n != m) :
			#  When mice and holes are not equal
			return
		
		result = 0
		#  Sort given data
		holes.sort()
		mices.sort()
		i = 0
		#  Find max absolute distance
		while (i < n) :
			if (result < self.absValue(mices[i] - holes[i])) :
				result = self.absValue(mices[i] - holes[i])
			
			i += 1
		
		print(result)
	

def main() :
	task = MiceHoles()
	#  Position of mouses
	mices = [-3, 7, 5, 9, 16]
	#  Position of holes
	holes = [1, 9, 15, 4, 14]
	n = len(mices)
	m = len(holes)
	#      -3  -2  -1  0  1     4          9        14  15
	#    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	#       ♉                      ♉    ♉  ♉                ♉
	#      -3                      5    7  9               16
	#                             Mics position   
	#    ---------------------------------------------------
	#      Step ➀ position -3  Mice are enter in nearest holes
	#      -3  -2  -1  0  1     4          9         14 15
	#    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	#       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	#      -3   ← Mics position   
	#      Time to move : 4
	#    ---------------------------------------------------
	#      Step ➁ position 5 Mice are enter in nearest holes 4
	#      -3  -2  -1  0  1     4          9        14  15
	#    ───────────────────────☉──────────☉─────────☉──☉─────
	#                           └  ♉    ♉  ♉                ♉
	#                              5 ← Mics position   
	#      Time to move : 1
	#    ---------------------------------------------------
	#       mice ➂ position 7 are enter in nearest holes 9
	#      -3  -2  -1  0  1     4          9        14  15
	#    ──────────────────────────────────☉─────────☉──☉─────
	#                                  ♉ - ┘               ♉
	#                                      ♉      
	#                                  7 ← Mics position   
	#      Time to move : 2
	#    ---------------------------------------------------
	#       mice ➃ position 9 are enter in nearest holes 14
	#      -3  -2  -1  0  1     4          9        14  15
	#    ────────────────────────────────────────────☉───☉─────
	#                                      ♉ -- - -  ┘      ♉
	#                                      9 ← Mics position   
	#      Time to move : 5
	#    --------------------------------------------------
	#       mice ➄ position 16 are enter in nearest holes 15
	#      -3  -2  -1  0  1     4          9        14  15
	#    ───────────────────────────────────────────────☉─────
	#                                                   └  ♉
	#                                    Mics position->16  
	#      Time to move : 1
	#    ----------------------------------------------------
	#    Result = 5
	task.assignHole(mices, holes, n, m)

if __name__ == "__main__": main()

Output

5
#    Ruby program for
#    Assign Mice to Holes
class MiceHoles 
	def absValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def assignHole(mices, holes, n, m) 
		if (n != m) 
			#  When mice and holes are not equal
			return
		end

		result = 0
		#  Sort given data
		holes = holes.sort
		mices = mices.sort
		i = 0
		#  Find max absolute distance
		while (i < n) 
			if (result < self.absValue(mices[i] - holes[i])) 
				result = self.absValue(mices[i] - holes[i])
			end

			i += 1
		end

		print(result, "\n")
	end

end

def main() 
	task = MiceHoles.new()
	#  Position of mouses
	mices = [-3, 7, 5, 9, 16]
	#  Position of holes
	holes = [1, 9, 15, 4, 14]
	n = mices.length
	m = holes.length
	#      -3  -2  -1  0  1     4          9        14  15
	#    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	#       ♉                      ♉    ♉  ♉                ♉
	#      -3                      5    7  9               16
	#                             Mics position   
	#    ---------------------------------------------------
	#      Step ➀ position -3  Mice are enter in nearest holes
	#      -3  -2  -1  0  1     4          9         14 15
	#    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	#       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	#      -3   ← Mics position   
	#      Time to move : 4
	#    ---------------------------------------------------
	#      Step ➁ position 5 Mice are enter in nearest holes 4
	#      -3  -2  -1  0  1     4          9        14  15
	#    ───────────────────────☉──────────☉─────────☉──☉─────
	#                           └  ♉    ♉  ♉                ♉
	#                              5 ← Mics position   
	#      Time to move : 1
	#    ---------------------------------------------------
	#       mice ➂ position 7 are enter in nearest holes 9
	#      -3  -2  -1  0  1     4          9        14  15
	#    ──────────────────────────────────☉─────────☉──☉─────
	#                                  ♉ - ┘               ♉
	#                                      ♉      
	#                                  7 ← Mics position   
	#      Time to move : 2
	#    ---------------------------------------------------
	#       mice ➃ position 9 are enter in nearest holes 14
	#      -3  -2  -1  0  1     4          9        14  15
	#    ────────────────────────────────────────────☉───☉─────
	#                                      ♉ -- - -  ┘      ♉
	#                                      9 ← Mics position   
	#      Time to move : 5
	#    --------------------------------------------------
	#       mice ➄ position 16 are enter in nearest holes 15
	#      -3  -2  -1  0  1     4          9        14  15
	#    ───────────────────────────────────────────────☉─────
	#                                                   └  ♉
	#                                    Mics position->16  
	#      Time to move : 1
	#    ----------------------------------------------------
	#    Result = 5
	task.assignHole(mices, holes, n, m)
end

main()

Output

5
import scala.collection.mutable._;
/*
    Scala program for
    Assign Mice to Holes
*/
class MiceHoles()
{
	def absValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def assignHole(mices: Array[Int], 
      holes: Array[Int], n: Int, m: Int): Unit = {
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		var result: Int = 0;
		// Sort given data
		var ho = holes.sorted;
		var mi = mices.sorted;
       
		var i: Int = 0;
		// Find max absolute distance
		while (i < n)
		{
			if (result < absValue(mi(i) - ho(i)))
			{
				result = absValue(mi(i) - ho(i));
			}
			i += 1;
		}
		println(result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: MiceHoles = new MiceHoles();
		// Position of mouses
		var mices: Array[Int] = Array(-3, 7, 5, 9, 16);
		// Position of holes
		var holes: Array[Int] = Array(1, 9, 15, 4, 14);
		var n: Int = mices.length;
		var m: Int = holes.length;
		/*
		      -3  -2  -1  0  1     4          9        14  15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉                      ♉    ♉  ♉                ♉
		       
		      -3                      5    7  9               16
		                             Mics position   
		    ---------------------------------------------------
		      Step ➀ position -3  Mice are enter in nearest holes
		      -3  -2  -1  0  1     4          9         14 15
		    ─────────────────☉─────☉──────────☉─────────☉──☉─────
		       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
		       
		      -3   ← Mics position   
		      
		      Time to move : 4
		    ---------------------------------------------------
		      Step ➁ position 5 Mice are enter in nearest holes 4
		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────☉──────────☉─────────☉──☉─────
		                           └  ♉    ♉  ♉                ♉
		       
		                              5 ← Mics position   
		      
		      Time to move : 1
		    ---------------------------------------------------
		       mice ➂ position 7 are enter in nearest holes 9
		      -3  -2  -1  0  1     4          9        14  15
		    ──────────────────────────────────☉─────────☉──☉─────
		                                  ♉ - ┘               ♉
		                                      ♉      
		                                  7 ← Mics position   
		      
		      Time to move : 2
		    ---------------------------------------------------
		       mice ➃ position 9 are enter in nearest holes 14
		      -3  -2  -1  0  1     4          9        14  15
		    ────────────────────────────────────────────☉───☉─────
		                                      ♉ -- - -  ┘      ♉
		                                            
		                                      9 ← Mics position   
		      
		      Time to move : 5
		    --------------------------------------------------
		       mice ➄ position 16 are enter in nearest holes 15
		      -3  -2  -1  0  1     4          9        14  15
		    ───────────────────────────────────────────────☉─────
		                                                   └  ♉
		                                            
		                                    Mics position->16  
		      
		      Time to move : 1
		    ----------------------------------------------------
		    Result = 5
		*/
		task.assignHole(mices, holes, n, m);
	}
}

Output

5
import Foundation;
/*
    Swift 4 program for
    Assign Mice to Holes
*/
class MiceHoles
{
	func absValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func assignHole(_ mices: [Int], _ holes: [Int], _ n: Int, _ m: Int)
	{
		if (n  != m)
		{
			// When mice and holes are not equal
			return;
		}
		var result: Int = 0;
		// Sort given data
		let ho = holes.sorted();
		let mi = mices.sorted();
		var i: Int = 0;
		// Find max absolute distance
		while (i < n)
		{
			if (result < self.absValue(mi[i] - ho[i]))
			{
				result = self.absValue(mi[i] - ho[i]);
			}
			i += 1;
		}
		print(result);
	}
}
func main()
{
	let task: MiceHoles = MiceHoles();
	// Position of mouses
	let mices: [Int] = [-3, 7, 5, 9, 16];
	// Position of holes
	let holes: [Int] = [1, 9, 15, 4, 14];
	let n: Int = mices.count;
	let m: Int = holes.count;
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	task.assignHole(mices, holes, n, m);
}
main();

Output

5
/*
    Kotlin program for
    Assign Mice to Holes
*/
class MiceHoles
{
	fun absValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun assignHole(mices: Array < Int > , 
                   holes: Array < Int > , n: Int, m: Int): Unit
	{
		if (n != m)
		{
			// When mice and holes are not equal
			return;
		}
		var result: Int = 0;
		// Sort given data
		holes.sort();
		mices.sort();
		var i: Int = 0;
		// Find max absolute distance
		while (i < n)
		{
			if (result < this.absValue(mices[i] - holes[i]))
			{
				result = this.absValue(mices[i] - holes[i]);
			}
			i += 1;
		}
		println(result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: MiceHoles = MiceHoles();
	// Position of mouses
	val mices: Array < Int > = arrayOf(-3, 7, 5, 9, 16);
	// Position of holes
	val holes: Array < Int > = arrayOf(1, 9, 15, 4, 14);
	val n: Int = mices.count();
	val m: Int = holes.count();
	/*
	      -3  -2  -1  0  1     4          9        14  15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉                      ♉    ♉  ♉                ♉
	       
	      -3                      5    7  9               16
	                             Mics position   
	    ---------------------------------------------------
	      Step ➀ position -3  Mice are enter in nearest holes
	      -3  -2  -1  0  1     4          9         14 15
	    ─────────────────☉─────☉──────────☉─────────☉──☉─────
	       ♉  -    -  -  ┘        ♉    ♉  ♉                ♉   
	       
	      -3   ← Mics position   
	      
	      Time to move : 4
	    ---------------------------------------------------
	      Step ➁ position 5 Mice are enter in nearest holes 4
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────☉──────────☉─────────☉──☉─────
	                           └  ♉    ♉  ♉                ♉
	       
	                              5 ← Mics position   
	      
	      Time to move : 1
	    ---------------------------------------------------
	       mice ➂ position 7 are enter in nearest holes 9
	      -3  -2  -1  0  1     4          9        14  15
	    ──────────────────────────────────☉─────────☉──☉─────
	                                  ♉ - ┘               ♉
	                                      ♉      
	                                  7 ← Mics position   
	      
	      Time to move : 2
	    ---------------------------------------------------
	       mice ➃ position 9 are enter in nearest holes 14
	      -3  -2  -1  0  1     4          9        14  15
	    ────────────────────────────────────────────☉───☉─────
	                                      ♉ -- - -  ┘      ♉
	                                            
	                                      9 ← Mics position   
	      
	      Time to move : 5
	    --------------------------------------------------
	       mice ➄ position 16 are enter in nearest holes 15
	      -3  -2  -1  0  1     4          9        14  15
	    ───────────────────────────────────────────────☉─────
	                                                   └  ♉
	                                            
	                                    Mics position->16  
	      
	      Time to move : 1
	    ----------------------------------------------------
	    Result = 5
	*/
	task.assignHole(mices, holes, n, m);
}

Output

5


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