Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    you mean what r the conditions that render a sequence in-complete...
    if so they are:

    - there is a number greater than n - 1 // where n is the length of the sequence
    - not all numbers between 1 and n - 1 are found as differences
    - when there is a number between 1 and n - 1 that is repeated more than once

    EDIT: Well i got it right finally

    I added another for loop with an if statement in it that check the array diff in all of its elements if it has equal elements to return false

    heres the code

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class test
    {
        public static void main (String[] args) throws FileNotFoundException
        {
            Scanner input = new Scanner (new File(args[0]));
            int n;
            int end;
            Boolean status = false;
            
            while (input.hasNextLine())
            {
                n = input.nextInt();
                end = n - 1;
                int [] array = new int [n];
                int [] diff = new int [n-1];
                
                for ( int i = 0; i < n; i++)
                {
                    array [i] = input.nextInt();
                }
                
                for ( int j = 0; j < n - 1; j++)
                {
                    diff [j] = Math.abs(array[j] - array[j + 1]);
                }
                
                for ( int k = 0; k < n - 1; k++)
                {
                    if ( diff [k] >= 1 && diff [k] <= end )
                    {
                        status = true;
                    }else{
                        status = false;
                        break;
                    }
                }
                
                if ( array.length == 1)
                status = true;
                
                [B]for ( int p = 0; p < n - 2; p++)
                {
                    if ( diff [p] == diff [p + 1])
                    status = false;
                }[/B]
                
                if (status == true )
                {
                    System.out.println("Complete Sequence");
                }else{
                    System.out.println("Not Complete Sequence");
                }
            }
        }
    }
    Last edited by aizen92; 12-28-2010 at 04:41 PM.

  2. #22
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default

    [QUOTE=aizen92;166331]you mean what r the conditions that render a sequence in-complete...
    if so they are:

    - there is a number greater than n - 1 // where n is the length of the sequence
    - not all numbers between 1 and n - 1 are found as differences
    - when there is a number between 1 and n - 1 that is repeated more than once

    Isn't that a complicated way of saying: a sequence of n numbers has to be the a permutations of the numbers m, m+1, m+2 ... m+n-1 for m <= 0?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I want to add something
    I want to make sure that n is not greater than 3000

    so i added this if statement after having n get the nextInt() the following statement

    Java Code:
    if ( n >= 3000 )
    {
                    System.out.println("Your sequence is greater than 3000 elements");
                    break;
    }
    but the thing is after the break its finishing everything, i want it to continue for the next sequences, not to stop the whole program on this

  4. #24
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You could try something like:

    Java Code:
    while (input.hasNextLine())
    {
        n = input.nextInt();
        if(n >= 3000)
        {
            System.out.println("Message");
            input.nextLine(); // read the line and throw it away
            continue;
        }
        end = n - 1;
        int [] array = new int [n];
        int [] diff = new int [n-1];
            
        // etc as before
    }
    -------------------------

    Already the method is becoming quite long. You should think about using separate methods each doing just one thing.

  5. #25
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Ok, it did the trick
    thnx very much pbrockway2

  6. #26
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM
  2. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  3. calling sequence
    By rocky in forum Web Frameworks
    Replies: 0
    Last Post: 04-27-2009, 08:35 PM
  4. Fetching sequence
    By Shivraj in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 02:30 PM
  5. Escape Sequence
    By Punter in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 07:04 AM

Posting Permissions

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