0

I am currently using segment tree to find out the smallest and largest number in a range in an unsorted array by comparing and storing the smallest and largest of sub-arrays.

However, I am getting confused on what information needs to be saved while finding two smallest numbers and one largest number in a range in an unsorted array.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75

1 Answers1

0

It's quite simple actually,
instead of storing min and max in range of a node, you can store max and array/ArrayList of minimum values.

When mergining 2 nodes:

  1. Create new array/ArrayList,
  2. copy both children arrays into it
  3. Sort it
  4. Delete elements from back till you have array with size <= 2
Photon
  • 2,717
  • 1
  • 18
  • 22