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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:39+00:00 2026-06-10T16:36:39+00:00

Assuming you have a sequence like this: 0, 1, 2, 3, 4, 5, 6,

  • 0

Assuming you have a sequence like this: 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, etc. Basically a sequence consisting of N numbers, repeating over and over.

What is the simplest algorithm for finding the distance/difference between two numbers in this sequence? For example the distance from 5 to 7 is +2 and the distance from 0 to 6 is -2. For a more high level view what I have a looping/repeating sequence number, and I need to find out how much “before” or “after” a number is of another on the closest path (fewest number between them).

  • 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-10T16:36:41+00:00Added an answer on June 10, 2026 at 4:36 pm

    Assuming X>Y:

    dist(X, Y) = min { X-Y, N-(X-Y-1) }
    

    Examples for N = 7:

    dist(7, 5) = min {7-5, 7-(7-5-1)} = min {2, 6} = 2

    dist(6, 0) = min {6-0, 7-(6-0-1)} = min {6, 2} = 2

    dist(5, 1) = min {5-1, 7-(5-1-1)} = min {4, 4} = 4

    The last example points a small flaw in your distance definition: Is dist(5, 1) = 4 or dist(5, 1) = -4 ? I’ve changed your definition a little bit to avoid negative distances (so my algorithm calculates the absolute value of the distance). If you want to keep your definition then make the distance negative if and only if the first argument of the min is greater than the second.

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

Sidebar

Related Questions

Assuming I have an Entity Framework 4.2 class like this: class Company { public
I have a schema fragment that looks like <xs:element name=dataValue> <xs:complexType> <xs:sequence> <xs:element name=value
Assuming I have a JQuery object stored in $e obtained through this selector: var
Assuming I have HTML similar to this: <div class=fooContainer> <div class=barContainer> <a href=#>foo-bar</a> </div>
Assuming you have this url : http://www.domain.com////// . How is possible to change the
Assuming I have an element called menu how would I remove this element from
Assuming I have an SQL table with this schema: CREATE TABLE( foo INTEGER, bar
Assuming I have HTML like <body id=doc> <div id=container> ddd </div> </body> And my
Assuming I have a numerical string: var foo = 0; Assume I want to
Assuming I have a ASP.NET MVC 3 application that runs in a web farm

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.