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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:32:22+00:00 2026-06-12T22:32:22+00:00

I encountered the problem in a programming interview and have no idea about it

  • 0

I encountered the problem in a programming interview and have no idea about it by now.

A list whose length is n, the elements in it are all positive integers without order.
To find out all possible triples (a, b, c), that a < b < c, and a appears before b and b before c in the list.

And analyse the time complexity of your algorithm.

  • 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-12T22:32:23+00:00Added an answer on June 12, 2026 at 10:32 pm

    If it is known that there are only a small set of triples (say k), then you may prefer to find all the triples by storing pointers to the previous smallest element.

    ALGORITHM

    Prepare an empty data structure (possible choices described later).

    Prepare an empty array B of length n.

    Then for each element c in the list:

    1. Store the index (in the array B) of the most recent element in the list that is smaller than c (if it exists) using the data structure.
    2. Store c (and its index in the original list) in the data structure
    3. Then use array B to find all elements b smaller than c, and then again to find all elements a smaller than b, and emit all these combinations as output triples.

    DATA STRUCTURE

    The data structure needs to be able to store value,position pairs to make it easy to find the largest position (i.e. most recent) over all elements with value less than c.

    One easy way to do this if the range of allowed values is fairly small is to use a series of arrays where A[k][x] stores the maximum position for all elements in the range [x*2^k,(x+1)*2^k).

    If the values have up to M bits (i.e. the values are in the range 0 to 2^M-1) then updating or accessing this data structure are both O(M) operations.

    COMPLEXITY

    The given method is O(nM+k).

    If the values have a larger range, then you could use a form of binary search tree instead of the series of arrays, or instead sort the values and replace the values with their ordinal value. This would then have complexity O(nlogn+k).

    COUNTING TRIPLES

    If you just wish to know the total number of triples of this form then you can do this in O(n).

    The idea is similar to before:

    1. Find the most recent smaller element for each index, and the count of smaller elements for each index
    2. Find the next greater element for each index, and the count of greater elements
    3. Compute the sum of the product of the count of smaller elements and the count of larger elements for each index.

    To make this O(n) we need to be able to find the next greater element in O(n). This can be done by:

    1. Push the current index i to the stack
    2. while A[top(stack)] < A[i+1], pop an index x off the stack and store NGE[x]=i+1
    3. increment i and return to step 1

    We also need to be able to find the count of greater elements in O(n). Once the NGE array has been prepared, we can find the counts by iterating backwards over the array and computing

    count_greater_elements[i] = count_greater_elements[ NGE[i] ] + 1 if NGE[i] is defined
                              = 0 otherwise
    

    The most recent smaller elements and counts can be computed in an analogous way.

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

Sidebar

Related Questions

I've been programming Java for 2 years now, and I have encountered a problem
I'm fighting with socket programming now and I've encountered a problem, which I don't
I have recently taken up programming and encountered a problem when it comes to
I am beginner in C# programming. I have encountered small problem during building application
I encountered a problem with the scope variables have when IPython is invoked at
I have encountered a problem where when i use any theme in the android
I have encountered a problem, for which no matter how long I study the
Here is an interesting problem that I encountered in a programming competition: Problem statement:
I was reading through this Advanced Linux Programming tutorial when I encountered a problem.
Since I've never encountered this kind of problem I thought about making language parser

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.