Skip to content

Commit 9ca9d30

Browse files
The longest ascending sequence
1 parent c0369ca commit 9ca9d30

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Write a program that reads an array of ints and outputs the length of the longest sequence in strictly ascending order.
5+
* Elements of the sequence must go one after another. A single number is assumed to be an ordered sequence with the length = 1.
6+
*/
7+
public class longestAscendingSequence {
8+
9+
public static void main(String[] args) {
10+
Scanner scanner = new Scanner(System.in);
11+
int tam = scanner.nextInt();
12+
int[] numbers = new int[tam];
13+
14+
for (int i = 0; i < numbers.length; i++) {
15+
numbers[i] = scanner.nextInt();
16+
}
17+
18+
int maxTmp = 0;
19+
int count = 1;
20+
21+
for (int i = 0; i < numbers.length - 1; i++) {
22+
if (numbers[i] < numbers[i + 1]) {
23+
++count;
24+
} else {
25+
if (count > maxTmp) {
26+
maxTmp = count;
27+
}
28+
count = 1;
29+
}
30+
}
31+
32+
int max = Math.max(count, maxTmp);
33+
34+
System.out.println(max);
35+
36+
scanner.close();
37+
}
38+
39+
}

0 commit comments

Comments
 (0)