Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Equilibrium-index/Go/equilibrium-index.go
Normal file
36
Task/Equilibrium-index/Go/equilibrium-index.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(ex([]int32{-7, 1, 5, 2, -4, 3, 0}))
|
||||
|
||||
// sequence of 1,000,000 random numbers, with values
|
||||
// chosen so that it will be likely to have a couple
|
||||
// of equalibrium indexes.
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
verylong := make([]int32, 1e6)
|
||||
for i := range verylong {
|
||||
verylong[i] = rand.Int31n(1001) - 500
|
||||
}
|
||||
fmt.Println(ex(verylong))
|
||||
}
|
||||
|
||||
func ex(s []int32) (eq []int) {
|
||||
var r, l int64
|
||||
for _, el := range s {
|
||||
r += int64(el)
|
||||
}
|
||||
for i, el := range s {
|
||||
r -= int64(el)
|
||||
if l == r {
|
||||
eq = append(eq, i)
|
||||
}
|
||||
l += int64(el)
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue