Skip to main content

Assigning mice to holes in node js

Js program for Assigning mice to holes. Here problem description and explanation.

/*
    Node JS program for
    Assign mice to holes
*/
class Deliver
{
	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 Deliver();
	// Position of mice
	var mices = [-3, 7, 5, 9, 16];
	// Position of holes
	var holes = [1, 9, 15, 4, 14];
	// Get the length
	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);
}
// Start program execution
main();

Output

5




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment