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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:11:08+00:00 2026-05-27T18:11:08+00:00

Can someone give me ideas for the following problem? Given an array ar[] of

  • 0

Can someone give me ideas for the following problem?

Given an array ar[] of length n, and some queries, each query is of the form a, b, c, find the number with the smallest index i such that the index lies in the range [c, n] and such that a < ar[i] < b. There are (n - 1) in total, c goes from 1 to n - 1. The expected complexity for each query should be about O(log n), and a precomputation of complexity at most O(n log n) is suitable. Intuitively, segment tree came up to my mind, but I couldn’t think of a way of building it, nor what to keep in each node.

  • 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-05-27T18:11:09+00:00Added an answer on May 27, 2026 at 6:11 pm

    I’ve modified Mason Bryant’s technique to something that works. The problems were more bugs in FindLowestIndex and a more major bug searching the tree (it can return multiple results).

    Despite doing that work, it still doesn’t actually solve the problem. O(n log n) time setting up is easy enough, but using this technique I’m only able to get O((log n)^2) query time. I wonder if you have a link to the original problem in case there are more clarifications there? Or I wonder if the problem is really solvable. Or maybe O((log n)^2) is “about” O(log n) as the problem requests, it’s less than O(n) anyway.

    The technique is to store our array in a typical segment tree, but along with the usual segment information we also store, in order, all the indexes of the elements under each node. This extra storage only takes an extra O(n log n) time/space if you add it up properly (n items stored at each of log n levels), so it doesn’t hurt our setup time in any way that matters. Then we query the tree to find the minimum set of nodes that are contained by our range of (a, b). This query takes about the same time as a typical segment tree query (O(log n)) and finds at most about 2*log n matching segments. As we query, we find the lowest index in each matching segment that matches our constraint c. We can use a binary search to find this index since we kept the indices in order, so it takes O(log n) time worst case for each matching node. When we add it all up appropriately, the total time is O((log n)^2).

    Let me know whichever steps you’d like clarified.

    C# Code:

    void Test() {
      DateTime start;
      TimeSpan at = new TimeSpan(), bt = new TimeSpan();
    
      Random rg = new Random();
      for(int i = 0; i < 20; i++) {
        // build arrays from 5 to 10000 elements long, random values between 0 and 100
        // Break even time for queries is around 10000 elements in array for the values and queries used
        int[] array = (from n in Enumerable.Range(1, rg.Next(5, 10000)) select rg.Next(0, 100)).ToArray<int>();
    
        // Setup should be O(n log n) time/space
        ArraySearcher Searcher = new ArraySearcher(array);
    
        // Test each array a number of times equal to its length, with random values for a, b, c
        for(int j = 0; j < array.Length; j++) {
          int a = rg.Next(-1, 101), b = rg.Next(a, 102), c = rg.Next(0, array.Length);
          start = DateTime.Now;
          int expected = BruteSearch(array, a, b, c);
          at += DateTime.Now - start;
          // Search should be O(log n) time, but in reality is O((log n)^2) :(
          start = DateTime.Now;
          int got = Searcher.QuickSearch(a, b, c);
          bt += DateTime.Now - start;
          System.Diagnostics.Debug.Assert(got == expected);
        }
      }
      MessageBox.Show(at.ToString() + ", " + bt.ToString());
    }
    
    int BruteSearch(int[] array, int a, int b, int c) {
      for(int i = c; i < array.Length; i++)
        if(a < array[i] && array[i] < b)
          return i;
      return -1;
    }
    
    class ArraySearcher {
    
      SegmentNode Root;
      List<SegmentNode> Nodes;
    
      public ArraySearcher(int[] array) {
        Nodes = array.Select((value, index) => new SegmentNode(value, value, new List<int> { index }, null, null)).ToList<SegmentNode>();
        // Sorting will take us O(n log n)
        Nodes.Sort();
        // Creating a normal segment tree takes O(n log n)
        // In addition, in this tree each node stores all the indices below it in order
        // There are a total of n of these indices at each tree level, kept in order as we go at no extra time cost
        // There are log n levels to the tree
        // So O(n log n) extra time and space is spent making our lists, which doesn't hurt our big O notation compared to just sorting
        this.Root = SegmentNode.Merge(Nodes, 0, Nodes.Count - 1);
      }
    
      public int QuickSearch(int a, int b, int c) {
        return this.Root.FindLowestIndex(a, b, c);
      }
    
      class SegmentNode : IComparable {
    
        public int Min, Max;
        public List<int> Indices;
        public SegmentNode Left, Right;
    
        public SegmentNode(int Min, int Max, List<int> Indices, SegmentNode Left, SegmentNode Right) {
          this.Min = Min;
          this.Max = Max;
          this.Indices = Indices;
          this.Left = Left;
          this.Right = Right;
        }
    
        int IComparable.CompareTo(object other) {
          return this.Min - ((SegmentNode)other).Min;
        }
    
        public static SegmentNode Merge(List<SegmentNode> Nodes, int Start, int End) {
          int Count = End - Start;
          if(Start == End)
            return Nodes[Start];
          if(End - Start == 1)
            return SegmentNode.Merge(Nodes[Start], Nodes[End]);
          return SegmentNode.Merge(SegmentNode.Merge(Nodes, Start, Start + Count/2), SegmentNode.Merge(Nodes, Start + Count/2 + 1, End));
        }
    
        public static SegmentNode Merge(SegmentNode Left, SegmentNode Right) {
          int LeftCounter = 0, RightCounter = 0;
          List<int> NewIndices = new List<int>();
          while(LeftCounter < Left.Indices.Count || RightCounter < Right.Indices.Count) {
            if(LeftCounter < Left.Indices.Count && (RightCounter == Right.Indices.Count || Left.Indices[LeftCounter] < Right.Indices[RightCounter]))
              NewIndices.Add(Left.Indices[LeftCounter++]);
            else
              NewIndices.Add(Right.Indices[RightCounter++]);
          }
          return new SegmentNode(Left.Min, Right.Max, NewIndices, Left, Right);
        }
    
        public int FindLowestIndex(int SeekMin, int SeekMax, int c) {
          // This will find at most O(log n) matching segments, always less than 2 from each level of the tree
          // Each matching segment is binary searched in at worst O(log n) time
          // Total does indeed add up to O((log n)^2) if you do it right
          if(SeekMin < this.Min && SeekMax > this.Max)
            return FindLowestIndex(this.Indices, c);
          if(SeekMax <= this.Min || SeekMin >= this.Max)
            return -1;
          int LeftMatch = this.Left.FindLowestIndex(SeekMin, SeekMax, c);
          int RightMatch = this.Right.FindLowestIndex(SeekMin, SeekMax, c);
          if(LeftMatch == -1)
            return RightMatch;
          if(RightMatch == -1)
            return LeftMatch;
          return LeftMatch < RightMatch ? LeftMatch : RightMatch;
        }
    
        int FindLowestIndex(List<int> Indices, int c) {
          int left = 0, right = Indices.Count - 1, mid = left + (right - left) / 2;
          while(left <= right) {
            if(Indices[mid] == c)
              return c;
            if(Indices[mid] > c)
              right = mid - 1;
            else
              left = mid + 1;
            mid = left + (right - left) / 2;
          }
          if(mid >= Indices.Count)
            return -1;
          // no exact match, so return the next highest.
          return Indices[mid];
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give some hints of how to delete the last n lines from
Can someone give me some example code that creates a surface with a transparent
Can someone give me some info/background info on how I might go about writing
Could someone please give me some advice/ideas about how to deal with the situations
This is a weird one, but hopefully someone can give me an idea here.
Can someone give an example of a good time to actually use unsafe and
Can someone give me an example of how I would delete a row in
Can someone give me a real-world scenario of a method/function with a string argument
Can someone give an example for finding greatest common divisor algorithm for more than
can someone give me a hint on how a histogram's pseudo code would look

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.