# complete sequence

Printable View

• 12-28-2010, 10:40 AM
aizen92
complete sequence
Hello again,
I got a bit of a problem, i am supposed to write a code that check the difference between the numbers of the sequence, and check if they are between 1 and their number - 1
eg. if the sequence is of 8 elements, to check if the difference occupies all the numbers between 1 and 7 (8-1)

ive wrote the following code, and gave it a not complete sequence, but it is giving me a complete sequence, can someone check it for and and tell me where is my problem

Code:

```public class Problem1 {     public static void main (String[] args)     {         int [] array = {1, 3, 4, 8, 11, 17, 22, 30};         int n = array.length;         int end = n - 1;         int [] diff = new int [n-1];         Boolean status = false;                 for ( int i = 0; i < n - 1; i++)         {             diff [i] = Math.abs(array[i] - array[i + 1]);         }                 for ( int i = 0; i < n - 1; i++)         {             if ( diff [i] >= 1 && diff [i] <= end)             {                 status = true;             }else{                 status = false;                 break;             }         }                 if ( status = true )         {             System.out.println("Complete Sequence");         }else{             System.out.println("Not Complete Sequence");         }     } }```
Thanks in advance
• 12-28-2010, 10:54 AM
JosAH
Quote:

Code:

`        if ( status = true )`

This assigns the value true to variable status and the if-clause also evaluates to true; you probably meant '==' instead of '='.

kind regards,

Jos
• 12-28-2010, 10:57 AM
aizen92
Great :D
Thnx very much, I didnt notice this
• 12-28-2010, 12:35 PM
aizen92
Ok
after doing some more coding, I've got to a complete code for the whole problem which is the following
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));         int n; // for number of elements         int len; // for the length of the array         int end; // for the checking of difference between 1 and end         Boolean status = false; // true if complete, false if not complete                 while (input.hasNextLine()) // to check for each line         {             n = input.nextInt(); // number of element in each line             end = n - 1; // used for the checking             int [] array = new int [n]; // to put the numbers in an array for checking the sequence             int [] diff = new int [n-1]; // to write down the difference between the elements of the array                         for ( int i = 0; i < n; i++) // to fill up the array with the inegers found in each line             {                 array [i] = input.nextInt();             }                         for ( int j = 0; j < n - 1; j++) // to find out the difference between the elements and put them in new array             {                 diff [j] = Math.abs(array[j] - array[j + 1]);             }                         for ( int k = 0; k < n - 1; k++) // to check is the the elements in the array diff if they are between 1 and end             {                 if ( diff [k] >= 1 && diff [k] <= end )                 {                     status = true;                 }else{                     status = false;                     break;                 }             }                         if (status == true ) // to print the result             {                 System.out.println("Complete Sequence");             }else{                 System.out.println("Not Complete Sequence");             }         }     } }```
My input and output should be the following

Quote:

input:

4 1 4 2 3
5 1 4 2 -1 6
1 10
10 10 9 8 7 6 5 4 3 2 1
9 1 3 4 8 11 17 22 30 37
8 1 3 4 8 11 17 22 30

output:

Complete
Not Complete
Complete
Not Complete
Complete
Not Complete
but im getting is

Quote:

Complete Sequence
Not Complete Sequence
Not Complete Sequence
Complete Sequence
Complete Sequence
Not Complete Sequence
what is the problem with my code?
• 12-28-2010, 02:52 PM
pbrockway2
A few things that could make your code easier to follow:

Use Java naming conventions and call your class Test.

Don't declare variables you don't use like len. Give other variables names that are descriptive enough that you can mostly dispense with the comments.

Code:

```if(someCondition) {     status = true; } else {     status = false;     break; }```

is better written as

Code:

`status = someCondition;`

(and the "break" is quite redundant)

Finally,

Code:

```if()status == true) {     doSomething(); } else {     doSomethingElse(); }```

is better written as

Code:

```if(status) {     doSomething(); } else {     doSomethingElse(); }```

--------------------

As to the logic of what you have written, the final for loop is quite strange: you keep evaluating the value of status and doing nothing with that value. Only the last time around the loop will status aquire a value that is used for anything.

Try and express an algorithm (a recipe, a precise set of instructions) that will give status the value you want it to have before you begin to write code.
• 12-28-2010, 03:01 PM
JosAH
Quote:

input:

4 1 4 2 3
5 1 4 2 -1 6
1 10
10 10 9 8 7 6 5 4 3 2 1
9 1 3 4 8 11 17 22 30 37
8 1 3 4 8 11 17 22 30

output:

Complete
Not Complete
Complete
Not Complete
Complete
Not Complete

I don't understand the problem; according to your original post the difference between two consecutive numbers should be less than the total number of numbers, right? If so, why is the sequence starting with "10 10 ..." not complete? The length of the sequence is 11 so the maximum difference should be 10 or less ...

