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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:34:48+00:00 2026-06-01T06:34:48+00:00

I am writing an algorithm to count the number of bars have passed since

  • 0

I am writing an algorithm to count the number of bars have passed since the n-th peak.
Assuming that I have a list of integer which keeps track of all the occurrences of peak as below.

3, 7, 10, 13

The above tells me that the peak occurs at the index 3, 7, 10 and 13.
The length of the source data is 15.

Let n = 1

then I should be seeing a resultant list as below:

  • index 0 = null – there is no peak yet

  • index 1 = null – there is no peak yet

  • index 2 = null – there is no peak yet

  • index 3 = 0 – peak was hit here so there bars passed is 0

  • index 4 = 1 – peak was hit in previous bar so the bars passed is 1

  • index 5 = 2 – peak was already hit and the number of bars passed is 2

  • index 6 = 3 – peak was already hit and the number of bars passed is 3

  • index 7 = 0 – peak hit here again so the number of bars passed since the last 1 (n) peak is 0 and result follows as below:

    index 8 = 1,
    index 9 = 2,
    index 10 = 0,
    index 11 = 1,
    index 12 = 2,
    index 13 = 0,
    index 14 = 1

Assume n = 2

number of bars passed since 2 most recent peaks. So the result is as below:

  • index 0 = null

  • index 1 = null

  • index 2 = null

  • index 3 = null – first peak but n = 2 so there is no 2nd peak so far…

  • index 4 = null

  • index 5 = null

  • index 6 = null

  • index 7 = 4 – second peak only hit here, so the number of days passed since the last 2 peak is 4, this is counted from the index where the first peak was hit, in this case is 3

  • index 8 = 5

  • index 9 = 6

  • index 10 = 3 – another peak hit here, the peak before this (n = 2) happened at index = 7, so it is counted from index = 7

  • index 11 = 4

  • index 12 = 5

  • index 13 = 3 – another peak hit here, the peak before this (n = 2) happened at index = 10, so it is counted from index = 10

  • index 14 = 4

I wrote an algorithm to accomplish this however, it’s not giving me the right output. I would appreciate if someone could tell me what’s wrong with my algo or propose a better way doing this.

    int nth =  GetNValue();// Get the N Value...
    //SourceData = listData
    int peakCount = 0;
    int value= 0; 
    int barssince = 0;
    List<Data> listValue = new List<Data>();
    List<int> listResult =  new List<int); //to hold the result..
    List<int> listPeaks = GetPeakValue();
    for (int index = 0; index < listData.Count; index++)
    {
     if (peakcount > 0)
     {
           barssince++;
           listValue[listValue.Count - 1].Value = barssince;
     }
     int foundPeak = listPeaks.Find(delegate(int p) { return p == index; });
     if (foundPeak != -1)//Peak FOund
     {
         peakcount++;
         listValue.Add(new Data() { Value = barssince });
         if (peakcount > nth)
         {
               listValue.RemoveAt(0);
         }
         barssince = 0;
     }
     value = listValue.Count >= nth ? listValue.Sum(p => p.Value) : null;
     listResult.Add(value);
    }
    private class Data
    {
       public int Value { get; set; }
    }
  • 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-01T06:34:49+00:00Added an answer on June 1, 2026 at 6:34 am

    Try this:

    int n =  GetNValue();// Get the N Value...   
    List<int> listPeaks = GetPeakValue(); 
    List<int?> result = new List<int?>(listData.Count);
    for (int index = 0, peakIndex = -1, nextPeakIndex = peakIndex + n; index < listData.Count; index++)
    {
        if (nextPeakIndex < listPeaks.Length && index == listPeaks[nextPeakIndex])
        {
            peakIndex++;
            nextPeakIndex++;
        }
    
        if (peakIndex < 0)
        {
            result.Add((int?)null);
        }
        else
        {
            result.Add((int?)(index - listPeaks[peakIndex]));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an algorithm that iterates over a list of points, calculates the distance
I'm writing a program that calculates the Jacobi algorithm. It's written in Objective-C since
I am writing an algorithm that divides a floating point number by 2. In
I have an algorithm that performs some file I/O (reading, writing) and computation. If
Lets say I am writing Dijkstra's Algorithm , and I have a priority queue
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
I'm writing a low level image processing algorithm which needs to do alot of
Let's start from the beginning: I'm writing an algorithm for a Silverlight application, which
I'm writing an algorithm that detects clones in source code. E.g. if there is
I am writing a paper and implemented an algorithm using OpenMP. Since Visual Studio

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.