# Imply of array generated by merchandise of all pairs of the given array

Given an array arr[] consisting of N integers, the duty is to seek out the mean of the array fashioned by the merchandise of unordered pairs of the given array.

Examples:

Enter: arr[] = {2, 5, 7}
Output: 19.67
Clarification:
Product of unordered pairs of array arr[] are 2 * 5 = 10, 2 * 7 = 14 and 5 * 7 = 35.
Subsequently, the resultant array of product of pairs is {10, 14, 35}.
Imply of the array of product of pairs is 59/3 = 19.67

Enter: arr[] = {1, 2, 4, 8}
Output: 11.67
Clarification:
Product of unordered pairs of array arr[] are 1 * 2 = 2, 1 * 4 = 4, 1 * 8 = 8, 2 * 4 = 8, 2 * 8 = 16, 4 * 8 = 32.
Subsequently, the resultant array of product of pairs is {2, 4, 8, 8, 16, 32}.
Imply of the array of product of pairs is 70/6 i.e., 11.67

Naive Strategy: The best strategy to unravel the issue is to generate all possible pairs of array pairProductArray[] i.e., array fashioned by the product of unordered pairs of the array arr[]. Then, discover the mean of the pairProductArray[]. Observe the steps under to unravel the issue:

• Generate all doable pairs of the array arr[] and retailer their merchandise in pairProductArray[].
• Initialize a variable sum to retailer the sum of the weather of pairProductArray[].
• Divide the variable sum with the dimension of pairProductArray[] to get the required imply.
• Lastly, print the worth of the sum because the resultant imply.

Under is the implementation of the above strategy:

## C++

 ` `  `#embrace ` `utilizing` `namespace` `std; ` ` `  `float` `pairProductMean(``int` `arr[], ``int` `N) ` `{ ` `    ` `    ``vector<``int``> pairArray; ` ` `  `    ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = i + 1; j < N; j++) { ` ` `  `            ``int` `pairProduct ` `                ``= arr[i] * arr[j]; ` ` `  `            ` `            ``pairArray.push_back(pairProduct); ` `        ``} ` `    ``} ` ` `  `    ` `    ``int` `size = pairArray.dimension(); ` ` `  `    ` `    ``float` `sum = 0; ` `    ``for` `(``int` `i = 0; i < size; i++) ` `        ``sum += pairArray[i]; ` ` `  `    ` `    ``float` `imply; ` ` `  `    ` `    ``if` `(size != 0) ` `        ``imply = sum / size; ` `    ``else` `        ``imply = 0; ` ` `  `    ` `    ``return` `imply; ` `} ` ` `  `int` `foremost() ` `{ ` `    ` `    ``int` `arr[] = { 1, 2, 4, 8 }; ` ` `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ` `    ``cout << fastened << setprecision(2) ` `         ``<< pairProductMean(arr, N); ` ` `  `    ``return` `0; ` `} `

Time Complexity: O(N2)
Auxiliary House: O(N2)

Environment friendly Strategy: The thought is to make use of the truth that each ingredient arr[i] is multiplied with each ingredient arr[j] which is on the appropriate aspect of the ingredient arr[i], extra formally ingredient at index i is multiplied to all the weather positioned at index j such that j > i. Observe the steps under to unravel the issue:

• Create a suffix sum array suffixSumArray[] for the given array arr[].
• Initialize variable res to retailer the sum of product pairs of array arr[].
• Iterate array arr[] and for every place i incrementing res with arr[i]*suffixSumArray[i+1].
• Divide the variable res with N*(N – 1)/ 2 which is the variety of doable merchandise.
• Lastly, print the worth of res because the resultant imply.

Under is the implementation of the above strategy:

## C++

 `#embrace ` `utilizing` `namespace` `std; ` ` `  `float` `pairProductMean(``int` `arr[], ``int` `N) ` `{ ` `    ` `    ``int` `suffixSumArray[N]; ` `    ``suffixSumArray[N - 1] = arr[N - 1]; ` ` `  `    ` `    ``for` `(``int` `i = N - 2; i >= 0; i--) { ` `        ``suffixSumArray[i] ` `            ``= suffixSumArray[i + 1] ` `              ``+ arr[i]; ` `    ``} ` ` `  `    ` `    ``int` `size = (N * (N - 1)) / 2; ` ` `  `    ` `    ``float` `res = 0; ` ` `  `    ``for` `(``int` `i = 0; i < N - 1; i++) { ` `        ``res += arr[i] ` `               ``* suffixSumArray[i + 1]; ` `    ``} ` ` `  `    ` `    ``float` `imply; ` ` `  `    ` `    ``if` `(size != 0) ` `        ``imply = res / size; ` `    ``else` `        ``imply = 0; ` ` `  `    ` `    ``return` `imply; ` `} ` ` `  `int` `foremost() ` `{ ` `    ` `    ``int` `arr[] = { 1, 2, 4, 8 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ` `    ``cout << fastened << setprecision(2) ` `         ``<< pairProductMean(arr, N); ` ` `  `    ``return` `0; ` `} `

Time Complexity: O(N)
Auxiliary House: O(N)

Consideration reader! Don’t cease studying now. Pay money for all of the essential DSA ideas with the DSA Self Paced Course at a student-friendly worth and grow to be trade prepared.

When you like GeeksforGeeks and wish to contribute, you may as well write an article utilizing contribute.geeksforgeeks.org or mail your article to [email protected] See your article showing on the GeeksforGeeks foremost web page and assist different Geeks.

Please Enhance this text when you discover something incorrect by clicking on the “Enhance Article” button under.

Article Tags :

Be the First to upvote.

Please write to us at [email protected] to report any subject with the above content material.

0 comment