Welcome To Golang By Example

Find the first and last position of a target element in a sorted array in Go (Golang)

Table of Contents

Overview

The objective is to find the first and last position of an element in a sorted array.

Eg

Input: [1, 2, 2, 5]
Target: 2
Output: [1, 2]

Input: [1, 2, 5]
Target: 2
Output: [1, 1]

Input: []
Target: 1
Output: [-1,-1]

Strategy is to

Program

Below is the program for the same

package main

import "fmt"

func main() {
	output := searchRange([]int{1, 2, 2, 5}, 2)
	fmt.Println(output)

	output = searchRange([]int{1, 2, 5}, 2)
	fmt.Println(output)

	output = searchRange([]int{}, 1)
	fmt.Println(output)
}

func searchRange(nums []int, target int) []int {

	output := make([]int, 2)
	output[0] = findLeftPivot(nums, 0, len(nums)-1, target, len(nums))

	output[1] = findRightPivot(nums, 0, len(nums)-1, target, len(nums))

	return output
}

func findLeftPivot(nums []int, start, end, target, len int) int {
	if start > end {
		return -1
	}

	if start == end && nums[start] == target {
		return start
	}

	mid := (start + end) / 2

	if (mid == 0 || nums[mid-1] < nums[mid]) && nums[mid] == target {
		return mid
	}

	if target <= nums[mid] {
		return findLeftPivot(nums, start, mid-1, target, len)
	}

	return findLeftPivot(nums, mid+1, end, target, len)

}

func findRightPivot(nums []int, start, end, target, len int) int {
	if start > end {
		return -1
	}

	if start == end && nums[start] == target {
		return start

	}

	mid := (start + end) / 2

	if mid+1 <= end && nums[mid] == target && nums[mid] < nums[mid+1] {
		return mid
	}

	if (mid == len-1 || nums[mid] < nums[mid+1]) && nums[mid] == target {
		return mid - 1
	}

	if target >= nums[mid] {
		return findRightPivot(nums, mid+1, end, target, len)
	}

	return findRightPivot(nums, start, mid-1, target, len)

}

Output

[1 2]
[1 1]
[-1 -1]

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –All Design Patterns Golang