Find sequence of consecutive numbers which's multiple is N - c++

I have an exercise with an input N and it wants me to find a sequence of consecutive numbers which's multiple is equal to N. Example
Input | Output
60 | 3 5
3*4*5=60
What i have tried
cin>>b;
for (int i = 2; i <=b; ++i)
{
c=1;
for (int j = i; j <=b; ++j)
{
c*=j;
if(c==b){
cout<<i<<" "<<j<<endl;
return 0;
}else if(c>b){
j=b;
}
}
}

Here is a working solution in Python (see end of the post for C++ version):
def seq_with_product(n):
"""Return a sequence of consecutive numbers whose product is n."""
i, curr_prod = 1, 1
max_j = int(n**0.5) + 1 # n**0.5 is square root of n
for j in range(2, max_j + 1):
curr_prod *= j
while curr_prod > n and i < j-1:
curr_prod /= i
i += 1
if curr_prod == n:
return range(i, j+1)
return []
Let i and j be the start and end number in the current sequence. You start from the sequence with the smallest product [1, 2], and check if it is smaller than the given target n. If it is, you want to increase the product, so you include the next number by increasing j. As soon as you get to a product which is larger, you start removing numbers from the product, starting from the smallest (the while loop). If you get the exact product, your answer are the numbers between i and j. So for example, this is how you reach the answer to 60:
[1, 2] # too small, include the next number
[1, 2, 3] # too small, include the next number
[1, 2, 3, 4] # too small, include the next number
[1, 2, 3, 4, 5] # too big, remove the first number
[2, 3, 4, 5] # too big, remove the first number
[3, 4, 5] # found
Note that you don't need to consider numbers greater than the square root of the target number plus one, so you can stop at max_j.
In C++, it would be something like:
int i = 1, curr_prod = 1;
int max_j = sqrt(n) + 1;
for (int j = 2; j <= max_j; j++) {
curr_prod *= j;
while (curr_prod > n && i < j - 1) {
curr_prod /= i;
i += 1;
}
if (curr_prod == n) cout << i << " " << j << endl;
}

Because of this assignment, the inner loop will never terminate if no sequence with the given starting value i is found:
else if (c > b) {
j = b;
}
Instead you should do this:
else if (c > b) {
break;
}
This will terminate the inner loop and check for a sequence with the next start value i+1.
You should also consider the (very common) case that the only sequence contains only one element which is the number N itself.

Related

Find pairs in an array such that a+b%10 = k

There is a ordered list like
A=[7, 9, 10, 11, 12, 13, 20]
and I have to find pairs a+b%10=k where 0<=k<=9
For example k = 0
Pairs: (7, 13), (9, 11), (10, 20)
How can i find the number of pairs in O(n) time?
I tried to find convert all the list with take mod(10)
for (auto i : A) {
if (i <= k) {
B.push_back(i);
}
else {
B.push_back(i % 10);
}
}
After that i tried to define summations that gives k via unorderep_map
unordered_map<int, int> sumList;
int j = k;
for (int i = 0; i < 10; i++) {
sumList[i] = j;
if (j==0) j=9;
j--;
}
But i can't figure out that how can i count the number of pairs in O(n), what can i do now?
Let’s begin with a simple example. Assume that k = 0. That means that we want to find the number of pairs that sum up to a multiple of 10. What would those pairs look like? Well, they could be formed by
adding up a number whose last digit is 1 with a number whose last digit is 9,
adding up a number whose last digit is 2 with a number whose last digit is 8,
adding up a number whose last digit is 3 with a number whose last digit is 7,
adding up a number whose last digit is 4 with a number whose last digit is 6, or
adding up two numbers whose last digit is 5, or
adding up two numbers whose last digit is 0.
So suppose you have a frequency table A where A[i] is the number of numbers with last digit i. Then the number of pairs of numbers whose last digits are i and j, respectively, is given by
A[i] * A[j] if i ≠ j, and
A[i] * A[i-1] / 2 if i = j.
Based on this, if you wanted to count the number of pairs summing to k mod 10, you could
fill in the A array, then
iterate over all possible pairs that sum to k, using the above formula to count up the number of pairs without explicitly listing all of them.
That last step takes time O(1), since there are only ten buckets and iterating over the pairs you need therefore requires at most a constant amount of work.
I’ll leave the rest of the details to you.
Hope this helps!
You can modify counting sort for this.
Below is an untested, unoptimized and only illustrative version:
int mods[10];
void count_mods(int nums[], int n) {
for (int i = 0; i < n; i++)
mods[nums[i]%10]++;
}
int count_pairs(int k) {
// TODO: there's definitely a better way to do this, but it's O(1) anyway..
int count = 0;
for (int i = 0; i < 10; i++)
for (int j = i+1; j < n; j++)
if ((i + j) % 10 == k) {
int pairs = mods[i] > mods[j] ? mods[j] : mods[i];
if (i == j)
pairs /= 2;
count += pairs;
}
return count;
}
EDIT:
With a smaller constant.
int mods[10];
void count_mods(int nums[], int n) {
for (int i = 0; i < n; i++)
mods[nums[i]%10]++;
}
int count_pairs(int k) {
int count = 0;
for (int i = 0; i < 10; i++) {
int j = k - i;
if (j < 0)
j += 10;
count += min(mods[i], mods[j]);
// When k = 2*i we count half (rounded down) the items to make the pairs.
// Thus, we substract the extra elements by rounding up the half.
if (i == j)
count -= (mods[i]+1) / 2;
}
// We counted everything twice.
return count / 2;
}

Solving T(n) time complexity that contains "variables"

So, I need to find the T(n) and then Big-O (tight upper bound) for the following piece of code:
int sum = 0;
for(int i = 1; i < n; i *= 2) {
for(int j = n; j > 0; j /= 2) {
for(int k = j; k < n; k += 2) {
sum += i + j * k;
}
}
}
Now from what I calculated for the loops, first loop runs log(n) times, second loop runs (log(n) * log(n)) times and the third loop is the one which is causing confusion, because I believe it runs for (n - j)/2 times. My question is can I assume it to be n/2 times, because I think it won't be a tight upper bound if I do that. Or is there a different approach that I am missing?
for(int i = 1; i < n; i *= 2) // (1)
for(int j = n; j > 0; j /= 2) // (2)
for(int k = j; k < n; k += 2) // (3)
For the first iteration of (3) (where k = j = n) no iteration will occur. After j is divided by 2 the third loop will run (n/2)/2 or n/4 times. After the third iteration of (2), (3) will run n/4/2 or n/8 times. We can sum the running time as follows:
n/4 + n/8 + n/16 + ... + n/2^k
This can also be written as:
n * (1/4 + 1/8 + 1/16 + ... + 1/2^k)
Which asymptotically is in O(n).
This is a very interesting question. Let give n a real number and see how it's going. Say, n=100. If we only look at the two inner loops
j k
100 None
50 50, 52, ..., 98
25 25, 27, ..., 99
12 12, 14, ..., 98
6 6, 8, ..., 98
3 3, 5, ..., 99
1 1, 3, ..., 99
As you can see, the complexity of the third loop is actually O(n). Especially when n is a very large number, it will be close to Θ(n)

count distinct slices in an array

I was trying to solve this problem.
An integer M and a non-empty zero-indexed array A consisting of N
non-negative integers are given. All integers in array A are less than
or equal to M.
A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice
of array A. The slice consists of the elements A[P], A[P + 1], ...,
A[Q]. A distinct slice is a slice consisting of only unique numbers.
That is, no individual number occurs more than once in the slice.
For example, consider integer M = 6 and array A such that:
A[0] = 3
A[1] = 4
A[2] = 5
A[3] = 5
A[4] = 2
There are exactly nine distinct slices: (0, 0), (0, 1), (0, 2), (1,
1), (1,2), (2, 2), (3, 3), (3, 4) and (4, 4).
The goal is to calculate the number of distinct slices.
Thanks in advance.
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX 100002
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
using namespace std;
bool check[MAX];
int solution(int M, vector<int> &A) {
memset(check, false, sizeof(check));
int base = 0;
int fibot = 0;
int sum = 0;
while(fibot < A.size()){
if(check[A[fibot]]){
base = fibot;
}
check[A[fibot]] = true;
sum += fibot - base + 1;
fibot += 1;
}
return min(sum, 1000000000);
}
The solution is not correct because your algorithm is wrong.
First of all, let me show you a counter example. Let A = {2, 1, 2}. The first iteration: base = 0, fibot = 0, sum += 1. That's right. The second one: base = 0, fibot = 1, sum += 2. That's correct, too. The last step: fibot = 2, check[A[fibot]] is true, thus, base = 2. But it should be 1. So your code returns1 + 2 + 1 = 4 while the right answer 1 + 2 + 2 = 5.
The right way to do it could be like this: start with L = 0. For each R from 0 to n - 1, keep moving the L to the right until the subarray contais only distinct values (you can maintain the number of occurrences of each value in an array and use the fact that A[R] is the only element that can occur more than once).
There is one more issue with your code: the sum variable may overflow if int is 32-bit type on the testing platform (for instance, if all elements of A are distinct).
As for the question WHY your algorithm is incorrect, I have no idea why it should be correct in the first place. Can you prove it? The base = fibot assignment looks quite arbitrary to me.
I would like to share the explanation of the algorithm that I have implemented in C++ followed by the actual implementation.
Notice that the minimum amount of distinct slices is N because each element is a distinct one-item slice.
Start the back index from the first element.
Start the front index from the first element.
Advance the front until we find a duplicate in the sequence.
In each iteration, increment the counter with the necessary amount, this is the difference between front and back.
If we reach the maximum counts at any iteration, just return immediately for slight optimisation.
In each iteration of the sequence, record the elements that have occurred.
Once we have found a duplicate, advance the back index one ahead of the duplicate.
While we advance the back index, clear all the occurred elements since we start a new slice beyond those elements.
The runtime complexity of this solution is O(N) since we go through each
element.
The space complexity of this solution is O(M) because we have a hash to store
the occurred elements in the sequences. The maximum element of this hash is M.
int solution(int M, vector<int> &A)
{
int N = A.size();
int distinct_slices = N;
vector<bool> seq_hash(M + 1, false);
for (int back = 0, front = 0; front < N; ++back) {
while (front < N and !seq_hash[A[front]]) { distinct_slices += front - back; if (distinct_slices > 1000000000) return 1000000000; seq_hash[A[front++]] = true; }
while (front < N and back < N and A[back] != A[front]) seq_hash[A[back++]] = false;
seq_hash[A[back]] = false;
}
return distinct_slices;
}
100% python solution that helped me, thanks to https://www.martinkysel.com/codility-countdistinctslices-solution/
def solution(M, A):
the_sum = 0
front = back = 0
seen = [False] * (M+1)
while (front < len(A) and back < len(A)):
while (front < len(A) and seen[A[front]] != True):
the_sum += (front-back+1)
seen[A[front]] = True
front += 1
else:
while front < len(A) and back < len(A) and A[back] != A[front]:
seen[A[back]] = False
back += 1
seen[A[back]] = False
back += 1
return min(the_sum, 1000000000)
Solution with 100% using Ruby
LIMIT = 1_000_000_000
def solution(_m, a)
a.each_with_index.inject([0, {}]) do |(result, slice), (back, i)|
return LIMIT if result >= LIMIT
slice[back] = true
a[(i + slice.size)..-1].each do |front|
break if slice[front]
slice[front] = true
end
slice.delete back
[result + slice.size, slice]
end.first + a.size
end
Using Caterpillar algorithm and the formula that S(n+1) = S(n) + n + 1 where S(n) is count of slices for n-element array java solution could be:
public int solution(int top, int[] numbers) {
int len = numbers.length;
long count = 0;
if (len == 1) return 1;
int front = 0;
int[] counter = new int[top + 1];
for (int i = 0; i < len; i++) {
while(front < len && counter[numbers[front]] == 0 ) {
count += front - i + 1;
counter[numbers[front++]] = 1;
}
while(front < len && numbers[i] != numbers[front] && i < front) {
counter[numbers[i++]] = 0;
}
counter[numbers[i]] = 0;
if (count > 1_000_000_000) {
return 1_000_000_000;
}
}
return count;
}

Finding the T(n) of An Algorithm

