Skip to content

Commit 16cc332

Browse files
Largest sequential multiple subsequence task
1 parent 4f8c7b9 commit 16cc332

File tree

1 file changed

+35
-0
lines changed
  • src/algorithms/largest_sequential_multiple_subsequence

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package algorithms.largest_sequential_multiple_subsequence;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
private void run() {
7+
Scanner scanner = new Scanner(System.in);
8+
int arraySize = scanner.nextInt();
9+
int[] array = new int[arraySize];
10+
for (int i = 0; i < arraySize; i++) {
11+
array[i] = scanner.nextInt();
12+
}
13+
int largestSubsequenceLength = calcLargestSequentialMultipleSubsequenceLength(array);
14+
System.out.println(largestSubsequenceLength);
15+
}
16+
17+
private int calcLargestSequentialMultipleSubsequenceLength(int[] array) {
18+
int largestSubsequenceLength = 1;
19+
int[] sequencesLength = new int[array.length];
20+
for (int i = 0; i < array.length; i++) {
21+
sequencesLength[i] = 1;
22+
for (int j = 0; j < i; j++) {
23+
if (sequencesLength[j] + 1 > sequencesLength[i] && array[i] % array[j] == 0) {
24+
sequencesLength[i] = sequencesLength[j] + 1;
25+
}
26+
}
27+
largestSubsequenceLength = Integer.max(largestSubsequenceLength, sequencesLength[i]);
28+
}
29+
return largestSubsequenceLength;
30+
}
31+
32+
public static void main(String[] args) {
33+
new Main().run();
34+
}
35+
}

0 commit comments

Comments
 (0)