-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathIdenticals.java
66 lines (60 loc) · 1.67 KB
/
Identicals.java
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
/*
* File: Identicals.java
* Author: Ashish Chopra
* Date: 9 Apr, 2015
* --------------------------------------
* Identicals is an implementation of an algorithm to print
* identical integers in the given two sorted arrays of integers of size N.
*
* You are given two sorted arrays of integers, you need to print the
* integer present in both the arrays, in sorted order.
*
* The expectation is to perform this task at T(N) ~ N for worst case.
*
*/
package com.applications;
public class Identicals {
private int[] first; // first array to hold N integers
private int[] second; // second array to hold N integers
Identicals(int[] arr1, int[] arr2) {
this.first = arr1;
this.second = arr2;
}
/**
* returns the identical numbers found in
* both the sorted array of N integers in running
* time of T(N) ~ N for worst case.
*
*/
public String printIdenticals() {
// instead of printing, we return the list
// to compare with expected values
String list = "[";
int i = 0, j = 0;
while (i < first.length && j < second.length) {
int result = compare(first[i], second[j]);
if (result == 0) {
list += first[i] + ",";
i++; j++;
} else if (result == -1)
j++;
else
i++;
}
list += "]";
System.out.println(list);
return list;
}
/**
* compares the two integers a and b
* @param a Integer at first position
* @param b Integer at second position
* @return <code>0</code> when a and b are equal,
* <code>1</code> when b is greater than a,
* <code>-1</code> when a is greater than b
*/
private int compare(int a, int b) {
if (a == b) return 0;
return a < b ? 1 : -1;
}
}