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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:16:10+00:00 2026-06-17T10:16:10+00:00

For every run of x or more consecutive zeros in a list in C++,

  • 0

For every run of x or more consecutive zeros in a list in C++, I would like to delete all zeros in the run except for x of them. If x = 0, then delete all zeros.

I was thinking of a C++ function that took a list, list<int> L, and a number, int x, as inputs.

For example, let L = {7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8}.

  • If x = 0, then return L = {7, 12, 2, 27, 10, 8}
  • If x = 1, then return L = {7, 0, 12, 0, 2, 0, 27, 10, 0, 8}
  • If x = 2, then return L = {7, 0, 12, 0, 0, 2, 0, 0, 27, 10, 0, 0, 8}
  • If x = 3, then return L = {7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 8}
  • If x = 4, then return L = {7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8} (Same as original L)
  • If x >= 5, then return original L as there are no runs of 5 or more consecutive zeros.

Several months ago, I asked the same question above using Python (stackoverflow.com/questions/11732554/…) and received excellent answers. Now I would like to complete this task in C++.

Any help would be sincerely appreciated.

  • 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-17T10:16:11+00:00Added an answer on June 17, 2026 at 10:16 am

    Here’s some code that should do the job:

    void DeleteAllZerosInARow(std::list<int>& theList, int x)
    {
        if(x == 0)
        {
            theList.remove(0);
            return;
        }
    
        int streak = 0;
        std::list<int>::iterator itor = theList.begin();
        while(itor != theList.end())
        {
            if(*itor == 0)
                ++streak;
            else
                streak = 0;
    
            if(streak > x)
                itor = theList.erase(itor);
            else
                ++itor;
        }
    }
    

    Basically, you count how many zeros you have in a row, and delete them if you’re > x, otherwise continue iterating the list.

    Giving the following output:

    • 0 : 7,12,2,27,10,8
    • 1 : 7,0,12,0,2,0,27,10,0,8
    • 2 : 7,0,12,0,0,2,0,0,27,10,0,0,8
    • 3 : 7,0,12,0,0,2,0,0,0,27,10,0,0,0,8
    • 4 : 7,0,12,0,0,2,0,0,0,27,10,0,0,0,0,8
    • 5 : 7,0,12,0,0,2,0,0,0,27,10,0,0,0,0,8

    It depends on your style, remove_if might be the more C++ish way to do it, but I find it clearer to manipulate the values directly and it doesn’t involve a new data type (a struct to keep track of the number of 0 you encountered).

    The reason why the code doesn’t work using NTL::ZZ is simply that there is no implicit conversion between an int, 0, and a NTL::ZZ big number, therefore it cannot remove(0). What you can do though could be something along the lines of:

    if(x == 0)
    {
        static ZZ zero; // default value is 0, static so that it is only constructed once
        theList.remove(zero); // remove all items who are equal to "zero"
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For every run of x or more consecutive zeros in a list in Python,
I have a program which I would like to run every X min the
I have an ajax app that will run functions on every interaction. I'd like
i currently have a list of classes every class starts a scheduler like this:
I have a long-running process that must run every five minutes, but more than
I'm creating a simple job that I'd like to run every 60 seconds (1
I am trying to get a timer run every minute in sync with the
If I have a cron job that run every 10 minutes and for some
I have a PHP page that I run every minute through a CRON job.
I have a php script I need to run every 5 seconds (run, wait

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.