Go Min Perimeter Rectangle
func minPerimeterRectangle(n int) int {
minPerimeter := math.MaxInt
for i := 1; i*i < n; i++ {
if n%i == 0 {
if p := 2 * (i + n/i); p < minPerimeter {
minPerimeter = p
}
}
}
return minPerimeter
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Go Missing Integer
func missingInteger(a []int) int {
seen := make(map[int]bool, len(a))
for _, v := range a {
seen[v] = true
}
unique := make([]int, 0, len(seen))
for v := range seen {
unique = append(unique, v)
}
sort.Ints(unique)
min := 1
for _, v := range unique {
if v > 0 {
if min != v {
break
}
min++
}
}
return min
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Go Nesting
func nesting(s string) int {
if s == "" {
return 1
}
depth := 0
for i := 0; i < len(s); i++ {
if s[i] == ')' {
if depth == 0 {
return 0
}
depth--
} else {
depth++
}
}
if depth == 0 {
return 1
}
return 0
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Go Number Of Disc Intersections
func numberOfDiscIntersections(a []int) int {
c := len(a)
start := make([]int, c)
end := make([]int, c)
for k, v := range a {
key := k - v
if k < v {
key = 0
}
start[key]++
key = k + v
if k+v >= c {
key = c - 1
}
end[key]++
}
sum, active := 0, 0
for k := range a {
sum += active*start[k] + (start[k]*(start[k]-1))/2
active += start[k] - end[k]
if sum > 10000000 {
return -1
}
}
return sum
}
This sorts disc start and end points and counts active overlaps without comparing every pair directly.
Go Odd Occurrences In Array
// oddOccurrencesInArray assumes exactly one value occurs an odd number of
// times (per the problem's constraints) and recovers it via XOR.
func oddOccurrencesInArray(a []int) int {
result := 0
for _, v := range a {
result ^= v
}
return result
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Go Palindrome Rearranging
func palindromeRearranging(inputString string) bool {
counts := make(map[rune]int)
for _, r := range inputString {
counts[r]++
}
odd := 0
for _, c := range counts {
if c%2 != 0 {
odd++
}
}
return odd <= 1
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Go Passing Cars
func passingCars(a []int) int {
passing, eastbound := 0, 0
for _, v := range a {
if v == 0 {
eastbound++
} else if eastbound > 0 {
passing += eastbound
if passing > 1000000000 {
return -1
}
}
}
return passing
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Go Peaks
func peaks(a []int) int {
n := len(a)
if n <= 2 {
return 0
}
sum := make([]int, n)
last := -1
dist := 0
for i := 1; i+1 < n; i++ {
sum[i] = sum[i-1]
if a[i] > a[i-1] && a[i] > a[i+1] {
if i-last > dist {
dist = i - last
}
last = i
sum[i]++
}
}
sum[n-1] = sum[n-2]
if sum[n-1] == 0 {
return 0
}
if n-last > dist {
dist = n - last
}
j := 0
for i := dist>>1 + 1; i < dist; i++ {
if n%i == 0 {
last = 0
for j = i; j <= n; j += i {
if sum[j-1] <= last {
break
}
last = sum[j-1]
}
if j > n {
return n / i
}
}
}
last = dist
for n%last != 0 {
last++
}
return n / last
}
This finds the peak positions, then tests how many equal blocks can each contain at least one peak.
Go Perm Check
func permCheck(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
for k, v := range sorted {
if k+1 < len(sorted) && v != k+1 {
return 0
}
}
return 1
}
This validates that every value from 1 to N appears exactly once.
Go Perm Missing Element
func permMissingElement(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
for k, v := range sorted {
if v != k+1 {
return k + 1
}
}
return len(sorted) + 1
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.