Go Count Semi Primes
func countSemiPrimes(n int, p, q []int) []int {
primes := make([]bool, n+1)
for i := range primes {
primes[i] = true
}
semiPrimes := make([]int, n+1)
result := make([]int, len(p))
for i := 2; i*i <= n; i++ {
if primes[i] {
for k := i * i; k <= n; k += i {
primes[k] = false
}
}
}
for k := 2; k*k <= n; k++ {
if primes[k] {
for i := 2; i*k <= n; i++ {
if primes[i] {
semiPrimes[k*i] = 1
}
}
}
}
for i := 1; i <= n; i++ {
semiPrimes[i] += semiPrimes[i-1]
}
for k := range p {
result[k] = semiPrimes[q[k]] - semiPrimes[p[k]-1]
}
return result
}
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
Go Cyclic Rotation
func cyclicRotation(a []int, k int) []int {
size := len(a)
if size == 0 || k <= 0 {
return a
}
k %= size
result := make([]int, size)
for i, v := range a {
result[(i+k)%size] = v
}
return result
}
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.
Go Distinct
func distinct(a []int) int {
seen := make(map[int]struct{}, len(a))
for _, v := range a {
seen[v] = struct{}{}
}
return len(seen)
}
This counts unique values by tracking what has already been seen.
Go Dominator
func dominator(a []int) int {
size, value, index := 0, 0, 0
for k, v := range a {
switch {
case size == 0:
size++
value = v
index = k
case value != v:
size--
default:
size++
}
}
candidate := -1
if size > 0 {
candidate = value
}
count := 0
for _, v := range a {
if v == candidate {
count++
}
}
if count <= len(a)/2 {
index = -1
}
return index
}
This finds a value that appears in more than half of the array, then returns one valid index for it.
Go Equi Leader
func equiLeader(a []int) int {
leaderSize, value := 0, 0
for _, v := range a {
switch {
case leaderSize == 0:
leaderSize++
value = v
case value != v:
leaderSize--
default:
leaderSize++
}
}
candidate := -1
if leaderSize > 0 {
candidate = value
}
leaderCount := 0
for _, v := range a {
if v == candidate {
leaderCount++
}
}
leader := -1
if leaderCount > len(a)/2 {
leader = candidate
}
count := len(a)
lLeaderCount, equiLeaders := 0, 0
for k, v := range a {
leftHalf := (k + 1) / 2
rightHalf := (count - k - 1) / 2
if v == leader {
lLeaderCount++
}
rLeaderCount := leaderCount - lLeaderCount
if lLeaderCount > leftHalf && rLeaderCount > rightHalf {
equiLeaders++
}
}
return equiLeaders
}
This keeps leader counts on both sides of the split and counts positions where the same leader survives in each half.
Go Fib Frog
func fibFrog(a []int) int {
size := len(a)
fib := []int{0, 1}
for i := 1; fib[i] <= size; {
i++
fib = append(fib, fib[i-1]+fib[i-2])
}
type path struct {
idx int
jmp int
}
paths := []path{{idx: -1, jmp: 0}}
steps := make([]bool, size)
for len(paths) > 0 {
cur := paths[0]
paths = paths[1:]
for i := len(fib) - 1; i >= 2; i-- {
idx := cur.idx + fib[i]
if idx == size {
return cur.jmp + 1
}
if idx > size || steps[idx] || a[idx] == 0 {
continue
}
if a[idx] == 1 {
steps[idx] = true
paths = append(paths, path{idx: idx, jmp: cur.jmp + 1})
}
}
}
return -1
}
This precomputes Fibonacci jumps, then uses a breadth-first search to find the shortest valid path across the river.
Go Fish
func fish(a, b []int) int {
size := len(a)
dead := 0
downstream := make([]int, 0, size)
for i := 0; i < size; i++ {
if b[i] == 1 {
downstream = append(downstream, a[i])
} else {
for len(downstream) > 0 {
dead++
if a[i] > downstream[len(downstream)-1] {
downstream = downstream[:len(downstream)-1]
} else {
break
}
}
}
}
return size - dead
}
This uses a stack for downstream fish and resolves fights only when opposite directions meet.
Go Flags
func flags(a []int) int {
size := len(a)
peaks := make([]bool, size)
for i := 1; i < size; i++ {
nextVal := 0
if i+1 < size {
nextVal = a[i+1]
}
peaks[i] = a[i-1] < a[i] && a[i] > nextVal
}
next := make([]int, size)
next[size-1] = -1
for i := size - 2; i >= 0; i-- {
if peaks[i] {
next[i] = i
} else {
next[i] = next[i+1]
}
}
result := 0
for i := 1; i*(i-1) <= size; i++ {
pos, num := 0, 0
for pos < size && num < i {
pos = next[pos]
if pos == -1 {
break
}
num++
pos += i
}
if num > result {
result = num
}
}
return result
}
This finds all peaks first, then checks how many flags can be placed while keeping the required distance.
Go Frog Jmp
func frogJmp(x, y, d int) int {
return int(math.Ceil(float64(y-x) / float64(d)))
}
This computes the jump count with math instead of simulation, which is the cleanest way to solve it.
Go Frog River One
func frogRiverOne(x int, a []int) int {
existing := make(map[int]bool)
for k, v := range a {
if v <= x && !existing[v] {
existing[v] = true
if len(existing) == x {
return k
}
}
}
return -1
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.