Skip to content

Commit 3894d39

Browse files
The longest ascending sequence #47
1 parent 0e043b2 commit 3894d39

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

Arrays/1D Arrays/longestAscendingSequence.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
/**
44
* Write a program that reads an array of ints and outputs the length of the longest sequence in strictly ascending order.
5+
* Print the elements of longest ascending sequence in an array
56
* Elements of the sequence must go one after another. A single number is assumed to be an ordered sequence with the length = 1.
67
*/
8+
79
public class longestAscendingSequence {
810

911
public static void main(String[] args) {
@@ -17,23 +19,57 @@ public static void main(String[] args) {
1719

1820
int maxTmp = 0;
1921
int count = 1;
22+
int pos = -1;
2023

2124
for (int i = 0; i < numbers.length - 1; i++) {
2225
if (numbers[i] < numbers[i + 1]) {
2326
++count;
2427
} else {
2528
if (count > maxTmp) {
2629
maxTmp = count;
30+
pos = i;
2731
}
2832
count = 1;
2933
}
3034
}
3135

36+
if (maxTmp > count) {
37+
pos = (pos - maxTmp);
38+
}
39+
3240
int max = Math.max(count, maxTmp);
41+
System.out.println("The max value is : " + max);
3342

34-
System.out.println(max);
43+
for (int i = pos + 1; i <= pos + max; i++) {
44+
System.out.print(numbers[i] + " ");
45+
}
3546

3647
scanner.close();
48+
49+
50+
/*
51+
Value test
52+
10
53+
1 2 4 1 2 3 5 7 4 3
54+
55+
12
56+
1 2 4 1 2 3 5 7 8 9 10 11
57+
58+
20
59+
1 2 4 1 2 3 5 7 4 3 5 8 9 10 12 15 16 2 5 6
60+
61+
15
62+
1 0 2 4 6 3 8 7 0 1 5 8 2 1 3
63+
64+
3
65+
1 2 4
66+
67+
4
68+
1 2 0 0
69+
70+
8
71+
1 2 3 4 5 6 0 3*/
72+
3773
}
3874

3975
}

0 commit comments

Comments
 (0)