Given a set of positive integers s = {a1, a2, ..., an},

There exists ai * aj = ak, i != j != k

Find the maximum number satisfying the above conditions, if there are no three numbers satisfying the above condition, the output –1

public static int findProduct(int a[]) {

int n = a.length;

for (int i = 0; i < n; i++) {

for (int j = 0, k = i + 1; j < i && k < n;) {

if (a[i] * a[j] == a[k]) {

return a[k]; // find

} else if (a[i] * a[j] > a[k])

k++;

else

j++;

}

}

return -1;

}

## No comments:

Post a Comment