# Thread: complete sequence

1. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11

## 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

Java 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

2. Originally Posted by aizen92
Java 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

3. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Great :D
Thnx very much, I didnt notice this

4. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Ok
after doing some more coding, I've got to a complete code for the whole problem which is the following
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; // 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

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

Complete Sequence
Not Complete Sequence
Not Complete Sequence
Complete Sequence
Complete Sequence
Not Complete Sequence
what is the problem with my code?
Last edited by aizen92; 12-28-2010 at 12:38 PM.

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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.

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

is better written as

Java Code:
`status = someCondition;`

(and the "break" is quite redundant)

Finally,

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

is better written as

Java 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.

6. Originally Posted by aizen92
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

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
I don't understand the problem

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

8. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Originally Posted by JosAH
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

9. Originally Posted by pbrockway2
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)

10. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Originally Posted by JosAH
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

11. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Originally Posted by 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.
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

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

Now all thats left is the 10 element sequence
Last edited by aizen92; 12-28-2010 at 03:49 PM.

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
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.
Last edited by pbrockway2; 12-28-2010 at 03:53 PM.

14. Originally Posted by aizen92
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

15. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
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

16. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
My guess: a sequence is complete iff the differences form a permutation of the first n-1 natural numbers. (no special case required)
Last edited by pbrockway2; 12-28-2010 at 04:04 PM.

17. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
Originally Posted by pbrockway2
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

18. Originally Posted by aizen92
well yeh in a sense it is
I don't trust that "in a sense" part. Can't you give a complete definition?

kind regards,

Jos

19. Senior Member
Join Date
Nov 2010
Posts
155
Rep Power
11
what do you mean by a complete definition, im lost here lol

20. Originally Posted by aizen92
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

Page 1 of 2 12 Last

#### Posting Permissions

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