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 https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3#inputoutput for detailed explanation.

I used faster I/O in your solution, and it passed https://www.codechef.com/viewsolution/30117559

1 Like