# Thread: Help with Array problem!

1. Member
Join Date
Jun 2011
Posts
6
Rep Power
0

## Help with Array problem!

I need some guidance again. I am given this problem to solve. Can someone please help me soon!?!

Write a method named minGap that accepts an integer array as a parameter and returns the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value minus the first value. For example, suppose a variable called array is an array of integers that stores the following sequence of values.

int[] array = {1, 3, 6, 7, 12};
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. Notice that the minimum gap could be a negative number. For example, if array stores the following sequence of values:

{3, 5, 11, 4, 8}
The gaps would be computed as 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4). Of these values, -7 is the smallest, so it would be returned.

This gap information can be helpful for determining other properties of the array. For example, if the minimum gap is greater than or equal to 0, then you know the array is in sorted (nondecreasing) order. If the gap is greater than 0, then you know the array is both sorted and unique (strictly increasing).

If you are passed an array with fewer than 2 elements, you should return 0.

This is what i got below.

public static void minGap(int[] array) {

for (int i=0; i < (array.length); i++)

2. and you want us to do the rest?

Before writing any code, grab a piece of paper and a pencil and work out how you would do it by hand.

4. Member
Join Date
May 2011
Location
Maryland
Posts
38
Rep Power
0
if you read the example it basically tells you exactly how to do it
all you need to do is subtract each value from the one before it and store it as a variable
then if the for loop ever reaches another value thats less than that one then it should change it to that value

5. "If you are passed an array with fewer than 2 elements, you should return 0."

6. Member
Join Date
Jun 2011
Posts
6
Rep Power
0
Junky: I'm not asking for you to do this for me, I'm asking on some advice on how to get it solved as I did with the previous time I asked about binary numbers. I'm having difficulties on forming a code to compute the gaps. I know I have to add indexes 0 &1 , 1 & 2, 2 & 3, ect...., but am I supposed to create another array to assign the computed gaps into it?

Fubarable: I guess I edited the wrong thread... Need some help!!! I apologize if you feel unthankful for helping me out in my last thread. I really do appreciate all for the help.

7. You only need 2 variables: the smallest gap so far and the current gap. Each time you calculate a new current gap compare it to the smallest so far to see if it is the new smallest.

8. Originally Posted by mr2insane
Fubarable: I guess I edited the wrong thread... Need some help!!! I apologize if you feel unthankful for helping me out in my last thread. I really do appreciate all for the help.
I'm not talking about my reply which was nothing but a little house-keeping, but rather Norm and pbrockway's replies. Again we/they would appreciate it if you reply in that thread with a thanks for their help, that's all. That's how folks are "paid" in here.

9. Member
Join Date
Jun 2011
Posts
6
Rep Power
0
Thanks again guys... this helped me out alot!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•