# Accenture Coding Question 3

## Coding Question 3

Implement the following Function

The function def ProductSmallestPair(sum, arr) accepts an integers sum and an integer array arr of size n. Implement the function to find the pair, (arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k] are the least two elements of array (arr[j] + arr[k] <= sum) and return the product of element of this pair

NOTE

• Return -1 if array is empty or if n<2
• Return 0, if no such pairs found
• All computed values lie within integer range

Example

Input

sum:9

Arr:5 2 4 3 9 7 1

Output

2

Explanation

Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2

Sample Input

sum:4

Arr:9 8 3 -7 3 9

Sample Output

-21

```n = int(input())
sum1 = int(input())
arr = list(map(int, input().split()))
if n < 2:
print('-1')
arr = sorted(arr)
for i in range(n-1):
if arr[i] + arr[i+1] < sum1:
print(arr[i] * arr[i+1])
break
else:
print('0')```
```Input:
6
4
9 8 3 -7 3 9
Output:
-21```
```#include<stdio.h>;

int productSmallestPair(int *array, int n, int sum)
{
int answer, temp, i, j, check;
if(n<=2)
{
}
else
{
for(i=0; i<n; i++)          //sorting of array
{
for(j=i+1; j<n; j++)
{
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
check = array[0] + array[1];
if(check<=sum)
{
}
else
{
}
}
}

int main()
{
int n, sum, result, i;
scanf("%d",&sum);
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d",result);
return 0;
}```
`#include<bits/stdc++.h>using namespace std; int productSmallestPair(int *array, int n, int sum){    int answer, temp, i, j, check;    if(n<=2)    {        answer = -1;    }    else    {        sort(array, array+n);        check = array[0] + array[1];        if(check<=sum)        {            answer = array[0] * array[1];        }        else        {            answer = 0;        }       }    return answer;} int main(){    int n, sum, result;        cin>>sum>>n;    int array[n];        for(int i=0; i<n; i++)    cin>>array[i];        result = productSmallestPair(array, n, sum);        cout<<result;        return 0;}`
`import java.util.*;class Solution {    public static int productSmallestPair (int arr[], int n, int sum)     {        if (n <= 2)            return -1;        int ans, temp, check;        for (int i = 0; i < n; i++)        {            for (int j = i + 1; j < n; j++)    	    {                if (arr[i] > arr[j])    	        {                    temp = arr[i];                    arr[i] = arr[j];                    arr[j] = temp;                }            }        }        check = arr[0] + arr[1];                if (check <= sum)            return arr[0] * arr[1];        else            return 0;    }        public static void main (String[]args)     {        Scanner sc = new Scanner (System.in);        int sum = sc.nextInt ();        int n = sc.nextInt ();        int arr[] = new int[n];            for (int i = 0; i < n; i++)            arr[i] = sc.nextInt ();        System.out.println (productSmallestPair (arr, n, sum));    } }`