-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFindPairWithProduct.java
49 lines (43 loc) · 1.33 KB
/
FindPairWithProduct.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
package com.geeksforgeeks.array;
import java.util.ArrayList;
import java.util.List;
public class FindPairWithProduct {
public static void main(String[] args) {
int[][] arr = {
{10, 20, 9, 40},
{10, 20, 9, 40},
{-10, 20, 9, -40},
{-10, 20, 9, 40},
{0, 20, 9, 40},
{2, 2, 3}
};
int[] products = {400, 190, 400, -400, 0, 6};
int counter = 0;
for (int[] input : arr) {
System.out.println(getStringRepresentation(input) + "-->" + isPairExist(input, products[counter++]));
}
}
public static String getStringRepresentation(int[] arr) {
StringBuffer sb = new StringBuffer();
sb.append("{");
for (int i : arr) {
sb.append(i + ",");
}
sb.append("}");
return sb.toString();
}
public static boolean isPairExist(int[] input, int product) {
List<Integer> helperList = new ArrayList<>();
int temp = 0;
for (int i : input) {
if (helperList.contains(i) || (product == 0 && product == i)) {
return true;
}
if (product % i == 0) {
temp = product / i;
helperList.add(temp);
}
}
return false;
}
}