Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Search Array for Consecutive Numbers (Java)

1. ## Search Array for Consecutive Numbers (Java)

Hi,

I'm fairly new to Java. I have an array of 5 numbers, all of which are between 1 and 6. I'm looking for a way to see if the array contains 4 consecutive numbers, for example:

5,6,4,3,1 would return true because it contains 3-4-5-6
1,6,4,5,6 would return false because there aren't 4 consecutive numbers

Is there a way to do this?

• Sure you can. Though I think your example is wrong, since the first one isn't consecutive either :P
What you need to do is track the last known number, and whether its up or down. I will just use simple variables and branches.
PHP Code:
``` public boolean hasConsecutive(int[] iaNumbers, int iConsecutive) {     int iLastKnown, iDir, iConsec;     boolean bResult = false;     if (iaNumbers.length >= iConsecutive) // Pointless if its less     {         iLastKnown = iaNumbers[0];         iDir = 0;         iConsec = 1;         for (int i = 1; i < iaNumbers.length && bResult == false; ++i)         {             if (iaNumbers[i] == (iLastKnown + 1) && iDir >= 0)             {                 iDir = 1;                 ++iConsec;             }             else if (iaNumbers[i] == (iLastKnown - 1) && iDir <= 0)             {                 iDir = -1;                 ++iConsec;             }             else             {                 iConsec = 1;                 iDir = 0;             }             iLastKnown = iaNumbers[i];             if (iConsec >= iConsecutive)             {                 bResult = true;             }         }     }     return bResult; }  ```
Seems to work ok up in the attic. I know that I can clean up those if/else branches, but I'm to tired to look into that right now >.<
Try that.

• Originally Posted by Fou-Lu
I think your example is wrong, since the first one isn't consecutive either :P
I don't think it is. Maybe you didn’t quite understand exactly what I'm looking for. I'd like it to return true even if the consecutive numbers aren't in order in the array.

1,2,3,4,5 would be true, and
5,2,4,1,3 is also true, because it contains the consecutive numbers, even though they aren't in order.

Does your code account for the numbers not being in order?

• I dont know whether I understood it correctly, but a try.

Algo:-

Set a boolean array of 6 elements.

for each element in input array
if element <= 6 and >=1 {
boolean arr[element-1] = true;
}
end for

count = 0;
for each element in boolean arr[]
if boolean arr[elem] = true{
count++;
if(count>=4)
return true;
}else{
count = 0;
}
end for
return false;

• @sandeep.e:

Surely your code is just counting the number of elements which are between 1 and 6? That's not what I'm looking for at all. Thanks for trying, though.

• @jobbogamer

I think you havenot checked the algorithm properly.
The following is the implementation..

Check it out....

PHP Code:
``` public static void main(String[] args) {         int array[] = new int[]{1,3,2,4,5};         boolean boolArray[] = new boolean[6];         for(int i=0;i<5;i++){             int elem = array[i];             if((elem<=6)&&(elem>=1)){                 boolArray[elem-1] = true;             }         }         int count = 0;         for(int i=0;i<6;i++){             if (boolArray[i]){                 count++;                 if(count>=4){                     System.out.println("true");                     System.exit(0);                 }             }else{                 count = 0;             }         }         System.out.println("false");      }  ```

• Yeah, our definitions of consecutive is different, I assumed you wanted to keep the ordering as well.
That makes this a lot easier then. Simply sort the array and see if the next item is 1 greater than the previous. The above has a few too many magic numbers in use, so if you go that route make sure to use something a little more dynamic. I did nothing to bind the values from 1 to 6, as that is irrelevant to this method and is something that would be taken care of when populating the array itself.
PHP Code:
```     public static boolean isConsecutive(int[] ia, int iConsecutive)     {         boolean bResult = false;         int iLastKnown, iConsec;         if (ia.length >= iConsecutive)         {             int[] iaClone = Arrays.copyOf(ia, ia.length); // Copy it so we don't sort the original             Arrays.sort(iaClone);             iLastKnown = iaClone[0];             iConsec = 1;             for (int i = 1; i < iaClone.length && !bResult; ++i)             {                 if (iaClone[i] == (iLastKnown + 1))                 {                     ++iConsec;                 }                 else                 {                     iConsec = 1;                 }                 if (iConsec >= iConsecutive)                 {                     bResult = true;                 }                 iLastKnown = iaClone[i];             }         }         return bResult;     }  ```
Try that. Overload it to chain a call if you don't want to constantly set how many consecutive numbers you want:
PHP Code:
``` public static boolean isConsecutive(int[] ia) {     return isConsecutive(ia, 4); }  ```

• I've built the following solution for you:

Code:
```package consecutive;

public class Main {

private long[] array = null;

private int n;

private int m;

public Main()
{
n = ((int)(Math.random() * 1000)) + 1;
m = ((int)(Math.random() * 1000)) + 1;
array = new long[n];
for (int i = 0; i < n; i++)
array[i] = ((long)(Math.random() * n)) + 1;
}

public boolean isThereConsecutiveM()
{
if ((m > n) || (m <= 0) || (n <= 0))
return false;
if (m == 1)
return true;
java.util.Arrays.sort(array);
for (int i = 0; i + m - 1 < n; i++)
if (array[i + m - 1] - array[i] == m - 1)
{
boolean consecutive = true;
for (int j = i; j < i + m - 1; j++)
{
if (array[j + 1] - array[j] != 1)
consecutive = false;
}
if (consecutive) return true;
}
return false;
}

public static void main(String[] args) {
Main main = new Main();
System.out.println((main.isThereConsecutiveM()) ? "We have enough consecutive numbers" : "We don't have enough consecutive numbers");
// TODO code application logic here
}

}```
Note that n is the number of elements, m is the number of consecutive numbers searched, array is the array. In your particular case n = 5, m = 4.

•