🚀 Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

Challenge 18 - Min Rotations

👋 Welcome Gophers! In this challenge, you will be tasked with finding out how many rotations an ordered int slice has undergone and shifted by.

Examples:

arr := []int{1, 2, 3, 4, 5}
MinRotations(arr) // returns 2

arr := []int{3, 4, 5, 1, 2}
MinRotations(arr) // returns 3

See the Solution

View Solution
package main

import "fmt"

func MinRotations(array []int) int {
	// Implement me :)
	size := len(array)
	if size <= 1 {
		return 0
	}

	lowIndex := 0
	middleIndex := 0
	highIndex := size - 1
	for array[lowIndex] > array[highIndex] {
		middleIndex = (lowIndex + highIndex) / 2
		if array[middleIndex] > array[highIndex] {
			lowIndex = middleIndex + 1
		} else {
			highIndex = middleIndex
		}
	}
	return lowIndex
}

func main() {
	fmt.Println("Min Rotation Challenge")

	testArr := []int{15, 18, 2, 3, 6, 12}
	min := MinRotations(testArr) // returns 2
	fmt.Println(min)

	testArr2 := []int{7, 9, 11, 12, 5}
	min2 := MinRotations(testArr2) // return 4
	fmt.Println(min2)

}

Further Reading:

If you enjoyed this challenge, you may also enjoy some of the other challenges on this site:


Other Challenges