Posted on by Kalkicode
Code Greedy

Assigning mice to holes in golang

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

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 mice
	var mices = [] int { -3, 7, 5, 9, 16 }
	// Position of holes
	var holes = [] int { 1, 9, 15, 4, 14 }
	// Get the length
	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

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