Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given an array **A** of **N** elements. The task is to find the greatest number **S** such that it is product of two elements of given array (S cannot be included in pair. Also, pair must be from different indices).

**Example 1:**

**Input :** arr[] = {10, 3, 5, 30, 35}
**Output:** 30
**Explanation:** 30 is the product of 10 and 3.

**Input :** arr[] = {2, 5, 7, 8}
**Output:** -1
**Explanation:** Since, no such element exists.

**Input :** arr[] = {10, 2, 4, 30, 35}
**Output:** -1

**Input :** arr[] = {10, 2, 2, 4, 30, 35}
**Output:** 4

**Input :** arr[] = {17, 2, 1, 35, 30}
**Output :** 35

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **findGreatest()** which takes the array **arr[]** and its size **N **as inputs and returns the answer. If the answer is not present in the array, return **-1**.

**Expected Time Complexity:** O(N. Log(N))

**Expected Auxiliary Space:** O(N)

**Constraints:**

1 ≤ N ≤ 10^{6}

1 ≤ A_{i} ≤ 10^{7}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Pair with greatest product in array

...