Sum and Product of minimum and maximum element of an Array
Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array.
Examples:
Input : arr[] = {12, 1234, 45, 67, 1}
Output : Sum = 1235
Product = 1234
Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
Output : Sum = 10
Product = 9
Take to variables min and max to store the minimum and maximum elements of the array. Find the minimum and the maximum element and store in these variables respectively. Finally, print the sum and product of the minimum and maximum elements.
Below is the program to illustrate above approach:
C++
// CPP program to find the sum and product // of minimum and maximum element in an array #include <iostream> using namespace std; // Function to find minimum element int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = min(res, arr[i]); return res; } // Function to find maximum element int getMax(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = max(res, arr[i]); return res; } // Function to get Sum int findSum(int arr[], int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min + max; } // Function to get product int findProduct(int arr[], int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min * max; } // Driver Code int main() { int arr[] = { 12, 1234, 45, 67, 1 }; int n = sizeof(arr) / sizeof(arr[0]); // Sum of min and max element cout << "Sum = " << findSum(arr, n) << endl; // Product of min and max element cout << "Product = " << findProduct(arr, n); return 0; } |
Java
// Java program to find the sum and product // of minimum and maximum element in an array import java.io.*; class GFG { // Function to find minimum element static int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = Math.min(res, arr[i]); return res; } // Function to find maximum element static int getMax(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = Math.max(res, arr[i]); return res; } // Function to get Sum static int findSum(int arr[], int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min + max; } // Function to get product static int findProduct(int arr[], int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min * max; } // Driver Code public static void main (String[] args) { int arr[] = { 12, 1234, 45, 67, 1 }; int n = arr.length; // Sum of min and max element System.out.println ("Sum = " + findSum(arr, n)); // Product of min and max element System.out.println( "Product = " + findProduct(arr, n)); } } //This Code is contributed by anuj_67.... |
Python 3
# Python 3 program to find the sum and product # of minimum and maximum element in an array # Function to find minimum element def getMin(arr, n): res = arr[0] for i in range(1, n): res = min(res, arr[i]) return res # Function to find maximum element def getMax(arr, n): res = arr[0] for i in range(1, n): res = max(res, arr[i]) return res # Function to get Sum def findSum(arr, n): min = getMin(arr, n) max = getMax(arr, n) return min + max # Function to get product def findProduct(arr, n): min = getMin(arr, n) max = getMax(arr, n) return min * max # Driver Code if __name__ == "__main__": arr = [ 12, 1234, 45, 67, 1 ] n = len(arr) # Sum of min and max element print("Sum = " , findSum(arr, n)) # Product of min and max element print("Product = " , findProduct(arr, n)) # This code is contributed # by ChitraNayal |
C#
// C# program to find the sum and product // of minimum and maximum element in an array using System; class GFG { // Function to find minimum element static int getMin(int []arr, int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = Math.Min(res, arr[i]); return res; } // Function to find maximum element static int getMax(int []arr, int n) { int res = arr[0]; for (int i = 1; i < n; i++) res = Math.Max(res, arr[i]); return res; } // Function to get Sum static int findSum(int []arr, int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min + max; } // Function to get product static int findProduct(int []arr, int n) { int min = getMin(arr, n); int max = getMax(arr, n); return min * max; } // Driver Code public static void Main() { int []arr = { 12, 1234, 45, 67, 1 }; int n = arr.Length; // Sum of min and max element Console.WriteLine("Sum = " + findSum(arr, n)); // Product of min and max element Console.WriteLine( "Product = " + findProduct(arr, n)); } } // This Code is contributed by anuj_67.... |
PHP
<?php // PHP program to find the sum and product // of minimum and maximum element in an array // Function to find minimum element function getMin($arr, $n) { $res = $arr[0]; for ($i = 1; $i < $n; $i++) $res = min($res, $arr[$i]); return $res; } // Function to find maximum element function getMax($arr, $n) { $res = $arr[0]; for ($i = 1; $i < $n; $i++) $res = max($res, $arr[$i]); return $res; } // Function to get Sum function findSum($arr, $n) { $min = getMin($arr, $n); $max = getMax($arr, $n); return $min + $max; } // Function to get product function findProduct($arr, $n) { $min = getMin($arr, $n); $max = getMax($arr, $n); return $min * $max; } // Driver Code $arr = array(12, 1234, 45, 67, 1); $n = sizeof($arr); // Sum of min and max element echo "Sum = " . findSum($arr, $n) . "\n"; // Product of min and max element echo "Product = " . findProduct($arr, $n); // This code is contributed // by Akanksha Rai |
Sum = 1235 Product = 1234
Optimizations
We can use a single loop to find both maximum and minimum. This would require only one traversal of array.
In C++, there are direct function to find maximum and minimum : max_element() and min_element()
Recommended Posts:
- Maximum element in an array such that its previous and next element product is maximum
- Sum and Product of maximum and minimum element in Binary Tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Product of maximum in first array and minimum in second
- Minimum element whose n-th power is greater than product of an array of size n
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Program to find the minimum (or maximum) element of an array
- Minimum and Maximum element of an array which is divisible by a given number k
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Maximum previous and next element product
- Maximum and Minimum Product Subsets
- Product of all Subsequences of size K except the minimum and maximum Elements
- Find minimum value to assign all array elements so that array product becomes greater
- Maximum possible remainder when an element is divided by other element in the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