Okay so when my professor was going over it in class it seemed quite simple, but when I got to my homework I became confused. This is a homework example.
for (int i = 0; i < n; i++) // I know this runs at T(n)
for (int j = n - 1; j >= i; j--)
cout << i << " " << j << endl;
Here's an example I understand
for(int i=0; i<n-1; i++) {
for(int j=i+1; j<n; j++) {
1 Simple statement
}
For that example I just plugged in 0, 1, and 2. For 0, it ran for n-1, at 1 for n-2 and at 2 n-3. So I think that for the homework example if I plugged in 0 it would run for n+1 since j has to be greater than or equal to i which is 0. If it's not obvious, i'm pretty confused. If anyone could show me how to solve it, that'd make my day. Thanks guys.
Let's dig into the functon. Let's pick some numbers.
say, n = 5
So our code looks like this (magical pseudo-code uses INCLUSIVE loops, not that it's too important)
(1)for i = 0 to 4
(2)for j = 4 to i
(3)print i j
next
next
So this is a matter of preference, but usually loops are assumed to cost 1 simple statement per execution (comparison, and incrementation). So we'll assume that statements (1) and (2) have a cost of 2. Statement (3) has a cost of 1.
Now to determine T(n).
Our outer loop for i = 0 to 4 runs exactly n times.
Our inner loop for j = 4 to i . . . We'll dig in there for a minute.
For our example with n = 5 loop (2) will execute like so
j = 4; i = 0; j = 4; i = 1; j = 4; i = 2; j = 4; i = 3 j = 4; i = 4;
j = 3; i = 0; j = 3; i = 1; j = 3; i = 2; j = 3; i = 3;
j = 2; i = 0; j = 2; i = 1; j = 2; i = 2;
j = 1; i = 0; j = 1; i = 1;
j = 0; i = 0;
So it makes this kind of pyramid shape, where we do 1 less iteration each time. This particular example ran 5 + 4 + 3 + 2 + 1 = 15 times.
We can write this down as SUM(i; i = 0 to n).
Which we know from precalc: = (1/2)(n)(n+1).
And (3) will execute the exact same number of times as that inner loop since it's the only statement. So our total runtime is going to be. . .
COST(1) + COST(2) + COST(3)
(2)(n) + 2(1/2)(n)(n+1) + (1/2)(n)(n+1)
We can clean this up to be
(3/2)(n)(n+1) + 2n = T(n).
That said, this assumes that loops cost 2 and the statement costs 1. It's usually more meaningful to say loops cost 0 and statements cost 1. If that were the case, T(n) = (1/2)(n)(n+1).
And givent that T(n), we know T(n) is O(n^2).
Hope this helps!
It's not that hard.
3 examples for single loops:
for (int i = 0; i < n; i++)
for(int i = 0; i < n-1; i++)
for(int i = 2; i < n-1; i++)
The first loop executs it's content n times (i=0,1,2,3,...,n-1).
The same way, the second loop is just n-1 times.
The third would be n-3 because it starts not at 0, but 2
(and if n is less than 3, ie. n-3<0, it won't execute at all)
In a nested loop like
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n; j++) {
//something
}
}
For each pass of the outer loop, the whole inner loop is executed, ie. you can multiply both single loop counts to get how often "something" is executed in total. Here, it is (n-1) * n = n^2 - n.
If the inner loop depends on the value of the outer loop, it gets a bit more complicated:
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
//something
}
}
The inner loop alone is n - (i+1) times, the outer one n-1 times (with i going from 0 to n-2).
While there are "proper" ways to calculate this, a bit logical thinking is often easier, as you did already:
i-value => inner-loop-time
0 => n-1
1 => n-2
...
n-2 => n - (n-2+1) = 1
So you´ll need the sum 1+2+3+...+(n-1).
For calculating sums from 1 to x, following formula helps:
sum[1...x] = x*(x+1)/2
So, the sum from 1 to n-1 is
sum[1...n-1] = (n-1)*(n-1+1)/2 = (n^2 - n)/2
and that´s the solution for the loops above (your second code).
About the first code:
Outer loop: n
Inner loop: From n-1 down to i included, or the other way from i up to <=n-1,
or from i up to <n, that´s n-i times
i >= innerloop
0 n
1 n-1
2 n-2
...
n-1 1
...and the sum from 1 to n is (n^2 + n)/2.
One easy way to investigate a problem is to model it and look at resulting data.
In your case, the question is: how many iterations does the inner loop depending on the the value of the outer loop variable?
let n = 10 in [0..n-1] |> List.map (fun x -> x,n-1-x);;
The 1 line above is the model showing what happens. If you now look at the resulting output, you will quickly notice something...
val it : (int * int) list =
[(0, 9); (1, 8); (2, 7); (3, 6); (4, 5); (5, 4); (6, 3); (7, 2); (8, 1);
(9, 0)]
What is it you notice? For a given N you run the outer loop N times - this is trivial. Now we need to sum up the second numbers and we have the solution:
sum(N-1..0) = sum(N-1..1) = N * (N-1) / 2.
So the total count of cout calls is N * (N-1) / 2.
Another easy way to achieve the same is to modify your function a bit:
int count(int n) {
int c = 0;
<outer for loop>
<inner for loop>
c++;
return c;
}

Finding the balance point in an array

This question is from a great youtube channel, giving problems that can be asked in interviews.
It's basically related to finding the balance point in an array. Here is an example to best explain it;
{1,2,9,4,-1}. In here since sum(1+2)=sum(4+(-1)) making the 9 the balance point. Without checking the answer I've decided to implement the algorithm before wanted to ask whether a more efficient approach could be done;
Sum all the elements in array O(n)
Get the half of the sum O(1)
Start scanning the array, from left, and stop when the sumleft is bigger than half of the general sum. O(n)
Do the same for the right, to obtain sum right. O(n).
If sumleft is equal to sumright return arr[size/2] else return -1
I'm asking because this solution popped into my head without any effort, providing the O(n) running time. Is this solution, if true, could be developed or if not true any alternative methods?
Your algorithm is not good (counter-example: 1 -1 1 0 1 -1 1), the good solution is to compute partial sum of your array (so that you can can compute sumleft and sumright in O(1) for each cell of the array) and then (or in the same time if you already know the global sum) search in your array a cell such that sumleft = sumright which is O(n).
The partial sum of the array A is
[A[0], A[0]+A[1], A[0]+A[1]+A[2], …, A[0]+A[1]+A[2]+…+A[n-1]]
example:
A=[5,2,3,1,4,6]
partial sum = [5,7,10,11,15,21]
With this array you can compute sumleft[i]=partial_sum[i-1] and sumright[i]=partial_sum[n-1]-partial_sum[i]
Improvement:
Computing the global sum first and then only the partial sum for the current index enable you to use only O(1) extra space instead of O(n) extra space if you store all the partial_sum array.
Basically add up all the numbers first. This will be an O(n) operation. Then substract one element from the array at a time starting from the beginning of the array till upper == lower. Thus the total order will be O(n).
int BalancePoint(int a[], int begin, int end) // find index of an array (balance point) such that sum of all elements before the index = sum of all elements after it; else return -1
{
if(!a) return -1;
else if(begin == end) return begin;
long long upper = 0;
long long lower = 0;
for(int i = begin; i <= end; ++i)
{
upper += *(a+i);
}
for(int j = begin; j <= end; ++j)
{
upper -= *(a+j);
if(upper == lower) return j;
lower += *(a+j);
}
return -1;
}
Using STL
int BalancePointSTL( const vector<int> &A ) // find index of an array (balance point) such that sum of all elements before the index = sum of all elements after it; else return -1
{
if(A.empty()) return -1;
long long upper = 0;
long long lower = 0;
for(unsigned int i = 0; i <= A.size(); ++i)
{
upper += A[i];
}
for(unsigned int j = 0; j < A.size(); ++j)
{
upper -= A[j];
if(upper == lower) return j;
lower += A[j];
}
return -1;
}
The following would have a better worst case performance but a couple more if-else comparisons
int BalancePoint2(int a[], int begin, int end) // Better worst case senario by factor of 2
{
if(!a) return -1;
else if(begin == end) return begin;
long long upper = 0;
long long lower = 0;
int mid = (end-begin)/2;
for(int i = begin; i < mid; ++i)
{
lower += *(a+i);
}
for(int i = mid+1; i <= end; ++i)
{
upper += *(a+i);
}
if(upper == lower) return mid;
else if(lower < upper)
{
lower += *(a+mid);
for(int i= mid + 1 ; i <= end ; ++i)
{
upper -= *(a + i);
if(upper == lower) return i;
lower += *(a + i);
}
}
else {
upper += *(a + mid);
for(int i = mid - 1; i >=begin; --i)
{
lower -= *(a + i);
if(upper == lower) return i;
upper += *(a + i);
}
}
return -1;
}
I would actually have 2 start points, one on the leftmost point (leftLoc), and one at the right most point (rightLoc). Hold a sumLeft and sumRight numbers.
leftLoc = 0;
rightLoc = (n - 1);
sumRight = array[rightLoc];
sumLeft = array[leftLoc];
while(leftLoc < rightLoc){
if(sumRight > sumLeft){
leftLoc++;
sumLeft += array[leftLoc];
}else{
rightLoc--;
sumRight += array[rightLoc];
}
}
if( (sumRight + array[rightLoc - 1]) == sumLeft ){
return rightLoc--;
}else if( (sumLeft + array[leftLoc + 1]) == sumRight){
return leftLoc++;
}else{
// return floating point number location in the middle of the 2 locations
}
All the while keeping track of how many total positions have been moved O(n)
You may find that your balance point is a floating point number in the middle of the final points (once they are at the integer locations right next to one another).
This should even work with the negative numbers example. Perhaps I am missing some fine grain details, but some variation on this theme should result you in an O(n) runtime algorithm.
You're looking for the centroid or center of mass. In pure Python:
def centroid(input_list):
idx_val_sum = 0.0
val_sum = 0.0
for idx,val in enumerate(input_list):
idx_val_sum += idx*val
val_sum += val
return idx_val_sum/float(val_sum)
It's O(n) and if non-integer results are ill-formed, you can reject them with a modulo check:
def integer_centroid(input_list):
idx_val_sum = 0.0
val_sum = 0.0
for idx,val in enumerate(input_list):
idx_val_sum += idx*val
val_sum += val
out = idx_val_sum/float(val_sum)
if out%1.0==0.0:
return out
else:
raise ValueError("Input list has non-integer centorid.")
This post should have been a comment replying to trumpetlicks June 14 2012 comment, but I don't have enough reputation. "Order" is implicitly tracked in idx_val_sum, which is the cumulative position sum weighted by value.
Edit:
Matt, thank you for your observation. I assumed this was a pseudocode question, but now I see the C++ tag. Here's some (untested) C++, with comments.
An intuitive example is a simple lever arm problem: if you have a lever with two forces f1 and f2 acting on it at positions x1 and x2, you can prevent the system from rotating by applying a force at position (f1*x1+f2*x2)/(f1+f2). A continuous system requires integration over the product of x and f, but levers with discrete locations and forces are a good analogy for this problem.
// untested code:
float centroid(float * vec, int vec_length){
float idx_val_sum = 0.0;
float val_sum = 0.0;
for (idx = 0; idx < vec_length; idx++){
// keep a running sum of the product of the index and the value
idx_val_sum += float(idx)*vec[idx];
// similarly, keep a running sum of the index
val_sum += vec[idx];
}
// return the quotient of the product-sum and the index sum:
return idx_val_sum/val_sum;
}
A solution that's O(n) and doesn't require more space
def balance_array(arr):
if len(arr) < 3:
return False
for i in range(1, len(arr)+1):
lsum = sum(arr[:i])
rsum = sum(arr[(i+1):])
if lsum == rsum:
return True
return False
Testing
test_arrays = [[5, 3, 7, 0, 9], [5,2,3,1,4,6], [1,0,1], [1,6,5,1,2,3,1], [1,1], [], [1], [1,2,9,4,-1], [5, 4, 7, 0, 9], [1, -1, 1, 0, 1, -1, 1]]
for i in test_arrays:
print(f'{i}\t{balance_array(i)}')
[5, 3, 7, 0, 9] False
[5, 2, 3, 1, 4, 6] True
[1, 0, 1] True
[1, 6, 5, 1, 2, 3, 1] True
[1, 1] False
[] False
[1] False
[1, 2, 9, 4, -1] True
[5, 4, 7, 0, 9] True
[1, -1, 1, 0, 1, -1, 1] True
I believe you are looking for the Center of Mass, here is a solution written in Go:
func centerOfGravity(a []int) float64 {
tot := 0.0
mass := 0.0
for i := range a {
tot += float64(i) * float64(a[i])
mass += float64(a[i])
}
return tot / mass
}
This gives you the index of the center of mass in the array, assuming a 0-based array. It can return a non-integer result since the center of mass can be anywhere in the range of the array.