Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Best Data Structure to solve this problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Could you please suggest me which data structure is best to use to solve this problem


I have set of int ranges in the form of two dimensional array e.g int[][] array= {{1- 4},{6-10}{8-20},{20- 30},{50-60}};

I want to combining ranges wherever possible to make a range which cover all of sub-set of the ranges e.g {{6-10}{8-20},{20- 30}}={6-30}

and my final set of ranges will be like array= {{1- 4},{6-30},{50-60}}


Thanks in advance!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try to use a doubly linked list.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could, 1) add each individual value to a Set. This would
eliminate any duplicates. Then 2) sort the set, and 3) scan
the set to formulate the new ranges.

Jim ... ...
 
danial leksevo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim,

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're on the right track. If the Set is sorted on Integer, you can
just look for a missing value to find the end of a range. Give it a try.

Jim ... ...
 
Saloon Keeper
Posts: 12024
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should create a Range class. You can then write a utility method
 
What are you doing? You are supposed to be reading this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic