Skip to content

Commit 02cdc3a

Browse files
committed
complexity analysis to print all unordered pairs of items in array
1 parent f03bd8c commit 02cdc3a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package BasicPractice;
2+
3+
// complexity: O(mn)
4+
// where m is length of arr1 and n is length of arr2
5+
6+
public class complexityAnalysis3 {
7+
8+
public static void main(String[] args) {
9+
10+
int[] arr1 = { 10, 2, 13, 4 };
11+
int[] arr2 = { 5, 6, 7, 80, 9, 10 };
12+
printUnorderedPairs(arr1, arr2);
13+
}
14+
15+
public static void printUnorderedPairs(int[] arr1, int[] arr2) {
16+
for (int i = 0; i < arr1.length; i++) { // O(arr1.length) time
17+
for (int j = 0; j < arr2.length; j++) { // O(arr2.length) time
18+
if (arr1[i] < arr2[j]) { // O(1) constant time
19+
System.out.println(arr1[i] + " " + arr2[j]);
20+
}
21+
}
22+
}
23+
}
24+
}
25+
26+
/*
27+
output:
28+
10 80
29+
2 5
30+
2 6
31+
2 7
32+
2 80
33+
2 9
34+
2 10
35+
13 80
36+
4 5
37+
4 6
38+
4 7
39+
4 80
40+
4 9
41+
4 10
42+
*/

0 commit comments

Comments
 (0)