balance_index

package
v0.0.0-...-86b9fec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 0 Imported by: 0

README

Balance Index

Write a function that takes in a non-empty array of integers and returns its balance index.

An array a's balance index is an index i such that a[0] + a[1] + ... + a[i - 1] is equal to a[i + 1] + a[i + 2] + ... + a[lastIdx].

In order for 0 to be an array's balance index, a[1] + a[2] + ... + a[lastIdx] must be equal to 0.

Similarly, in order for the last index of an array to be the array's balance index, a[0] + a[1] + ... + a[lastIdx - 1] must be equal to 0.

If there is no balance index in the array, your function should return -1; if there are multiple possible answers, your function can return any of them.

Sample Input

  array = [0, 9, -8, 2, 7, 1, 11, -2, 1]

Sample Output

  5 // 0 + 9 + -8 + 2 + 7 == 11 + -2 + 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BalanceIndex

func BalanceIndex(array []int) int

BalanceIndex O(n) time | O(1) space - where n is the length of the input array

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL