Circular Array

Vikas Kore
1 min readAug 8, 2019

--

We will understand how to go through all the elements in array by starting at any Index.

Suppose we have array: testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]

If we start from Index 3, i.e. value 4 and to iterate through all the element in array we have to recreate it like

resultArray = [4, 5, 6, 7, 8, 9, 1, 2, 3]

Problem: Golf Game, player choose 9 Holes to play and started from 4th hole, how he/she can add score for hole by hole

We solve the problem by following way:
1. If user start from 1 Hole (initial, i.e. 0th index) -> return same array

2. Otherwise divide array as left indexes and right indexes of the passed index
suppose we pass index 4 then
then
left values are = 1, 2, 3, 4
right values are(including start index) = 5, 6, 7, 8, 9
Put right values first then left = i.e 5, 6, 7, 8, 9, 1, 2, 3, 4

lets check the following code:

Thank you.

--

--

Vikas Kore
Vikas Kore

No responses yet