Go Array Maximal Adjacement Difference
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func arrayMaximalAdjacentDifference(a []int) int {
diff := 0
for i := 1; i < len(a)-1; i++ {
diff = max(diff, abs(a[i]-a[i-1]), abs(a[i]-a[i+1]))
}
return diff
}
This checks the gap between each pair of neighbors and returns the largest difference.
Go Binary Gap
func binaryGap(n int) int {
trimmed := strings.Trim(strconv.FormatInt(int64(n), 2), "0")
gap := 0
for _, zeroes := range strings.Split(trimmed, "1") {
if len(zeroes) > gap {
gap = len(zeroes)
}
}
return gap
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Go Bracket
func bracket(s string) int {
pairs := map[byte]byte{')': '(', ']': '[', '}': '{'}
stack := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
c := s[i]
if open, isClose := pairs[c]; isClose {
if len(stack) == 0 || stack[len(stack)-1] != open {
return 0
}
stack = stack[:len(stack)-1]
} else {
stack = append(stack, c)
}
}
if len(stack) == 0 {
return 1
}
return 0
}
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.
Go Century From Year
func centuryFromYear(year int) int {
return int(math.Ceil(float64(year) / 100))
}
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
Go Check Palindrome
func checkPalindrome(inputString string) bool {
for i, j := 0, len(inputString)-1; i < j; i, j = i+1, j-1 {
if inputString[i] != inputString[j] {
return false
}
}
return true
}
This compares the string with its reverse. If they match, it is a palindrome.
Go Chocolates By Numbers
func chocolatesByNumbers(n, m int) int {
var gcd func(n, m int) int
gcd = func(n, m int) int {
if n%m == 0 {
return m
}
return gcd(m, n%m)
}
return n / gcd(n, m)
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Go Common Prime Divisors
func commonPrimeDivisors(a, b []int) int {
var gcd func(n, m int) int
gcd = func(n, m int) int {
if n%m == 0 {
return m
}
return gcd(m, n%m)
}
removeCommonPrimeDivisors := func(n, m int) int {
for n != 1 {
d := gcd(n, m)
if d == 1 {
break
}
n /= d
}
return n
}
counter := 0
for i := range a {
x, y := a[i], b[i]
d := gcd(x, y)
x = removeCommonPrimeDivisors(x, d)
if x != 1 {
continue
}
y = removeCommonPrimeDivisors(y, d)
if y == 1 {
counter++
}
}
return counter
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Go Count Div
func countDiv(a, b, k int) int {
firstDiv := a
if a%k != 0 {
firstDiv = a + (k - a%k)
}
lastDiv := b - b%k
return (lastDiv-firstDiv)/k + 1
}
This counts how many numbers in a range are divisible by K without looping through every value.
Go Count Factors
func countFactors(n int) int {
count := 0
i := 1
for i*i < n {
if n%i == 0 {
count += 2
}
i++
}
if i*i == n {
count++
}
return count
}
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Go Count Non Divisible
func countNonDivisible(a []int) []int {
size := len(a)
nonDivisors := make([]int, size)
maxVal := a[0]
for _, v := range a {
if v > maxVal {
maxVal = v
}
}
occurrences := make([]int, maxVal+1)
for _, v := range a {
occurrences[v]++
}
for k, v := range a {
count := 0
for i := 1; i*i <= v; i++ {
if v%i == 0 {
count += occurrences[i]
if v/i != i {
count += occurrences[v/i]
}
}
}
nonDivisors[k] = size - count
}
return nonDivisors
}
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.