Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6554971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:45:47+00:00 2026-05-25T12:45:47+00:00

Start with an array A of positive numbers. Start at index 0. From index

  • 0

Start with an array A of positive numbers. Start at index 0. From index i, you can move to index i+x for any x <= A[i]. The goal is to find the minimum number of moves needed to get to the end of the array.

Here’s an example:

{ 2 , 4 , 1 , 2 , 3 , 2 , 4 , 2} 

If you always go as far as possible in each move, then this is what you get:

0 , 2 , 3 , 5 , 7

This takes 4 moves. But you can get through it faster by doing it this way

0 , 1 , 4 , 7

This only takes 3 moves.

I thought about this for a bit and did the first thing I thought of, but after thinking for a few more days, I still don’t know how to do it any better.

Here’s my idea. Start at the end of the array and keep track of the minimum number of moves from some position to the end. So for the example, moves[7] = 0 because it’s the end already. Then moves[6] = 1 because it takes one move to get to the end. My formula is

moves[i] = 1 + min(moves[i+1], moves[i+2], ... , moves[i+A[i]])

By the time I get to the beginning, I know the number of moves.

So this is O(n^2) which is okay I guess, but probably there is a faster way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:45:48+00:00Added an answer on May 25, 2026 at 12:45 pm

    Since you can chose any x in [1,A[i]] I guess there is a pretty simple solution :

    start at 0:

    select the next reachable element from which you can reach the farther element.
    i.e chose i that maximize i+A[i+x] for x in [1,A[i]]

    until you arrive at the end of the list.


    Example:

    {2 , 4 , 1 , 2 , 3 , 2 , 4 , 2}

    start at 0

    from 0 you can get to 1 or to 2:

    • from 1 you can get to 4
    • from 2 you can get to 3

    therefore max(0+A[0+x]) is for i = 1

    chose 1
    from 1 you can get to 2 3 4:

    • from 4 you can get to 7
    • from 3 you can get to 5
    • from 2 you can get to 3

    therefore max(1+A[1+x]) is for i = 4

    chose 4

    you can reach 7

    stop

    the resulting list is : 
    
    0,1,4,7
    

    As explained in my comments I think it’s O(N), because from i you reach i+x+1 in at least 2*x operations.


    ‘Pseudo’ proof

    You start at 0 (it’s optimal)

    then you select i that maximize(0+A[0+x]) (i.e that maximize the reachability for the next element)

    from that i you can reach any other element that is reachable from all other elements reachable from 0 (it’s a long sentence, but it means : who can do more, can do less, therefore if i is not optimal,it’s as good as optimal)

    So i is optimal

    then following step by step this reasoning, it proves the optimality of the method.

    If someone knows how to formulate that more mathematically, feel free to update it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I expect it to go from the start of an array to the
Is it possible to start an array at an index not zero...I.E. you have
I wonder how is this possible in groovy to start an array from the
I want to convert PHP list (array), i.e. array(start, end, coords) into associative array
There is a string array myDownloadList containing 100 string URIs. I want to start
Ok, so lets start with the code <?php function LuhnCalc($number) { $chars = array_reverse(str_split($number,
Ok, lets start with the code. I am looping through a returned array of
When key name starts with digit, in javascript we can define array-like object like
So I have this array: $dates[0] = array('start'=>'2010-07-22', 'end'=>'2010-07-23'); $dates[1] = array('start'=>'2010-07-22', 'end'=>'0000-00-00'); $dates[2]
I have the folloring array structure: $list = array(); $element1 = array('start' => '10',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.