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

  • Home
  • SEARCH
  • 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 6601967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:49:15+00:00 2026-05-25T18:49:15+00:00

A snail creeps x ft up a wall during the daytime. After all the

  • 0

A snail creeps x ft up a wall during the daytime. After all the labor it does throughout the day, it stops to rest a while… but falls asleep!! The next morning it wakes up and discovers that it has slipped down y ft while sleeping.

If this happens every day, how many times will the snail creep to cover n walls of different height?

I have written a function to count the number of creeps of the snail as shown below:

void count(int move_forward, int move_backward, int number_walls, int[] height)
    {
        int count = number_walls, diff = move_forward - move_backward;
        while (number_walls--)
            for (move_backward = move_forward; move_backward < height[number_walls]; move_backward += diff)
                count++;
    }

It’s working fine. But I wanted to know if there is any other way of solving this problem to optimize the speed of the program further.

  • 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-25T18:49:16+00:00Added an answer on May 25, 2026 at 6:49 pm

    solution is ((height-x)/(x-y))+1, no loops required.

    the snail needs to climb to: height-x, and it takes him ((height-x)/(x-y)) days. Once it is there, it takes him an extra day to climb the remaining x.

    EDIT: as mentioned in the comments, this solution solves the problem for each wall, you’ll need to iterate over your heights array, and summarize these results, saving you at least the inner loop, making it O(n), instead O(n*h), where n is the number of walls, and h is the walls’ heights.

    (*)note: that you might want to save the reminder for each wall [i.e. how much the snail can keep going after he had passed this wall], and subtract it from the next wall, dependes on the task description… If the snail can pass a maximum of one wall per day, ignore this last comment.

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

Sidebar

Related Questions

I am interested in doing some snail mail based surveys but I am looking
Ok, I googled and found some answers here on this site regarding this, but
I apologize but I am new to programming and I'm having difficulty coding a
I'm new to JSON, but I have experience in PHP. Can someone explain to
There are some events expose via WMI, but I can't seem to find any
What do you think is the best way to markup a snail mail address?
I have a textarea input on a form for users to submit their snail
I'm using forms authentication for an app that allows users to register but not
I have a simple address book type of database. I've probably overnormalised, but it's
Is it possible to create the curved sail shape below using HTML and CSS

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.