How do I define the index within the array?

How do I come up with a summation formula for iterating over an array and changing the starting index with each iteration?

  • For example, let's say I have an array consisting of the elements: 10, 20, 30, 40, 50. I'm looking for the number of iterations it will take to traverse the entire array, while offsetting the starting index after each iteration. So the output will look something like this: 10, 20, 30, 40, 50 20, 30, 40, 50 30, 40, 50 40, 50 This is a total of 4 iterations. I need to find a formula that will determine how many iterations there are, regardless of how many elements are in the array. This is NOT a homework assignment. My friend has to solve this for his job and I'm just doing it for personal insight. I'm also taking a Discrete Math course and I recognize that this deals with sequences/summations, but I've never been good at determining formulas for these things. Any help would be appreciated.

  • Answer:

    It's the length of the array minus 1. You start iterations at every index aside from the last one. The number of indices in an array is the length of the array, but you subtract one because you don't start iterating at the last term.

Alok Tripathy at Quora Visit the source

Was this solution helpful to you?

Other answers

Consider the jth element of an array of length n, given as; An−k+1,k(j) A_{n-k+1,k}(j) where, initially; k=1 k=1 Such that k begins at 1. The length of the array decreases as k grows. So, now, we can define the subsequent arrays using a recursive formula as; An−k+1,k(j+1)=An−k,k+1(j) A_{n-k+1,k}(j+1) = A_{n-k,k+1}(j) Where your original array is given by; An,1(j)=[E1,E2,E3...En] A_{n,1}(j) = [E_1,E_2,E_3...E_n]  The n−k+1 n-k+1 makes sure that array decreases in size as iterations are applied. The j+1 j+1 is just a way of saying take the last array and start one later. The solitary j j on the right hand side suggests that this new array takes on the second element of the last array as it's first and so on as 3 to 2 and 4 to 3. The subscripts of k k and k+1 k+1 tells us that this is how we use the last array to determine the next, no matter what k k is. Running this from k= 1 to n-1, gives what I feel you are looking for. This process is iterated 1 less than the total number cells, because we are deleting an array.of n cells, cell by cell starting from the left. It will always take 'n-1' iterations (assuming you always want to leave at least two elements left {if you want 1 element left, you've gotta go all the way from 1 to 'n'})

Carl Christian Barfield

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.