Skip to main content

Assigning mice to holes in kotlin

Kotlin program for Assigning mice to holes. Here more information.

/*
    Kotlin program for
    Assign mice to holes
*/
class Deliver
{
	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: Deliver = Deliver();
	// Position of mice
	val mices: Array < Int > = arrayOf(-3, 7, 5, 9, 16);
	// Position of holes
	val holes: Array < Int > = arrayOf(1, 9, 15, 4, 14);
	// Get the length
	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




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