Skip to content

Commit 99707f6

Browse files
The longest ascending sequence #47
1 parent 3894d39 commit 99707f6

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

Arrays/1D Arrays/longestAscendingSequence.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@
99
public class longestAscendingSequence {
1010

1111
public static void main(String[] args) {
12+
13+
//Read elements and size
1214
Scanner scanner = new Scanner(System.in);
1315
int tam = scanner.nextInt();
1416
int[] numbers = new int[tam];
1517

1618
for (int i = 0; i < numbers.length; i++) {
1719
numbers[i] = scanner.nextInt();
1820
}
21+
22+
//Defining auxiliary variables
1923

2024
int maxTmp = 0;
2125
int count = 1;
2226
int pos = -1;
2327

28+
//Traverse the array and increment the amount by 1 the longest amount in the array, in addition to obtaining the position
2429
for (int i = 0; i < numbers.length - 1; i++) {
2530
if (numbers[i] < numbers[i + 1]) {
2631
++count;
@@ -33,13 +38,16 @@ public static void main(String[] args) {
3338
}
3439
}
3540

41+
//Check if count or maxTemp is the highest to assign the position from which the numbers will start printing
3642
if (maxTmp > count) {
3743
pos = (pos - maxTmp);
3844
}
3945

46+
//Get max value
4047
int max = Math.max(count, maxTmp);
4148
System.out.println("The max value is : " + max);
4249

50+
//Print elements
4351
for (int i = pos + 1; i <= pos + max; i++) {
4452
System.out.print(numbers[i] + " ");
4553
}
@@ -72,4 +80,4 @@ public static void main(String[] args) {
7280

7381
}
7482

75-
}
83+
}

0 commit comments

Comments
 (0)