kind regards,

Jos
• 12-28-2010, 03:24 PM
pbrockway2
Quote:

I don't understand the problem

You're not allowed a difference of 10-10=0.
• 12-28-2010, 03:27 PM
aizen92
Quote:

I don't understand the problem; according to your original post the difference between two consecutive numbers should be less than the total number of numbers, right? If so, why is the sequence starting with "10 10 ..." not complete? The length of the sequence is 11 so the maximum difference should be 10 or less ...

kind regards,

Jos

yeh about that sorry, but i forgot to mention that when the sequence consist of 1 integer its automatically considered a complete sequence
• 12-28-2010, 03:33 PM
JosAH
Quote:

You're not allowed a difference of 10-10=0.

Ah, yes, got it, but why is the previous series "1 10" a 'complete' series? the max difference has to be 2-1 == 1 ...

kind regards,

Jos (<--- extremely stupid)
• 12-28-2010, 03:36 PM
aizen92
Quote:

Ah, yes, got it, but why is the previous series "1 10" a 'complete' series? the max difference has to be 2-1 == 1 ...

kind regards,

Jos (<--- extremely stupid)

I said, this is a special case, when a sequence is of only 1 integer its directly considered a complete sequence, so "1 10" 1 is for the length of the sequence and 10 is the element in this sequence, and since its 1 element only, its complete
• 12-28-2010, 03:43 PM
pbrockway2
OK. If the first number is the sequence length and not part of the sequence, then we're back to Jos' question since the 10 element sequnce has a difference of 1 and so should be complete.
• 12-28-2010, 03:46 PM
aizen92
Quote:

OK. If the first number is the sequence length and not part of the sequence, then we're back to Jos' question since the 10 element sequnce has a difference of 1 and so should be complete.

but the thing is to be complete it should occupy all the numbers between the 1 and the length - 1, in the 10 element sequence the difference between them all is 1

EDIT: Ok i added an if statement that says

Code:

```if ( array.length == 1 ) status = true;```
and it worked for the single sequence

Now all thats left is the 10 element sequence
• 12-28-2010, 03:50 PM
pbrockway2
Are you sure you aren't supposed to get each of the numbers 1->(len-1) exactly once as a difference?

 Ok, so it appears you are.

Adding a special case for 1 element sequences is not going to help. You need to figure out an algorithm that will work in general. How would you go about dooing this with pen and paper if you were given the numbers one at a time (and told how many there would be)? Start from there before you begin to write code or it will all be a bit random.
• 12-28-2010, 03:52 PM
JosAH
Quote:

but the thing is to be complete it should occupy all the numbers between the 1 and the length - 1, in the 10 element sequence the difference between them all is 1

For the more stupid people among us (i.e. me) could you please give a complete definition of a (in)complete sequence before we discuss matters further?

kind regards,

Jos
• 12-28-2010, 03:58 PM
aizen92
LOL, sorry for the much trouble

lets say for example, the sequence "1 4 2 3",

the diff between 1 and 4 is 3
the diff between 4 and 2 is 2
the diff between 2 and 3 is 1

then length of the sequence is 4, and the differences occupy the numbers from 1 to the length of the sequence - 1, which is 3, which is true (note: not necessarily consecutive numbers in the difference, but it happened to be the case in this sequence)

another example, is the sequence of "1 4 2 -1 6"

the diff between 1 and 4 is 3
the diff between 4 and 2 is 2
the diff between 2 and -1 is 3
the diff between -1 and 6 is 7 (we use absolute value when negative number)

the length of the sequence is 5, and the difference do not occupy all the numbers from 1 to length of the sequence - 1, which is 4, and we have two numbers repeated so its not a complete sequence

I hope this is a good explanation of the question
• 12-28-2010, 04:01 PM
pbrockway2
My guess: a sequence is complete iff the differences form a permutation of the first n-1 natural numbers. (no special case required)
• 12-28-2010, 04:03 PM
aizen92
Quote:

My guess: a sequence if complete iff the differences form a permutation of the first n-1 natural numbers. (no special case required)

well yeh in a sense it is
• 12-28-2010, 04:19 PM
JosAH
Quote:
I don't trust that "in a sense" part. Can't you give a complete definition?

kind regards,

Jos
• 12-28-2010, 04:21 PM
aizen92
what do you mean by a complete definition, im lost here lol
• 12-28-2010, 04:26 PM
JosAH
Quote:

what do you mean by a complete definition, im lost here lol

Pbrockway2 "guessed" a definition and you replied "in a sense". I hate it when there is no definition and the developer has to guess what to do. You said you want to decide when a series of numbers is (in)complete. You must have a definition for it.

kind regards,

Jos