programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Ranch Hand
Posts: 147
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?

Regards,
Prabhu

Ranch Hand
Posts: 934
4

Tushar Goel
Ranch Hand
Posts: 934
4
My thought is:

Marshal
Posts: 56608
172
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?
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Tushar Goel
Ranch Hand
Posts: 934
4

prabhu pandurangan
Ranch Hand
Posts: 147
Hi Tushal/Campbell,

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: 2087
44
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
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: 56608
172
• 1
What you need to do is provide some specification for the problem which will cover all those cases.

prabhu pandurangan
Ranch Hand
Posts: 147
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: 56608
172
That is not a specification. A few examples do not constitute a specification.

prabhu pandurangan
Ranch Hand
Posts: 147
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: 56608
172
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
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: 56608
172
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.