Finding intersections in a given range ?

  • Thread starter Thread starter mohamed el teir
  • Start date Start date
  • Tags Tags
    Range
AI Thread Summary
The discussion revolves around efficiently counting values in an array that are divisible by at least one number from a given small set S within specified ranges. A proposed method involves using cumulative sums stored in multiple arrays for each divisor from the set {1,2,...,10} to achieve O(1) query time. However, calculating intersections of multiples for various combinations of S presents a challenge due to the potential for high computational complexity. Suggestions include leveraging dynamic programming and advanced data structures like red-black trees to optimize the intersection calculations. The focus remains on finding a solution that minimizes both time and memory usage while handling numerous queries efficiently.
mohamed el teir
Messages
88
Reaction score
1
assume array of N (N<=100000) elements a1, a2, ... ,an, and you are given range in it L, R where 1<=L<=R<=N, you are required to get number of values in the given range which are divisible by at least one number from a set S which is given also, this set can be any subset of {1,2,...,10}. a fast way must be used because it may ask you for more than one range and more than one S (many queries Q, Q<=100000), so looping on the values each time will be very slow.

i thought of storing numbers of values divisible by each number in the big set {1,2,...,10} in 10 arrays of N elements each, and do cumulative sum to get the number of values divisible by any specific number in any range in O(1) time, for example if it requires to get number of values divisible by at least one of the following: 2,3,5, then i add the numbers of values divisible by each of them and then remove the intersections, but i didn't properly figure out how to calculate the intersections without 2^10 or 2^9 calculations each time which will be also very slow (and possibly hugely memory consuming) because it may be done 100000 times, any ideas ?
 
Technology news on Phys.org
Well, the main I need to make sure you are aware of is the complexity of the factorization a number into its prime factors, which is so hard its used in encryption schemes. You are pretty much left with brute force methods. One thing that isn't hard to do if S is small is to produce the multiples of numbers in your set S that might fall within the range given, then you are left with a set intersection problem which has pretty fast implementations in dynamic programming and in data structures like red black trees.
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top