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 8987027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:41:32+00:00 2026-06-15T21:41:32+00:00

Apart from this code being horrible inefficient, is this way I´m writing recursive function

  • 0

Apart from this code being horrible inefficient, is this way I´m writing recursive function here considered “good style”. Like for example what I am doing creating a wrapper then passing it the int mid and a counter int count.

What this code does is getting values from the array then see if that combined with blockIndex is greater than the mid. So, appart from being inefficient would I get a job writing recursive functions like this?

int NumCriticalVotes :: CountCriticalVotesWrapper(Vector<int> & blocks, int blockIndex)
{

    int indexValue = blocks.get(blockIndex);
    blocks.remove(blockIndex);
    int mid = 9;

    return CountCriticalVotes(blocks, indexValue, mid, 0);
}


int NumCriticalVotes :: CountCriticalVotes(Vector<int> & blocks, int blockIndex, int mid, int counter)
{
    if (blocks.isEmpty())
    {
        return counter;
    }

    if (blockIndex + blocks.get(0) >= mid)
    {
        counter += 1;
    } 

    Vector<int> rest = blocks;
    rest.remove(0);
    return CountCriticalVotes(rest, blockIndex, mid, counter);
}
  • 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-06-15T21:41:33+00:00Added an answer on June 15, 2026 at 9:41 pm

    This is valid to the extent that it’ll work for sufficiently small collections.

    It is, however, quite inefficient — for each recursive call you’re creating a copy of the entire uncounted part of the Vector. So, if you count a vector containing, say, 1000 items, you’ll first create a Vector of 999 items, then another of 998 items, then another of 997, and so on, all the way down to 0 items.

    This would be pretty wasteful by itself, but seems to even get worse. You’re then removing an item from your Vector — but you’re removing the first item. Assuming your Vector is something like std::vector, removing the last item takes constant time but removing the first item takes linear time — i.e., to remove he first item, each item after that is shifted “forward” into the vacated spot.

    This means that instead of taking constant space and linear time, your algorithm is quadratic in both space and time. Unless the collection involved is extremely small, it’s going to be quite wasteful.

    Instead of creating an entire new Vector for each call, I’d just pass around offsets into the existing Vector. This will avoid both copying and removing items, so it’s pretty trivial to make it linear in both time and space (which is still well short of optimum, but at least not nearly as bad as quadratic).

    To reduce the space used still further, treat the array as two halves. Count each half separately, then add together the results. This will reduce recursion depth to logarithmic instead of linear, which is generally quite substantial (e.g., for a 1000 items, it’s a depth of about 10 instead of about a 1000. For a million items, the depth goes up to about 20 instead of a million).

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

Sidebar

Related Questions

I've used this code https://github.com/johannilsson/android-pulltorefresh It all works perfectly apart from one thing, when
This is a part of the syntax highlighting code, taken from TextMate. Do you
I have this tabs that are working perfect in any browser apart from IE
I don't often reformat text, apart from the plain gq so this is probably
I've seen here that what sets a programming language apart from a scripting language
Apart from rewriting a lot of XSLT code (which I'm not going to do),
I am writing some code in Node.js that jumped out at me as being
Apart from what is shown in the NXP websites regarding mifare plus and mifare
Apart from lxml , is anyone aware of Python packages that depend on libxml2
Apart from Sql server profiler ,is there any third party user friendly tool available

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.