-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommon2Arrays.java
More file actions
51 lines (43 loc) · 914 Bytes
/
LongestCommon2Arrays.java
File metadata and controls
51 lines (43 loc) · 914 Bytes
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
package Arrays;
public class LongestCommon2Arrays {
public static void main(String[] args) {
int []a= {2,2,5,9,7,7,3,8};
int []b= {2,2,2,5,9,7,7,3,8};
logestcommonSubsequence(a,b);
}
public static void logestcommonSubsequence(int []a,int []b){
int indexA=0,indexB=0;
int maxA=0,maxB=0;
int ctA=0,ctB=0;
for(int i=0;i<a.length-1;i++) {
if(a[i]==a[i+1]) {
ctA++;
}
else if(ctA+1>0) {
if(ctA>maxA){
maxA=ctA+1;
indexA=a[i];
}
ctA=0;
}
}
for(int j=0;j<b.length-1;j++) {
if(b[j]==b[j+1]) {
ctB++;
}
else if(ctB+1>0) {
if(ctB>maxB){
maxB=ctB+1;
indexB=b[j];
}
ctB=0;
}
}
if(maxA>maxB) {
System.out.println("Arrays A Element "+indexA+" Is Repeating For "+maxA+" Times");
}
else {
System.out.println("Arrays B Element "+indexB+" Is Repeating For "+maxB+" Times");
}
}
}