Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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[] iaNumbersint iConsecutive)
    {
        
    int iLastKnowniDiriConsec;
        
    boolean bResult false;

        if (
    iaNumbers.length >= iConsecutive// Pointless if its less
        
    {
            
    iLastKnown iaNumbers[0];
            
    iDir 0;
            
    iConsec 1;

            for (
    int i 1iaNumbers.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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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?

  • #4
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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;
    Last edited by sandeep.e; 02-01-2011 at 05:26 AM. Reason: no proper indentation

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    @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.

  • #6
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    @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"); 
        } 

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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[] iaint iConsecutive)
        {
            
    boolean bResult false;
            
    int iLastKnowniConsec;
            if (
    ia.length >= iConsecutive)
            {
                
    int[] iaClone Arrays.copyOf(iaia.length); // Copy it so we don't sort the original
                
    Arrays.sort(iaClone);
                
    iLastKnown iaClone[0];
                
    iConsec 1;
                for (
    int i 1iaClone.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(ia4);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

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