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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:14:14+00:00 2026-06-04T19:14:14+00:00

Given a long sequence of N (not necessary distinct) numbers, say {1, 50, 3,

  • 0

Given a long sequence of N (not necessary distinct) numbers, say

{1, 50, 3, 99, 1, 2, 100, 99, 4, 100, 4, 100} (could be very long)

and a small set of M ordered pairs, say

(1, 2)

(2, 1)

(1, 3)    

(99, 50)

(99, 100)

I would like to detect whether the ordered pair occurs anywhere in the list (they could be separated, but order matters). For example, the counts above would be:

(1, 2): 2 (each 1 pairs with the later 2)

(2, 1): 0 (no 1's come after the 2)

(1, 3): 1 (only one of the 1's come before the 3)

(99, 50): 0 (no 99's come before the 50)

(99, 100): 5 (3 times for the first 99 and 2 times for the second)

Assuming that every number in the ordered pairs is guaranteed to appear in the list, does there exist an algorithm to extract these counts faster than the naive O(N * M) time (achieved by brute force searching for each ordered pair)?

As a side question, might there be a fast algorithm if we restrict ourselves to boolean occurrences only instead of counts? That is:

(1, 2): yes

(2, 1): no

(1, 3): yes

(99, 50): no

(99, 100): yes

Any help would be 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-04T19:14:16+00:00Added an answer on June 4, 2026 at 7:14 pm

    Keep two hashes, one mapping numbers to the least position at which they occur, and one mapping numbers to the greatest position at which they occur. The ordered pair (a, b) appears in order if least[a] < greatest[b] (and both hash keys are present). Preprocessing time is linear, space usage is linear, query time is constant (under standard assumptions about the complexity of hashing).

    As for the counting version, the best I can think of is to keep one hash mapping each number to the positions at which it occurs in sorted order. To query a pair, “merge” the position lists, keeping track of the number of a-elements so far and the number of pair occurrences. When a b-element is selected to be next, increment the number of pairs by the number of a-elements. When an a-element is selected to be next, increment the number of a-elements. (If a == b, return length choose 2.)

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

Sidebar

Related Questions

What is the best algorithm to take a long sequence of integers (say 100,000
Given I have a long phrase, say, in line :12 , as this example:
I want to multiply long numbers which are given in a 2^32 basis. I
I have a very long sequence of strings which individually need to be processed
Given a string that isn't too long, what is the best way to read
Given a function y = f(A,X): unsigned long F(unsigned long A, unsigned long x)
I need to draw a line one inch long on any device given a
I have a long, lazy sequence that I want to reduce and test lazily.
I face a situation where we have many very long methods, 1000 lines or
Given a list of slices, how do I separate a sequence based on them?

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.