GOLANG help

I am new to GO and tried to implement it to solve a simple beginner problem.

Solution : Link

Idk what is missing. I’m getting TLE for every case.
Can anyone help me with this?

Note : Solution has T*log N (base 2) time complexity

Hi @anon32925213,

You are using slow I/O as you are directly reading from STDIN and printing to STDOUT. You can switch to buffered I/O (buffio package), and flush the write buffer at the end.

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(s string, v ...interface{}) {
	fmt.Fscanf(reader, s, v...)
}

func printf(s string, v ...interface{}) {
	fmt.Fprintf(writer, s, v...)
}

func main() {
	defer writer.Flush() // flush only once, at the end
        // your implementation
}

See Using Golang for competitive programming | Codementor for detailed explanation.

I used faster I/O in your solution, and it passed CodeChef: Practical coding for everyone

2 Likes