-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccenture_40.java
More file actions
67 lines (58 loc) · 1.71 KB
/
Copy pathaccenture_40.java
File metadata and controls
67 lines (58 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Your are given an array A of Ssize N. Your task is to find the maximum length of the subsequence in which the difference of every consecutive element is divisible by k.
Not:
The input array is sorted in ascending order. The length of the subsequence must be greater than 1 and if there is no subsequence satisfying the condition, return -1.
Input format:
The inpur vonsists of two lines:
The first line consists two space seperated integers n and k.
The second line contains n soace-seperated integers denoting the array A
Output format:
Print a number representing the maximum length of the subsequence satisfying the consition
Constraints:
- 1<=N,k<=10^4
- 1<=arr[i]<=10^3
Example:
Input:
2 2
2 4
Output:
2
input:
3 3
1 1 2
Output:
2
*/
import java.util.Scanner;
public class accenture_40 {
static int subsequence(int[] a, int n, int k) {
int[] m = new int[n];
for (int i = 0; i < n; i++) {
m[i] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if ((a[i] - a[j]) % k == 0) {
m[i] = Math.max(m[i], m[j] + 1);
}
}
}
int max = 0;
for (int i = 0; i < n; i++) {
max = Math.max(max, m[i]);
}
if (max < 2)
return -1;
return max;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
sc.close();
System.out.println(subsequence(a, n, k));
}
}