This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data Structure to retrieve list of numbers between 2 given numbers in a Array

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

I have a array of integers = [1,3,2,5,4,6, 9,1, 11, 13, 7,12, 0]
My Input is startIndex = 3 & endIndex = 11

total numbers between (3 & 11) inclusive of start and end index = [3, 2, 5,4,6,9,1,11] = 8 (total numbers)

Which data structure can perform this operation in an efficient way & how?

Please share your thoughts.

Regards,
Prabhu
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think about it? What is your proposal?
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thought is:

 
Marshal
Posts: 79007
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by start index? That is not an index. You mean starting value and ending value.
What will happen if there is no 3 or no 11 in your array? What will happen with this array?
[1, 2, 69, 4]
How are you going to find 3 and 11?
What about this array, which does have 3 and 11 in?
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about this array, which does have 3 and 11 in?


Thanks Campbell.. I forgot about this..
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tushal/Campbell,

Thanks for sharing your thoughts.

Yes, it's start and end value.

Case 1: if the start & end value (3,7) are not in array {2, 1,8,5,4} output array should be empty {} and the count will be zero

Case 2: if start & end value (3,7) in the given array {2,1,8,7,4,6,11,10,3,5} then the output array should be {3,5,2,1,8,7} and count will be 6 inclusive of start and end value

Regards,
Prabhu
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More things needed clarification.
What should be a result if there is only one match? Example, {1, 2, 3, 4} against start/end 2 and 5?
What is there is more than one match? Example, {1, 2, 5, 1, 3 ,1, 3, 4, 5, 4, 2, 1, 5} against 2 and 5?
What if start and end are the same? Example {1, 2, 3, 2, 1} against 2 and 2?
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pawel,

Topic is getting more interesting, I'm learning something new from you guys. Thank you

Case 1 will be no result found
Case 2 when there is multiple match we can have two result set one with the closest match and the second will be the last value match
Case 3 start and end value can be same

 
Campbell Ritchie
Marshal
Posts: 79007
375
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you need to do is provide some specification for the problem which will cover all those cases.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

Below is some specifications for my problem: {3,1,2,1,5,6,0,9,13,7,12,11,8,-1,10}

Case 1: Start Value: 2 End value: 7
output: {2,1,5,6,9,13,7} [Note: '0' can be ignored]
Total count between no 2 & 7 (inclusive of start & end no): 7

Case 2: Start Value: -1 End Value: 2
output: {-1, 10,3,1,2}
Total Count between no -1 & 2 (inclusive of start & end no): 5

Case 1: I am clear with this

Case 2: ?? not clear on how to perform this logic
 
Campbell Ritchie
Marshal
Posts: 79007
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not a specification. A few examples do not constitute a specification.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

In this array: {3,1,2,1,5,6,0,9,13,7,12,11,8,-1,10} how to retrieve the numbers between two given no: startValue = 7 & endValue = 5?

is there a way to iterate using recursive function or what would be the best way to achieve this?


Regards,
Prabhu
 
Campbell Ritchie
Marshal
Posts: 79007
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by writing down on paper which numbers are between start = 7 and end = 5.

If you do not have a specification for such exercises, you will not be able to do them.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Camphell,

What do you mean by specification here?

Here is my specification: "How to get the elements between 3 and 11? in the given array [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] ".

Please Ranch Hand's with proper solution or direct them in a right way; instead of giving a reply like this 'Start by writing down on paper which numbers are between start = 7 and end = 5.'

Regards,
Prabhu
 
Campbell Ritchie
Marshal
Posts: 79007
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you wrote down the numbers between start = 5 and end = 5, what did you get? Did you get any at all?

It is not my business to be clear about 3 to 11 in that array. It is your business. If you aren't clear about it, nobody can solve it. You have to tell us what you mean by 3 to 11 because otherwise nobody knows.
 
Tell me how it all turns out. Here is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic