Generate 1, 3, 6, 10.… Using Golang

Before I get into the solution, let me tell you how this sequence helped me in solving the Best Index problem on Hackerearth.

What is the Best Index of an array?

An index of the array is called best if the special sum of this index is maximum across the special sum of all the other indices.

The problem expects to print the best index of the input array. The best index for any index i is calculated using the below formula -

( A[i] ) + ( A[i+1] + A[i+2] ) +( A[i+3] + A[i+4] + A[i+5] )……

So for array with size 10, best index for ‘index 2’ will be

( A[2] ) + ( A[3] + A[4] ) +( A[5] + A[6] + A[7] )

Indices 8 and 9 will be ignored since we need 4 more elements, after grouping 3 elements at indices 5, 6 and 7.

I solved the above problem in Golang. Please refer the solution here. Line 28–32 in the above solution handles the sequence generation of 1, 3, 6, 10…

Sequence generation 1, 3, 6, 10, 15….

So lets say i is initialised to 1 and n to 10. We need to print the sequence till n. Check this program which will print the required result.

Software Engineer. Working Saeloun. My portfolio

Software Engineer. Working Saeloun. My portfolio