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

The Archive Base Latest Questions

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

I have a sequence of k nodes N1 , N2 , N3 , …

  • 0

I have a sequence of k nodes N1, N2, N3, … Nk each of which gets hit in succession (with possible skipping).

Every time I visit one of these nodes I need to += the time it took to get there from the previous node. The tricky part is that if I come back to N1 without reaching Nk, then these += updates should be dropped.

One method is to keep in each node two quantities: x and y. As we hop nodes we += values into y. If we get to N1 we reset y to 0. whereas if we reach Nk we do x += y for each node.

The problem is that every time we hit Nk it requires an O(n) operation–even if it might not be the common case for a sequence to return to N1 without hitting Nk. Is there a smarter way to do this more efficiently without an O(n) “commit” on every iteration reaching the end?

Consider this example with 3 nodes: N_1, N_2, N_3:

The left shows the subsequence of nodes hit on an iteration and the right shows what
the accumulation counters should contain:

(N_1, 2)(N_2, 3)(N_3, 7) ---> (N_1, 2)(N_2, 3)(N_3, 7)
(N_1, 4)(N_3, 2)         ---> (N_1, 6)(N_2, 3)(N_3, 9)
(N_1, 6)(N_2, 3)         ---> (N_1, 4)(N_2, 3)(N_3, 2) //nothing changes as this was an "invalid" op because we never hit the end node
etc...
  • 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-06T13:15:34+00:00Added an answer on June 6, 2026 at 1:15 pm

    You can maintain two accumulators (accum_[2]) in each node, and a global 1-bit counter (k_counter) that is incremented when the k-th node is reached. Then maintain the invariant that accum_[k_counter] always has the right accumulation value for each node. In this scheme, if you skip nodes, you are forced to visit them, and perform node[i] += 0 on them. That requirement could be optimized away with a visit counter, which I’ll leave as an exercise :-).

    enum { K = 100 };
    struct Node *node;
    struct Node {
        static bool k_counter;
        unsigned accum_[2];
        unsigned id_;
        Node () : accum_(), id_(this - node + 1) {}
        void operator += (unsigned time_data) {
            accum_[!k_counter] = accum_[k_counter] + time_data;
            if (id_ == K) k_counter = !k_counter;
        }
        operator unsigned () const { return accum_[k_counter]; }
    };
    bool Node::k_counter;
    
    node = new Node[K];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file with a group of protein nodes, each one with
I was wondering if this was possible... I have a sequence of variables that
I have some types with a sequence inside, which does restrict the order of
I have a project where I have multiple nodes providing services. One ( and
I have a sequence. a1 = 1 - cos(x); ai = a1 + (-1)^(i-1)
suppose i have a sequence, called TEST_SEQ what would be the correct way of
Suppose I have a sequence x1,x2,x3.....xn, and I want to find the longest continuous
I have an xts sequence of stock trade events that I want to process
I have a question regarding sequence diagrams. When drawing sequence diagrams, Is it enough
Given a sequence of data (it may have duplicates), a fixed-sized moving window, move

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.