-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintersection.java
More file actions
66 lines (58 loc) · 1.35 KB
/
intersection.java
File metadata and controls
66 lines (58 loc) · 1.35 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
/*
* @author Vishesh Jain
* @date 27-Feb-2019
*/
package lecture6;
import java.util.ArrayList;
import java.util.Scanner;
public class intersection {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) {
int[] a = takeinput();
int[] b = takeinput();
System.out.println(intersection(a,b));
}
public static int[] takeinput() {
int[] array;
System.out.println(" enter size ");
int n = a.nextInt();
array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = a.nextInt();
}
return array;
}
// public static ArrayList<Integer> intersection(int[] one, int[] two) {
// ArrayList<Integer> list = new ArrayList<>();
// int j = 0;
// for (int i = 0; i < one.length; i++) {
// while (j < two.length &&(one[i] >= two[j]) ) {
//
// if (one[i] == two[j]) {
// list.add(one[i]);
// j++;
// break;
//
// }
// j++;
// }
// }
// return list;
// }
public static ArrayList<Integer> intersection(int[] one, int[] two) {
ArrayList<Integer> list = new ArrayList<>();
int j = 0;
int i = 0;
while (i<one.length && j<two.length ) {
if(one[i] > two[j])
j++;
else if(one[i] < two[j])
i++;
else {
list.add(one[i]);
i++;
j++;}
}
return list;
}
}