Problem solution · Go

Codeforces 1493E — Enormous XOR

Codeforces 1493E — Enormous XOR: a Go solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Sliding window or two pointers
Source
EndlessCheng Codeforces Go
Length
42 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Codeforces 1493E — Enormous XOR, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 42 lines of Go from the credited upstream file 1493E.go.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1493E — Enormous XOR · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"strings") // github.com/EndlessCheng/codeforces-gofunc CF1493E(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n int	var l, r string	Fscan(in, &n, &l, &r)	if l[0] != r[0] {		Fprint(out, strings.Repeat("1", n))		return	}	// 判断 r-l > 1 的非 bigint 写法	// 判断方法是找到第一个不同的位,并且后续不是 l=011...1, r=100...0 的情况	// 此时 r-l > 1 成立	i := 0	for ; i < n; i++ {		if r[i] == '1' && l[i] == '0' {			break		}	}	for i++; i < n; i++ {		if r[i] == '1' || l[i] == '0' {			Fprint(out, r[:n-1]+"1")			return		}	}	Fprint(out, r)} //func main() { CF1493E(os.Stdin, os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